将拟合值和预测值保存到 r 中的数据框

Save fitted and predicted values to data frame in r

我正在尝试从对象的不同 类 中提取拟合值和预测值。但是当我将它们保存到数据框中的一列时,我得到了一个带有 NA 的列。我试图通过将拟合值设为数字数据框来解决这个问题。但是当谈到将值保存在数据框中时,我仍然遇到 NA 或错误。调整行数也没有帮助。

这是我的代码: 图书馆(预测)

#generate time series
b=sin(runif(10, min=0, max=100))
plot(b,type="l")
#initiate data frame for fitted values
df1= data.frame(matrix(ncol =3, nrow=10))
colnames(df1)=c("month","Fit1", "Fit2", "Fit3")

#initiate data frame for predicted values
df2= data.frame(matrix(ncol =3, nrow=4))
colnames(df1)=c("month","Pred1", "Pred2","Pred3" )

#find model for time series
model_1= arima(b)
model_2=naive(b)
model_3=HoltWinters(ts(b, frequency=4))


#store fitted values in data frame
df1[,1]=1:10
df1[,2]=model_1$fitted
df1[,3]=model_2$fitted
df1[,4]=model_3$xhat 


#find short term predicted values for 4 periods
Predictedmodel_1= forecast(model_1, 4)#
Predictedmodel_2= forecast(model_2, 4)#
Predictedmodel_3= forecast(model_3, 4)#



#store predicted values in data frame
df2[,1]=11:14
df2[,2]=Predictedmodel_1$fitted
df2[,3]=Predictedmodel_2$fitted
df2[,4]=Predictedmodel_3$fitted

有人可以帮我吗?

编辑:我设法使它工作,但有很多变化,更不用说你正在使用的一些功能甚至不在你的 post 中描述的包中。我只使用 forecast 包解决了这个问题。让我知道这是否有帮助。

library(forecast)

#generate time series
b=sin(runif(10, min=0, max=100))
plot(b,type="l")
#initiate data frame for fitted values
df1= data.frame(matrix(ncol =4, nrow=10))
colnames(df1)=c("month","Fit1", "Fit2", "Fit3")

#initiate data frame for predicted values
df2= data.frame(matrix(ncol =4, nrow=4))
colnames(df2)=c("month","Pred1", "Pred2","Pred3" )

#find model for time series
model_1 <- forecast::Arima(b)
model_2 = forecast::naive(b)
model_3 = forecast::holt(ts(b, frequency=4))

model_1$fitted

model_2$fitted

model_3$x

#store fitted values in data frame
df1[,1]=1:10
df1[,2]=model_1$fitted
df1[,3]=model_2$fitted
df1[,4]=model_3$x 

df1

#find short term predicted values for 4 periods
Predictedmodel_1= forecast(model_1, 4)#
Predictedmodel_2= forecast(model_2, 4)#
Predictedmodel_3= forecast(model_3, 4)#


#store predicted values in data frame
df2[,1]=11:14
df2[,2]=Predictedmodel_1$fitted[1:4]
df2[,3]=Predictedmodel_2$fitted[1:4]
df2[,4]=Predictedmodel_3$fitted[1:4]

df2

第一个结果集:

> model_1$fitted
Time Series:
Start = 1 
End = 10 
Frequency = 1 
 [1] -0.03040639 -0.03040639 -0.03040639 -0.03040639 -0.03040639 -0.03040639 -0.03040639 -0.03040639 -0.03040639 -0.03040639
> 
> model_2$fitted
Time Series:
Start = 1 
End = 10 
Frequency = 1 
 [1]           NA -0.858431677  0.091330323 -0.380993711 -0.004967207 -0.188007449  0.408278796 -0.538164307 -0.782095624
[10]  0.998132989
> 
> model_3$x
          Qtr1         Qtr2         Qtr3         Qtr4
1 -0.858431677  0.091330323 -0.380993711 -0.004967207
2 -0.188007449  0.408278796 -0.538164307 -0.782095624
3  0.998132989  0.950853919                          
> 

第二个结果集:

> df1
   month        Fit1         Fit2         Fit3
1      1 -0.03040639           NA -0.858431677
2      2 -0.03040639 -0.858431677  0.091330323
3      3 -0.03040639  0.091330323 -0.380993711
4      4 -0.03040639 -0.380993711 -0.004967207
5      5 -0.03040639 -0.004967207 -0.188007449
6      6 -0.03040639 -0.188007449  0.408278796
7      7 -0.03040639  0.408278796 -0.538164307
8      8 -0.03040639 -0.538164307 -0.782095624
9      9 -0.03040639 -0.782095624  0.998132989
10    10 -0.03040639  0.998132989  0.950853919

第三个结果集:

> df2
  month       Pred1       Pred2      Pred3
1    11 -0.03040639          NA -0.5862699
2    12 -0.03040639 -0.85843168 -0.4627794
3    13 -0.03040639  0.09133032 -0.3391509
4    14 -0.03040639 -0.38099371 -0.2155862
> 

您正在尝试合并列表,但您需要数据框。要了解差异,请参阅此 post:What is difference between dataframe and list in R?。要修复它,请在组合之前将对象存储为数据框而不是列表。

df1 = as.data.frame(11:20)
df1.Temp1 = as.data.frame(Predictedmodel_1$fitted)
df1.Temp2 = as.data.frame(Predictedmodel_2$fitted)
df1.Temp3 = as.data.frame(Predictedmodel_3$fitted)

df1 = cbind(df1,df1.Temp1,df1.Temp2,df1.Temp3)

colnames(df1) = c("Col1","col2","col3","col4")

HoltWinters() 对我来说效果更好,尽管它有时会给我下一条警告消息:

optimization difficulties: ERROR: ABNORMAL_TERMINATION_IN_LNSRCH

但我找到了一些建议,似乎有效。

也感谢您的 tictoc 建议!