R version 2.12.0 (2010-10-15) Copyright (C) 2010 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: i486-pc-linux-gnu (32-bit) 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(336.02,333.15,314.95,302.48,307.31,305.50,308.57,322.58,337.09,323.81,333.06,331.90,327.90,319.93,331.51,336.42,319.77,323.20,324.51,328.34,331.88,336.45,337.95,330.75,323.87,325.26,328.73,331.72,332.54,354.25,352.69,356.15,372.50,390.90,404.65,430.04,453.54,464.98,463.31,497.20,528.62,470.91,499.53,493.51,469.97,464.41,487.15,476.45,484.91,509.61,495.19,504.75,493.43,488.58,484.82,488.46,512.32,530.29,549.38,551.45,604.41,625.29,623.56,577.42,572.28,571.69,596.28,560.00,577.93,606.51,597.31,607.58,648.14,737.48,708.73,674.01,679.90,674.93,663.38,665.69,684.21,703.71,755.42,772.43) > par9 = '1' > par8 = '2' > par7 = '1' > par6 = '3' > par5 = '12' > par4 = '0' > par3 = '1' > par2 = '0.5' > 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] [,7] [1,] 0.29957482 -0.1278883 0.2011640 -0.2462614 0.2638581 0.1697109 -0.1453726 [2,] 0.29903883 -0.1242512 0.2051790 -0.2497015 0.1252119 0.1883522 0.0000000 [3,] 0.05943773 -0.1170653 0.1633720 0.0000000 0.1150298 0.1894838 0.0000000 [4,] 0.00000000 -0.1159922 0.1572197 0.0000000 0.1394414 0.1974312 0.0000000 [5,] 0.00000000 0.0000000 0.1600701 0.0000000 0.1511198 0.1533054 0.0000000 [6,] 0.00000000 0.0000000 0.1646117 0.0000000 0.1582048 0.0000000 0.0000000 [7,] 0.00000000 0.0000000 0.1520380 0.0000000 0.0000000 0.0000000 0.0000000 [8,] 0.00000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 [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 [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 0.36915 0.29582 0.08235 0.45359 0.67181 0.32102 0.82003 [2,] 0.36227 0.30406 0.07184 0.43916 0.33306 0.17331 NA [3,] 0.61662 0.31634 0.14465 NA 0.36827 0.17207 NA [4,] NA 0.32238 0.15861 NA 0.23653 0.15186 NA [5,] NA NA 0.15550 NA 0.21019 0.25054 NA [6,] NA NA 0.14482 NA 0.21054 NA NA [7,] NA NA 0.17585 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.2996 -0.1279 0.2012 -0.2463 0.2639 0.1697 -0.1454 s.e. 0.3316 0.1215 0.1143 0.3269 0.6204 0.1699 0.6368 sigma^2 estimated as 0.191: log likelihood = -49.74, aic = 115.47 [[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.2996 -0.1279 0.2012 -0.2463 0.2639 0.1697 -0.1454 s.e. 0.3316 0.1215 0.1143 0.3269 0.6204 0.1699 0.6368 sigma^2 estimated as 0.191: log likelihood = -49.74, aic = 115.47 [[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.2990 -0.1243 0.2052 -0.2497 0.1252 0.1884 0 s.e. 0.3263 0.1201 0.1124 0.3211 0.1285 0.1370 0 sigma^2 estimated as 0.1912: log likelihood = -49.76, aic = 113.52 [[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.0594 -0.1171 0.1634 0 0.1150 0.1895 0 s.e. 0.1182 0.1161 0.1109 0 0.1271 0.1375 0 sigma^2 estimated as 0.1926: log likelihood = -50.02, aic = 112.05 [[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.1160 0.1572 0 0.1394 0.1974 0 s.e. 0 0.1165 0.1105 0 0.1169 0.1364 0 sigma^2 estimated as 0.1927: log likelihood = -50.15, aic = 110.3 [[3]][[6]] 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.1601 0 0.1511 0.1533 0 s.e. 0 0 0.1116 0 0.1196 0.1325 0 sigma^2 estimated as 0.1959: log likelihood = -50.64, aic = 109.28 [[3]][[7]] 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.1646 0 0.1582 0 0 s.e. 0 0 0.1118 0 0.1254 0 0 sigma^2 estimated as 0.2006: log likelihood = -51.29, aic = 108.58 [[3]][[8]] 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.1520 0 0 0 0 s.e. 0 0 0.1113 0 0 0 0 sigma^2 estimated as 0.2052: log likelihood = -52.07, aic = 108.15 $aic [1] 115.4700 113.5157 112.0493 110.3019 109.2812 108.5802 108.1451 107.9849 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 5: In max(i) : no non-missing arguments to max; returning -Inf 6: In max(i) : no non-missing arguments to max; returning -Inf 7: In max(try.data.frame[, 4], na.rm = TRUE) : no non-missing arguments to max; returning -Inf > postscript(file="/var/www/rcomp/tmp/14mxt1293271871.ps",horizontal=F,onefile=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 = 84 Frequency = 1 [1] 0.018330839 -0.077539190 -0.499689010 -0.350755775 0.150234960 [6] 0.025164062 0.141557394 0.373323608 0.407359209 -0.378608335 [11] 0.195253617 -0.092547652 -0.054575678 -0.260223760 0.325665044 [16] 0.151081020 -0.425977087 0.046871876 0.015972329 0.175876445 [21] 0.082876959 0.119465760 0.024727978 -0.211694416 -0.209149631 [26] 0.032367821 0.125880489 0.111178476 0.016632024 0.571263350 [31] -0.053995675 0.088473798 0.339250606 0.477239794 0.330751516 [36] 0.556370448 0.487473175 0.214505329 -0.133248242 0.688344600 [41] 0.653174241 -1.285384153 0.532129117 -0.240560069 -0.339970295 [46] -0.227397636 0.541838517 -0.162203144 0.212492202 0.474613531 [51] -0.284620427 0.184443123 -0.337567096 -0.060531173 -0.117719571 [56] 0.121022711 0.549994582 0.406495719 0.398286674 -0.036974436 [61] 1.041945853 0.358586136 -0.041323234 -1.109130556 -0.171205963 [66] -0.007071793 0.651967241 -0.738228992 0.377730924 0.509891250 [71] -0.072780730 0.152067223 0.720171729 1.726495896 -0.566407714 [76] -0.783348921 -0.144969362 -0.014197737 -0.122862817 0.027595543 [81] 0.370955322 0.404067324 0.950560068 0.253527664 > postscript(file="/var/www/rcomp/tmp/24mxt1293271871.ps",horizontal=F,onefile=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/rcomp/tmp/34mxt1293271871.ps",horizontal=F,onefile=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/rcomp/tmp/4fvwe1293271871.ps",horizontal=F,onefile=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/rcomp/tmp/5fvwe1293271871.ps",horizontal=F,onefile=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/rcomp/tmp/6fvwe1293271871.ps",horizontal=F,onefile=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/rcomp/tmp/78meh1293271871.ps",horizontal=F,onefile=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/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/www/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/rcomp/tmp/8w6w31293271872.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/rcomp/tmp/9pxv61293271872.tab") > > try(system("convert tmp/14mxt1293271871.ps tmp/14mxt1293271871.png",intern=TRUE)) character(0) > try(system("convert tmp/24mxt1293271871.ps tmp/24mxt1293271871.png",intern=TRUE)) character(0) > try(system("convert tmp/34mxt1293271871.ps tmp/34mxt1293271871.png",intern=TRUE)) character(0) > try(system("convert tmp/4fvwe1293271871.ps tmp/4fvwe1293271871.png",intern=TRUE)) character(0) > try(system("convert tmp/5fvwe1293271871.ps tmp/5fvwe1293271871.png",intern=TRUE)) character(0) > try(system("convert tmp/6fvwe1293271871.ps tmp/6fvwe1293271871.png",intern=TRUE)) character(0) > try(system("convert tmp/78meh1293271871.ps tmp/78meh1293271871.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 4.410 2.410 6.837