R version 2.8.0 (2008-10-20) Copyright (C) 2008 The R Foundation for Statistical Computing ISBN 3-900051-07-0 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(451,450,444,429,421,400,389,384,432,446,431,423,416,416,413,399,386,374,365,365,418,428,424,421,417,423,423,419,406,398,390,391,444,460,455,456,452,459,461,451,443,439,430,436,488,506,502,501,501,515,521,520,512,509,505,511,570,592,594,586,586,592,594,586,572,563,555,554,601,622,617,606,595,599,600,592,575,567,555,555,608,631,629,624,610,616,621,604,584,574,555,545,599,620,608,590,579,580,579,572,560,551,537,541,588,607,599,578,563,566,561,554,540,526,512,505,554,584,569,540,522,526,527,516,503,489,479,475,524,552,532,511,492,492,493,481,462,457,442,439,488,521,501,485,464,460,467,460,448,443,436,431,484,510,513,503,471,471,476,475,470,461,455,456,517,525,523,519,509,512,519,517,510,509,501,507,569,580,578,565,547,555,562,561,555,544,537,543,594,611,613,611,594,595,591,589,584,573,567,569,621,629,628,612,595,597,593,590,580,574,573,573,620,626,620,588,566,557,561,549,532,526,511,499,555,565,542,527,510,514,517,508,493,490,469,478,528,534,518,506,502) > 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.0.44 () > #Author: Prof. Dr. P. Wessa > #To cite this work: AUTHOR(S), (YEAR), YOUR SOFTWARE TITLE (vNUMBER) in Free Statistics Software (v$_version), Office for Research Development and Education, URL http://www.wessa.net/rwasp_YOURPAGE.wasp/ > #Source of accompanying publication: Office for Research, Development, and Education > #Technical description: Write here your technical program description (don't use hard returns!) > 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.8308783 -0.008164905 0.09968918 -0.7884427 0.4254332 0.1105673 [2,] 0.8254910 0.000000000 0.09626711 -0.7872287 0.4260647 0.1113115 [3,] 0.8231035 0.000000000 0.08937500 -0.7769496 0.4266511 0.0000000 [4,] 0.9461693 0.000000000 0.00000000 -0.8566334 0.4500417 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 [13,] NA NA NA NA NA NA [14,] NA NA NA NA NA NA [,7] [1,] -0.9999682 [2,] -1.0000625 [3,] -0.9197450 [4,] -1.0018588 [5,] NA [6,] NA [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 0.926 0.18805 0 0e+00 0.1495 0.00000 [2,] 0 NA 0.14230 0 0e+00 0.1443 0.00000 [3,] 0 NA 0.19721 0 7e-04 NA 0.00000 [4,] 0 NA NA 0 0e+00 NA 0.02512 [5,] NA NA NA NA NA NA NA [6,] NA NA NA NA 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.8309 -0.0082 0.0997 -0.7884 0.4254 0.1106 -1.0000 s.e. 0.1184 0.0878 0.0755 0.0975 0.0738 0.0765 0.1242 sigma^2 estimated as 29.50: log likelihood = -720.47, aic = 1456.95 [[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.8309 -0.0082 0.0997 -0.7884 0.4254 0.1106 -1.0000 s.e. 0.1184 0.0878 0.0755 0.0975 0.0738 0.0765 0.1242 sigma^2 estimated as 29.50: log likelihood = -720.47, aic = 1456.95 [[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.8255 0 0.0963 -0.7872 0.4261 0.1113 -1.0001 s.e. 0.1060 0 0.0654 0.0978 0.0735 0.0760 0.1234 sigma^2 estimated as 29.51: log likelihood = -720.48, aic = 1454.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.8231 0 0.0894 -0.7769 0.4267 0 -0.9197 s.e. 0.1186 0 0.0691 0.1125 0.1242 0 0.1330 sigma^2 estimated as 31.09: log likelihood = -721.49, aic = 1454.99 [[3]][[5]] NULL [[3]][[6]] NULL [[3]][[7]] NULL $aic [1] 1456.947 1454.956 1454.988 1454.677 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 log(s2) : NaNs produced 4: In arima(series, order = order, seasonal = seasonal, include.mean = include.mean, : some AR parameters were fixed: setting transform.pars = FALSE 5: In log(s2) : NaNs produced 6: In log(s2) : NaNs produced > postscript(file="/var/www/html/rcomp/tmp/1drfj1229958966.ps",horizontal=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 = 241 Frequency = 1 [1] 2.603849e-01 1.156730e-01 7.065472e-02 3.943404e-02 2.422109e-02 [6] 8.358893e-04 -9.524667e-03 -1.302576e-02 3.382966e-02 4.375413e-02 [11] 2.543455e-02 -2.242567e-01 -1.626374e+00 8.702284e-01 2.552989e+00 [16] 6.685545e-01 -4.645992e+00 7.697903e+00 1.204537e+00 4.337632e+00 [21] 3.530931e+00 -4.481018e+00 8.770952e+00 3.007305e+00 1.749773e+00 [26] 3.922396e+00 1.478567e+00 7.466751e+00 -3.686802e+00 3.937210e+00 [31] -1.200715e+00 2.868440e-01 -7.551254e-01 2.774889e+00 3.274507e-01 [36] 3.609038e+00 -8.103073e-01 1.370160e+00 1.569529e+00 -3.876873e+00 [41] 2.242112e+00 5.356321e+00 -1.439608e+00 4.829706e+00 -1.663042e+00 [46] 1.737021e+00 1.305736e+00 -8.273465e-01 3.168316e+00 7.155236e+00 [51] 4.144060e+00 7.265881e+00 -1.671561e+00 2.412829e+00 2.030491e+00 [56] 3.768934e-01 4.672818e+00 2.863168e+00 5.245530e+00 -8.281751e+00 [61] -3.677173e-01 -6.020308e+00 -2.151598e+00 -4.036349e+00 -5.560867e+00 [66] -2.142045e+00 -1.425298e+00 -3.497943e+00 -7.376090e+00 3.396854e+00 [71] -1.295388e+00 -2.591765e+00 -7.264967e+00 3.704809e-01 2.010775e+00 [76] 2.807427e+00 -2.839120e+00 3.413172e+00 -2.472319e+00 1.763694e+00 [81] 4.673254e+00 5.156168e+00 4.156183e+00 3.195110e+00 -6.407280e+00 [86] 1.108019e+00 3.763330e+00 -8.489782e+00 -5.670711e+00 -8.839703e-01 [91] -7.589959e+00 -8.469399e+00 3.358274e+00 2.861962e+00 -5.466474e+00 [96] -1.028792e+01 1.172102e+00 -1.954785e+00 -4.466481e-01 9.020992e+00 [101] 6.270845e+00 2.978962e+00 1.362156e+00 9.457628e+00 -6.085782e+00 [106] -3.363500e-01 4.010609e-01 -8.524767e+00 -5.678964e+00 6.398549e-01 [111] -3.545247e+00 2.990083e+00 -5.699728e-01 -2.956619e+00 -4.738469e-01 [116] -7.419352e+00 1.438449e+00 1.283439e+01 -6.121143e+00 -1.260726e+01 [121] -5.830069e+00 1.919430e+00 5.953999e+00 -3.385198e-01 2.217358e+00 [126] -6.028353e-01 3.730148e+00 7.805610e-01 -2.355789e-01 4.019313e+00 [131] -8.487811e+00 -6.312571e-01 -5.351405e+00 -2.820138e+00 1.878462e+00 [136] -2.433650e-01 -4.504763e+00 8.774451e+00 -3.438215e+00 1.177388e+00 [141] -4.102145e-01 9.839203e+00 -5.413848e+00 9.841542e-01 -6.968042e+00 [146] -5.404367e+00 7.148761e+00 4.640855e+00 4.967190e+00 2.793927e+00 [151] 5.618600e+00 -3.680387e+00 2.231168e+00 -2.318897e+00 1.688633e+01 [156] 2.523290e+00 -1.741504e+01 -1.636475e+00 -5.339499e-01 7.218157e+00 [161] 7.159791e+00 -2.245383e+00 1.904901e+00 2.619353e+00 7.646372e+00 [166] -1.755587e+01 2.214580e-01 5.541780e+00 1.179599e+01 7.161926e-01 [171] 2.700247e+00 7.833933e-01 -1.424016e-01 6.278138e+00 -1.845388e+00 [176] 4.453775e+00 3.295688e+00 -5.952974e+00 1.121508e+00 -7.275406e+00 [181] -7.630333e+00 4.138142e+00 2.296435e+00 4.251632e+00 2.965915e+00 [186] -6.672018e+00 1.402280e+00 2.638970e+00 -6.528459e+00 1.073342e+00 [191] 6.364749e+00 1.004234e+01 -2.174524e+00 -5.591794e+00 -9.968950e+00 [196] 1.702985e+00 3.568065e+00 -6.485037e-01 2.697749e+00 -9.963728e-01 [201] -2.275478e-02 -1.064610e+01 1.909001e+00 -9.136630e+00 -2.653377e-01 [206] 9.722413e-01 -1.305954e+00 3.224211e+00 -7.386520e-01 5.319163e+00 [211] 7.561602e+00 -5.061411e-01 -5.319508e+00 -7.954518e+00 -2.080695e+00 [216] -1.776373e+01 -4.489693e+00 -9.373962e+00 9.076976e+00 -3.742237e+00 [221] -2.670353e+00 4.462499e+00 -6.971232e+00 -8.688370e+00 9.184208e+00 [226] 7.843067e-01 -1.345717e+01 9.754266e+00 3.213407e+00 1.023041e+01 [231] 1.860589e+00 1.383060e+00 -9.270069e-01 4.550733e+00 -9.110276e+00 [236] 1.556072e+01 -4.861699e+00 -7.197392e+00 -1.883057e+00 1.954017e+00 [241] 1.236567e+01 > postscript(file="/var/www/html/rcomp/tmp/240s91229958966.ps",horizontal=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/www/html/rcomp/tmp/34ofb1229958966.ps",horizontal=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/www/html/rcomp/tmp/43w941229958966.ps",horizontal=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/www/html/rcomp/tmp/53jx21229958966.ps",horizontal=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/www/html/rcomp/tmp/6s6kg1229958966.ps",horizontal=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/www/html/rcomp/tmp/7suux1229958966.ps",horizontal=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/www/html/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/www/html/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/www/html/rcomp/tmp/86vme1229958966.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/www/html/rcomp/tmp/97sxn1229958966.tab") > > system("convert tmp/1drfj1229958966.ps tmp/1drfj1229958966.png") > system("convert tmp/240s91229958966.ps tmp/240s91229958966.png") > system("convert tmp/34ofb1229958966.ps tmp/34ofb1229958966.png") > system("convert tmp/43w941229958966.ps tmp/43w941229958966.png") > system("convert tmp/53jx21229958966.ps tmp/53jx21229958966.png") > system("convert tmp/6s6kg1229958966.ps tmp/6s6kg1229958966.png") > system("convert tmp/7suux1229958966.ps tmp/7suux1229958966.png") > > > proc.time() user system elapsed 19.160 2.024 20.477