Skip to content

Instantly share code, notes, and snippets.

@chiral
Created April 14, 2014 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chiral/10635658 to your computer and use it in GitHub Desktop.
Save chiral/10635658 to your computer and use it in GitHub Desktop.
brand score estimation toy model with bayesian logistic regression
model {
for (i in 1:N) {
y[i] ~ dbern(p[i])
for (j in 1:K) {
q[i,j] <- b[j]*x[i,j]
}
logit(p[i]) <- a+sum(q[i,1:K])
}
a ~ dnorm(a_mu,a_tau)
b[1] ~ dnorm(0,1.0)
b[2] ~ dnorm(0,1.0)
}
library(R2WinBUGS)
library(coda)
setwd("~/R")
eps <- 1.0E-4
N <- 10
K <- 2
data1 <- list(
x=cbind(
c(100,120,90,100,80,70,200,100,150,110),
c(0,1,0,1,0,0,1,0,0,0)
),
y=c(0,1,0,1,0,1,1,1,0,0),
a_mu=0.5,
a_tau=1.0,
N=N,K=K)
data2 <- list(
x=cbind(
c(100,120,90,100,80,70,200,100,150,110),
c(0,1,0,1,0,0,1,0,0,0)
),
y=c(1,0,1,0,1,1,0,0,1,1),
a_mu=0.5,
a_tau=1.0,
N=N,K=K)
inits <- function() {
list(a=eps,b=rep(eps,K))
}
parameters <- c("a","b")
result.sim <- bugs(data2, inits, parameters,
model.file="brand2.bugs",
n.chains = 4, n.iter = 1000,
debug=F,
working.directory=getwd())
result.sim$sims.list$beta1
print(result.sim, digits=3)
plot(as.mcmc(result.sim$sims.matrix))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment