R version 2.9.0 (2009-04-17) Copyright (C) 2009 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(100.00,102.04,102.51,102.71,103.00,103.39,102.32,103.88,104.65,104.46,104.65,104.36,102.71,104.55,104.76,105.72,106.20,106.50,105.14,106.50,106.69,106.50,106.50,106.39,105.43,107.18,107.37,107.46,107.66,107.37,106.30,107.85,107.95,107.85,107.66,107.76,106.69,108.92,109.22,109.02,108.62,109.02,107.76,109.60,109.80,109.41,109.60,109.60,108.15,110.18,110.27,110.87,111.25,111.15,109.99,111.83,111.73,112.31,112.12,111.73,110.27,112.71,113.38,113.57,113.77,114.15,112.99,115.03,115.03,114.84,114.75,114.84,113.32,115.92,115.84,116.49,116.90,116.99,115.74,117.73,117.17,116.83,117.08,117.23,115.25,117.98,117.97,118.56,118.42,118.51,117.25,119.08,118.85,119.41,120.43,120.87,119.31,122.24,123.14,123.39,124.46,125.33,124.17,125.48,125.35,125.15,124.31,124.14,121.81,124.62,123.93,124.29,124.16,124.02,122.00,124.58,124.06) > par9 = '1' > par8 = '2' > par7 = '1' > par6 = '3' > par5 = '12' > par4 = '1' > par3 = '1' > par2 = '-1.4' > 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.3125753 0.12498072 -0.02994452 0.4909178 -0.7406631 -0.4291441 [2,] -0.4628639 0.15812692 0.00000000 0.6380071 -0.7354449 -0.4275930 [3,] 0.0000000 0.06994566 0.00000000 0.1762787 -0.7352134 -0.4267117 [4,] 0.0000000 0.00000000 0.00000000 0.1627224 -0.7294417 -0.4151685 [5,] 0.0000000 0.00000000 0.00000000 0.1933509 -0.9697591 -0.5552288 [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.3054329 [2,] -0.3063002 [3,] -0.3043554 [4,] -0.3205568 [5,] 0.0000000 [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.75811 0.53857 0.79729 0.62706 0.00094 0.00966 0.20617 [2,] 0.53308 0.27350 NA 0.39076 0.00091 0.00947 0.20148 [3,] NA 0.49382 NA 0.08916 0.00092 0.00969 0.20265 [4,] NA NA NA 0.08979 0.00121 0.01457 0.18775 [5,] NA NA NA 0.03412 0.00000 0.00000 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.3126 0.1250 -0.0299 0.4909 -0.7407 -0.4291 -0.3054 s.e. 1.0124 0.2026 0.1163 1.0075 0.2178 0.1629 0.2402 sigma^2 estimated as 3.041e-11: log likelihood = 1103.92, aic = -2191.84 [[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.3126 0.1250 -0.0299 0.4909 -0.7407 -0.4291 -0.3054 s.e. 1.0124 0.2026 0.1163 1.0075 0.2178 0.1629 0.2402 sigma^2 estimated as 3.041e-11: log likelihood = 1103.92, aic = -2191.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 sma1 -0.4629 0.1581 0 0.6380 -0.7354 -0.4276 -0.3063 s.e. 0.7403 0.1437 0 0.7405 0.2156 0.1619 0.2384 sigma^2 estimated as 3.046e-11: log likelihood = 1103.89, aic = -2193.78 [[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 0.0699 0 0.1763 -0.7352 -0.4267 -0.3044 s.e. 0 0.1019 0 0.1028 0.2158 0.1621 0.2375 sigma^2 estimated as 3.052e-11: log likelihood = 1103.81, aic = -2195.62 [[3]][[5]] 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 0 0 0.1627 -0.7294 -0.4152 -0.3206 s.e. 0 0 0 0.0951 0.2196 0.1673 0.2419 sigma^2 estimated as 3.063e-11: log likelihood = 1103.58, aic = -2197.15 [[3]][[6]] NULL [[3]][[7]] NULL $aic [1] -2191.838 -2193.780 -2195.624 -2197.151 -2197.515 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 4: 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/1ifh51260470490.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 = 117 Frequency = 1 [1] 9.150381e-07 3.749940e-07 2.371399e-07 1.730740e-07 1.326245e-07 [6] 1.029682e-07 1.088352e-07 6.495352e-08 4.309245e-08 4.237487e-08 [11] 3.489389e-08 -8.246190e-07 -5.531874e-06 4.307152e-06 3.014775e-06 [16] -1.005308e-05 -4.937622e-07 1.554257e-06 2.438782e-06 3.379621e-06 [21] 7.141460e-06 -1.293914e-06 2.655500e-06 -2.857850e-06 -9.104064e-06 [26] 7.758801e-06 2.115666e-06 6.390230e-06 1.877173e-06 1.001062e-05 [31] -4.558130e-06 1.321188e-06 7.185341e-06 -2.724834e-06 5.234505e-06 [36] -5.981821e-06 -4.832184e-06 2.864482e-07 1.787106e-06 1.297596e-05 [41] 1.208836e-05 -2.804528e-06 -2.362367e-07 -3.650929e-06 6.942092e-06 [46] 2.140417e-06 -1.830896e-06 -3.553478e-06 3.929155e-08 6.559661e-07 [51] 3.201298e-06 5.790377e-07 8.859629e-07 3.949732e-06 -4.170795e-06 [56] -3.165822e-06 7.248485e-06 -1.388910e-05 4.795237e-06 4.311341e-06 [61] 3.226621e-06 -5.236750e-06 -5.559677e-06 5.415705e-07 -1.993954e-06 [66] -4.959454e-06 -2.480382e-06 -2.630472e-06 4.007093e-06 -1.998922e-06 [71] 1.307889e-06 -1.033051e-06 2.604563e-06 -3.610732e-06 5.516163e-06 [76] -8.444654e-06 -5.582344e-06 1.978172e-06 -3.150718e-06 1.911216e-06 [81] 1.022387e-05 2.460700e-06 -3.879733e-06 -3.594898e-06 6.514044e-06 [86] -4.464040e-06 6.255704e-06 -4.145004e-06 5.958062e-06 -3.627181e-07 [91] -1.581645e-06 5.350098e-06 2.879942e-06 -5.589979e-06 -1.558830e-05 [96] -6.714992e-06 -1.179857e-06 -5.155846e-07 -6.201692e-06 3.472794e-06 [101] -1.029099e-05 -6.104527e-06 -3.423284e-06 1.577109e-05 -3.253612e-06 [106] 8.018453e-07 1.010933e-05 6.245605e-07 4.780011e-06 2.826324e-06 [111] 9.356234e-06 2.348979e-06 4.010476e-06 2.755000e-06 7.383872e-06 [116] -5.150096e-06 2.443454e-06 > postscript(file="/var/www/html/rcomp/tmp/21plt1260470490.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/3yr4q1260470490.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/4vun61260470490.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/5ldbx1260470490.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/6guce1260470490.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/7jovf1260470490.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/8mh5a1260470490.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/9a3vu1260470490.tab") > system("convert tmp/1ifh51260470490.ps tmp/1ifh51260470490.png") > system("convert tmp/21plt1260470490.ps tmp/21plt1260470490.png") > system("convert tmp/3yr4q1260470490.ps tmp/3yr4q1260470490.png") > system("convert tmp/4vun61260470490.ps tmp/4vun61260470490.png") > system("convert tmp/5ldbx1260470490.ps tmp/5ldbx1260470490.png") > system("convert tmp/6guce1260470490.ps tmp/6guce1260470490.png") > system("convert tmp/7jovf1260470490.ps tmp/7jovf1260470490.png") > > > proc.time() user system elapsed 9.510 1.743 12.468