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 = '1' > par3 = '1' > par2 = '1' > par1 = 'FALSE' > par9 <- '1' > par8 <- '2' > par7 <- '1' > par6 <- '3' > par5 <- '12' > par4 <- '1' > 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.08219311 0.08276633 -0.07864390 -0.9279119 0.04797418 -0.0367593 [2,] 0.07812353 0.07675432 -0.09074052 -0.9257863 0.05281734 0.0000000 [3,] 0.08089687 0.07297782 -0.09831910 -1.0842779 0.00000000 0.0000000 [4,] 0.07193391 0.00000000 -0.10530290 -0.9066325 0.00000000 0.0000000 [5,] 0.00000000 0.00000000 -0.11589766 -1.1290463 0.00000000 0.0000000 [6,] 0.00000000 0.00000000 0.00000000 -1.0975518 0.00000000 0.0000000 [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.9999894 [2,] -1.0002038 [3,] -0.9999865 [4,] -0.9999928 [5,] -1.0000487 [6,] -0.9988077 [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.41964 0.41448 0.46442 0 0.65912 0.74669 0.00031 [2,] 0.44030 0.44175 0.36770 0 0.62581 NA 0.00001 [3,] 0.42339 0.46224 0.32240 0 NA NA 0.00326 [4,] 0.48600 NA 0.29403 0 NA NA 0.00112 [5,] NA NA 0.25049 0 NA NA 0.00741 [6,] NA NA NA 0 NA NA 0.00383 [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.0822 0.0828 -0.0786 -0.9279 0.0480 -0.0368 -1.0000 s.e. 0.1015 0.1011 0.1072 0.0522 0.1085 0.1135 0.2696 sigma^2 estimated as 9.544: log likelihood = -313.45, aic = 642.89 [[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.0822 0.0828 -0.0786 -0.9279 0.0480 -0.0368 -1.0000 s.e. 0.1015 0.1011 0.1072 0.0522 0.1085 0.1135 0.2696 sigma^2 estimated as 9.544: log likelihood = -313.45, aic = 642.89 [[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.0781 0.0768 -0.0907 -0.9258 0.0528 0 -1.0002 s.e. 0.1009 0.0995 0.1004 0.0524 0.1080 0 0.2175 sigma^2 estimated as 9.624: log likelihood = -313.5, aic = 641 [[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.0809 0.073 -0.0983 -1.0843 0 0 -1.0000 s.e. 0.1007 0.099 0.0990 0.0614 0 0 0.3333 sigma^2 estimated as 8.127: log likelihood = -313.62, aic = 639.24 [[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.0719 0 -0.1053 -0.9066 0 0 -1.0000 s.e. 0.1029 0 0.0999 0.0568 0 0 0.2999 sigma^2 estimated as 9.616: log likelihood = -313.89, aic = 637.78 [[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 0 -0.1159 -1.1290 0 0 -1.0000 s.e. 0 0 0.1004 0.0808 0 0 0.3674 sigma^2 estimated as 7.59: log likelihood = -314.13, aic = 636.26 [[3]][[7]] NULL $aic [1] 642.8918 640.9952 639.2372 637.7785 636.2567 635.5760 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/15hbw1386164842.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] 2.309399e-03 1.807391e-03 2.873521e-03 3.779645e-04 1.206045e-03 [6] 8.362505e-05 -1.790285e-03 2.191783e-03 1.947450e-03 5.556944e-03 [11] 6.961733e-03 4.170282e-03 -7.999837e-03 -1.397161e+00 -2.408160e+00 [16] -2.004034e+00 -1.073028e+00 -2.817329e+00 1.289222e+00 -3.134101e+00 [21] 1.426082e+00 -4.101923e+00 -5.198035e+00 -1.826603e-01 2.598307e+00 [26] 8.998421e-01 1.455372e+00 -1.602005e+00 2.419912e+00 1.739614e+00 [31] 1.135564e+00 5.360626e+00 -4.354897e+00 -2.144801e+00 -1.342558e+00 [36] 7.162255e-01 2.902123e+00 2.082753e-01 -7.677649e-03 4.765474e+00 [41] -2.469954e+00 5.148223e+00 -1.824301e+00 -1.833962e+00 -1.013114e+00 [46] -2.822414e+00 7.134940e-01 -4.975344e+00 8.849797e-02 -2.904085e+00 [51] 1.173743e+00 1.148198e+00 2.781524e+00 2.348620e+00 5.381322e+00 [56] -3.316773e+00 7.621286e-01 9.762729e-01 2.356371e+00 -1.826654e+00 [61] 3.100748e-01 1.030055e+00 3.145443e+00 8.284129e+00 1.048752e+00 [66] -1.661824e+00 3.515605e+00 -2.801272e+00 1.065817e+00 -3.460207e+00 [71] -2.463750e+00 -2.865086e+00 3.880878e-02 7.109402e+00 1.081302e+00 [76] 1.711233e+00 -1.367453e+00 -1.790435e+00 -1.117674e+00 -8.723933e-01 [81] 4.962018e+00 2.748913e+00 -7.988825e+00 -7.924466e-01 3.376087e+00 [86] 3.543356e-01 7.186202e-01 -1.000709e+00 -1.755327e+00 -2.404203e+00 [91] -1.182721e+00 9.969227e-01 -2.527148e+00 -2.552673e-01 3.368033e+00 [96] 7.459761e+00 -1.051430e+00 4.039452e+00 5.793114e-01 2.730240e+00 [101] -1.852355e+00 5.593737e-01 -5.984710e-01 -1.713791e+00 -7.981912e-01 [106] 7.546103e-01 1.035487e+00 -5.857917e-01 -6.166530e-01 2.521228e-01 [111] 1.908633e+00 -5.860782e-01 -5.243530e-01 2.924365e+00 -1.494543e+00 [116] 3.143691e+00 2.681613e+00 3.390767e+00 1.393516e+00 -1.050009e+00 [121] 1.485807e+00 8.980355e-01 5.597349e+00 -2.724857e+00 -3.356098e+00 [126] 1.332090e+00 1.022021e+00 -5.282858e+00 -4.466635e-01 1.682666e-01 > postscript(file="/var/wessaorg/rcomp/tmp/22ris1386164842.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/3kus21386164842.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/4t62t1386164842.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/5a2au1386164842.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/615al1386164842.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/7vx8d1386164842.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/8211b1386164842.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/961rx1386164842.tab") > > try(system("convert tmp/15hbw1386164842.ps tmp/15hbw1386164842.png",intern=TRUE)) character(0) > try(system("convert tmp/22ris1386164842.ps tmp/22ris1386164842.png",intern=TRUE)) character(0) > try(system("convert tmp/3kus21386164842.ps tmp/3kus21386164842.png",intern=TRUE)) character(0) > try(system("convert tmp/4t62t1386164842.ps tmp/4t62t1386164842.png",intern=TRUE)) character(0) > try(system("convert tmp/5a2au1386164842.ps tmp/5a2au1386164842.png",intern=TRUE)) character(0) > try(system("convert tmp/615al1386164842.ps tmp/615al1386164842.png",intern=TRUE)) character(0) > try(system("convert tmp/7vx8d1386164842.ps tmp/7vx8d1386164842.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 21.956 4.244 26.197