Skip to content
Prev 360831 / 398503 Next

Creating data frame of predicted and actual values in R for plotting

I have achieved this use case by writing the following commands:

all_predictions <- data.frame(pid = testPFI$project_id, actual_delay = testPFI$project_delay,lm_pred, tree_pred, best_tree_pred, rf_pred)

str(all_predictions)

all_pred <- sqldf("SELECT pid, actual_delay, ROUND(lm_pred,2) lm_pred,
                               ROUND(tree_pred,2) tree_pred,
                               ROUND(best_tree_pred,2) train_pred,
                               ROUND(rf_pred,2) rf_pred
                     FROM all_predictions
                      ORDER BY actual_delay")
all_pred

#Plotting all the predictions on the graph
ggplot(all_pred, aes(x=pid)) + geom_line(aes(y=actual_delay), colour="blue") +
  geom_line(aes(y=lm_pred), colour="red", size=1)  +
  geom_line(aes(y=tree_pred), colour="green", size=1)  +
  geom_line(aes(y=train_pred), colour="yellow", size=1)  +
  geom_line(aes(y=rf_pred), colour="black", size=1)

So I am done.

Many Thanks and

Kind Regards
--
Muhammad Bilal
Research Fellow and Doctoral Researcher,
Bristol Enterprise, Research, and Innovation Centre (BERIC),
University of the West of England (UWE),
Frenchay Campus,
Bristol,
BS16 1QY

muhammad2.bilal at live.uwe.ac.uk