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(67.8,66.9,71.5,75.9,71.9,70.7,73.5,76.1,82.5,87.1,83.2,86.1,85.9,77.4,74.4,69.9,73.8,69.2,69.7,71,71.2,75.8,73,66.4,58.6,55.5,52.6,54.9,54.6,51.2,50.9,49.6,53.4,52,47.5,42.1,44.5,43.2,51.4,59.4,60.3,61.4,68.8,73.6,81.8,79.6,85.8,88.1,89.1,95,96.2,84.2,96.9,103.1,99.3,103.5,112.4,111.1,113.7,92,93,98.4,92.6,94.6,99.5,97.6,91.3,93.6,93.1,78.4,70.2,69.3,71.1,73.5,85.9,91.5,91.8,88.3,91.3,94,99.3,96.7,88,96.7,106.8,114.3,105.7,90.1,91.6,97.7,100.8,104.6,95.9,102.7,104,107.9,113.8,113.8,123.1,125.1,137.6,134,140.3,152.1,150.6,167.3,153.2,142,154.4,158.5,180.9,181.3,172.4,192,199.3,215.4,214.3,201.5,190.5,196,215.7,209.4,214.1,237.8,239,237.8,251.5,248.8,215.4,201.2,203.1,214.2,188.9,203,213.3,228.5,228.2,240.9,258.8,248.5,269.2,289.6,323.4,317.2,322.8,340.9,368.2,388.5,441.2,474.3,483.9,417.9,365.9,263,199.4) > par9 = '1' > par8 = '0' > par7 = '1' > par6 = '3' > par5 = '12' > par4 = '0' > par3 = '2' > par2 = '-0.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] [1,] 0.2927004 -0.01058286 0.02894534 -0.9999630 -0.02704735 [2,] 0.2907517 0.00000000 0.02698375 -0.9999991 -0.02839570 [3,] 0.2903288 0.00000000 0.00000000 -1.0000015 -0.03406514 [4,] 0.2919226 0.00000000 0.00000000 -0.9999989 0.00000000 [5,] NA NA NA NA NA [6,] NA NA NA NA NA [7,] NA NA NA NA NA [8,] NA NA NA NA NA [9,] NA NA NA NA NA [10,] NA NA NA NA NA [[2]] [,1] [,2] [,3] [,4] [,5] [1,] 0.00055 0.90567 0.74779 0 0.76271 [2,] 0.00046 NA 0.76061 0 0.74951 [3,] 0.00047 NA NA 0 0.69610 [4,] 0.00043 NA NA 0 NA [5,] NA NA NA NA NA [6,] NA NA NA NA NA [7,] NA NA NA NA NA [8,] NA NA NA NA NA [9,] NA NA NA NA NA [10,] 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 sma1 0.2927 -0.0106 0.0289 -1.0000 -0.0270 s.e. 0.0829 0.0892 0.0898 0.0206 0.0894 sigma^2 estimated as 2.198e-05: log likelihood = 601.18, aic = -1190.37 [[3]][[2]] Call: arima(x = series, order = order, seasonal = seasonal, include.mean = include.mean, method = "ML") Coefficients: ar1 ar2 ar3 ma1 sma1 0.2927 -0.0106 0.0289 -1.0000 -0.0270 s.e. 0.0829 0.0892 0.0898 0.0206 0.0894 sigma^2 estimated as 2.198e-05: log likelihood = 601.18, aic = -1190.37 [[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 sma1 0.2908 0 0.0270 -1.0000 -0.0284 s.e. 0.0812 0 0.0884 0.0205 0.0888 sigma^2 estimated as 2.198e-05: log likelihood = 601.18, aic = -1192.35 [[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 sma1 0.2903 0 0 -1.0000 -0.0341 s.e. 0.0811 0 0 0.0208 0.0870 sigma^2 estimated as 2.198e-05: log likelihood = 601.13, aic = -1194.26 [[3]][[5]] NULL $aic [1] -1190.366 -1192.351 -1194.258 -1196.107 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 > postscript(file="/var/www/html/rcomp/tmp/1ye8j1228757436.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 = 155 Frequency = 1 [1] 2.933540e-04 -8.780977e-04 -4.199791e-03 -1.192069e-03 5.556338e-03 [6] 3.422947e-04 -2.414812e-03 -8.435029e-04 -3.599089e-03 -7.553279e-04 [11] 4.950006e-03 -2.176705e-03 1.696271e-03 7.250988e-03 6.978341e-04 [16] 3.330504e-03 -4.434465e-03 5.335878e-03 -1.788837e-03 -1.045320e-03 [21] 1.141732e-04 -3.892026e-03 3.959312e-03 5.370233e-03 6.225621e-03 [26] 1.035613e-03 2.081466e-03 -4.250303e-03 6.715486e-04 3.938903e-03 [31] -1.420156e-03 1.100457e-03 -5.903195e-03 2.725572e-03 5.245887e-03 [36] 6.004383e-03 -6.624526e-03 2.596209e-03 -1.277831e-02 -6.579667e-03 [41] 1.714379e-03 -9.036652e-04 -7.225272e-03 -2.106242e-03 -5.589944e-03 [46] 4.021018e-03 -4.955269e-03 1.702009e-04 -1.972046e-04 -3.487537e-03 [51] 2.759966e-04 8.726602e-03 -1.111831e-02 -9.826187e-04 3.590705e-03 [56] -3.044284e-03 -4.210579e-03 2.762926e-03 -1.434034e-03 1.403517e-02 [61] -4.369310e-03 -3.238466e-03 5.140306e-03 -1.959495e-03 -2.924229e-03 [66] 2.369697e-03 4.210383e-03 -2.719357e-03 8.638413e-04 1.112975e-02 [71] 3.945127e-03 -7.719175e-04 -2.056335e-03 -1.749360e-03 -9.149046e-03 [76] -9.948584e-04 1.044071e-03 2.775616e-03 -2.551072e-03 -1.146811e-03 [81] -2.701405e-03 3.268416e-03 5.795406e-03 -7.601054e-03 -4.352861e-03 [86] -2.209866e-03 6.055791e-03 8.805621e-03 -3.818707e-03 -3.518038e-03 [91] -6.756004e-04 -1.587217e-03 6.243898e-03 -5.626716e-03 8.680204e-04 [96] -2.126664e-03 -2.568230e-03 1.130516e-03 -4.409212e-03 9.983009e-04 [101] -5.387655e-03 3.510071e-03 -2.996505e-03 -3.805248e-03 2.583014e-03 [106] -6.337377e-03 7.526611e-03 3.302715e-03 -6.185295e-03 2.779370e-04 [111] -7.226557e-03 2.597843e-03 3.225535e-03 -6.764540e-03 -2.934734e-05 [116] -3.616366e-03 2.147892e-03 3.720714e-03 2.902919e-03 -2.148323e-03 [121] -4.933935e-03 3.791571e-03 -1.643863e-03 -5.208285e-03 2.037985e-03 [126] 5.805328e-04 -2.874002e-03 1.883287e-03 8.661063e-03 2.061465e-03 [131] -1.252953e-03 -2.655199e-03 8.486735e-03 -5.921062e-03 -1.353690e-03 [136] -2.960812e-03 1.701600e-03 -2.745438e-03 -2.898349e-03 4.008313e-03 [141] -4.559013e-03 -2.315526e-03 -4.598033e-03 3.274270e-03 -5.489151e-04 [146] -2.498118e-03 -2.953477e-03 -1.318265e-03 -5.518028e-03 -1.442615e-03 [151] 5.109198e-04 8.932182e-03 5.322795e-03 1.680278e-02 1.082974e-02 > postscript(file="/var/www/html/rcomp/tmp/2048i1228757436.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/3eij71228757436.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/4rscb1228757436.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/590ah1228757436.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/6q03a1228757436.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/7op2a1228757436.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/8av8q1228757436.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/95mua1228757436.tab") > > system("convert tmp/1ye8j1228757436.ps tmp/1ye8j1228757436.png") > system("convert tmp/2048i1228757436.ps tmp/2048i1228757436.png") > system("convert tmp/3eij71228757436.ps tmp/3eij71228757436.png") > system("convert tmp/4rscb1228757436.ps tmp/4rscb1228757436.png") > system("convert tmp/590ah1228757436.ps tmp/590ah1228757436.png") > system("convert tmp/6q03a1228757436.ps tmp/6q03a1228757436.png") > system("convert tmp/7op2a1228757436.ps tmp/7op2a1228757436.png") > > > proc.time() user system elapsed 2.287 1.056 2.862