Skip to content

Instantly share code, notes, and snippets.

View MJacobs1985's full-sized avatar

Marc Jacobs MJacobs1985

View GitHub Profile
par(mfrow = c(2, 2))
Book<- read_excel("X:/StatisticsPlatform/Website/ELearning/Workshops/Bayesian Analysis/Data/Book1.xlsx")
d<-Book%>%dplyr::select(ADF,ADG,BW_start)
d<-d[complete.cases(d), ]
summary(d$ADG)
summary(d$ADF)
cov(d$ADG, d$ADF)
pairs(d)
m_dnorm <- ulam(
alist(
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) )
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,
m11.4 <- ulam(
alist(
Included2017 ~ dbinom( 1 , p ) ,
logit(p) <- a + b*ADG +c*BW_start + d[Country] ,
a ~ dnorm( 2 , 1 ),
b ~ dnorm( 0.5 , 1 ),
c ~ dnorm( 0.5 , 1 ),
d[Country] ~ dnorm( 3 , 1 )
) , data=d, chains=4, cores=4)
show(m11.4)
@MJacobs1985
MJacobs1985 / m11.3
Last active December 2, 2021 21:46
d<-Book%>%dplyr::select(Included2017,ADG,BW_start,Country)
table(d$Country, d$Included2017)
d$Included2017<-as.factor(d$Included2017)
levels(d$Included2017)<-list("1"="Yes", "0"="No")
fit<-glm(Included2017~ ADG + BW_start + Country,
data=d,
family="binomial",x=TRUE, y=TRUE)
summary(fit)
d$Included2017<-as.numeric(d$Included2017)-1
d<-d[complete.cases(d), ]
d<-Book%>%dplyr::select(Included2017, ADG,ADF,BW_start)
d<-d[complete.cases(d), ]
d$Included2017<-as.factor(d$Included2017)
levels(d$Included2017)<-list("1"="Yes", "0"="No")
fit<-glm(Included2017~ADF*ADG + BW_start,
data=d,
family="binomial",x=TRUE, y=TRUE)
d$Included2017<-as.numeric(d$Included2017-1)
m11.2 <- quap(
m8.6 <- ulam(
alist(
ADG ~ dnorm( mu , sigma ) ,
mu <- a + b*ADF + c*BW_start + d*ADF*BW_start ,
a ~ dnorm( 0.5 , 0.25 ) ,
b ~ dnorm( 0 , 0.25 ) ,
c ~ dnorm( 0 , 0.25 ) ,
d ~ dnorm( 0 , 0.25 ) ,
sigma ~ dexp( 1 )
) , data=d, chains=4, cores=4)
d<-Book%>%dplyr::select(ADG,ADF,BW_start)
d<-d[complete.cases(d), ]
fit<-lm(ADG~ADF*BW_start, data=d)
m8.5 <- quap(
alist(
ADG ~ dnorm( mu , sigma ) ,
mu <- a + b*ADF + c*BW_start + d*ADF*BW_start ,
a ~ dnorm( 0.5 , 0.25 ) ,
b ~ dnorm( 0 , 0.25 ) ,
c ~ dnorm( 0 , 0.25 ) ,
d<-Book%>%dplyr::select(ADG,ADF,Country, Challenge)
d$Country<-as.factor(d$Country);levels(d$Country)
d$Challenge<-as.factor(d$Challenge);levels(d$Challenge)
d$Country_id <- as.integer(d$Country )
d$Challenge_id <- as.integer(d$Challenge )
m5.9 <- quap(
alist(
ADG ~ dnorm( mu , sigma ),
mu <- a[Country_id],
a[Country_id] ~ dnorm(0 , 0.5 ),
m5.3_B <- quap(
alist(
ADG ~ dnorm(mu , sigma ) ,
mu <- a + b*ADF + bB*B + bI*I + bS*S + bUK*UK,
a ~ dnorm (-2, 3 ) ,
b ~ dnorm (1 , 2 ) ,
bB ~ dnorm (0 , 2 ) ,
bI ~ dnorm (-10 , 3 ) ,
bS ~ dnorm (4 , 2 ) ,
bUK ~ dnorm (-1 , 2 ) ,