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 = '0' > par6 = '0' > par5 = '1' > 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] [1,] 0.4306407 -0.3446855 -0.9999995 [2,] 0.0000000 -0.2084372 -1.0000004 [3,] NA NA NA [4,] NA NA NA [5,] NA NA NA [6,] NA NA NA [[2]] [,1] [,2] [,3] [1,] 0 0.0000 0 [2,] NA 0.0011 0 [3,] NA NA NA [4,] NA NA NA [5,] NA NA NA [6,] NA NA NA [[3]] [[3]][[1]] Call: arima(x = series, order = order, seasonal = seasonal, include.mean = include.mean, method = "ML") Coefficients: sar1 sar2 sma1 0.4306 -0.3447 -1.0000 s.e. 0.0606 0.0604 0.0134 sigma^2 estimated as 279.2: log likelihood = -1014.99, aic = 2037.97 [[3]][[2]] Call: arima(x = series, order = order, seasonal = seasonal, include.mean = include.mean, method = "ML") Coefficients: sar1 sar2 sma1 0.4306 -0.3447 -1.0000 s.e. 0.0606 0.0604 0.0134 sigma^2 estimated as 279.2: log likelihood = -1014.99, aic = 2037.97 [[3]][[3]] NULL $aic [1] 2037.973 2081.888 Warning message: In arima(series, order = order, seasonal = seasonal, include.mean = include.mean, : some AR parameters were fixed: setting transform.pars = FALSE > postscript(file="/var/www/html/rcomp/tmp/1auvv1229703607.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] 0.201693297 -0.607313369 -3.813893928 -7.339916618 2.962271691 [6] -14.628619226 4.640679654 1.290886821 51.213091050 -6.676782636 [11] -2.229856262 5.382806891 -6.583724720 2.582366372 -3.072298537 [16] -9.889544157 -4.678470291 -7.507162177 -4.234204050 3.833382925 [21] 52.481551400 -11.320735835 11.468366111 3.328376499 -2.938120108 [26] 7.737220420 -3.012824551 -0.904688708 -10.044730375 -2.321009475 [31] -7.406096130 3.387172202 50.630221818 -6.372229744 6.484916522 [36] 8.550364563 -6.306037868 8.880464230 -2.666388669 -8.572049379 [41] -2.977053944 -3.887587768 -9.752544886 8.792344142 45.966625274 [46] -3.168548642 5.305261863 5.936368296 -1.981682221 12.511541411 [51] -1.289339358 -0.005453887 -6.682475423 -1.005776233 -6.500448334 [56] 5.662001569 53.469763673 -3.361648464 10.775261090 -3.429778972 [61] 1.995392789 1.077526929 -2.735249476 -8.850480466 -11.757862541 [66] -7.469182088 -10.550755287 -2.160587398 42.864542371 -1.699881917 [71] 0.053754868 -3.685354488 -9.966577076 3.014295039 -6.422019688 [76] -8.855845340 -14.854994135 -4.949900617 -15.792380803 1.127404195 [81] 47.273372281 -1.702199238 4.468278943 1.854858288 -14.394599060 [86] 8.482955725 -4.257278994 -18.798247302 -12.487744284 -8.657913396 [91] -22.820626672 -6.334741181 50.444324770 -7.251590643 -3.920094604 [96] -7.025044337 -8.732412931 -1.759940775 -6.472212302 -7.403510822 [101] -10.418547904 -7.244596536 -15.148562747 6.083645373 39.387136060 [106] -1.111586956 -1.221004221 -12.181464862 -9.786415552 1.188963794 [111] -12.442712249 -4.714772219 -13.528468459 -11.092853049 -13.396767009 [116] -6.309831639 46.500592941 5.571563856 -11.916792814 -12.980985523 [121] -11.360083949 1.120252561 -7.535744951 -10.586151924 -8.375065453 [126] -12.565220699 -8.738316516 -4.751758116 46.876965822 4.917563211 [131] -15.728724206 -3.223056883 -17.258910831 0.598635578 -5.874296936 [136] -12.686412484 -13.645467596 -1.057523186 -19.424357957 1.772013639 [141] 44.988869980 10.532450387 -17.628573782 3.729692217 -21.200033217 [146] -0.591991092 1.361039037 -11.481640418 -6.599362044 -2.241745457 [151] -8.942024999 -3.625862105 52.660832080 1.194926198 9.778039536 [156] -2.647039133 -26.881007809 10.166147196 -6.210169049 -3.303207827 [161] -2.976117375 -7.289495612 -3.910751038 0.429389499 58.268566889 [166] -18.279796966 15.234713765 -0.771001507 -9.326281410 5.579451474 [171] 1.890816115 -4.343626072 -4.064449534 0.995558340 -10.284828025 [176] 8.802128934 56.174317415 -14.235799866 14.029968622 -8.965031694 [181] -13.645555883 10.724348682 -3.216599705 -1.810277502 -3.694756930 [186] -9.263737216 -4.795827777 4.758320318 45.404357271 -3.605255687 [191] 11.526414591 2.230373911 -16.180785689 6.924344972 -10.986979654 [196] -0.599450448 -6.166779333 -10.143771062 -3.559080897 0.228051561 [201] 48.384029150 -14.475749351 12.711210446 -13.577122805 -11.158577264 [206] 3.121581579 -11.385114185 -1.221529794 -10.691459393 -3.298543675 [211] -2.420183837 -2.183653642 46.005159873 -14.963862470 6.911643809 [216] -28.003466521 -10.852380926 -11.069732269 -0.195151100 -17.272451314 [221] -10.836926677 -3.166760032 -18.577956565 -7.850942636 55.647702608 [226] -18.684732333 -8.376330226 -1.997691866 -18.771622094 5.875560651 [231] -4.859518134 -9.159941305 -10.294727802 0.174781647 -25.007665624 [236] 16.897571909 38.655582812 -12.715342586 -1.604734443 -3.286503540 [241] -4.575663092 > postscript(file="/var/www/html/rcomp/tmp/2kxn51229703607.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/3sstp1229703607.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/4d4zz1229703607.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/531fc1229703607.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/6g4aq1229703607.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/7yo5z1229703607.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/8dtmc1229703607.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/967m91229703607.tab") > > system("convert tmp/1auvv1229703607.ps tmp/1auvv1229703607.png") > system("convert tmp/2kxn51229703607.ps tmp/2kxn51229703607.png") > system("convert tmp/3sstp1229703607.ps tmp/3sstp1229703607.png") > system("convert tmp/4d4zz1229703607.ps tmp/4d4zz1229703607.png") > system("convert tmp/531fc1229703607.ps tmp/531fc1229703607.png") > system("convert tmp/6g4aq1229703607.ps tmp/6g4aq1229703607.png") > system("convert tmp/7yo5z1229703607.ps tmp/7yo5z1229703607.png") > > > proc.time() user system elapsed 1.774 1.077 2.349