Loop Error in R

Asked

Viewed 117 times

5

I created this loop:

library(forecast)
a=1
b=60
while(b<=(NROW(tsarroz))){
  janela=dput(tsarroz [a:b])
  serie=ts(janela,freq=6)
  HW=HoltWinters(serie)
  prev=forecast(HW,6)
  result=data.frame(prev$mean)
  a=a+6
  b=b+6
}

It should create 1 window of the data, run the function HoltWinters and then calculate the forecast. But it seems to compute all windows at once, and the function HoltWinters of error:

Warning messages:
1: In HoltWinters(serie) :
  optimization difficulties: ERROR: ABNORMAL_TERMINATION_IN_LNSRCH

Another problem is that in the "result" should come the sum of the calculated predictions of each window, but only returns the result of the last.

My data is:

tsarroz
Time Series:
Start = c(1, 1) 
End = c(55, 5) 
Frequency = 6 
  [1]  604  552  580  561  603  751  444  411  375  383  477  649  330  365  336  370  355
 [18]  418  258  355  407  435  615  676  258  355  556  630  636  925  562  498  396  383
 [35]  486  791  435  322  322  410  402  490  340  328  294  323  342  567  418  469  500
 [52]  478  608 1049  556  455  477  378  431  628  362  637  609  581  431  385  362  375
 [69]  417  287  455  556  362  429  561  287  922  863  544  567  715  590  613  782  413
 [86]  375  328  580  556  654  309  331  363  370  422  451  374  316  313  329  558  745
[103]  574  546  596  513  640  966  447  509  466  268  494  749  509  286  410  268  606
[120]  511  236  371  377  401  462  354  468  586  640  613  347  900  741  283  583  463
[137]  465  528  352  568  477  316  479  528  315  331  339  290  348  451  364  336  380
[154]  393  711  826  485  562  579  624  604  734  453  330  307  359  629  685  346  332
[171]  387  328  453  428  360  378  332  335  462  625  532  550  518  528  751  858  568
[188]  423  490  424  450  616  539  409  379  369  356  436  290  348  286  364  455  497
[205]  323  470  523  547  573  817  506  558  535  428  566  594  342  351  585  445  416
[222]  448  315  348  296  351  394  437  392  317  296  287  468  783  508  456  528  593
[239]  618  819  403  525  362  361  605  481  422  341  323  412  352  545  300  299  309
[256]  352  389  558  504  467  520  424  627  796  556  484  424  516  440  473  473  514
[273]  477  430  437  551  575  571  324  430  341  240  472  392  287  430  365  315  465
[290]  470  549  558  509  463  373  363  307  522  452  407  325  437  245  314  294  327
[307]  339  323  388  382  447  490  616  557  542  582  629  778  436  457  382  434  508
[324]  581  436  457  410  398  378

1 answer

3

The error message can be solved with tryCatch.
Also I made other changes to your code. Now the cycle while stores the results of all iterations on list result, not only the last.

library(forecast)

a <-  1
b <- 60
result <- list()
inx <- 1
while(b <= NROW(tsarroz)){
  janela <- tsarroz[a:b]
  serie <- ts(janela, freq = 6)
  HW <- tryCatch(HoltWinters(serie),
                 warning = function(w) w
  )
  if(inherits(HW, "warning"))
    result[[inx]] <- HW
  else{
    prev <- forecast(HW, 6)
    result[[inx]] <- prev$mean
  }
  a <- a + 6
  b <- b + 6
  inx <- inx + 1
}

See how many members have the list result.

length(result)
#[1] 45

See the first element, to get an idea of what the list members are like.

result[[1]]
#Time Series:
#Start = c(11, 1) 
#End = c(11, 6) 
#Frequency = 6 
#[1] 342.4086 324.2339 296.7258 342.0511 398.6264 564.9517

Now, see which iterations went well, without warnings, and which went wrong.

ok <- sapply(result, inherits, "ts")
bons <- result[ok]
length(bons)
#[1] 41

which(!ok)
#[1] 13 24 36 40

The list bons There’s only the ones that went well.

Data in dput format.

tsarroz <-
structure(c(604, 552, 580, 561, 603, 751, 444, 411, 375, 383, 
477, 649, 330, 365, 336, 370, 355, 418, 258, 355, 407, 435, 615, 
676, 258, 355, 556, 630, 636, 925, 562, 498, 396, 383, 486, 791, 
435, 322, 322, 410, 402, 490, 340, 328, 294, 323, 342, 567, 418, 
469, 500, 478, 608, 1049, 556, 455, 477, 378, 431, 628, 362, 
637, 609, 581, 431, 385, 362, 375, 417, 287, 455, 556, 362, 429, 
561, 287, 922, 863, 544, 567, 715, 590, 613, 782, 413, 375, 328, 
580, 556, 654, 309, 331, 363, 370, 422, 451, 374, 316, 313, 329, 
558, 745, 574, 546, 596, 513, 640, 966, 447, 509, 466, 268, 494, 
749, 509, 286, 410, 268, 606, 511, 236, 371, 377, 401, 462, 354, 
468, 586, 640, 613, 347, 900, 741, 283, 583, 463, 465, 528, 352, 
568, 477, 316, 479, 528, 315, 331, 339, 290, 348, 451, 364, 336, 
380, 393, 711, 826, 485, 562, 579, 624, 604, 734, 453, 330, 307, 
359, 629, 685, 346, 332, 387, 328, 453, 428, 360, 378, 332, 335, 
462, 625, 532, 550, 518, 528, 751, 858, 568, 423, 490, 424, 450, 
616, 539, 409, 379, 369, 356, 436, 290, 348, 286, 364, 455, 497, 
323, 470, 523, 547, 573, 817, 506, 558, 535, 428, 566, 594, 342, 
351, 585, 445, 416, 448, 315, 348, 296, 351, 394, 437, 392, 317, 
296, 287, 468, 783, 508, 456, 528, 593, 618, 819, 403, 525, 362, 
361, 605, 481, 422, 341, 323, 412, 352, 545, 300, 299, 309, 352, 
389, 558, 504, 467, 520, 424, 627, 796, 556, 484, 424, 516, 440, 
473, 473, 514, 477, 430, 437, 551, 575, 571, 324, 430, 341, 240, 
472, 392, 287, 430, 365, 315, 465, 470, 549, 558, 509, 463, 373, 
363, 307, 522, 452, 407, 325, 437, 245, 314, 294, 327, 339, 323, 
388, 382, 447, 490, 616, 557, 542, 582, 629, 778, 436, 457, 382, 
434, 508, 581, 436, 457, 410, 398, 378), .Tsp = c(1, 55.6666666666667, 
6), class = "ts")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.