0

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?

Ris
  • 31
  • 5
  • 1
    I'm trying to run your code but it's full of errors. There are variables mistaken. Columns not well initialized. The values from the model functions contains NA values. There is a lot going on – AugtPelle Feb 06 '22 at 02:04
  • Thank you AugtPelle, in the end I added a model and indeed forgot to adjust the initials – Ris Feb 06 '22 at 10:03

3 Answers3

0

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
> 
AugtPelle
  • 549
  • 1
  • 10
  • The holts() is running forever here. My R is 4.0.5. Do you have another suggestion? Working with forecast : : () is also working. I will incorporate your suggestion in my situation. Thank you. – Ris Feb 06 '22 at 10:27
  • That is really weird, because holt() finishes in less than 1 second on my pc. I have R 3.6.2, linux, 34gb RAM and I7. Maybe that helps – AugtPelle Feb 07 '22 at 15:21
  • If you have the package "tictoc" You can use tic() {code here} toc() And that will tell you have much seconds it takes. – AugtPelle Feb 07 '22 at 15:22
0

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")
SIE_Vict0ria
  • 174
  • 1
  • 10
  • Thank you SIE_Vict0ria, I also got the impression that the really problem was the object. How to go into that object or class to get the find the name of the fitted/predicted values in order to get them. I did, play around with the data.frame()/as.(numeric), but it still was seen as a part of class ts or so. I will try your suggestion of cbind. – Ris Feb 07 '22 at 00:25
  • If this answer worked for you, please mark it as accepted. – SIE_Vict0ria Feb 16 '22 at 00:05
0

HoltWinters() works better for me, although it gives me sometimes the next warning message:

optimization difficulties: ERROR: ABNORMAL_TERMINATION_IN_LNSRCH

But I found some advise and it seems to work.

Thank you also for that tictoc suggestion!

Grzegorz Sapijaszko
  • 1,913
  • 1
  • 5
  • 12