Skip to content

Instantly share code, notes, and snippets.

View MJacobs1985's full-sized avatar

Marc Jacobs MJacobs1985

View GitHub Profile
tsdat<-day%>%
filter(location_identification==1)%>%
mutate(egg_count = zoo::na.aggregate(egg_count))%>%
dplyr::select(egg_count, date_interval)
tsdat<-xts(tsdat$egg_count, order.by=as.Date(tsdat$date_interval))
soliq_arimax = auto.arima(tsdat,
seasonal= TRUE,
stepwise=FALSE,
approximation=FALSE,
allowdrift = TRUE,
soliq_arimax = auto.arima(dfy[,"Eggs"], # specify main trend
xreg = dfexo[,c("TempMIN",
"TempMAX",
"TempACT",
"AnimalACT")], # specify exogenous variables here
trace = TRUE,
seasonal= FALSE,
stepwise=FALSE,
approximation=FALSE)
summary(soliq_arimax);autoplot(soliq_arimax);checkresiduals(soliq_arimax)+theme_bw()
registerDoParallel(cores=8)
set.seed(123)
Eggs2<-day%>%
filter(location_identification==1)%>%
mutate(egg_count = zoo::na.aggregate(egg_count),
feed_provided = zoo::na.aggregate(feed_provided),
water_provided = zoo::na.aggregate(water_provided),
animals_actual = zoo::na.aggregate(animals_actual),
t_min_out = zoo::na.aggregate(t_min_out),
t_max_out = zoo::na.aggregate(t_min_out),
m2 <- auto.arima(test,
ic="aic",
seasonal=FALSE,
stationary=TRUE,
stepwise=FALSE,
approximation=FALSE)
ggtsdiag(m2, gof.lag = 14)+theme_bw()
m1.Mul.F <- forecast(m1, length(test))
plot(m1.Mul.F)
Eggsdf<-as.data.frame(Eggs)
far1 <- function(x, h){forecast(Arima(x, order=c(1,0,0), seasonal=FALSE), h=h)}
e <- tsCV(Eggsdf$egg_count, far1, h=1)
fc <- c(NA, Eggsdf$egg_count[2:149] - e[1:148])
train <-window(Eggs, end=2021.310)
autoplot(train)+theme_bw()
test <-window(Eggs, start=2021.311)
m1 <- auto.arima(train,
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)
## External validation of VAR model ##
ext <- read_csv("ExternalVal.csv")
ext_var<-ext%>%
filter(location_identification==1)%>%
mutate(TempMIN = zoo::na.aggregate(t_min_out),
TempMAX = zoo::na.aggregate(t_act_out),
TempACT = zoo::na.aggregate(t_max_out),
AnimalACT = zoo::na.aggregate(animals_actual))%>%
dplyr::select(TempMIN,TempMAX,TempACT,AnimalACT)%>%
ts(.,
Serial1 <- serial.test(var.model_lev,
lags.pt = 5,
type = "PT.asymptotic");Serial1;plot(Serial1)
Arch1 <- arch.test(var.model_lev,
lags.multi = 14,
multivariate.only = TRUE);Arch1;plot(Arch1)
Norm1 <- normality.test(var.model_lev,
multivariate.only = TRUE);Norm1
lagselect<-VARselect(dfy,
lag.max = 14,
type = "both",
season = 7);lagselect$selection
var.model_lev<-vars::VAR(dfy,
p = 14,
type = "both",
season = 7,
exogen=cbind(dfexo))
inds <- seq(as.Date(min(day$date_interval)),
as.Date(max(day$date_interval)),
by = "day")
Eggs<-day%>%
filter(location_identification==1)%>%
mutate(egg_count = zoo::na.aggregate(egg_count))%>%
dplyr::select(egg_count)%>%
ts(.,
start=c(2021,9,4),
frequency=365.25)