Skip to content

Instantly share code, notes, and snippets.

@MJacobs1985
Created February 21, 2022 12:29
Show Gist options
  • Select an option

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

Select an option

Save MJacobs1985/db224323b5fedd6a282837ab668187dd to your computer and use it in GitHub Desktop.
set.seed(1123)
inTraining <- createDataPartition(dfnew$newField,
p = .50,
list = FALSE)
training <- dfnew[ inTraining,]
testing <- dfnew[-inTraining,]
m1_gam_comp1 <- gamm4(comp1_provided~
s(curve_day)+
s(animals_actual)+
s(feed_provided_cum)+
s(comp2_provided)+
ogn_id+
newField,
data=training,
random=~(1+curve_day|lcn_id))
par(mfrow = c(2, 2))
plot(m1_gam_comp1$gam)
m1_gam_comp1_aug<-augment(m1_gam_comp1$gam, confint=TRUE)
m1_gam_comp1_aug%>%filter(lcn_id%in%(901:909))%>%
ggplot(., aes(x=comp1_provided, y=.fitted, colour=.resid))+
geom_point()+
theme_bw()+
facet_wrap(~lcn_id+newField)+
geom_abline(intercept=0, slope=1, lty=2, col="black")+
scale_color_viridis_c(option="turbo")
testing$comp2_provided<-ifelse(is.na(testing$comp2_provided),0,
testing$comp2_provided)
testing$pred<-predict(m1_gam_comp1$gam,
newdata=testing)
testing%>%filter(lcn_id%in%(901:920))%>%
ggplot(., aes(x=curve_day, group=newField))+
geom_point(aes(y=comp1_provided), alpha=0.5, color="black")+
geom_point(aes(y=pred, colour=newField), lty=2, lwd=1)+
theme_bw()+
facet_wrap(~lcn_id+newField)
g1<-testing%>%
group_by(ogn_id, curve_day)%>%
summarize(prov = sum(comp1_provided),
pred = sum(pred))%>%
ggplot(., aes(x=curve_day))+
geom_area(aes(y=prov,
fill=as.factor(ogn_id)),
alpha=0.5)+
geom_point(aes(y=pred), alpha=0.5)+
geom_line(aes(y=pred))+
theme_bw()+
labs(x="Day in Curve",
y="Comp1",
fill="Farm",
title="Predicted vs Observed on Comp1 - TEST data")+
facet_wrap(~ogn_id)
g2<-m1_gam_comp1_aug%>%
group_by(ogn_id,curve_day)%>%
summarize(prov = sum(comp1_provided, na.rm = TRUE),
pred = sum(.fitted, na.rm = TRUE))%>%
ggplot(., aes(x=curve_day))+
geom_area(aes(y=prov,
fill=as.factor(ogn_id)),
alpha=0.5)+
geom_point(aes(y=pred), alpha=0.5)+
geom_line(aes(y=pred))+
theme_bw()+
labs(x="Day in Curve",
y="Comp1",
fill="Farm",
title="Predicted vs Observed on Component 1 - TRAIN data")+
facet_wrap(~ogn_id)
grid.arrange(g2,g1,ncol=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment