我是R的新手,想知道你们中是否有人能帮我做一个预测和实际的图。我正在尝试使用GLM Logit模型来预测股价走势的方向。您的帮助我们将不胜感激。
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")发布于 2020-01-16 19:18:38
假设test也有一列您要预测的变量,那么您可以只运行下面这样的命令
test$prediction <- prob然后,实际结果和您的预测结果都在同一个data.frame中,您可以很容易地将它们绘制出来
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))当然,这是一个没有普通回归模型那么漂亮的图,因为你的因变量是二进制的。
https://stackoverflow.com/questions/59750790
复制相似问题