R version 3.0.2 (2013-09-25) -- "Frisbee Sailing" Copyright (C) 2013 The R Foundation for Statistical Computing Platform: i686-pc-linux-gnu (32-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > x <- c(4,5,7,5,6,5,3,7,7,11,13,13,9,7,6,3,5,1,5,2,9,4,4,10,8,6,7,0,7,4,5,11,2,4,5,12,10,6,6,8,3,10,2,5,4,3,8,5,7,1,7,4,8,7,10,2,6,6,11,8,8,6,11,15,9,5,10,4,9,3,7,7,9,15,11,10,6,5,6,6,14,11,1,9,13,10,11,7,6,4,6,8,6,7,12,20,10,14,11,13,7,9,8,7,9,10,12,13,11,11,14,10,9,12,8,13,14,15,14,14,15,14,21,10,8,12,13,6,12,12) > par9 = '1' > par8 = '2' > par7 = '1' > par6 = '3' > par5 = '12' > par4 = '0' > par3 = '1' > par2 = '1' > par1 = 'FALSE' > par9 <- '1' > par8 <- '2' > par7 <- '1' > par6 <- '3' > par5 <- '12' > par4 <- '0' > par3 <- '1' > par2 <- '1' > par1 <- 'FALSE' > #'GNU S' R Code compiled by R2WASP v. 1.2.327 () > #Author: root > #To cite this work: Wessa P., (2013), ARIMA Backward Selection (v1.0.5) in Free Statistics Software (v$_version), Office for Research Development and Education, URL http://www.wessa.net/rwasp_arimabackwardselection.wasp/ > #Source of accompanying publication: Office for Research, Development, and Education > # > library(lattice) > if (par1 == 'TRUE') par1 <- TRUE > if (par1 == 'FALSE') par1 <- FALSE > par2 <- as.numeric(par2) #Box-Cox lambda transformation parameter > par3 <- as.numeric(par3) #degree of non-seasonal differencing > par4 <- as.numeric(par4) #degree of seasonal differencing > par5 <- as.numeric(par5) #seasonal period > par6 <- as.numeric(par6) #degree (p) of the non-seasonal AR(p) polynomial > par7 <- as.numeric(par7) #degree (q) of the non-seasonal MA(q) polynomial > par8 <- as.numeric(par8) #degree (P) of the seasonal AR(P) polynomial > par9 <- as.numeric(par9) #degree (Q) of the seasonal MA(Q) polynomial > armaGR <- function(arima.out, names, n){ + try1 <- arima.out$coef + try2 <- sqrt(diag(arima.out$var.coef)) + try.data.frame <- data.frame(matrix(NA,ncol=4,nrow=length(names))) + dimnames(try.data.frame) <- list(names,c('coef','std','tstat','pv')) + try.data.frame[,1] <- try1 + for(i in 1:length(try2)) try.data.frame[which(rownames(try.data.frame)==names(try2)[i]),2] <- try2[i] + try.data.frame[,3] <- try.data.frame[,1] / try.data.frame[,2] + try.data.frame[,4] <- round((1-pt(abs(try.data.frame[,3]),df=n-(length(try2)+1)))*2,5) + vector <- rep(NA,length(names)) + vector[is.na(try.data.frame[,4])] <- 0 + maxi <- which.max(try.data.frame[,4]) + continue <- max(try.data.frame[,4],na.rm=TRUE) > .05 + vector[maxi] <- 0 + list(summary=try.data.frame,next.vector=vector,continue=continue) + } > arimaSelect <- function(series, order=c(13,0,0), seasonal=list(order=c(2,0,0),period=12), include.mean=F){ + nrc <- order[1]+order[3]+seasonal$order[1]+seasonal$order[3] + coeff <- matrix(NA, nrow=nrc*2, ncol=nrc) + pval <- matrix(NA, nrow=nrc*2, ncol=nrc) + mylist <- rep(list(NULL), nrc) + names <- NULL + if(order[1] > 0) names <- paste('ar',1:order[1],sep='') + if(order[3] > 0) names <- c( names , paste('ma',1:order[3],sep='') ) + if(seasonal$order[1] > 0) names <- c(names, paste('sar',1:seasonal$order[1],sep='')) + if(seasonal$order[3] > 0) names <- c(names, paste('sma',1:seasonal$order[3],sep='')) + arima.out <- arima(series, order=order, seasonal=seasonal, include.mean=include.mean, method='ML') + mylist[[1]] <- arima.out + last.arma <- armaGR(arima.out, names, length(series)) + mystop <- FALSE + i <- 1 + coeff[i,] <- last.arma[[1]][,1] + pval [i,] <- last.arma[[1]][,4] + i <- 2 + aic <- arima.out$aic + while(!mystop){ + mylist[[i]] <- arima.out + arima.out <- arima(series, order=order, seasonal=seasonal, include.mean=include.mean, method='ML', fixed=last.arma$next.vector) + aic <- c(aic, arima.out$aic) + last.arma <- armaGR(arima.out, names, length(series)) + mystop <- !last.arma$continue + coeff[i,] <- last.arma[[1]][,1] + pval [i,] <- last.arma[[1]][,4] + i <- i+1 + } + list(coeff, pval, mylist, aic=aic) + } > arimaSelectplot <- function(arimaSelect.out,noms,choix){ + noms <- names(arimaSelect.out[[3]][[1]]$coef) + coeff <- arimaSelect.out[[1]] + k <- min(which(is.na(coeff[,1])))-1 + coeff <- coeff[1:k,] + pval <- arimaSelect.out[[2]][1:k,] + aic <- arimaSelect.out$aic[1:k] + coeff[coeff==0] <- NA + n <- ncol(coeff) + if(missing(choix)) choix <- k + layout(matrix(c(1,1,1,2, + 3,3,3,2, + 3,3,3,4, + 5,6,7,7),nr=4), + widths=c(10,35,45,15), + heights=c(30,30,15,15)) + couleurs <- rainbow(75)[1:50]#(50) + ticks <- pretty(coeff) + par(mar=c(1,1,3,1)) + plot(aic,k:1-.5,type='o',pch=21,bg='blue',cex=2,axes=F,lty=2,xpd=NA) + points(aic[choix],k-choix+.5,pch=21,cex=4,bg=2,xpd=NA) + title('aic',line=2) + par(mar=c(3,0,0,0)) + plot(0,axes=F,xlab='',ylab='',xlim=range(ticks),ylim=c(.1,1)) + rect(xleft = min(ticks) + (0:49)/50*(max(ticks)-min(ticks)), + xright = min(ticks) + (1:50)/50*(max(ticks)-min(ticks)), + ytop = rep(1,50), + ybottom= rep(0,50),col=couleurs,border=NA) + axis(1,ticks) + rect(xleft=min(ticks),xright=max(ticks),ytop=1,ybottom=0) + text(mean(coeff,na.rm=T),.5,'coefficients',cex=2,font=2) + par(mar=c(1,1,3,1)) + image(1:n,1:k,t(coeff[k:1,]),axes=F,col=couleurs,zlim=range(ticks)) + for(i in 1:n) for(j in 1:k) if(!is.na(coeff[j,i])) { + if(pval[j,i]<.01) symb = 'green' + else if( (pval[j,i]<.05) & (pval[j,i]>=.01)) symb = 'orange' + else if( (pval[j,i]<.1) & (pval[j,i]>=.05)) symb = 'red' + else symb = 'black' + polygon(c(i+.5 ,i+.2 ,i+.5 ,i+.5), + c(k-j+0.5,k-j+0.5,k-j+0.8,k-j+0.5), + col=symb) + if(j==choix) { + rect(xleft=i-.5, + xright=i+.5, + ybottom=k-j+1.5, + ytop=k-j+.5, + lwd=4) + text(i, + k-j+1, + round(coeff[j,i],2), + cex=1.2, + font=2) + } + else{ + rect(xleft=i-.5,xright=i+.5,ybottom=k-j+1.5,ytop=k-j+.5) + text(i,k-j+1,round(coeff[j,i],2),cex=1.2,font=1) + } + } + axis(3,1:n,noms) + par(mar=c(0.5,0,0,0.5)) + plot(0,axes=F,xlab='',ylab='',type='n',xlim=c(0,8),ylim=c(-.2,.8)) + cols <- c('green','orange','red','black') + niv <- c('0','0.01','0.05','0.1') + for(i in 0:3){ + polygon(c(1+2*i ,1+2*i ,1+2*i-.5 ,1+2*i), + c(.4 ,.7 , .4 , .4), + col=cols[i+1]) + text(2*i,0.5,niv[i+1],cex=1.5) + } + text(8,.5,1,cex=1.5) + text(4,0,'p-value',cex=2) + box() + residus <- arimaSelect.out[[3]][[choix]]$res + par(mar=c(1,2,4,1)) + acf(residus,main='') + title('acf',line=.5) + par(mar=c(1,2,4,1)) + pacf(residus,main='') + title('pacf',line=.5) + par(mar=c(2,2,4,1)) + qqnorm(residus,main='') + title('qq-norm',line=.5) + qqline(residus) + residus + } > if (par2 == 0) x <- log(x) > if (par2 != 0) x <- x^par2 > (selection <- arimaSelect(x, order=c(par6,par3,par7), seasonal=list(order=c(par8,par4,par9), period=par5))) [[1]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 0.1340488 0.1032607 -0.06530748 -0.9241385 1.0443703 -0.04603744 [2,] 0.1329230 0.1009457 -0.07308354 -0.9221329 0.9959497 0.00000000 [3,] 0.1603110 0.1178884 0.00000000 -0.9317792 0.1920134 0.00000000 [4,] 0.1613412 0.1188291 0.00000000 -0.9317387 0.1452593 0.00000000 [5,] 0.1665723 0.0000000 0.00000000 -0.9188428 0.1415598 0.00000000 [6,] 0.2006889 0.0000000 0.00000000 -0.9147384 0.0000000 0.00000000 [7,] NA NA NA NA NA NA [8,] NA NA NA NA NA NA [9,] NA NA NA NA NA NA [10,] NA NA NA NA NA NA [11,] NA NA NA NA NA NA [12,] NA NA NA NA NA NA [13,] NA NA NA NA NA NA [14,] NA NA NA NA NA NA [,7] [1,] -0.97966521 [2,] -0.96642817 [3,] -0.04737804 [4,] 0.00000000 [5,] 0.00000000 [6,] 0.00000000 [7,] NA [8,] NA [9,] NA [10,] NA [11,] NA [12,] NA [13,] NA [14,] NA [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 0.16533 0.26889 0.48857 0 0.00000 0.66479 0.00000 [2,] 0.16790 0.27816 0.43312 0 0.00000 NA 0.00000 [3,] 0.10345 0.21191 NA 0 0.83229 NA 0.95879 [4,] 0.09415 0.19900 NA 0 0.13501 NA NA [5,] 0.09074 NA NA 0 0.14825 NA NA [6,] 0.03513 NA NA 0 NA NA NA [7,] NA NA NA NA NA NA NA [8,] NA NA NA NA NA NA NA [9,] NA NA NA NA NA NA NA [10,] NA NA NA NA NA NA NA [11,] NA NA NA NA NA NA NA [12,] NA NA NA NA NA NA NA [13,] NA NA NA NA NA NA NA [14,] NA NA NA NA NA NA NA [[3]] [[3]][[1]] Call: arima(x = series, order = order, seasonal = seasonal, include.mean = include.mean, method = "ML") Coefficients: ar1 ar2 ar3 ma1 sar1 sar2 sma1 0.134 0.1033 -0.0653 -0.9241 1.0444 -0.046 -0.9797 s.e. 0.096 0.0930 0.0940 0.0360 0.1067 0.106 0.0775 sigma^2 estimated as 9.882: log likelihood = -335.38, aic = 686.75 [[3]][[2]] Call: arima(x = series, order = order, seasonal = seasonal, include.mean = include.mean, method = "ML") Coefficients: ar1 ar2 ar3 ma1 sar1 sar2 sma1 0.134 0.1033 -0.0653 -0.9241 1.0444 -0.046 -0.9797 s.e. 0.096 0.0930 0.0940 0.0360 0.1067 0.106 0.0775 sigma^2 estimated as 9.882: log likelihood = -335.38, aic = 686.75 [[3]][[3]] Call: arima(x = series, order = order, seasonal = seasonal, include.mean = include.mean, fixed = last.arma$next.vector, method = "ML") Coefficients: ar1 ar2 ar3 ma1 sar1 sar2 sma1 0.1329 0.1009 -0.0731 -0.9221 0.9959 0 -0.9664 s.e. 0.0958 0.0927 0.0929 0.0360 0.0054 0 0.0263 sigma^2 estimated as 9.917: log likelihood = -335.48, aic = 684.96 [[3]][[4]] Call: arima(x = series, order = order, seasonal = seasonal, include.mean = include.mean, fixed = last.arma$next.vector, method = "ML") Coefficients: ar1 ar2 ar3 ma1 sar1 sar2 sma1 0.1603 0.1179 0 -0.9318 0.1920 0 -0.0474 s.e. 0.0977 0.0940 0 0.0310 0.9048 0 0.9149 sigma^2 estimated as 10.73: log likelihood = -336.9, aic = 685.79 [[3]][[5]] Call: arima(x = series, order = order, seasonal = seasonal, include.mean = include.mean, fixed = last.arma$next.vector, method = "ML") Coefficients: ar1 ar2 ar3 ma1 sar1 sar2 sma1 0.1613 0.1188 0 -0.9317 0.1453 0 0 s.e. 0.0957 0.0920 0 0.0309 0.0966 0 0 sigma^2 estimated as 10.73: log likelihood = -336.9, aic = 683.79 [[3]][[6]] Call: arima(x = series, order = order, seasonal = seasonal, include.mean = include.mean, fixed = last.arma$next.vector, method = "ML") Coefficients: ar1 ar2 ar3 ma1 sar1 sar2 sma1 0.1666 0 0 -0.9188 0.1416 0 0 s.e. 0.0977 0 0 0.0343 0.0973 0 0 sigma^2 estimated as 10.86: log likelihood = -337.73, aic = 683.46 [[3]][[7]] NULL $aic [1] 686.7501 684.9647 685.7903 683.7930 683.4629 683.5718 Warning messages: 1: In arima(series, order = order, seasonal = seasonal, include.mean = include.mean, : some AR parameters were fixed: setting transform.pars = FALSE 2: In arima(series, order = order, seasonal = seasonal, include.mean = include.mean, : some AR parameters were fixed: setting transform.pars = FALSE 3: In arima(series, order = order, seasonal = seasonal, include.mean = include.mean, : some AR parameters were fixed: setting transform.pars = FALSE 4: In arima(series, order = order, seasonal = seasonal, include.mean = include.mean, : some AR parameters were fixed: setting transform.pars = FALSE 5: In arima(series, order = order, seasonal = seasonal, include.mean = include.mean, : some AR parameters were fixed: setting transform.pars = FALSE > postscript(file="/var/wessaorg/rcomp/tmp/11ntm1386163034.ps",horizontal=F,onefile=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > resid <- arimaSelectplot(selection) > dev.off() null device 1 > resid Time Series: Start = 1 End = 130 Frequency = 1 [1] 0.003999997 0.787034877 2.087577766 -0.578869219 0.781029490 [6] -0.459569426 -2.133474195 2.331386574 1.385100880 5.102497900 [11] 5.832907143 4.939499874 0.875843677 -0.745085545 -1.603990206 [16] -3.963031115 -1.318844854 -5.368087327 0.001883001 -4.272744114 [21] 3.669070829 -3.361496607 -2.440169107 3.804321979 1.059677879 [26] -0.504662508 0.963685985 -5.878491817 2.411785684 -1.336841209 [31] -0.388843525 5.994260219 -5.553438972 -0.729965263 -0.121680534 [36] 5.871803266 2.653244608 -0.993089378 -0.434850303 2.614852113 [41] -4.086442020 4.667796308 -5.089334574 -1.169359827 -1.158614576 [46] -2.393297450 2.873104979 -2.160278464 0.962952107 -4.929234677 [51] 2.375941287 -2.099439491 3.325616199 0.280596185 4.721917764 [56] -4.774336728 1.158022455 0.515727767 4.742485216 1.067307232 [61] 1.126542402 -0.068367209 4.279484065 7.665461863 -0.259918674 [66] -3.003509398 2.458277346 -3.370872797 2.147253504 -4.765552768 [71] -0.087157713 -0.203794346 1.742004988 7.550602445 1.183422886 [76] 0.305329198 -2.609199535 -2.306395099 -1.754760615 -0.811662896 [81] 6.404930640 2.519803917 -7.892698086 2.507894299 4.688662424 [86] -0.160344513 2.060104253 -2.226420363 -1.836781820 -3.473901332 [91] -1.023964231 0.749573198 -2.776882945 -0.605055352 5.622334956 [96] 10.964902640 -1.635156475 4.682270412 0.423681527 3.478832687 [101] -3.089404291 0.420296629 -1.277237528 -2.242967861 0.435916930 [106] 0.878674855 1.956572761 1.450059570 0.770041808 0.238653433 [111] 3.738284090 -1.418680958 -0.740736960 2.061352295 -2.416938579 [116] 3.563482499 3.134719136 3.619341828 1.899494167 1.817508894 [121] 2.976704426 1.521391181 8.139812250 -4.049819473 -3.841612014 [126] 0.355047863 1.296922905 -6.777022733 0.915327642 -0.276371469 > postscript(file="/var/wessaorg/rcomp/tmp/2d8ht1386163034.ps",horizontal=F,onefile=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > acf(resid,length(resid)/2, main='Residual Autocorrelation Function') > dev.off() null device 1 > postscript(file="/var/wessaorg/rcomp/tmp/3m2fn1386163034.ps",horizontal=F,onefile=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > pacf(resid,length(resid)/2, main='Residual Partial Autocorrelation Function') > dev.off() null device 1 > postscript(file="/var/wessaorg/rcomp/tmp/4y6mr1386163034.ps",horizontal=F,onefile=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > cpgram(resid, main='Residual Cumulative Periodogram') > dev.off() null device 1 > postscript(file="/var/wessaorg/rcomp/tmp/5rnsc1386163034.ps",horizontal=F,onefile=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > hist(resid, main='Residual Histogram', xlab='values of Residuals') > dev.off() null device 1 > postscript(file="/var/wessaorg/rcomp/tmp/650981386163034.ps",horizontal=F,onefile=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > densityplot(~resid,col='black',main='Residual Density Plot', xlab='values of Residuals') > dev.off() null device 1 > postscript(file="/var/wessaorg/rcomp/tmp/7sh591386163034.ps",horizontal=F,onefile=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > qqnorm(resid, main='Residual Normal Q-Q Plot') > qqline(resid) > dev.off() null device 1 > ncols <- length(selection[[1]][1,]) > nrows <- length(selection[[2]][,1])-1 > > #Note: the /var/wessaorg/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/wessaorg/rcomp/createtable") > > a<-table.start() > a<-table.row.start(a) > a<-table.element(a,'ARIMA Parameter Estimation and Backward Selection', ncols+1,TRUE) > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,'Iteration', header=TRUE) > for (i in 1:ncols) { + a<-table.element(a,names(selection[[3]][[1]]$coef)[i],header=TRUE) + } > a<-table.row.end(a) > for (j in 1:nrows) { + a<-table.row.start(a) + mydum <- 'Estimates (' + mydum <- paste(mydum,j) + mydum <- paste(mydum,')') + a<-table.element(a,mydum, header=TRUE) + for (i in 1:ncols) { + a<-table.element(a,round(selection[[1]][j,i],4)) + } + a<-table.row.end(a) + a<-table.row.start(a) + a<-table.element(a,'(p-val)', header=TRUE) + for (i in 1:ncols) { + mydum <- '(' + mydum <- paste(mydum,round(selection[[2]][j,i],4),sep='') + mydum <- paste(mydum,')') + a<-table.element(a,mydum) + } + a<-table.row.end(a) + } > a<-table.end(a) > table.save(a,file="/var/wessaorg/rcomp/tmp/80vvh1386163035.tab") > a<-table.start() > a<-table.row.start(a) > a<-table.element(a,'Estimated ARIMA Residuals', 1,TRUE) > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,'Value', 1,TRUE) > a<-table.row.end(a) > for (i in (par4*par5+par3):length(resid)) { + a<-table.row.start(a) + a<-table.element(a,resid[i]) + a<-table.row.end(a) + } > a<-table.end(a) > table.save(a,file="/var/wessaorg/rcomp/tmp/9it4s1386163035.tab") > > try(system("convert tmp/11ntm1386163034.ps tmp/11ntm1386163034.png",intern=TRUE)) character(0) > try(system("convert tmp/2d8ht1386163034.ps tmp/2d8ht1386163034.png",intern=TRUE)) character(0) > try(system("convert tmp/3m2fn1386163034.ps tmp/3m2fn1386163034.png",intern=TRUE)) character(0) > try(system("convert tmp/4y6mr1386163034.ps tmp/4y6mr1386163034.png",intern=TRUE)) character(0) > try(system("convert tmp/5rnsc1386163034.ps tmp/5rnsc1386163034.png",intern=TRUE)) character(0) > try(system("convert tmp/650981386163034.ps tmp/650981386163034.png",intern=TRUE)) character(0) > try(system("convert tmp/7sh591386163034.ps tmp/7sh591386163034.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 22.642 6.506 29.276