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. Natural language support but running in an English locale 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(20503,22885,26217,26583,27751,28158,27373,28367,26851,26733,26849,26733,27951,29781,32914,33488,35652,36488,35387,35676,34844,32447,31068,29010,29812,30951,32974,32936,34012,32946,31948,30599,27691,25073,23406,22248,22896,25317,26558,26471,27543,26198,24725,25005,23462,20780,19815,19761,21454,23899,24939,23580,24562,24696,23785,23812,21917,19713,19282,18788,21453,24482,27474,27264,27349,30632,29429,30084,26290,24379,23335,21346,21106,24514,28353,30805,31348,34556,33855,34787,32529,29998,29257,28155,30466,35704,39327,39351,42234,43630,43722,43121,37985,37135,34646,33026,35087,38846,42013,43908,42868,44423,44167,43636,44382,42142,43452,36912,42413,45344,44873,47510,49554,47369,45998,48140,48441,44928,40454,38661,37246,36843,36424,37594,38144,38737,34560,36080,33508,35462,33374,32110,35533,35532,37903,36763,40399,44164,44496,43110,43880,43930,44327) > par9 = '1' > par8 = '2' > par7 = '1' > par6 = '3' > par5 = '12' > 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] [,4] [,5] [,6] [1,] 0.56650754 0.1750021 0.02135467 -0.6549952 0.1437653 -0.04160346 [2,] 0.60380142 0.1841274 0.00000000 -0.6893965 0.1423494 -0.04010086 [3,] 0.61601652 0.1806410 0.00000000 -0.6993059 0.1440095 0.00000000 [4,] 0.69589731 0.1342651 0.00000000 -0.7425936 0.0000000 0.00000000 [5,] -0.65871516 0.0000000 0.00000000 0.6005394 0.0000000 0.00000000 [6,] -0.03738405 0.0000000 0.00000000 0.0000000 0.0000000 0.00000000 [7,] 0.00000000 0.0000000 0.00000000 0.0000000 0.0000000 0.00000000 [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.9998794 [2,] -0.9998236 [3,] -1.0000841 [4,] -1.0008075 [5,] -1.0000743 [6,] -0.9999519 [7,] -0.9999820 [8,] NA [9,] NA [10,] NA [11,] NA [12,] NA [13,] NA [14,] NA [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 0.07919 0.09800 0.84897 0.03204 0.17246 0.69042 0.00000 [2,] 0.01103 0.05427 NA 0.00206 0.17531 0.69930 0.00000 [3,] 0.00797 0.05823 NA 0.00134 0.17105 NA 0.00000 [4,] 0.00291 0.14758 NA 0.00076 NA NA 0.01581 [5,] 0.11772 NA NA 0.17359 NA NA 0.00068 [6,] 0.67044 NA NA NA NA NA 0.00004 [7,] NA NA NA NA NA NA 0.00001 [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.5665 0.175 0.0214 -0.6550 0.1438 -0.0416 -0.9999 s.e. 0.3203 0.105 0.1119 0.3023 0.1048 0.1042 0.1544 sigma^2 estimated as 2301749: log likelihood = -1150.37, aic = 2316.75 [[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.5665 0.175 0.0214 -0.6550 0.1438 -0.0416 -0.9999 s.e. 0.3203 0.105 0.1119 0.3023 0.1048 0.1042 0.1544 sigma^2 estimated as 2301749: log likelihood = -1150.37, aic = 2316.75 [[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.6038 0.1841 0 -0.6894 0.1423 -0.0401 -0.9998 s.e. 0.2343 0.0948 0 0.2194 0.1045 0.1036 0.1557 sigma^2 estimated as 2303032: log likelihood = -1150.39, aic = 2314.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.6160 0.1806 0 -0.6993 0.1440 0 -1.0001 s.e. 0.2288 0.0946 0 0.2136 0.1047 0 0.1388 sigma^2 estimated as 2322345: log likelihood = -1150.47, aic = 2312.93 [[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.6959 0.1343 0 -0.7426 0 0 -1.0008 s.e. 0.2296 0.0922 0 0.2155 0 0 0.4096 sigma^2 estimated as 2300727: log likelihood = -1151.43, aic = 2312.86 [[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.6587 0 0 0.6005 0 0 -1.0001 s.e. 0.4185 0 0 0.4391 0 0 0.2876 sigma^2 estimated as 2343421: log likelihood = -1152.65, aic = 2313.31 [[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.0374 0 0 0 0 0 -1.0000 s.e. 0.0877 0 0 0 0 0 0.2372 sigma^2 estimated as 2354289: log likelihood = -1152.95, aic = 2311.89 $aic [1] 2316.749 2314.784 2312.931 2312.858 2313.305 2311.891 2310.073 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 arima(series, order = order, seasonal = seasonal, include.mean = include.mean, : some AR parameters were fixed: setting transform.pars = FALSE 6: 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/freestat/rcomp/tmp/1jiuy1293661427.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 = 143 Frequency = 1 [1] 11.837409 7.138933 7.489573 5.905167 5.767175 [6] 5.172948 3.699037 4.168397 2.269710 1.929886 [11] 1.864891 -13.029548 -62.321381 -390.088750 -155.337369 [16] 141.780846 709.741419 329.620570 -212.189416 -506.969605 [21] 464.927791 -1593.577906 -1117.529367 -1413.190786 -338.454729 [26] -798.740383 -1017.113079 -451.737911 -497.288013 -1395.927592 [31] -96.475869 -1627.021551 -1476.677016 -1163.889917 -887.117995 [36] -90.029760 -288.777124 541.805694 -1354.952115 -387.185164 [41] -356.690747 -1228.831194 -488.629152 244.927189 190.721547 [46] -834.231039 -21.399881 915.245085 734.892198 474.348404 [51] -1228.523398 -1444.372587 -399.325714 368.031301 173.644743 [56] -17.783250 -175.573335 -230.415952 477.042329 333.276376 [61] 1423.115629 951.410897 798.812415 -63.793845 -1105.699818 [66] 3144.584263 -17.315345 548.812716 -1855.505294 14.536491 [71] -160.101021 -1113.533543 -1542.185590 1055.971454 1472.425949 [76] 2439.992834 -418.314559 2604.072803 447.542739 737.664659 [81] -136.506963 -508.573001 123.759416 -109.361109 1088.192261 [86] 2714.660028 1137.100329 -165.697907 1741.743588 642.013123 [91] 1066.019013 -767.456203 -2864.015064 1031.387626 -1469.181751 [96] -640.327367 688.639977 990.382707 520.766886 1601.833783 [101] -2096.708446 577.838917 617.638942 -623.100785 3022.379471 [106] -193.615308 2237.127696 -5069.525766 3676.778321 220.495363 [111] -3014.970826 2007.636692 1076.772958 -2921.947062 -638.018701 [116] 1939.028263 2375.926424 -1396.659962 -3531.517229 -235.950821 [121] -3104.366583 -3224.813125 -2796.403685 419.065662 -502.821485 [126] -47.472182 -3153.568259 1060.828066 -612.429749 3846.969323 [131] -724.488140 375.977745 1824.898970 -2386.301193 132.919157 [136] -1729.293393 2412.997338 3104.154328 1551.878784 -1652.558157 [141] 2536.714018 1808.423419 1649.750422 > postscript(file="/var/www/html/freestat/rcomp/tmp/2bsb11293661427.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/html/freestat/rcomp/tmp/3bsb11293661427.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/html/freestat/rcomp/tmp/4bsb11293661427.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/html/freestat/rcomp/tmp/5bsb11293661427.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/html/freestat/rcomp/tmp/641sm1293661427.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/html/freestat/rcomp/tmp/741sm1293661427.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/html/freestat/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/www/html/freestat/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/freestat/rcomp/tmp/8itqc1293661427.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/freestat/rcomp/tmp/9t27f1293661427.tab") > > try(system("convert tmp/1jiuy1293661427.ps tmp/1jiuy1293661427.png",intern=TRUE)) character(0) > try(system("convert tmp/2bsb11293661427.ps tmp/2bsb11293661427.png",intern=TRUE)) character(0) > try(system("convert tmp/3bsb11293661427.ps tmp/3bsb11293661427.png",intern=TRUE)) character(0) > try(system("convert tmp/4bsb11293661427.ps tmp/4bsb11293661427.png",intern=TRUE)) character(0) > try(system("convert tmp/5bsb11293661427.ps tmp/5bsb11293661427.png",intern=TRUE)) character(0) > try(system("convert tmp/641sm1293661427.ps tmp/641sm1293661427.png",intern=TRUE)) character(0) > try(system("convert tmp/741sm1293661427.ps tmp/741sm1293661427.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 21.758 2.303 22.780