Skip to content

Instantly share code, notes, and snippets.

View MJacobs1985's full-sized avatar

Marc Jacobs MJacobs1985

View GitHub Profile
@MJacobs1985
MJacobs1985 / M5.3A
Last active December 2, 2021 13:01
d<-Book%>%dplyr::select(ADG,ADF,Country)
d$Country<-as.factor(d$Country)
d<-as.data.frame(d)
fit<-lm(ADG~ADF+Country, data=d);summary(fit)
dn <- mltools::one_hot(as.data.table(d))
dn$B <- dn$Country_Belgium
dn$I <- dn$Country_Italy
dn$P <- dn$Country_Poland
dn$S <- dn$Country_Spain
dn$UK <- dn$Country_UK
rethinking::compare(m4.3, m4.5, m4.6, func=WAIC )
plot(rethinking::compare(m4.3, m4.5, m4.6, func=WAIC ))
par(mfrow = c(1, 3))
d$ADF_s2 <- d$ADF^2
fit <-lm(ADG~ADF+ADF_s2, data=d)
m4.5 <- quap(
alist(
ADG ~ dnorm( mu , sigma ) ,
mu <- a + b1*ADF + b2*ADF_s2 ,
a ~ dnorm (30 , 16 ) ,
b1 ~ dlnorm(1 , 1 ) ,
b2 ~ dnorm (0.5 , 1 ) ,
@MJacobs1985
MJacobs1985 / m51
Last active December 2, 2021 12:33
par(mfrow = c(1, 4))
N <- 50
dN <- d[ 1:N , ]
mN51 <- quap(
alist(
ADG ~ dnorm( mu , sigma ) ,
mu <- a + b*(ADF) ,
a ~ dnorm(mean(d$ADG) , sd(d$ADG) ) ,
b ~ dlnorm( 0 , 1 ) ,
sigma ~ dunif( 0 , 50 )
par(mfrow = c(1, 2))
N <- 100
dN <- d[ 1:N , ]
mN <- quap(
alist(
ADG ~ dnorm( mu , sigma ) ,
mu <- a + b*(d$ADF) ,
a ~ dnorm(mean(d$ADG) , sd(d$ADG) ) ,
b ~ dnorm(0.6153863 , 0.02) ,
sigma ~ dunif( 0 , 50 )
@MJacobs1985
MJacobs1985 / m4.3
Last active December 2, 2021 12:01
set.seed(2971)
N <- 100 # 100 lines
a <- rnorm( N , mean(d$ADG) , sd(d$ADG) )
b <- rlnorm(1e4, as.vector(coef(fit)[2]) , 1 )
par(mfrow = c(2,2))
dens(a)
dens( b , xlim=c(0,5) , adj=0.1 )
abline(v=as.vector(coef(fit)[2]), lty=2, col="red")
m4.3 <- quap(
alist(
par(mfrow = c(2, 2))
fit<-lm(ADG~ADF, data=d);fit;plot(fit);summary(fit)
par(mfrow = c(1, 3))
plot(d$ADG ~ d$ADF )
set.seed(2971)
N <- 100 # 100 lines
a <- rnorm(N , 3.7 , 2 )
b <- rnorm(N , 0.6 , 2 )
plot( NULL , xlim=range(d$ADF) , ylim=range(d$ADG) ,
xlab="ADF" , ylab="ADG" )
m4.2 <- quap(
alist(
ADG ~ dnorm( mu , sigma ) ,
mu ~ dnorm( 70 , 1 ) ,
sigma ~ dunif(6 , 10 )
) , data=d )
precis( m4.2 )
flist <- alist(
ADG ~ dnorm( mu , sigma ) ,
mu ~ dnorm( 70 , 20 ) ,
sigma ~ dunif( 6 , 50 ))
m4.1 <- quap( flist , data=d )
precis( m4.1 )
par(mfrow = c(1, 2))
start <- list(
mu=mean(d$ADG),
sigma=sd(d$ADG))
par(mfrow = c(1, 3))
d3 <- sample( d$ADG , size=20 )
mu.list <- seq( from=60, to=70 , length.out=200 )
sigma.list <- seq( from=5 , to=10 , length.out=200 )
post2 <- expand.grid( mu=mu.list , sigma=sigma.list )
post2$LL <- sapply( 1:nrow(post2) , function(i)
sum( dnorm( d3 , mean=post2$mu[i] , sd=post2$sigma[i] ,
log=TRUE ) ) )
post2$prod <- post2$LL + dnorm( post2$mu , mean(d$ADG) , sd(d$ADG) , TRUE ) +
dunif( post2$sigma , 0 , 50 , TRUE )