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(112,118,132,129,121,135,148,148,136,119,104,118,115,126,141,135,125,149,170,170,158,133,114,140,145,150,178,163,172,178,199,199,184,162,146,166,171,180,193,181,183,218,230,242,209,191,172,194,196,196,236,235,229,243,264,272,237,211,180,201,204,188,235,227,234,264,302,293,259,229,203,229,242,233,267,269,270,315,364,347,312,274,237,278,284,277,317,313,318,374,413,405,355,306,271,306,315,301,356,348,355,422,465,467,404,347,305,336,340,318,362,348,363,435,491,505,404,359,310,337,360,342,406,396,420,472,548,559,463,407,362,405,417,391,419,461,472,535,622,606,508,461,390,432) > par9 = '0' > par8 = '2' > par7 = '1' > par6 = '3' > par5 = '12' > par4 = '1' > par3 = '0' > par2 = '-0.3' > par1 = 'FALSE' > par9 <- '0' > par8 <- '2' > par7 <- '1' > par6 <- '3' > par5 <- '12' > par4 <- '1' > par3 <- '0' > par2 <- '-0.3' > 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.2996616 0.9249177 0.34998561 0.9612653 -0.5441443 -0.2159815 [2,] 1.1899708 -0.1212153 -0.07365267 -0.6278245 -0.4749347 0.0000000 [3,] 1.1080083 -0.1141065 0.00000000 -0.5411727 -0.4767338 0.0000000 [4,] 0.9922295 0.0000000 0.00000000 -0.4473626 -0.4782545 0.0000000 [5,] NA NA NA NA NA NA [6,] NA NA NA NA NA NA [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 [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 0.00298 0.00000 0.00004 0.00000 0 0.03451 [2,] 0.00000 0.47652 0.56250 0.00168 0 NA [3,] 0.00000 0.53362 NA 0.00071 0 NA [4,] 0.00000 NA NA 0.00000 0 NA [5,] NA NA NA NA NA NA [6,] NA NA NA NA NA NA [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 [[3]] [[3]][[1]] Call: arima(x = series, order = order, seasonal = seasonal, include.mean = include.mean, method = "ML") Coefficients: ar1 ar2 ar3 ma1 sar1 sar2 -0.2997 0.9249 0.350 0.9613 -0.5441 -0.2160 s.e. 0.0991 0.0590 0.083 0.0653 0.0909 0.1011 sigma^2 estimated as 5.031e-06: log likelihood = 614.42, aic = -1214.84 [[3]][[2]] Call: arima(x = series, order = order, seasonal = seasonal, include.mean = include.mean, method = "ML") Coefficients: ar1 ar2 ar3 ma1 sar1 sar2 -0.2997 0.9249 0.350 0.9613 -0.5441 -0.2160 s.e. 0.0991 0.0590 0.083 0.0653 0.0909 0.1011 sigma^2 estimated as 5.031e-06: log likelihood = 614.42, aic = -1214.84 [[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 1.1900 -0.1212 -0.0737 -0.6278 -0.4749 0 s.e. 0.2174 0.1698 0.1269 0.1960 0.0793 0 sigma^2 estimated as 5.163e-06: log likelihood = 613.41, aic = -1214.82 [[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 1.1080 -0.1141 0 -0.5412 -0.4767 0 s.e. 0.1852 0.1828 0 0.1563 0.0790 0 sigma^2 estimated as 5.175e-06: log likelihood = 613.23, aic = -1216.46 [[3]][[5]] NULL [[3]][[6]] NULL $aic [1] -1214.840 -1214.820 -1216.462 -1218.102 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 log(s2) : NaNs produced 3: In arima(series, order = order, seasonal = seasonal, include.mean = include.mean, : some AR parameters were fixed: setting transform.pars = FALSE 4: In log(s2) : NaNs produced 5: In log(s2) : NaNs produced 6: In arima(series, order = order, seasonal = seasonal, include.mean = include.mean, : some AR parameters were fixed: setting transform.pars = FALSE 7: In log(s2) : NaNs produced > postscript(file="/var/fisher/rcomp/tmp/1px9n1385134834.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 = 144 Frequency = 1 [1] 2.427908e-04 2.390170e-04 2.311090e-04 2.327063e-04 2.372170e-04 [6] 2.295496e-04 2.233026e-04 2.233006e-04 2.290361e-04 2.383962e-04 [11] 2.482284e-04 2.389966e-04 -5.979726e-04 -2.363697e-03 -8.422988e-04 [16] 6.930280e-04 9.389015e-04 -3.481651e-03 -3.629925e-03 -1.818426e-03 [21] -1.942641e-03 9.262241e-04 1.147034e-03 -4.102613e-03 -5.288592e-03 [26] 5.915764e-04 -3.383793e-03 1.779343e-03 -7.928763e-03 4.313374e-03 [31] 1.904376e-03 8.812744e-04 2.701278e-05 -2.426445e-03 -4.655545e-03 [36] 1.238142e-03 -1.382753e-03 3.440824e-04 5.134438e-03 1.745695e-03 [41] -5.234770e-04 -4.032278e-03 2.732584e-03 -2.088898e-03 2.960350e-03 [46] -3.198445e-03 -3.545620e-03 1.960657e-03 2.042542e-03 3.642810e-03 [51] -1.949784e-03 -5.286517e-03 1.072153e-03 3.275807e-03 1.580115e-03 [56] 6.298110e-04 1.278853e-03 6.194653e-04 3.318064e-03 2.540587e-03 [61] 1.541719e-03 7.343692e-03 -2.336822e-03 -6.745808e-04 -2.641493e-03 [66] -1.220474e-03 -4.145803e-03 2.012425e-03 -7.827323e-04 5.559834e-04 [71] -9.125780e-04 -4.413810e-04 -2.735219e-03 -1.699560e-03 3.599943e-03 [76] -2.127366e-04 1.642035e-05 -3.203848e-03 -2.865993e-03 9.310284e-04 [81] -1.562703e-03 -6.283276e-04 -6.178748e-04 -2.410525e-03 -2.297391e-04 [86] -2.486763e-03 1.965588e-03 5.537034e-04 3.407209e-04 -6.313196e-04 [91] 2.193575e-03 -2.762820e-04 3.575681e-04 7.989042e-04 -8.119453e-04 [96] 1.127924e-03 9.701801e-04 1.068591e-03 -8.565115e-04 6.393715e-04 [101] -3.159553e-04 -4.286814e-04 1.252268e-03 -1.432832e-03 2.481381e-04 [106] 2.111183e-04 -5.702687e-04 2.360838e-03 1.685704e-03 2.374830e-03 [111] 2.438244e-03 2.235546e-03 -1.631826e-04 -4.236716e-04 -1.157987e-03 [116] -2.212902e-03 2.796477e-03 -7.535059e-04 7.249150e-04 1.702628e-03 [121] -1.615094e-03 -9.937372e-04 -1.474726e-03 -1.112200e-03 -1.878384e-03 [126] 2.317813e-03 -8.026871e-04 -3.975846e-04 -4.735962e-04 -8.111796e-04 [131] -1.808144e-03 -1.757617e-03 -2.622549e-04 -1.469060e-04 4.213616e-03 [136] -4.477925e-03 -3.375395e-04 1.018861e-03 -7.010855e-06 2.219470e-03 [141] -7.998193e-04 -1.837931e-03 5.398960e-04 1.071927e-04 > postscript(file="/var/fisher/rcomp/tmp/2skr91385134834.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/fisher/rcomp/tmp/3nji51385134834.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/fisher/rcomp/tmp/4f4891385134834.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/fisher/rcomp/tmp/5ipl61385134834.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/fisher/rcomp/tmp/6a59x1385134834.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/fisher/rcomp/tmp/7hbmm1385134834.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/fisher/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/fisher/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/fisher/rcomp/tmp/8wuc51385134834.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/fisher/rcomp/tmp/92o671385134834.tab") > > try(system("convert tmp/1px9n1385134834.ps tmp/1px9n1385134834.png",intern=TRUE)) character(0) > try(system("convert tmp/2skr91385134834.ps tmp/2skr91385134834.png",intern=TRUE)) character(0) > try(system("convert tmp/3nji51385134834.ps tmp/3nji51385134834.png",intern=TRUE)) character(0) > try(system("convert tmp/4f4891385134834.ps tmp/4f4891385134834.png",intern=TRUE)) character(0) > try(system("convert tmp/5ipl61385134834.ps tmp/5ipl61385134834.png",intern=TRUE)) character(0) > try(system("convert tmp/6a59x1385134834.ps tmp/6a59x1385134834.png",intern=TRUE)) character(0) > try(system("convert tmp/7hbmm1385134834.ps tmp/7hbmm1385134834.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 13.796 1.600 15.386