Skip to content

Instantly share code, notes, and snippets.

@MJacobs1985
Last active December 3, 2021 11:18
Show Gist options
  • Select an option

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

Select an option

Save MJacobs1985/3aab94ffeeaa0fa434b7b06f7d329191 to your computer and use it in GitHub Desktop.
Book<- read_excel("X:/StatisticsPlatform/Website/ELearning/Workshops/Bayesian Analysis/Data/Book1.xlsx")
d<-Book%>%dplyr::select(block,ADG,BW_start,Country)
d$bl<-d$block
table(d$Country, d$bl)
d$block<-NULL
d<-d[complete.cases(d), ]
d$Country<-as.factor(d$Country)
dim(d)
head(d)
fit<-glm(bl~ ADG + BW_start + Country,
data=d,
family="poisson",x=TRUE, y=TRUE)
summary(fit)
m11.5 <- ulam(
alist(
bl ~ dpois(lambda) ,
log(lambda) <- a + b*ADG +c*BW_start + d[Country] ,
a ~ dnorm( 4 , 0.3 ),
b ~ dnorm( 0.01 , 0.005 ),
c ~ dnorm( 0.1 , 0.01 ),
d[Country] ~ dnorm( 0.5 , 0.05 )
) , data=d, chains=4, cores=4,log_lik=TRUE)
show(m11.5)
precis(m11.5 , 2 )
pairs(m11.5 )
traceplot(m11.5)
trankplot(m11.5 )
fit<-glm.nb(bl~ ADG + BW_start + Country,
data=d, model=TRUE,x=TRUE, y=TRUE)
summary(fit)
m11.6 <- ulam(
alist(
bl ~ dgampois(mu, exp(log_scale)),
log(mu) <- a + b*ADG +c*BW_start + d[Country] ,
a ~ dnorm( 4 , 0.3 ),
b ~ dnorm( 0.01 , 0.005 ),
c ~ dnorm( 0.1 , 0.01 ),
d[Country] ~ dnorm( 0.5 , 0.05 ),
log_scale ~ dnorm( 3 , 0.5 )
) , data=d, chains=4, cores=4,log_lik=TRUE)
show(m11.6)
precis(m11.6 , 2 )
pairs(m11.6 )
traceplot(m11.6)
trankplot(m11.6 )
rethinking::compare( m11.5 , m11.6 , func=PSIS )
k <- PSIS( m11.6 , pointwise=TRUE )$k
xyplot(as.factor(bl)~ADG | Country, data=d,
xlab="log ADG" ,
ylab="total blocks" ,
col=rangi2 , lwd=2 ,
ylim=c(0,75) , cex=1+normalize(k) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment