0

I'm new to R, and was wondering if any of you could help me out with the code to make a predicted vs actual graph. I am trying to predict the direction of stock price movements using a GLM Logit Model. Your help will be appreciated.

   cat("\014");
   library(readxl);
   Smarket = read_excel("C:/Users/Sohaib/Desktop/data.xlsx");

   # Download introduction to statistical learning package
   library(ISLR)

   # Define train and test sample
   train = (Smarket$year<2019)
   test = Smarket[!train,]

   # Fitting the LR model on the data
   fit = glm(direction ~ lag1 + open + high + low , data=Smarket, family=binomial, subset=train)

   # Predicting against test data
   prob = predict(fit, test, type="response")
NelsonGon
  • 13,015
  • 7
  • 27
  • 57

1 Answers1

0

Provided that test also has a column of the variable you are predicting you can just run something like

test$prediction <- prob

Then both the actual outcome as well as your prediction are in the same data.frame and you can easily plot them

test <- test[order(test$prediction, decreasing = TRUE),]
test$id = seq(nrow(test),1)

library(ggplot2)
ggplot(data = test) + 
  geom_line(aes(x = id, y = prediction)) +
  geom_point(aes(x = id, y = direction))

Naturally this is a less beautiful graph than in an ordinary regression model because your dependant variable is binary.

LeroyFromBerlin
  • 395
  • 3
  • 12