我只是想知道是否有人有一些使用包 R2WinBUGS 运行逻辑回归的 R 代码 - 理想情况下使用模拟数据来生成“真相”和两个连续的协变量。
谢谢。
基督教
PS:
生成人工数据(一维案例)并通过 r2winbugs 运行 winbugs 的潜在代码(它还不能工作)。
library(MASS)
library(R2WinBUGS)
setwd("d:/BayesianLogisticRegression")
n.site <- 150
X1<- sort(runif(n = n.site, min = -1, max =1))
xb <- 0.0 + 3.0*X1
occ.prob <- 1/(1+exp(-xb))
plot(X1, occ.prob,xlab="X1",ylab="occ.prob")
true.presence <- rbinom(n = n.site, size = 1, prob = occ.prob)
plot(X1, true.presence,xlab="X1",ylab="true.presence")
# combine data as data frame and save
data <- data.frame(X1, true.presence)
write.matrix(data, file = "data.txt", sep = "\t")
sink("model.txt")
cat("
model {
# Priors
alpha ~ dnorm(0,0.01)
beta ~ dnorm(0,0.01)
# Likelihood
for (i in 1:n) {
C[i] ~ dbin(p[i], N) # Note p before N
logit(p[i]) <- alpha + beta *X1[i]
}
}
",fill=TRUE)
sink()
# Bundle data
win.data <- list(mass = X1, n = length(X1))
# Inits function
inits <- function(){ list(alpha=rlnorm(1), beta=rlnorm(1))}
# Parameters to estimate
params <- c("alpha", "beta")
# MCMC settings
nc <- 3 #Number of Chains
ni <- 1200 #Number of draws from posterior
nb <- 200 #Number of draws to discard as burn-in
nt <- 2 Thinning rate
# Start Gibbs sampling
out <- bugs(data=win.data, inits=inits, parameters.to.save=params,
model.file="model.txt", n.thin=nt, n.chains=nc, n.burnin=nb,
n.iter=ni, debug = TRUE)