R version 2.15.2 (2012-10-26) -- "Trick or Treat" Copyright (C) 2012 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: i686-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(236.422 + ,250.580 + ,279.515 + ,264.417 + ,283.706 + ,281.288 + ,271.146 + ,283.944 + ,269.155 + ,270.899 + ,276.507 + ,319.957 + ,250.746 + ,247.772 + ,280.449 + ,274.925 + ,296.013 + ,287.881 + ,279.098 + ,294.763 + ,261.924 + ,291.596 + ,287.537 + ,326.201 + ,255.598 + ,253.086 + ,285.261 + ,284.747 + ,300.402 + ,288.854 + ,295.433 + ,307.256 + ,273.189 + ,287.540 + ,290.705 + ,337.006 + ,268.335 + ,259.060 + ,293.703 + ,294.262 + ,312.404 + ,301.014 + ,309.942 + ,317.079 + ,293.912 + ,304.060 + ,301.299 + ,357.634 + ,281.493 + ,282.478 + ,319.111 + ,315.223 + ,328.445 + ,321.081 + ,328.040 + ,326.362 + ,313.566 + ,319.768 + ,324.315 + ,387.243 + ,293.308 + ,295.109 + ,339.190 + ,335.678 + ,345.401 + ,351.002 + ,351.889 + ,355.773 + ,333.363 + ,336.214 + ,343.910 + ,405.788 + ,318.682 + ,314.189 + ,362.141 + ,351.811 + ,373.727 + ,366.795 + ,362.393 + ,376.006 + ,346.423 + ,349.007 + ,357.224 + ,418.473 + ,329.169 + ,323.456 + ,374.439 + ,358.806 + ,391.816 + ,376.944 + ,372.665 + ,388.357 + ,354.241 + ,368.982 + ,378.233 + ,426.699 + ,343.241 + ,344.577 + ,373.623 + ,369.688 + ,398.816 + ,379.387 + ,384.666 + ,383.879 + ,351.578 + ,350.920 + ,336.629 + ,385.504 + ,311.330 + ,300.545 + ,329.718 + ,331.023 + ,348.944 + ,345.650 + ,349.260 + ,354.597 + ,325.769 + ,339.734 + ,340.543 + ,401.585 + ,315.998 + ,312.327 + ,362.217 + ,358.067 + ,367.321 + ,360.372 + ,363.830 + ,364.525 + ,347.945 + ,357.404 + ,368.182 + ,429.343) > par9 = '1' > par8 = '2' > par7 = '1' > par6 = '3' > par5 = '12' > par4 = '1' > par3 = '1' > par2 = '0.5' > par1 = 'FALSE' > 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.08154528 0.04640291 0.3130744 -0.5124640 0.1283891 -0.2439063 [2,] 0.00000000 0.01494464 0.3026283 -0.4313879 0.1246231 -0.2424010 [3,] 0.00000000 0.00000000 0.3013580 -0.4253867 0.1256491 -0.2421069 [4,] 0.00000000 0.00000000 0.3429253 -0.4475689 0.0000000 -0.2532764 [5,] NA NA NA NA NA NA [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.9998932 [2,] -0.9999578 [3,] -0.9997689 [4,] -0.8850037 [5,] NA [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.7976 0.76181 0.00335 0.11499 0.25310 0.02728 2e-05 [2,] NA 0.88141 0.00335 0.00002 0.26202 0.02801 2e-05 [3,] NA NA 0.00340 0.00000 0.25640 0.02798 2e-05 [4,] NA NA 0.00034 0.00000 NA 0.02509 1e-05 [5,] NA NA NA NA NA NA 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.0815 0.0464 0.3131 -0.5125 0.1284 -0.2439 -0.9999 s.e. 0.3173 0.1528 0.1047 0.3229 0.1118 0.1092 0.2263 sigma^2 estimated as 0.02694: log likelihood = 29.92, aic = -43.85 [[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.0815 0.0464 0.3131 -0.5125 0.1284 -0.2439 -0.9999 s.e. 0.3173 0.1528 0.1047 0.3229 0.1118 0.1092 0.2263 sigma^2 estimated as 0.02694: log likelihood = 29.92, aic = -43.85 [[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 0.0149 0.3026 -0.4314 0.1246 -0.2424 -1.0000 s.e. 0 0.1000 0.1012 0.0969 0.1106 0.1090 0.2254 sigma^2 estimated as 0.02694: log likelihood = 29.9, aic = -45.79 [[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 0.3014 -0.4254 0.1256 -0.2421 -0.9998 s.e. 0 0 0.1009 0.0874 0.1102 0.1089 0.2253 sigma^2 estimated as 0.02696: log likelihood = 29.89, aic = -47.77 [[3]][[5]] NULL [[3]][[6]] NULL [[3]][[7]] NULL $aic [1] -43.84707 -45.79245 -47.77001 -48.54148 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/fisher/rcomp/tmp/12xkw1353763019.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 = 132 Frequency = 1 [1] 0.0088773458 0.0043215022 0.0035804147 0.0022649388 0.0023338376 [6] 0.0018756992 0.0013221871 0.0015170388 0.0009272818 0.0008847812 [11] 0.0009661214 -0.0067988370 -0.0567302082 -0.3471550798 -0.0546825878 [16] 0.1392102584 0.2028663942 -0.0582443037 -0.0510212095 0.0152716936 [21] -0.3441874331 0.4659889572 -0.0627957671 -0.0145419069 -0.1430883951 [26] -0.1079147484 -0.0065297946 0.1684115797 -0.0132010134 -0.1220725377 [31] 0.2547177344 0.0550564837 -0.0807470881 -0.2669237223 -0.0468063968 [36] 0.1503388572 0.2625481535 -0.2642253162 -0.0628565471 0.1108024060 [41] 0.1570262455 -0.0444110124 0.1709539772 -0.0765198423 0.0812851989 [46] -0.0052353612 -0.1728075502 0.1385550107 0.0283711142 0.1079955592 [51] 0.0389327824 0.0808357416 -0.1518460500 -0.0623892799 0.1722820625 [56] -0.2114767879 0.1962027000 -0.1450995684 0.1279512730 0.2615860202 [61] -0.1894910923 -0.1364698796 0.0800417011 0.2153325528 -0.1015625548 [66] 0.2142041323 0.1063370292 -0.0283809635 -0.0604958580 -0.2700541889 [71] 0.0080116791 0.2017833597 0.1272354343 -0.0939552038 0.1259307740 [76] -0.0655015222 0.1060214606 -0.0717679282 -0.0817404017 -0.0176583370 [81] -0.0156759297 -0.1956337977 0.0154139761 0.1863109090 -0.0225251378 [86] -0.1514769650 0.1694085342 -0.0886281955 0.2747527474 -0.0765902804 [91] -0.0632265671 -0.0229416407 -0.1305787428 0.0636021928 0.1322502246 [96] -0.0825701864 0.0267283587 0.0222517808 -0.2401937341 -0.0495946442 [101] 0.1435432837 -0.0985600811 0.0505245192 -0.3006184773 -0.1727767408 [106] -0.4487827753 -0.5816517144 -0.2690699400 0.1729419737 -0.0965982470 [111] -0.1567970095 0.0112409837 0.0911721721 0.2056454964 0.0864316359 [116] 0.0156189501 -0.1109318405 0.1394513747 0.1111681893 0.2130300858 [121] -0.0369715854 -0.0281324552 0.1920541962 0.1380880989 -0.1736060742 [126] -0.1977548231 -0.0043734257 -0.1874254459 0.1923070431 -0.0329747680 [131] 0.1589115090 0.0499828101 > postscript(file="/var/fisher/rcomp/tmp/2m8x61353763019.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/fisher/rcomp/tmp/3rn7o1353763019.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/fisher/rcomp/tmp/45hvr1353763019.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/fisher/rcomp/tmp/5hzz01353763019.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/fisher/rcomp/tmp/6nkxg1353763019.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/fisher/rcomp/tmp/7r56v1353763019.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/fisher/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/fisher/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/fisher/rcomp/tmp/8hl1f1353763019.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/fisher/rcomp/tmp/9cseo1353763019.tab") > > try(system("convert tmp/12xkw1353763019.ps tmp/12xkw1353763019.png",intern=TRUE)) character(0) > try(system("convert tmp/2m8x61353763019.ps tmp/2m8x61353763019.png",intern=TRUE)) character(0) > try(system("convert tmp/3rn7o1353763019.ps tmp/3rn7o1353763019.png",intern=TRUE)) character(0) > try(system("convert tmp/45hvr1353763019.ps tmp/45hvr1353763019.png",intern=TRUE)) character(0) > try(system("convert tmp/5hzz01353763019.ps tmp/5hzz01353763019.png",intern=TRUE)) character(0) > try(system("convert tmp/6nkxg1353763019.ps tmp/6nkxg1353763019.png",intern=TRUE)) character(0) > try(system("convert tmp/7r56v1353763019.ps tmp/7r56v1353763019.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 11.882 2.160 14.030