Skip to content

Instantly share code, notes, and snippets.

View robjhyndman's full-sized avatar

Rob J Hyndman robjhyndman

View GitHub Profile
library(fpp3)
state_tourism <- tourism |>
group_by(State) |>
summarise(Trips = sum(Trips))
training <- state_tourism |>
filter(year(Quarter) <= 2014)
fit <- training |>
model(
ets = ETS(Trips),
arima = ARIMA(Trips),
@robjhyndman
robjhyndman / gist:a11eba538d26ba60454abd600ea6204d
Created March 27, 2023 21:47
Using stretch_tsibble with covariates
library(fpp3)
# Stretched data
stretch_uschange <- us_change |>
stretch_tsibble(.init = 5)
# Fit models to all folds of data
fit <- stretch_uschange |>
model(lm = TSLM(Consumption ~ Income))
par(mar=c(0,0,0,0))
plot(0,0,xlim=c(0,28),ylim=c(0,1),
xaxt="n",yaxt="n",bty="n",xlab="",ylab="",type="n")
i <- 1
for(j in 1:20)
{
test <- (6+j):26
train <- 1:(5+j)
arrows(0,1-j/20,27,1-j/20,0.05)
points(train,rep(1-j/20,length(train)),pch=19,col="blue")
@robjhyndman
robjhyndman / tscv-xreg.R
Last active July 14, 2022 10:00
tsCV with xreg
library(forecast)
fc <- function(y, h, xreg, newxreg) {
fit <- auto.arima(y, xreg=xreg)
forecast(fit, xreg=newxreg, h=h)
}
y <- ts(rnorm(100))
x <- matrix(ts(rnorm(100)),ncol=1)
tsCV(y, fc, xreg=x, h=1)
@robjhyndman
robjhyndman / tscv.R
Created December 7, 2016 08:30
Time series cross-validation diagrams
par(mar=c(0,0,0,0))
plot(0,0,xlim=c(0,28),ylim=c(0,1),
xaxt="n",yaxt="n",bty="n",xlab="",ylab="",type="n")
i <- 1
for(j in 1:20)
{
test <- (6+j):26
train <- 1:(5+j)
arrows(0,1-j/20,27,1-j/20,0.05)
points(train,rep(1-j/20,length(train)),pch=19,col="blue")
@robjhyndman
robjhyndman / fourier.R
Created August 4, 2016 00:26
Use of Fourier series in simulating from an ARIMA model
library(forecast)
fit <- auto.arima(USAccDeaths, xreg=fourier(USAccDeaths, 4))
fc <- forecast(fit, xreg=fourier(USAccDeaths, 4, 12))
plot(fc)
for(i in 1:12)
lines(simulate(fit, xreg=fourier(USAccDeaths, 4, 12)))