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(325412,326011,328282,317480,317539,313737,312276,309391,302950,300316,304035,333476,337698,335932,323931,313927,314485,313218,309664,302963,298989,298423,310631,329765,335083,327616,309119,295916,291413,291542,284678,276475,272566,264981,263290,296806,303598,286994,276427,266424,267153,268381,262522,255542,253158,243803,250741,280445,285257,270976,261076,255603,260376,263903,264291,263276,262572,256167,264221,293860,300713,287224) > par10 = 'FALSE' > par9 = '0' > par8 = '1' > par7 = '0' > par6 = '3' > par5 = '12' > par4 = '0' > par3 = '1' > par2 = '1' > par1 = '12' > par1 <- as.numeric(par1) #cut off periods > par1 <- 28 > par2 <- as.numeric(par2) #lambda > 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) #p > par6 <- 3 > par7 <- as.numeric(par7) #q > par7 <- 3 > par8 <- as.numeric(par8) #P > par9 <- as.numeric(par9) #Q > if (par10 == 'TRUE') par10 <- TRUE > if (par10 == 'FALSE') par10 <- FALSE > if (par2 == 0) x <- log(x) > if (par2 != 0) x <- x^par2 > lx <- length(x) > first <- lx - 2*par1 > nx <- lx - par1 > nx1 <- nx + 1 > fx <- lx - nx > if (fx < 1) { + fx <- par5 + nx1 <- lx + fx - 1 + first <- lx - 2*fx + } > first <- 1 > if (fx < 3) fx <- round(lx/10,0) > (arima.out <- arima(x[1:nx], order=c(par6,par3,par7), seasonal=list(order=c(par8,par4,par9), period=par5), include.mean=par10, method='ML')) Call: arima(x = x[1:nx], order = c(par6, par3, par7), seasonal = list(order = c(par8, par4, par9), period = par5), include.mean = par10, method = "ML") Coefficients: ar1 ar2 ar3 ma1 ma2 ma3 sar1 0.4162 -0.7024 0.1489 -0.1588 0.9433 -0.3305 0.7884 s.e. NaN 0.0922 NaN NaN NaN NaN 0.0944 sigma^2 estimated as 21281199: log likelihood = -332.69, aic = 681.37 Warning message: In sqrt(diag(x$var.coef)) : NaNs produced > (forecast <- predict(arima.out,par1)) $pred Time Series: Start = 35 End = 62 Frequency = 1 [1] 275098.6 288258.8 292535.4 288108.9 273788.9 262476.2 258583.4 259216.4 [9] 254132.6 247377.7 244025.3 238184.1 246365.2 256687.7 259914.2 256431.4 [17] 245239.1 236334.8 233204.6 233682.8 229711.3 224406.9 221743.9 217121.5 [25] 223581.0 231732.2 234271.8 231516.5 $se Time Series: Start = 35 End = 62 Frequency = 1 [1] 4760.266 7625.072 10567.618 12291.946 13295.254 14386.531 15733.794 [8] 16930.746 17849.728 18712.664 19652.635 20578.519 22716.170 25005.440 [15] 27590.417 29680.006 31291.991 32904.371 34661.133 36322.566 37775.914 [22] 39157.232 40570.170 41961.817 44078.680 46289.632 48684.268 50803.929 > (lb <- forecast$pred - 1.96 * forecast$se) Time Series: Start = 35 End = 62 Frequency = 1 [1] 265768.5 273313.6 271822.9 264016.7 247730.2 234278.6 227745.2 226032.2 [9] 219147.1 210700.9 205506.2 197850.2 201841.5 207677.1 205837.0 198258.6 [17] 183906.8 171842.2 165268.8 162490.5 155670.5 147658.7 142226.4 134876.3 [25] 137186.8 141004.5 138850.6 131940.8 > (ub <- forecast$pred + 1.96 * forecast$se) Time Series: Start = 35 End = 62 Frequency = 1 [1] 284428.7 303203.9 313248.0 312201.2 299847.6 290673.8 289421.6 292400.7 [9] 289118.0 284054.5 282544.5 278518.0 290888.9 305698.4 313991.4 314604.2 [17] 306571.4 300827.4 301140.4 304875.0 303752.1 301155.0 301261.4 299366.6 [25] 309975.2 322459.9 329692.9 331092.2 > if (par2 == 0) { + x <- exp(x) + forecast$pred <- exp(forecast$pred) + lb <- exp(lb) + ub <- exp(ub) + } > if (par2 != 0) { + x <- x^(1/par2) + forecast$pred <- forecast$pred^(1/par2) + lb <- lb^(1/par2) + ub <- ub^(1/par2) + } > if (par2 < 0) { + olb <- lb + lb <- ub + ub <- olb + } > (actandfor <- c(x[1:nx], forecast$pred)) [1] 325412.0 326011.0 328282.0 317480.0 317539.0 313737.0 312276.0 309391.0 [9] 302950.0 300316.0 304035.0 333476.0 337698.0 335932.0 323931.0 313927.0 [17] 314485.0 313218.0 309664.0 302963.0 298989.0 298423.0 310631.0 329765.0 [25] 335083.0 327616.0 309119.0 295916.0 291413.0 291542.0 284678.0 276475.0 [33] 272566.0 264981.0 275098.6 288258.8 292535.4 288108.9 273788.9 262476.2 [41] 258583.4 259216.4 254132.6 247377.7 244025.3 238184.1 246365.2 256687.7 [49] 259914.2 256431.4 245239.1 236334.8 233204.6 233682.8 229711.3 224406.9 [57] 221743.9 217121.5 223581.0 231732.2 234271.8 231516.5 > (perc.se <- (ub-forecast$pred)/1.96/forecast$pred) Time Series: Start = 35 End = 62 Frequency = 1 [1] 0.01730385 0.02645218 0.03612423 0.04266423 0.04856024 0.05481079 [7] 0.06084611 0.06531510 0.07023786 0.07564409 0.08053522 0.08639754 [13] 0.09220528 0.09741579 0.10615201 0.11574247 0.12759787 0.13922780 [19] 0.14862973 0.15543537 0.16444950 0.17449214 0.18295957 0.19326424 [25] 0.19714863 0.19975488 0.20781107 0.21943977 > postscript(file="/var/www/html/rcomp/tmp/104a51260468962.ps",horizontal=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > opar <- par(mar=c(4,4,2,2),las=1) > ylim <- c( min(x[first:nx],lb), max(x[first:nx],ub)) > plot(x,ylim=ylim,type='n',xlim=c(first,lx)) > usr <- par('usr') > rect(usr[1],usr[3],nx+1,usr[4],border=NA,col='lemonchiffon') > rect(nx1,usr[3],usr[2],usr[4],border=NA,col='lavender') > abline(h= (-3:3)*2 , col ='gray', lty =3) > polygon( c(nx1:lx,lx:nx1), c(lb,rev(ub)), col = 'orange', lty=2,border=NA) > lines(nx1:lx, lb , lty=2) > lines(nx1:lx, ub , lty=2) > lines(x, lwd=2) > lines(nx1:lx, forecast$pred , lwd=2 , col ='white') > box() > par(opar) > dev.off() null device 1 > prob.dec <- array(NA, dim=fx) > prob.sdec <- array(NA, dim=fx) > prob.ldec <- array(NA, dim=fx) > prob.pval <- array(NA, dim=fx) > perf.pe <- array(0, dim=fx) > perf.mape <- array(0, dim=fx) > perf.mape1 <- array(0, dim=fx) > perf.se <- array(0, dim=fx) > perf.mse <- array(0, dim=fx) > perf.mse1 <- array(0, dim=fx) > perf.rmse <- array(0, dim=fx) > for (i in 1:fx) { + locSD <- (ub[i] - forecast$pred[i]) / 1.96 + perf.pe[i] = (x[nx+i] - forecast$pred[i]) / forecast$pred[i] + perf.se[i] = (x[nx+i] - forecast$pred[i])^2 + prob.dec[i] = pnorm((x[nx+i-1] - forecast$pred[i]) / locSD) + prob.sdec[i] = pnorm((x[nx+i-par5] - forecast$pred[i]) / locSD) + prob.ldec[i] = pnorm((x[nx] - forecast$pred[i]) / locSD) + prob.pval[i] = pnorm(abs(x[nx+i] - forecast$pred[i]) / locSD) + } > perf.mape[1] = abs(perf.pe[1]) > perf.mse[1] = abs(perf.se[1]) > for (i in 2:fx) { + perf.mape[i] = perf.mape[i-1] + abs(perf.pe[i]) + perf.mape1[i] = perf.mape[i] / i + perf.mse[i] = perf.mse[i-1] + perf.se[i] + perf.mse1[i] = perf.mse[i] / i + } > perf.rmse = sqrt(perf.mse1) > postscript(file="/var/www/html/rcomp/tmp/21f4x1260468962.ps",horizontal=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > plot(forecast$pred, pch=19, type='b',main='ARIMA Extrapolation Forecast', ylab='Forecast and 95% CI', xlab='time',ylim=c(min(lb),max(ub))) > dum <- forecast$pred > dum[1:par1] <- x[(nx+1):lx] > lines(dum, lty=1) > lines(ub,lty=3) > lines(lb,lty=3) > dev.off() null device 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,'Univariate ARIMA Extrapolation Forecast',9,TRUE) > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,'time',1,header=TRUE) > a<-table.element(a,'Y[t]',1,header=TRUE) > a<-table.element(a,'F[t]',1,header=TRUE) > a<-table.element(a,'95% LB',1,header=TRUE) > a<-table.element(a,'95% UB',1,header=TRUE) > a<-table.element(a,'p-value
(H0: Y[t] = F[t])',1,header=TRUE) > a<-table.element(a,'P(F[t]>Y[t-1])',1,header=TRUE) > a<-table.element(a,'P(F[t]>Y[t-s])',1,header=TRUE) > mylab <- paste('P(F[t]>Y[',nx,sep='') > mylab <- paste(mylab,'])',sep='') > a<-table.element(a,mylab,1,header=TRUE) > a<-table.row.end(a) > for (i in (nx-par5):nx) { + a<-table.row.start(a) + a<-table.element(a,i,header=TRUE) + a<-table.element(a,x[i]) + a<-table.element(a,'-') + a<-table.element(a,'-') + a<-table.element(a,'-') + a<-table.element(a,'-') + a<-table.element(a,'-') + a<-table.element(a,'-') + a<-table.element(a,'-') + a<-table.row.end(a) + } > for (i in 1:fx) { + a<-table.row.start(a) + a<-table.element(a,nx+i,header=TRUE) + a<-table.element(a,round(x[nx+i],4)) + a<-table.element(a,round(forecast$pred[i],4)) + a<-table.element(a,round(lb[i],4)) + a<-table.element(a,round(ub[i],4)) + a<-table.element(a,round((1-prob.pval[i]),4)) + a<-table.element(a,round((1-prob.dec[i]),4)) + a<-table.element(a,round((1-prob.sdec[i]),4)) + a<-table.element(a,round((1-prob.ldec[i]),4)) + a<-table.row.end(a) + } > a<-table.end(a) > table.save(a,file="/var/www/html/rcomp/tmp/324121260468962.tab") > a<-table.start() > a<-table.row.start(a) > a<-table.element(a,'Univariate ARIMA Extrapolation Forecast Performance',7,TRUE) > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,'time',1,header=TRUE) > a<-table.element(a,'% S.E.',1,header=TRUE) > a<-table.element(a,'PE',1,header=TRUE) > a<-table.element(a,'MAPE',1,header=TRUE) > a<-table.element(a,'Sq.E',1,header=TRUE) > a<-table.element(a,'MSE',1,header=TRUE) > a<-table.element(a,'RMSE',1,header=TRUE) > a<-table.row.end(a) > for (i in 1:fx) { + a<-table.row.start(a) + a<-table.element(a,nx+i,header=TRUE) + a<-table.element(a,round(perc.se[i],4)) + a<-table.element(a,round(perf.pe[i],4)) + a<-table.element(a,round(perf.mape1[i],4)) + a<-table.element(a,round(perf.se[i],4)) + a<-table.element(a,round(perf.mse1[i],4)) + a<-table.element(a,round(perf.rmse[i],4)) + a<-table.row.end(a) + } > a<-table.end(a) > table.save(a,file="/var/www/html/rcomp/tmp/4z1jd1260468962.tab") > system("convert tmp/104a51260468962.ps tmp/104a51260468962.png") > system("convert tmp/21f4x1260468962.ps tmp/21f4x1260468962.png") > > > proc.time() user system elapsed 0.888 0.317 1.000