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(83.87 + ,84.23 + ,84.61 + ,84.82 + ,85.04 + ,85.06 + ,84.93 + ,84.98 + ,85.23 + ,85.30 + ,85.33 + ,85.55 + ,85.70 + ,85.88 + ,86.04 + ,86.07 + ,86.31 + ,86.38 + ,86.35 + ,86.55 + ,86.70 + ,86.74 + ,86.85 + ,86.95 + ,86.80 + ,87.01 + ,87.17 + ,87.43 + ,87.66 + ,87.68 + ,87.59 + ,87.65 + ,87.72 + ,87.70 + ,87.71 + ,87.80 + ,87.62 + ,87.84 + ,88.17 + ,88.47 + ,88.58 + ,88.57 + ,88.55 + ,88.68 + ,88.79 + ,88.85 + ,88.95 + ,89.27 + ,89.09 + ,89.42 + ,89.72 + ,89.85 + ,89.96 + ,90.25 + ,90.20 + ,90.27 + ,90.78 + ,90.79 + ,90.98 + ,91.25 + ,90.75 + ,91.01 + ,91.50 + ,92.09 + ,92.56 + ,92.66 + ,92.38 + ,92.38 + ,92.66 + ,92.69 + ,92.59 + ,92.98 + ,92.98 + ,93.15 + ,93.65 + ,94.06 + ,94.24 + ,94.24 + ,94.11 + ,94.16 + ,94.43 + ,94.67 + ,94.60 + ,95.00 + ,94.84 + ,95.26 + ,95.81 + ,95.92 + ,95.85 + ,95.90 + ,95.80 + ,96.00 + ,96.34 + ,96.43 + ,96.48 + ,96.75 + ,96.51 + ,96.69 + ,97.28 + ,97.69 + ,98.08 + ,98.09 + ,97.92 + ,98.06 + ,98.23 + ,98.57 + ,98.53 + ,98.92 + ,98.42 + ,98.73 + ,99.32 + ,99.73 + ,100.00 + ,100.08 + ,100.02 + ,100.26 + ,100.71 + ,100.95 + ,100.75 + ,101.03 + ,100.64 + ,100.93 + ,101.41 + ,102.07 + ,102.42 + ,102.53 + ,102.43 + ,102.60 + ,102.65 + ,102.74 + ,102.82 + ,103.21 + ,102.75 + ,103.09 + ,103.71 + ,104.30 + ,104.58 + ,104.71 + ,104.44 + ,104.57 + ,104.95 + ,105.49 + ,106.03 + ,106.48 + ,106.25 + ,106.70 + ,107.60 + ,108.05 + ,108.72 + ,109.17 + ,109.08 + ,109.04 + ,109.34 + ,109.37 + ,108.96 + ,108.77 + ,108.11 + ,108.67 + ,109.05 + ,109.43 + ,109.62 + ,109.85 + ,109.34 + ,109.65 + ,109.69 + ,109.91 + ,110.09) > par9 = '1' > par8 = '2' > par7 = '1' > par6 = '2' > par5 = '12' > par4 = '1' > par3 = '1' > par2 = '1' > par1 = 'TRUE' > 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.4419412 0.2255867 0.6846597 -0.5093515 -0.2972556 -0.2825381 [2,] -0.4343485 0.2338541 0.6809321 -0.7383175 -0.4250242 0.0000000 [3,] 0.0000000 0.1146931 0.2463409 -0.7422196 -0.4268334 0.0000000 [4,] 0.0000000 0.0000000 0.2198339 -0.7691787 -0.4289467 0.0000000 [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 [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 0.19892 0.03253 0.04486 0.02647 0.06655 0.25608 [2,] 0.15050 0.01910 0.02255 0.00000 0.00000 NA [3,] NA 0.18649 0.00280 0.00000 0.00000 NA [4,] NA NA 0.00239 0.00000 0.00000 NA [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 [[3]] [[3]][[1]] Call: arima(x = series, order = order, seasonal = seasonal, include.mean = include.mean, method = "ML") Coefficients: ar1 ar2 ma1 sar1 sar2 sma1 -0.4419 0.2256 0.6847 -0.5094 -0.2973 -0.2825 s.e. 0.3426 0.1046 0.3386 0.2274 0.1609 0.2479 sigma^2 estimated as 0.02652: log likelihood = 56.49, aic = -98.98 [[3]][[2]] Call: arima(x = series, order = order, seasonal = seasonal, include.mean = include.mean, method = "ML") Coefficients: ar1 ar2 ma1 sar1 sar2 sma1 -0.4419 0.2256 0.6847 -0.5094 -0.2973 -0.2825 s.e. 0.3426 0.1046 0.3386 0.2274 0.1609 0.2479 sigma^2 estimated as 0.02652: log likelihood = 56.49, aic = -98.98 [[3]][[3]] Call: arima(x = series, order = order, seasonal = seasonal, include.mean = include.mean, fixed = last.arma$next.vector, method = "ML") Coefficients: ar1 ar2 ma1 sar1 sar2 sma1 -0.4343 0.2339 0.6809 -0.7383 -0.4250 0 s.e. 0.3007 0.0988 0.2957 0.0817 0.0832 0 sigma^2 estimated as 0.02678: log likelihood = 55.93, aic = -99.86 [[3]][[4]] Call: arima(x = series, order = order, seasonal = seasonal, include.mean = include.mean, fixed = last.arma$next.vector, method = "ML") Coefficients: ar1 ar2 ma1 sar1 sar2 sma1 0 0.1147 0.2463 -0.7422 -0.4268 0 s.e. 0 0.0865 0.0811 0.0819 0.0835 0 sigma^2 estimated as 0.02686: log likelihood = 55.67, aic = -101.33 [[3]][[5]] NULL [[3]][[6]] NULL $aic [1] -98.98017 -99.86225 -101.33478 -101.55868 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 > postscript(file="/var/www/html/rcomp/tmp/1wcov1261919735.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 = 167 Frequency = 1 [1] 0.0484223525 0.0219339574 0.0146802646 0.0111271830 0.0090753738 [6] 0.0075695185 0.0063610785 0.0056095222 0.0052206201 0.0047637120 [11] 0.0043583270 -0.0433993715 -0.2901631090 -0.1336993560 -0.1339276393 [16] -0.0905399404 0.0567200168 0.0399667027 0.0648770755 0.0945288536 [21] -0.1105595796 -0.0111487528 0.0698889645 -0.1166253174 -0.2349954430 [26] 0.0023376786 -0.0788661184 0.1490451047 -0.0248433228 -0.0301590527 [31] -0.0003572471 -0.0541266373 -0.1061176965 -0.0373207565 -0.0329119278 [36] -0.0575521922 -0.1695637285 0.0011512122 0.0956288917 0.1154384963 [41] -0.1560499285 -0.0226830307 0.0873732642 0.0138400772 -0.0732866296 [46] 0.0372614217 0.0478636539 0.1569678718 -0.1947102188 0.1585387351 [51] 0.0743630245 -0.0753943553 -0.0857928420 0.2823591441 -0.0625063952 [56] -0.0818097731 0.4161143682 -0.1109620834 0.0960849881 0.0946342544 [61] -0.3692056505 0.0935078290 0.2554307063 0.2861479251 0.2107298811 [66] -0.0722958542 -0.2399937508 -0.0278124679 0.1163189031 -0.0019090902 [71] -0.1939447123 0.2268833323 0.2277926771 -0.1718847593 0.1504532124 [76] 0.0626928205 -0.0540972574 -0.1098368948 -0.0038430728 -0.0136615938 [81] -0.0027677547 0.2073460918 -0.1967621862 0.1028549038 0.0660259751 [86] 0.1281425234 0.0984065051 -0.2790826852 -0.2587215339 -0.0143749623 [91] 0.0824388912 0.1490040808 -0.0772499645 0.0153991099 0.0187738530 [96] -0.0776344680 0.0316659994 -0.0924764939 0.1024784772 -0.0140901066 [101] 0.1448007594 -0.0813005253 0.0190391844 0.0732113077 -0.1422164350 [106] 0.2550005197 -0.0369172585 0.0106893554 -0.3916656901 0.1518727694 [111] 0.0580811404 0.0735898857 0.0907316284 0.0284501905 0.0506844378 [116] 0.0999350208 0.1509569632 -0.0293618674 -0.1894159617 -0.0322304184 [121] -0.0890463075 0.0047493584 -0.0806633442 0.4008971402 0.0991776779 [126] -0.0029091959 -0.0089969002 -0.0266132589 -0.2595337131 -0.0511267903 [131] 0.1657883488 0.0522133959 -0.1262825479 0.1126257030 0.0420042810 [136] 0.0948112899 -0.0918913459 0.0815281740 -0.1657279387 -0.0167209877 [141] 0.1742624603 0.2587069177 0.5182929625 -0.0669315501 0.1727225570 [146] 0.0851651108 0.2901739065 -0.1726222441 0.3760683483 0.2647856211 [151] -0.0711659502 -0.2519090081 0.0520397142 -0.2265159480 -0.4326001923 [156] -0.4144186969 -0.1309871202 0.3081641900 -0.2951697471 -0.1555046918 [161] -0.1531544997 0.0871483419 -0.3551505397 0.2912500381 -0.2090988854 [166] 0.0313399188 0.0939897427 > postscript(file="/var/www/html/rcomp/tmp/20g3r1261919735.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/3tw0p1261919735.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/4myte1261919735.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/54rtk1261919735.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/6yota1261919735.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/76tvt1261919735.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/8csv21261919735.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/9tzo01261919735.tab") > > try(system("convert tmp/1wcov1261919735.ps tmp/1wcov1261919735.png",intern=TRUE)) character(0) > try(system("convert tmp/20g3r1261919735.ps tmp/20g3r1261919735.png",intern=TRUE)) character(0) > try(system("convert tmp/3tw0p1261919735.ps tmp/3tw0p1261919735.png",intern=TRUE)) character(0) > try(system("convert tmp/4myte1261919735.ps tmp/4myte1261919735.png",intern=TRUE)) character(0) > try(system("convert tmp/54rtk1261919735.ps tmp/54rtk1261919735.png",intern=TRUE)) character(0) > try(system("convert tmp/6yota1261919735.ps tmp/6yota1261919735.png",intern=TRUE)) character(0) > try(system("convert tmp/76tvt1261919735.ps tmp/76tvt1261919735.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 9.576 1.554 10.387