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) > par20 = '' > par19 = '' > par18 = '' > par17 = '' > par16 = '' > par15 = '' > par14 = '' > par13 = '' > par12 = '' > par11 = '' > par10 = '' > par9 = '1' > par8 = '2' > par7 = '0' > par6 = '0' > par5 = '12' > par4 = '1' > par3 = '1' > par2 = '1' > par1 = 'FALSE' > ylab = '' > xlab = '' > main = '' > #'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.4998187 0.0410734 -0.999982 [2,] 0.5141685 0.0000000 -1.000005 [3,] NA NA NA [4,] NA NA NA [5,] NA NA NA [6,] NA NA NA [[2]] [,1] [,2] [,3] [1,] 0 0.57952 0 [2,] 0 NA 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.4998 0.0411 -1.0000 s.e. 0.0708 0.0740 0.0752 sigma^2 estimated as 31.78: log likelihood = -729.09, aic = 1466.19 [[3]][[2]] Call: arima(x = series, order = order, seasonal = seasonal, include.mean = include.mean, method = "ML") Coefficients: sar1 sar2 sma1 0.4998 0.0411 -1.0000 s.e. 0.0708 0.0740 0.0752 sigma^2 estimated as 31.78: log likelihood = -729.09, aic = 1466.19 [[3]][[3]] NULL $aic [1] 1466.186 1464.495 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/1ypbm1229952643.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.065475e-02 3.943411e-02 2.422121e-02 [6] 8.360552e-04 -9.524474e-03 -1.302556e-02 3.382990e-02 4.375446e-02 [11] 2.543499e-02 -2.242566e-01 -1.626427e+00 8.710996e-01 2.613756e+00 [16] 8.708012e-01 -4.357765e+00 7.841744e+00 1.741787e+00 4.355868e+00 [21] 4.355742e+00 -3.487035e+00 9.583872e+00 4.355314e+00 2.612167e+00 [26] 5.675862e+00 3.450158e+00 9.296399e+00 -1.225303e+00 5.824868e+00 [31] 1.394473e+00 2.129290e+00 1.224027e+00 4.450322e+00 1.788462e+00 [36] 4.844476e+00 7.335861e-01 2.850806e+00 3.221403e+00 -2.415376e+00 [41] 3.769899e+00 6.353495e+00 -2.942570e-01 5.726426e+00 -8.965172e-02 [46] 2.939208e+00 2.454388e+00 1.660392e-01 4.179014e+00 8.267418e+00 [51] 5.440450e+00 8.721822e+00 9.037081e-01 4.361810e+00 4.831491e+00 [56] 2.542350e+00 6.907690e+00 5.083837e+00 7.264131e+00 -5.598780e+00 [61] 1.719832e+00 -3.608601e+00 -9.535677e-01 -2.940494e+00 -5.039809e+00 [66] -2.444907e+00 -1.840278e+00 -4.545553e+00 -8.479632e+00 1.553192e+00 [71] -3.077276e+00 -4.353306e+00 -8.985108e+00 -1.589062e+00 -5.769434e-02 [76] 3.464877e-01 -4.481224e+00 1.571158e+00 -3.723164e+00 2.155142e-01 [81] 3.541288e+00 3.590409e+00 3.179833e+00 3.497483e+00 -5.623996e+00 [86] 1.736061e+00 4.385346e+00 -7.976889e+00 -5.226611e+00 -7.296044e-01 [91] -7.952394e+00 -9.521654e+00 1.754687e+00 4.170106e-01 -7.804525e+00 [96] -1.195554e+01 -3.516406e-01 -4.076830e+00 -3.618408e+00 6.603337e+00 [101] 4.444245e+00 1.208414e+00 1.048883e+00 9.459996e+00 -5.807614e+00 [106] -6.553946e-01 1.385873e+00 -7.762693e+00 -5.457671e+00 4.996659e-01 [111] -4.435154e+00 1.613568e+00 -1.561850e+00 -4.089304e+00 -1.159977e+00 [116] -8.198260e+00 -8.870435e-02 1.098912e+01 -6.968407e+00 -1.284023e+01 [121] -6.081570e+00 6.592603e-01 3.840952e+00 -2.755118e+00 1.663017e-01 [126] -1.409613e+00 2.580947e+00 5.638699e-02 -7.672084e-01 3.008069e+00 [131] -8.035874e+00 -1.881821e-01 -4.975540e+00 -3.809354e+00 8.315280e-01 [136] -1.594263e+00 -6.038594e+00 7.574100e+00 -4.357463e+00 6.382705e-03 [141] -7.917452e-01 8.147099e+00 -4.773501e+00 1.108725e+00 -5.949331e+00 [146] -5.530710e+00 6.341510e+00 3.990724e+00 3.995599e+00 2.718545e+00 [151] 5.929419e+00 -2.531277e+00 3.130128e+00 -1.504294e+00 1.799063e+01 [156] 4.103447e+00 -1.518669e+01 7.888861e-01 1.100200e+00 7.174838e+00 [161] 7.378822e+00 -1.686667e+00 2.851844e+00 4.371666e+00 8.751224e+00 [166] -1.566551e+01 8.781743e-01 6.557175e+00 1.249475e+01 1.873858e+00 [171] 3.709123e+00 2.670612e+00 1.326402e+00 8.122489e+00 -6.230983e-02 [176] 6.137793e+00 5.165693e+00 -2.816152e+00 2.336025e+00 -5.719254e+00 [181] -6.247198e+00 5.014910e+00 2.610895e+00 3.741527e+00 2.918111e+00 [186] -5.784250e+00 1.846163e+00 3.126732e+00 -6.625963e+00 2.436720e+00 [191] 6.296785e+00 9.433039e+00 -1.917559e+00 -4.621613e+00 -8.321236e+00 [196] 2.123898e+00 3.331935e+00 -9.316097e-01 2.316340e+00 -1.133999e+00 [201] 1.192816e-02 -9.499280e+00 1.096071e+00 -9.695365e+00 -1.983543e+00 [206] -2.032306e-01 -2.543125e+00 1.489905e+00 -2.238821e+00 4.392313e+00 [211] 6.549872e+00 -1.078070e+00 -4.912278e+00 -6.828416e+00 -2.533195e+00 [216] -1.843250e+01 -6.801321e+00 -1.114255e+01 5.831160e+00 -6.841411e+00 [221] -6.567639e+00 1.742981e+00 -9.913407e+00 -1.160837e+01 6.510310e+00 [226] -1.260410e+00 -1.645273e+01 7.415843e+00 8.451726e-01 7.406323e+00 [231] 6.799615e-01 8.395572e-01 -6.932780e-01 4.395787e+00 -8.688953e+00 [236] 1.539127e+01 -3.847420e+00 -6.987584e+00 -3.556012e-01 2.347935e+00 [241] 1.127292e+01 > postscript(file="/var/www/html/rcomp/tmp/214ac1229952643.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/38wy41229952643.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/4kkpv1229952643.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/5dvbt1229952643.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/68j841229952643.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/7anfz1229952643.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/8q4hc1229952643.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/9jwe11229952643.tab") > > system("convert tmp/1ypbm1229952643.ps tmp/1ypbm1229952643.png") > system("convert tmp/214ac1229952643.ps tmp/214ac1229952643.png") > system("convert tmp/38wy41229952643.ps tmp/38wy41229952643.png") > system("convert tmp/4kkpv1229952643.ps tmp/4kkpv1229952643.png") > system("convert tmp/5dvbt1229952643.ps tmp/5dvbt1229952643.png") > system("convert tmp/68j841229952643.ps tmp/68j841229952643.png") > system("convert tmp/7anfz1229952643.ps tmp/7anfz1229952643.png") > > > proc.time() user system elapsed 3.658 1.109 4.190