Consider the following STAN model from Chapter 12 of book Statistical Rethinking
library(rethinking)
data(UCBadmit)
d <- UCBadmit
d$gid <- ifelse( d$applicant.gender=="male" , 1L , 2L )
dat <- list( A=d$admit , N=d$applications , gid=d$gid )
m12.1 <- ulam(
alist(
A ~ dbetabinom( N , pbar , theta ),
logit(pbar) <- a[gid],
a[gid] ~ dnorm( 0 , 1.5 ),
transpars> theta <<- phi + 2.0,
phi ~ dexp(1)
), data=dat , chains=4 )
Below is my equivalent Julia/Turing code:
using Turing, StatisticalRethinking
d = CSV.read(sr_datadir("UCBadmit.csv"), DataFrame)
d.gid = map((gender) -> (gender == "male" ? 1 : 2) , d.gender)
@model function m121(admit, applications, gid)
a = Vector{Real}(undef, 2)
ϕ ~ Exponential(1)
θ = ϕ + 2.0
a .~ Normal(0, 1.5)
for i = 1:length(gid)
p̄ = logistic(a[gid[i]])
admit[i] ~ BetaBinomial(applications[i], p̄, θ)
end
end
sample121 = sample(m121(d.admit, d.applications, d.gid), NUTS(), 2000)
describe(sample121)
However my parameter means are all off
Parameter Mean logit(mean)
1 :ϕ 0.211097
2 Symbol("a[1]") 1.84039 -
3 Symbol("a[2]") 1.57485 -
As opposed to the one given in the book
ulam posterior: 2000 samples from m12.1
mean sd 5.5% 94.5% histogram
a[1] -0.45 0.41 -1.1 0.21 ▁▁▇▇▂▁
a[2] -0.34 0.40 -1.0 0.27 ▁▁▃▇▂▁
phi 1.05 0.78 0.1 2.44 ▇▇▅▃▂▁▁▁▁▁▁
theta 3.05 0.78 2.1 4.44 ▇▇▅▃▂▁▁▁▁▁▁
- What is wrong in my translation?
- What is Turing equivalent of
transpars
in STAN?
From the book:
transpars> (transformed parameters) so that Stan will return it in the samples.