Skip to content

Instantly share code, notes, and snippets.

@MJacobs1985
Created February 21, 2022 13:52
Show Gist options
  • Select an option

  • Save MJacobs1985/8c2c416f25f7e02c37af87dfa05cb5e8 to your computer and use it in GitHub Desktop.

Select an option

Save MJacobs1985/8c2c416f25f7e02c37af87dfa05cb5e8 to your computer and use it in GitHub Desktop.
train <- window(Eggs,end=2021.310)
fit <- auto.arima(train)
refit <- Arima(Eggs, model=fit)
fc <- window(fitted(refit), start=2021.31)
# Multi-step, re-estimation
h <- 5
train <- window(Eggs,end=2021.310)
test <- window(Eggs,start=2021.311)
n <- length(test) - h + 1
fit <- auto.arima(train)
order <- arimaorder(fit)
fcmat <- matrix(0, nrow=n, ncol=h)
for(i in 1:n)
{
x <- window(Eggs, end=2021.310 + (i-1)/365.25)
refit <- auto.arima(x)
fcmat[i,] <- forecast(refit, h=h)$mean
}
heatmap(fcmat)
ind <- which( ! is.na(fcmat) , arr.ind = TRUE )
out <- cbind( fcmat[ ! is.na( fcmat ) ] , ind )
traindf<-as.data.frame(train)%>%tibble::rownames_to_column(., "row")%>%mutate(row=as.numeric(row))
outdf<-as.data.frame(out)%>%group_by(col)%>%mutate(row=row+max(traindf$row))
testdf<-as.data.frame(test)%>%
tibble::rownames_to_column(., "row")%>%
mutate(row=as.numeric(row))%>%
mutate(row=row+max(traindf$row))
ggplot()+
geom_point(data=outdf, aes(x=row, y=V1, colour=as.factor(col)))+
geom_line(data=outdf, aes(x=row, y=V1, colour=as.factor(col)))+
geom_point(data=traindf, aes(x=row, y=egg_count))+
geom_line(data=traindf, aes(x=row, y=egg_count))+
geom_point(data=testdf, aes(x=row, y=egg_count))+
geom_line(data=testdf, aes(x=row, y=egg_count))+
theme_bw()+
labs(x="Days", y="Egg forecast", col="Multi-step")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment