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(56,55,54,52,72,71,56,46,47,47,48,50,44,38,33,33,52,54,39,22,31,31,38,42,41,31,36,34,51,47,31,19,30,33,36,40,32,25,28,29,55,55,40,38,44,41,49,59,61,47,43,39,66,68,63,68,67,59,68,78,82,70,62,68,94,102,100,104,103,93,110,114,120,102,95,103,122,139,135,135,137,130,148,148,145,128,131,133,146,163,151,157,152,149,172,167,160,150,160,165,171,179,171,176,170,169,194,196,188,174,186,191,197,206,197,204,201,190,213,213) > 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.2652246 -0.07079466 -0.1463606 -0.3939420 0.7369737 -0.1256289 [2,] 0.3169632 0.00000000 -0.1539941 -0.4650768 0.7336626 -0.1513214 [3,] 0.0000000 0.00000000 -0.1714859 -0.1526251 0.7167768 -0.1527232 [4,] 0.0000000 0.00000000 -0.1416843 0.0000000 0.6440616 -0.1456466 [5,] 0.0000000 0.00000000 -0.1560629 0.0000000 -0.2177814 0.0000000 [6,] 0.0000000 0.00000000 -0.1559323 0.0000000 0.0000000 0.0000000 [7,] 0.0000000 0.00000000 -0.1559703 0.0000000 0.0000000 0.0000000 [8,] 0.0000000 0.00000000 0.0000000 0.0000000 0.0000000 0.0000000 [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.7306526 [2,] -0.7047175 [3,] -0.6990261 [4,] -0.6352597 [5,] 0.2482167 [6,] 0.0234151 [7,] 0.0000000 [8,] 0.0000000 [9,] NA [10,] NA [11,] NA [12,] NA [13,] NA [14,] NA [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 0.40070 0.52809 0.18634 0.20590 0.09164 0.28706 0.10875 [2,] 0.25989 NA 0.15795 0.08906 0.07170 0.15844 0.09582 [3,] NA NA 0.08735 0.15825 0.06104 0.15728 0.07794 [4,] NA NA 0.15064 NA 0.09073 0.17232 0.10317 [5,] NA NA 0.10977 NA 0.82734 NA 0.80217 [6,] NA NA 0.11001 NA NA NA 0.82744 [7,] NA NA 0.10973 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.2652 -0.0708 -0.1464 -0.3939 0.7370 -0.1256 -0.7307 s.e. 0.3144 0.1119 0.1101 0.3096 0.4332 0.1174 0.4519 sigma^2 estimated as 23.35: log likelihood = -320.93, aic = 657.86 [[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.2652 -0.0708 -0.1464 -0.3939 0.7370 -0.1256 -0.7307 s.e. 0.3144 0.1119 0.1101 0.3096 0.4332 0.1174 0.4519 sigma^2 estimated as 23.35: log likelihood = -320.93, aic = 657.86 [[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.3170 0 -0.1540 -0.4651 0.7337 -0.1513 -0.7047 s.e. 0.2799 0 0.1083 0.2712 0.4035 0.1066 0.4196 sigma^2 estimated as 23.41: log likelihood = -321.12, aic = 656.24 [[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 0 -0.1715 -0.1526 0.7168 -0.1527 -0.699 s.e. 0 0 0.0994 0.1075 0.3789 0.1073 0.393 sigma^2 estimated as 23.65: log likelihood = -321.69, aic = 655.37 [[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 0 -0.1417 0 0.6441 -0.1456 -0.6353 s.e. 0 0 0.0979 0 0.3776 0.1061 0.3867 sigma^2 estimated as 24.14: log likelihood = -322.66, aic = 655.31 [[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.1561 0 -0.2178 0 0.2482 s.e. 0 0 0.0968 0 0.9962 0 0.9884 sigma^2 estimated as 24.93: log likelihood = -323.93, aic = 655.87 [[3]][[7]] 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.1559 0 0 0 0.0234 s.e. 0 0 0.0968 0 0 0 0.1072 sigma^2 estimated as 24.94: log likelihood = -323.96, aic = 653.92 [[3]][[8]] 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.1560 0 0 0 0 s.e. 0 0 0.0968 0 0 0 0 sigma^2 estimated as 24.96: log likelihood = -323.98, aic = 651.96 $aic [1] 657.8550 656.2412 655.3747 655.3127 655.8661 653.9151 651.9625 652.5243 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 6: In arima(series, order = order, seasonal = seasonal, include.mean = include.mean, : some AR parameters were fixed: setting transform.pars = FALSE 7: In max(i) : no non-missing arguments to max; returning -Inf 8: In max(i) : no non-missing arguments to max; returning -Inf 9: In max(try.data.frame[, 4], na.rm = TRUE) : no non-missing arguments to max; returning -Inf > postscript(file="/var/wessaorg/rcomp/tmp/1gnc41387556180.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 = 120 Frequency = 1 [1] 3.233161e-02 1.368453e-02 8.113467e-03 4.283585e-03 2.150778e-02 [6] 1.697566e-02 5.728390e-04 -8.892417e-03 -6.955195e-03 -6.257821e-03 [11] -4.732146e-03 -3.450900e-02 -2.364930e-01 -4.938915e+00 -3.951162e+00 [16] 1.975388e+00 -1.779851e+00 2.376119e+00 3.119405e-01 -7.155970e+00 [21] 8.467911e+00 -1.411274e-10 4.908208e+00 3.247762e+00 5.000000e+00 [26] -3.064178e+00 1.031194e+01 -1.220149e+00 -2.623881e+00 -4.440297e+00 [31] -1.311941e+00 4.688059e+00 1.064178e+00 2.844030e+00 -3.220149e+00 [36] 3.119405e-01 -6.532089e+00 2.376119e+00 -2.000000e+00 1.908208e+00 [41] 9.467911e+00 3.688059e+00 1.467911e+00 1.140373e+01 -4.376119e+00 [46] -5.844030e+00 6.559703e+00 5.220149e+00 9.064178e+00 -6.220149e+00 [51] -6.064178e+00 -3.440297e+00 -9.179180e-02 9.082082e-01 9.220149e+00 [56] 7.155970e+00 -6.688059e+00 -3.440297e+00 2.091792e+00 -1.091792e+00 [61] 1.220149e+00 2.155970e+00 -4.000000e+00 1.031194e+01 -6.880595e-01 [66] 5.376119e+00 4.559703e+00 -1.155970e+00 9.358215e-01 -1.532089e+00 [71] 7.844030e+00 -6.000000e+00 1.688059e+00 -4.752238e+00 6.417846e-02 [76] 2.311941e+00 -7.935822e+00 9.155970e+00 -1.688059e+00 -5.091792e+00 [81] 4.403732e+00 2.688059e+00 3.761190e-01 -3.532089e+00 -8.532089e+00 [86] 1.155970e+00 9.376119e+00 -7.403732e+00 -5.844030e+00 1.559703e+00 [91] -8.935822e+00 5.064178e+00 -7.000000e+00 2.752238e+00 5.935822e+00 [96] -6.091792e+00 -3.376119e+00 7.779851e+00 6.220149e+00 2.376119e+00 [101] -5.908208e+00 -7.908208e+00 4.467911e+00 -2.091792e+00 -2.403732e+00 [106] 2.623881e+00 1.844030e+00 6.844030e+00 -6.880595e-01 -3.688059e+00 [111] 3.091792e+00 -1.559703e-01 -6.238810e-01 1.311941e+00 -1.000000e+00 [116] 2.000000e+00 3.155970e+00 -1.015597e+01 -1.688059e+00 -1.532089e+00 > postscript(file="/var/wessaorg/rcomp/tmp/2izm71387556180.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/3et4g1387556181.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/426fq1387556181.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/5c14o1387556181.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/6zba11387556181.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/7vh1t1387556181.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/837l41387556181.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/9i7301387556181.tab") > > try(system("convert tmp/1gnc41387556180.ps tmp/1gnc41387556180.png",intern=TRUE)) character(0) > try(system("convert tmp/2izm71387556180.ps tmp/2izm71387556180.png",intern=TRUE)) character(0) > try(system("convert tmp/3et4g1387556181.ps tmp/3et4g1387556181.png",intern=TRUE)) character(0) > try(system("convert tmp/426fq1387556181.ps tmp/426fq1387556181.png",intern=TRUE)) character(0) > try(system("convert tmp/5c14o1387556181.ps tmp/5c14o1387556181.png",intern=TRUE)) character(0) > try(system("convert tmp/6zba11387556181.ps tmp/6zba11387556181.png",intern=TRUE)) character(0) > try(system("convert tmp/7vh1t1387556181.ps tmp/7vh1t1387556181.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 25.404 4.812 30.200