I am trying to build a function where I estimate a lot of arima models with a for loop.
I got the for loops running and getting me my desired output, but as soon as I try to get my code into the function I get errors.
This is the loop:
model_acc <- data.frame()
for (p in 0:4) {
for(d in 0:2){
for (q in 0:4){
model <- arima(data, order=c(p,d,q), method = "ML")
acc <- accuracy(model)
acc <- as.data.frame(acc)
acc_ext <- data.frame(acc,
loglikeli=logLik(model),
AIC=AIC(model),
BIC=BIC(model),
order=paste(p,d,q,sep=","))
acc_ext <- select(acc_ext,
ME, RMSE, MAE, MAPE,loglikeli,AIC,BIC,order)
model_acc <- rbind(model_acc, acc_ext)
}
}
}
I am aware that there are some models that cannot be computed with Maximum Likelihood, due to the constraints in the optimization. But this loop gets me 61 models out of 75 (just with method="CSS"). I get the models that could be computed.
So the parameters I'd like to move are: data, p_max, d_max, q_max, and, method.
So the function goes like this:
which_arima <- function(data, p_max, d_max, q_max, method){
model_acc <- data.frame()
for (p in 0:p_max) {
for(d in 0:d_max){
for (q in 0:q_max){
model <- arima(data, order=c(p,d,q), method = method)
acc <- accuracy(model)
acc <- as.data.frame(acc)
acc_ext <- data.frame(acc,
loglikeli=logLik(model),
AIC=AIC(model),
BIC=BIC(model),
order=paste(p,d,q,sep=","))
acc_ext <- select(acc_ext,
ME, RMSE, MAE, MAPE,loglikeli,AIC,BIC,order)
model_acc <- rbind(model_acc, acc_ext)
}
}
}
return(model_acc)
}
a <- which_arima(data, p_max=4, d_max=2, q_max=4, method="ML")
But when I execute it, I get this error (referring to the models that could not be computed) and don't get anything. (in the for loop only I got the models that could be computed).
Error in optim(init[mask], armafn, method = optim.method, hessian = TRUE, :
non-finite finite-difference value [4]
In addition: Warning messages:
1: In arima(data, order = c(p, d, q), method = method) :
possible convergence problem: optim gave code = 1
2: In arima(data, order = c(p, d, q), method = method) :
possible convergence problem: optim gave code = 1
3: In log(s2) : NaNs produced
4: In log(s2) : NaNs produced
Called from: optim(init[mask], armafn, method = optim.method, hessian = TRUE,
control = optim.control, trans = as.logical(transform.pars))
Browse[1]> Q
What is going wrong? Because without the function environment is working "fine". And more importantly, how can I solve this?
Thanks in advance!!
Here is the data:
> dput(data)
structure(c(1.04885832686158, 1.06016074629379, 1.0517956106758,
1.02907998600003, 1.05054370620123, 1.07261670636915, 1.0706491823234,
1.0851355199628, 1.08488055975672, 1.08085233559646, 1.081489249884,
1.08587205516048, 1.07249155362154, 1.05497731364761, 1.05675866316574,
1.06428371643968, 1.06065865122313, 1.05621234529568, 1.05339905298902,
1.05787030302435, 1.0658034000068, 1.08707776713932, 1.08626056161822,
1.10238697375394, 1.11390088086972, 1.12120513732074, 1.11937921359653,
1.10341241626668, 1.1156190247407, 1.12376155972358, 1.12411603174635,
1.12183475077377, 1.12994175229071, 1.12956170931204, 1.12199732095331,
1.11645064755987, 1.12481242467782, 1.13066151473637, 1.13028712061827,
1.12694056065497, 1.12382226475179, 1.12352013167586, 1.13391069257413,
1.14763982976838, 1.14481816405703, 1.14852949174863, 1.14182560351963,
1.14086563926171, 1.14491904045717, 1.14897189333479, 1.14616964486707,
1.15074750127031, 1.14681353487065, 1.11151754535415, 1.10497749493861,
1.10963378437214, 1.12415745716768, 1.17507535290893, 1.20285968503846,
1.22784769136553, 1.23940795216891, 1.254741010879, 1.29442450660416,
1.30428779451896, 1.31314618462517, 1.32544236970695, 1.33728107423435,
1.34408499591568, 1.34199331033196, 1.34027541040719, 1.33616830504407,
1.33360421057602, 1.33332422301893, 1.34717794252774, 1.3502492092262,
1.35168291803248, 1.35827816606688, 1.36772644852242, 1.36755741578293,
1.36926148542701, 1.37264481021763, 1.37322962601678, 1.37643913938007,
1.37906284181634, 1.37644362054554, 1.38911039237937, 1.39412557349575,
1.40094895608589, 1.40630864159528, 1.40823485306921, 1.4138446752069,
1.42340582796496, 1.43641264727375, 1.43605231080207, 1.44839810240334,
1.45451041581127, 1.46166006472498, 1.46774816064695, 1.46930608347752,
1.47885183796249, 1.49059366171423, 1.49849145403671, 1.51209667142067,
1.5250141727637, 1.5392257264567, 1.55144303632514, 1.56488453313021,
1.58308777691125, 1.59737589266492, 1.60896279958586, 1.62553339664661,
1.63594174408691, 1.65233080464302, 1.67114336171075, 1.6897476078746,
1.71673790971729, 1.74453973794979, 1.76317526009814, 1.79187692264759,
1.84186982937622, 1.9460629324144, 2.05986108970089, 2.06767436493269,
2.0783176148561, 2.08271855277262, 2.09358626977224, 2.09674958523685,
2.11582742548029, 2.12810020369675, 2.13596929171732, 2.13972610568317,
2.14456803530813, 2.15013985201827, 2.16007349878874, 2.17165498940627,
2.18057666565755, 2.19162746118342, 2.20308765886345, 2.21304799942168,
2.22367586966847, 2.23629862083737, 2.24751866055731, 2.26100586740225,
2.40972893063106, 2.60366275683037, 2.68572993101095, 2.70501080420283,
2.6676315643757, 2.6479269687206, 2.64641010174172, 2.69966594490103,
2.69665303568271, 2.71396750774502, 2.71900427132191, 2.72876269360869,
2.76276620421252, 2.76620189252239, 2.74632816231219, 2.74196673817286,
2.72905831066292, 2.75190757584346, 2.77801573354251, 2.84089580821293,
2.85681823660541, 2.84754572013613, 2.85858396073969, 2.86184353545653,
2.86958309986952, 2.94279115543111, 2.98631808884879, 3.00648449252989,
3.00620698598987, 3.15207693676406, 3.27614511764022, 3.32011714920345,
3.39367422894347, 3.64822360464499, 3.61835354049394, 3.59374251055335,
3.63237359915986, 3.62209957896007, 3.64554153297999, 3.71611226971083,
3.76031231050606, 3.80307769833913, 3.77959145461296, 3.74772344909971,
3.95072671083008, 4.03652777624058, 4.06630193640976, 4.08838169421096,
4.09074775372752, 4.09286687677964, 4.11466378890098, 4.14350067096966,
4.18153835521181, 4.21299240125327, 4.23975062689892, 4.26683207875595,
4.29265054707555, 4.31835343358436, 4.34946580314932, 4.37865522989399,
4.41294135451665), .Dim = c(204L, 1L), .Dimnames = list(NULL,
"price"), .Tsp = c(2004, 2020.91666666667, 12), class = "ts")