model - Use of the auto.arima function with external regressors -
i new r , i´ve been having trouble following:
i want possible combinations (up 3 independent variables in example) arimax model.
suppose have thefollowing sample data:
library(forecast) airpassengers_1<-ts(airpassengers, start = c(1949,1), frequency = 12, end =c(1960,12)) nottem_1<-ts(nottem, start = c(1949,1), frequency = 12, end = c(1960,12)) sunspots_1<-ts(sunspots, start = c(1949,1), frequency = 12, end = c(1960,12)) sunspot.month_1<-ts(sunspot.month, start = c(1949,1), frequency = 12, end = c(1960,12)) base<-cbind(airpassengers_1,nottem_1,sunspots_1,sunspot.month_1)
i going use airpassengers_1 variable dependent variable , others independent variables.
next create matrix possible combinations of explanatory variables going included in auto.arima formula (forecast package).
cant_variables<-dim(base)[2]-1 cant_regresores<-3 x=c(1:cant_variables) if(cant_regresores==1){ v<-t(combn(x, 1)) v<-cbind(v,matrix(0,nrow(v),2)) h<-v c<-as.data.frame(t(x)) a<-as.data.frame(c[rep(seq_len(nrow(c)), each=nrow(h)),]) matriz_true_false<-t(mapply("%in%",data.frame(t(a)),data.frame(t(h)))) } else if(cant_regresores==2){ v<-t(combn(x, 1)) v<-cbind(v,matrix(0,nrow(v),2)) y<-t(combn(x, 2)) y<-cbind(y,matrix(0,nrow(y),1)) h<-rbind(v,y) c<-as.data.frame(t(x)) a<-as.data.frame(c[rep(seq_len(nrow(c)), each=nrow(h)),]) matriz_true_false<-t(mapply("%in%",data.frame(t(a)),data.frame(t(h)))) } else if(cant_regresores==3){ v<-t(combn(x, 1)) v<-cbind(v,matrix(0,nrow(v),2)) y<-t(combn(x, 2)) y<-cbind(y,matrix(0,nrow(y),1)) z<-t(combn(x, 3)) h<-rbind(v,y,z) c<-as.data.frame(t(x)) a<-as.data.frame(c[rep(seq_len(nrow(c)), each=nrow(h)),]) matriz_true_false<-t(mapply("%in%",data.frame(t(a)),data.frame(t(h)))) } matriz_true_false<-as.data.frame(matriz_true_false)
(maybe true-false matrix not necessary code , might have better idea make work.)
since 'xreg' field autoarima takes vectors or matrix data input, apply somehow previous 'matriz_true_false' in order have possible combinations arimax . example first combination using nottem_1 variable follows:
autoarima_1<-auto.arima(base[,1], xreg = base[,2])
the next 1 using sunspots_1
autoarima_2<-auto.arima(base[,1], xreg = base[,3])
and on. recursively last combination is
autoarima_7<-auto.arima(base[,1], xreg = base[,2:4])
which has possible combinations of variables.additionally have these models grouped in 1 list.
thank in advance can provide!
Comments
Post a Comment