I am trying to extract the fitted and predicted values from the different classes of the object. But when I save them to a column in a data frame I got a column with NA. I tried to fix that by making the fitted value numeric, a data frame. But when it comes to saving the values in a data frame I still got the NA or errors. Adjusting the number of rows did neither helped.
This is my code: 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 =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
Can somebody help me pls?
CodePudding user response:
You are trying to combine lists, but you want data frames. To understand the difference, see this post: What is difference between dataframe and list in R?. To fix it, store the objects as data frames rather than lists before combining.
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")
CodePudding user response:
Edit: I manage to make it work, but there are a lot of changes, not to mention that some functions you are using are not even in the package described in your post. I solved the problem only using forecast package. Let me know if this helps.
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
First Result Set:
> 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
>
Second Result Set:
> 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
Third Results Set:
> 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
>
