<div>Fala, galera. Primeira vez usando este fórum.</div>
<div> </div>
<div>Tenho tido problemas para rodar meu código para previsão sazonal via ARIMA.</div>
<div> </div>
<div>Abaixo segue parte do código.</div>
<div> </div>
<div> </div>
<div>>s.ts</div>
<div> Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec<br>2004 118 143 169 158 143 135 135 140 135 125 120 120<br>2005 143 158 180 180 150 150 153 148 150 145 145 143<br>2006 165 180 190 168 163 163 168 175 180 160 155 150<br>
2007 152 163 185 163 158 175 175 178 168 168 160 160<br>2008 155 170 195 195 195 185 185 165 165 153 154 159<br></div>
<div> </div>
<div>##### Início de ARIMA #####</div>
<div>linhaX <- 3<br>dados <- matrix()<br>nome <- names(db)</div>
<div> i=4<br> get.best.arima <- function (x.ts, maxord=c(1,1,1,1,1,1)) {<br> best.aic <- 1e8<br> n <- length(x.ts)<br> for (p in 0:maxord[1]) for (d in 0:maxord[2]) for (q in 0:maxord[3])<br> for (P in 0:maxord[4]) for (D in 0:maxord[5]) for (Q in 0:maxord[6])<br>
{<br> fit <- arima(x.ts, order=c(p,d,q),seasonal=list(order=c(P,D,Q),period=12), method = "CSS")<br> fit.aic <- -2 * fit$loglik + (log(n) + 1) * length(fit$coef)<br> if (fit.aic < best.aic) {<br>
<br> best.aic <- fit.aic<br> best.fit <- fit<br> best.model <- c(p,d,q,P,D,Q)<br> }<br> }<br> list(best.aic,best.fit,best.model)<br> } </div>
<div><br> <br> s.ts <- ts(db[,i],start=2004, fre=12)<br> best.arima.ss <- get.best.arima(log(s.ts),maxord = c(2,2,2,2,2,2))<br> best.fit.ss <- best.arima.ss[[2]]<br> best.arima.ss[[3]]<br> pdf(paste("AR.", i-4, ".pdf", sep=""),width=7,height=5) <br>
ts.plot( cbind( window(s.ts, start=2004),exp(predict(best.fit.ss,24)$pred)),col=c("black","green"))<br> #par(new=TRUE)<br> #ts.plot(s.ts,best.fit.ss,col=c("black","red"))<br>
legend("bottomright",c("Real","Previsão_Passado","Previsão_Futuro"),col=1:3,lty=1,cex=0.7,merge = TRUE,bg = 'gray90')<br> dev.off()<br> <br> report<-function(Actual,Forecast,Net,R2) {</div>
<div> result <- new.env()<br> if (R2==0) R2<-1-(sum((Forecast-Actual)^2)/sum((Actual-mean(Actual))^2))<br> result$Rsquare<-R2<br> result$MAPE <- 100*(sum(abs((Actual-Forecast))/Actual))/nrow(Forecast) ## Mean Absolute Percent Error<br>
result$CFE <- sum((Actual-Forecast)) ## Cumulative Forecast Error<br> result$ME <- result$CFE/nrow(Forecast) ## Mean Error<br> result$MSE <- sum((Actual-Forecast)^2)/nrow(Forecast) ## Mean Squared Error<br>
result$RMSE <- sqrt(result$MSE) ## Root Mean Squared Error<br> result$MAD <- (sum(abs(Actual-Forecast))/nrow(Forecast)) ## Mean Absolute Deviation<br> result$SUMMARY <- summary(Net)<br>
return(result) </div>
<div> } ## Fim da função report<br> pred.past<- db[,colY]<br> previsao <- predict(best.fit.ss)<br> forecast <- as.matrix(pred.past[1:length(pred.past)])<br> reportAR <- report(db[,colY],forecast,best.fit.ss,0)<br>
dados <- rbind(dados,"Produto:"=nome[colY],R2=reportAR$Rsquare,MAPE=reportAR$MAPE,<br> CFE=reportAR$CFE,ME=reportAR$ME,MSE=reportAR$MSE,RMSE=reportAR$RMSE,MAD=reportAR$MAD)<br> write.table(dados,sep=",",file="ReportAR.csv",)</div>
<div><br> } ## Fim do for<br> return(dados)<br> <br>##### Fim de ARIMA #####<br><br clear="all"><br>-- <br>Att.,<br><br>Vinicius Macedo Magalhães<br>(21) 9584-1533<br><br></div>