-1

I use loop for a function,

AllModel <- AllData[c(2:8)]
alladvancedfit <- data.frame(matrix(, nrow=7, ncol=1))
for (Model in AllModel) {
  advancedfit.model <- Arima(AllData$x1,order=c(0,1,2),xreg=Model)
  inSampleAdvancedFitMAE <- mean(abs(advancedfit.model$residuals))
  }

AllModel capture this data set

v1 v2 v3 v4 v5 v6 v7
1   39  4   84  75  41  6   83
2   40  6   86  77  44  6   84
3   39  5   82  73  40  6   81
4   37  5   100 71  39  6   90
5   39  5   83  70  37  5   79
6   44  6   82  78  40  6   78
7   41  5   76  76  40  7   78
8   40  5   74  72  42  6   81

And the result is lists of output.

[1] 61.01004
[1] 61.23916
[1] 60.78099
[1] 61.15394
[1] 61.16968
[1] 61.30191
[1] 61.00637

Would like to put this into one data frame. Try to rbind it inside the loop but so far it is unsuccessful.

aua
  • 97
  • 1
  • 7
  • Can you show the code where you tried to `rbind`? Also why use `rbind` when you already prepared an empty data.frame `alladvancedfit ` to receive the results? (Which, btw. **is** the correct way to do what you want) – dario Mar 09 '20 at 11:56
  • for (Model in AllModel) { advancedfit.model <- Arima(AllData$x1,order=c(0,1,2),xreg=Model) inSampleAdvancedFitMAE <- mean(abs(advancedfit.model$residuals)) inSampleAdvancedFitMAE <- rbind(inSampleAdvancedFitMAE,alladvancedfit) } so which method are you suggesting? – aua Mar 09 '20 at 13:39

1 Answers1

0

It's always helpful if you'd provide a minimal reproducible example

I'm assuming allModel is a list

1.Initialize data.frame to receive results:

alladvancedfit <- data.frame(mean_residuals = rep(NA, lenght(allModel)))

2.Loop through the models and fill in the summary information into alladvancedfit

for (i in seq_along(AllModel)) {
  advancedfit.model <- Arima(AllData$x1,order=c(0,1,2),xreg=AllModel[i])
  alladvancedfit$mean_residuals[i] <- mean(abs(advancedfit.model$residuals))
}
dario
  • 6,415
  • 2
  • 12
  • 26
  • already put the AllModel dataset on the question section. I'm trying to do your code but there's a warning - Error in Arima(AllData$x1, order = c(0, 1, 2), xreg = AllModel[i]) : xreg should be a numeric matrix or a numeric vector – aua Mar 09 '20 at 14:25
  • Still don't know the class and type of `AllModel`. You could check the links i provided if you would like to learn how to provide a MRE... With regard to your error: Since `AllModel` in your data is not a list it should probably be `seq_along(AllModel[1,])` and `xreg=AllModel[, i]`) – dario Mar 09 '20 at 14:32
  • yes change the code into seq_along(AllModel[1,]) and it works fine now. thank you! – aua Mar 09 '20 at 14:40
  • Have a bug free day ;) – dario Mar 09 '20 at 14:48