R中的线性回归预测
Linear regression Forecasting in R
在 R 中编写我的预测脚本时遇到问题。我将所有时间序列数据都保存在导入到全球环境的 .csv 文档中。该代码一直运行到 anova(reg1)。所以我的问题真的是为什么脚本的其余部分不起作用,以及我需要如何编辑脚本以使其看起来像示例 4.4 在 link(https://www.otexts.org/fpp/4/8)[ 中的样子=12=]
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(SeasonallyadjustedStockprice ~ Oilprice + Index)中的错误:
不是时间序列数据
好像是因为公式右边的项不是时间序列。这是一个简化的例子:
>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
对于这个过于简化的示例,这基本上是合理的输出。
在 R 中编写我的预测脚本时遇到问题。我将所有时间序列数据都保存在导入到全球环境的 .csv 文档中。该代码一直运行到 anova(reg1)。所以我的问题真的是为什么脚本的其余部分不起作用,以及我需要如何编辑脚本以使其看起来像示例 4.4 在 link(https://www.otexts.org/fpp/4/8)[ 中的样子=12=]
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(SeasonallyadjustedStockprice ~ Oilprice + Index)中的错误: 不是时间序列数据
好像是因为公式右边的项不是时间序列。这是一个简化的例子:
>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
对于这个过于简化的示例,这基本上是合理的输出。