[R-es] Pronósticos con modelos robusto de series de tiempo
Jose Betancourt B.
betanster en gmail.com
Vie Sep 6 00:27:35 CEST 2013
este ha sido bastante bueno
f52<-scan("f52.txt")
sb<-f52[1:200]; sp<-f52[201:245];
sb.ts <- ts(sb, start=c(2008, 1), frequency=52)
sb.ts # is important to see the end of this time series.
sp.ts<- ts(sp, start=c(2011, 45), frequency=52)
plot(sb.ts,ylab="No. focos")
# Main function
predAll<-function(x,h,tsname){
# INPUTS:
# x: time serie
# h: number of values to predict.
# tsname: maximun number of element in the hidden layer (nhmax=20).
#
##---- PACKAGES -----
library(forecast)
##---- MODELS -------
m1<-stl(x, s.window='periodic') #STL Decomposition
m2<-auto.arima(x) # arima
m3<-nnetar(x,lambda=0) #neural networks
# Predictions
f1<-forecast(m1, h=h);
f2<-forecast(m2, h=h);
f3<-forecast(m3, h=h);
# Fit accuracy
rmse1 <- mean((x-f1$fitted)^2)/var(x);
rmse2 <- mean((x-f2$fitted)^2)/var(x);
fitted<-f3$fitted;#fitted[1:52]<-x[1:52];
rmse3 <-
mean((x[(frequency(x)+1):length(x)]-fitted[(frequency(x)+1):length(x)])^2)/var(x[(frequency(x)+1):length(x)]);
#lab<-data.frame("F","R2","R2a","RSS")
mat<-cbind(c(rmse1),c(rmse2),c(rmse3))
tab<-as.data.frame(mat)
dimnames(tab)[[1]]<-c("RMSE")
dimnames(tab)[[2]] <-c("STL","ARIMA","NNAR")
print(tab)
# Plot all predictions together
par(mar=c(5.1, 4.1, 4.1, 8.1), xpd=TRUE)
plot(f1,xlab="Time in Weeks",ylab=tsname,main="All models")
lines(f2$mean,col=1,lwd=2)
lines(f3$mean,col=3,lwd=2)
lines(x,col=2,lwd=2)
legend.txt = c("STL","ARIMA","NNAR","Observed");
legend("topright",inset=c(-0.32,0),legend.txt,col = c(4,1,3,2),
lwd=c(2,2,2,2),lty = c(1,1,1,1,1), pch =
c(NA,NA,NA,NA),title="Models",bty="n");
return(list(stl=f1, arima=f2, nnar=f3))
}
result<-predAll(sb.ts,45,"Foci Number")
lines(sp.ts,col=2,lwd=2)# to test these predictions
ve al paquete AER
El 05/09/13, Enrique RAMOS <ceramos0 en yahoo.com.mx> escribió:
> Alguien me podría sugerir un paquete en R para generar pronóticos con
> modelos robusto de series de tiempo.
>
> Saludos
> Enrique RAMOS
> [[alternative HTML version deleted]]
>
>
Más información sobre la lista de distribución R-help-es