[英]Linear regression Forecasting in R
在R中编写我的预测脚本时遇到麻烦。我将所有时间序列数据都保存在导入到全局环境的.csv文档中。 该代码一直运行到anova(reg1)。 所以我的问题确实是为什么脚本的其余部分不起作用,以及如何编辑脚本以使其看起来像示例4.4,如以下链接所示: https://www.otexts.org/fpp / 4/8 )
data <- read.csv("Ny.csv", h=T, sep=";")
SeasonallyadjustedStockprice<-data$Seasonallyadj
Oilprice<-data$Oilaverage
Index<-data$DJI
plot.ts(Oilprice)
plot.ts(SeasonallyadjustedStockprice)
plot.ts(Index)
reg1<-lm(SeasonallyadjustedStockprice~Oilprice+Index)
summary(reg1)
anova(reg1)
Time <- tslm(SeasonallyadjustedStockprice~Oilprice+Index)
f <- forecast(Time, h=5,level=c(80,95))
错误消息如下:tslm中的错误(经季节性调整的股票价格〜石油价格+指数):不是时间序列数据
看起来像是因为公式右侧的术语不是时间序列。 这是一个简化的示例:
>df
a b
1 1 2
2 2 4
3 3 6
4 4 8
> y
[1] 1 3 5 7
>tslm(y ~ a + b, data=df)
Error in tslm(y ~ a + b, data = df) : Not time series data
> dfts = ts(df) # This is of class 'mts' (multiple time series)
> tslm(y ~ a + b, data=dfts)
Call:
lm(formula = formula, data = dfts, na.action = na.exclude)
Coefficients:
(Intercept) a b
-1 2 NA
对于这个过于简化的示例,这基本上是合理的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.