Skip to content

Instantly share code, notes, and snippets.

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

  • Save MJacobs1985/7c485dc701ebb0ce7338f0bbbb725008 to your computer and use it in GitHub Desktop.

Select an option

Save MJacobs1985/7c485dc701ebb0ce7338f0bbbb725008 to your computer and use it in GitHub Desktop.
## 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(.,
start=c(2021,9,4),
frequency=365.25)
autoplot(ts(ext_var[,1:3],
start=c(2021,9,4),
frequency=365.25))+theme_bw()
autoplot(ts(ext_var[,4],
start=c(2021,9,4),
frequency=365.25))+theme_bw()
nhor<-14
var.pred<-predict(var.model_lev,
dumvar=ext_var[143:156,],
n.ahead=nhor)
plot(var.pred);fanchart(var.pred)
Egg_for_data<-cbind(date_interval=seq(ymd('2022-01-24'),
ymd('2022-02-06'),
by = "day"),
as.data.frame(var.pred$fcst$Eggs))
g1<-autoplot(ts(ext_var[,1:3],
start=c(2021,9,4),
frequency=365.25))+
theme_bw()+theme(legend.position="bottom")
g2<-autoplot(ts(ext_var[,4],
start=c(2021,9,4),
frequency=365.25))+theme_bw()
g3<-plot(var.pred)
g4<-ext%>%
filter(location_identification==1)%>%
ggplot(.)+
geom_point(aes(x=date(date_interval), y=egg_count))+
geom_line(aes(x=date(date_interval), y=egg_count),alpha=0.7)+
geom_point(data=Egg_for_data,
aes(x=date_interval, y=fcst), col="red")+
geom_line(data=Egg_for_data,
aes(x=date_interval, y=fcst), col="red", lty=2)+
geom_ribbon(data=Egg_for_data,
aes(x=date_interval, ymin=lower, ymax=upper, fill="red"), alpha=0.3,
show.legend = FALSE)+
labs(x="Date",
y="Egg_count",
title="Egg count predicted for location 1")+
theme_bw()
grid.arrange(g1,g2,g4,ncol=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment