# NOT RUN {
data(cars)
flist <- alist(
dist ~ dnorm( mu , sigma ) ,
mu <- a+b*speed ,
c(a,b) ~ dnorm(0,1) ,
sigma ~ dexp(1)
)
fit <- quap( flist , start=list(a=40,b=0.1,sigma=20) , data=cars )
# regularized logistic regression example
y <- c( rep(0,10) , rep(1,10) )
x <- c( rep(-1,9) , rep(1,11) )
flist0 <- alist(
y ~ dbinom( 1 , p ) ,
logit(p) <- a + b*x
)
flist1 <- alist(
y ~ dbinom( 1 , p ),
logit(p) <- a + b*x ,
c(a,b) ~ dnorm(0,10)
)
# without priors, same problem as:
# glm3a <- glm( y ~ x , family=binomial , data=list(y=y,x=x) )
fit3a <- quap( flist0 , data=list(y=y,x=x) , start=list(a=0,b=0) )
precis(fit3a)
# now with regularization
fit3b <- quap( flist1 , data=list(y=y,x=x) , start=list(a=0,b=0) )
precis(fit3b)
# vector parameters
data(UCBadmit)
fit4 <- quap(
alist(
admit ~ dbinom( apps , p ),
logit(p) <- a[dept_id] + b*male,
a[dept_id] ~ dnorm(0,4),
b ~ dnorm(0,1)
),
data=list(
admit = UCBadmit$admit,
apps = UCBadmit$applications,
male = ifelse( UCBadmit$applicant.gender=="male" , 1 , 0 ),
dept_id = coerce_index(UCBadmit$dept)
)
)
# }
Run the code above in your browser using DataLab