R version 2.13.0 (2011-04-13) Copyright (C) 2011 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(1954,2302,3054,2414,2226,2725,2589,3470,2400,3180,4009,3924,2072,2434,2956,2828,2687,2629,3150,4119,3030,3055,3821,4001,2529,2472,3134,2789,2758,2993,3282,3437,2804,3076,3782,3889,2271,2452,3084,2522,2769,3438,2839,3746,2632,2851,3871,3618,2389,2344,2678,2492,2858,2246,2800,3869,3007,3023,3907,4209,2353,2570,2903,2910,3782,2759,2931,3641,2794,3070,3576,4106,2452,2206,2488,2416,2534,2521,3093,3903,2907,3025,3812,4209,2138,2419,2622,2912,2708,2798,3254,2895,3263,3736,4077,4097,2175,3138,2823,2498,2822,2738,4137,3515,3785,3632,4504,4451,2550,2867,3458,2961,3163,2880,3331,3062,3534,3622,4464,5411,2564,2820,3508,3088,3299,2939,3320,3418,3604,3495,4163,4882,2211,3260,2992,2425,2707,3244,3965,3315,3333,3583,4021,4904,2252,2952,3573,3048,3059,2731,3563,3092,3478,3478,4308,5029,2075,3264,3308,3688,3136,2824,3644,4694,2914,3686,4358,5587,2265,3685,3754,3708,3210,3517,3905,3670,4221,4404,5086,5725,2367,3819,4067,4022,3937,4365,4290) > par9 = '1' > par8 = '2' > par7 = '1' > par6 = '3' > par5 = '12' > par4 = '1' > par3 = '0' > 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] [1,] 0.9230367 -0.04939015 0.09686318 -0.7718620 -0.2442599 -0.09972127 [2,] 0.8946022 0.00000000 0.07594677 -0.7679789 -0.2311157 -0.08973002 [3,] 0.8966425 0.00000000 0.07747714 -0.7736725 -0.1142479 0.00000000 [4,] 0.9191637 0.00000000 0.05993759 -0.7929730 0.0000000 0.00000000 [5,] 0.9846186 0.00000000 0.00000000 -0.8337776 0.0000000 0.00000000 [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.4403682 [2,] -0.4612664 [3,] -0.5810190 [4,] -0.6602684 [5,] -0.6604118 [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 0.64548 0.29121 0 0.37640 0.57240 0.12050 [2,] 0 NA 0.34840 0 0.39973 0.61305 0.09954 [3,] 0 NA 0.33740 0 0.38035 NA 0.00000 [4,] 0 NA 0.44392 0 NA NA 0.00000 [5,] 0 NA NA 0 NA NA 0.00000 [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.9230 -0.0494 0.0969 -0.7719 -0.2443 -0.0997 -0.4404 s.e. 0.1141 0.1072 0.0915 0.0880 0.2755 0.1763 0.2823 sigma^2 estimated as 8.633e-07: log likelihood = 970.3, aic = -1924.61 [[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.9230 -0.0494 0.0969 -0.7719 -0.2443 -0.0997 -0.4404 s.e. 0.1141 0.1072 0.0915 0.0880 0.2755 0.1763 0.2823 sigma^2 estimated as 8.633e-07: log likelihood = 970.3, aic = -1924.61 [[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.8946 0 0.0759 -0.7680 -0.2311 -0.0897 -0.4613 s.e. 0.0967 0 0.0808 0.0903 0.2738 0.1771 0.2786 sigma^2 estimated as 8.636e-07: log likelihood = 970.2, aic = -1926.39 [[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.8966 0 0.0775 -0.7737 -0.1142 0 -0.5810 s.e. 0.0953 0 0.0805 0.0878 0.1299 0 0.1186 sigma^2 estimated as 8.64e-07: log likelihood = 970.08, aic = -1928.15 [[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.9192 0 0.0599 -0.793 0 0 -0.6603 s.e. 0.0904 0 0.0781 0.083 0 0 0.0684 sigma^2 estimated as 8.668e-07: log likelihood = 969.7, aic = -1929.41 [[3]][[6]] NULL [[3]][[7]] NULL $aic [1] -1924.607 -1926.394 -1928.153 -1929.407 -1930.815 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/wessaorg/rcomp/tmp/1jd3i1324406237.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 = 187 Frequency = 1 [1] 2.262233e-05 2.084236e-05 1.809526e-05 2.035311e-05 2.119516e-05 [6] 1.915649e-05 1.965319e-05 1.697595e-05 2.041236e-05 1.773312e-05 [11] 1.579358e-05 1.596372e-05 -5.289281e-04 -3.796153e-04 3.852493e-04 [16] -1.196686e-03 -1.354672e-03 6.292042e-04 -1.257825e-03 -7.158226e-04 [21] -1.457754e-03 9.058682e-04 8.158029e-04 3.114135e-04 -1.666166e-03 [26] 3.358114e-04 2.616236e-04 1.127886e-04 -4.659069e-04 -4.027320e-04 [31] -5.290943e-04 1.448445e-03 1.735698e-04 5.529042e-04 5.402334e-04 [36] 3.469212e-04 4.185346e-05 1.216733e-04 1.521834e-04 8.256722e-04 [41] -5.958926e-04 -1.659612e-03 8.815420e-04 -4.415497e-05 6.823004e-04 [46] 8.549262e-04 -2.692390e-05 6.380442e-04 -7.258899e-04 4.256512e-04 [51] 1.223884e-03 3.721116e-04 -9.325938e-04 2.705102e-03 -7.861193e-06 [56] -8.389031e-04 -1.453225e-03 -3.545361e-04 -2.960752e-04 -8.722985e-04 [61] -2.028934e-04 -5.730320e-04 2.837355e-04 -9.626251e-04 -2.573857e-03 [66] 4.268058e-04 4.869543e-04 8.741362e-04 5.297387e-04 2.012573e-04 [71] 9.688672e-04 -1.579997e-04 -3.919134e-04 1.323475e-03 1.571324e-03 [76] 9.305609e-04 1.397324e-03 2.089471e-04 -1.139664e-03 -9.142298e-04 [81] -7.362364e-04 -2.237809e-04 -2.600175e-04 -5.010371e-04 1.023650e-03 [86] -3.541921e-04 4.364631e-04 -1.282645e-03 4.915380e-04 -5.861949e-04 [91] -7.369395e-04 2.438166e-03 -1.421805e-03 -1.727308e-03 -4.408399e-04 [96] 2.192703e-04 8.428570e-04 -2.378406e-03 1.459656e-04 1.179488e-03 [101] 3.299108e-04 2.266484e-04 -2.259795e-03 3.287144e-04 -1.636867e-03 [106] -1.919639e-04 -4.110174e-04 1.894387e-04 -4.783207e-04 -1.330687e-05 [111] -1.204877e-03 -1.171054e-04 -8.664723e-05 4.990945e-04 1.100983e-03 [116] 1.904459e-03 -2.532745e-04 -7.604274e-05 -2.577025e-04 -1.353772e-03 [121] -2.622632e-04 2.343695e-04 -7.830498e-04 -3.343525e-04 -2.896774e-04 [126] 3.048245e-04 9.315775e-04 4.484607e-04 -7.260942e-05 4.731423e-04 [131] 5.594543e-04 -7.636806e-05 1.315597e-03 -1.408803e-03 8.085166e-04 [136] 1.765846e-03 1.148043e-03 -1.372397e-03 -1.428872e-03 1.855558e-04 [141] 3.392508e-04 -1.115142e-04 4.808127e-04 -2.833134e-04 5.314964e-04 [146] -1.439618e-04 -1.212086e-03 -9.739298e-04 -1.153738e-04 1.119935e-03 [151] 2.161367e-04 9.242393e-04 -1.654169e-04 1.510343e-04 -3.057005e-04 [156] -3.467573e-04 1.253792e-03 -1.058016e-03 -6.715458e-05 -2.364967e-03 [161] -9.755287e-05 5.706756e-04 2.123577e-04 -2.536956e-03 2.096636e-03 [166] -6.686050e-05 2.276070e-04 -6.245447e-04 2.665858e-04 -1.303681e-03 [171] -5.994338e-04 -1.010404e-03 2.681838e-04 -1.060870e-03 2.371486e-04 [176] 8.430033e-04 -1.391308e-03 -7.340603e-04 -3.483234e-04 2.527668e-04 [181] 4.789059e-04 -4.239783e-04 -4.122984e-04 -7.180290e-04 -9.573295e-04 [186] -1.713637e-03 2.441677e-04 > postscript(file="/var/wessaorg/rcomp/tmp/2isli1324406237.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/wessaorg/rcomp/tmp/3inb61324406237.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/wessaorg/rcomp/tmp/4au9d1324406237.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/wessaorg/rcomp/tmp/5dl8f1324406237.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/wessaorg/rcomp/tmp/68bm21324406237.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/wessaorg/rcomp/tmp/7z0dw1324406237.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/wessaorg/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/wessaorg/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/wessaorg/rcomp/tmp/8ygsh1324406237.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/wessaorg/rcomp/tmp/9uvzd1324406237.tab") > > try(system("convert tmp/1jd3i1324406237.ps tmp/1jd3i1324406237.png",intern=TRUE)) character(0) > try(system("convert tmp/2isli1324406237.ps tmp/2isli1324406237.png",intern=TRUE)) character(0) > try(system("convert tmp/3inb61324406237.ps tmp/3inb61324406237.png",intern=TRUE)) character(0) > try(system("convert tmp/4au9d1324406237.ps tmp/4au9d1324406237.png",intern=TRUE)) character(0) > try(system("convert tmp/5dl8f1324406237.ps tmp/5dl8f1324406237.png",intern=TRUE)) character(0) > try(system("convert tmp/68bm21324406237.ps tmp/68bm21324406237.png",intern=TRUE)) character(0) > try(system("convert tmp/7z0dw1324406237.ps tmp/7z0dw1324406237.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 14.467 2.168 16.710