我只是想知道是否有人使用包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)
答案 0 :(得分:5)
你的脚本正是这样做的。它几乎正常工作,它只需要一个简单的改变就可以使它工作:
win.data <- list(X1 = X1, n = length(X1), C = true.presence, N = 1)
哪个数据定义了WinBugs。变量C必须用true.presence填充,根据你生成的数据,N必须是1 - 请注意,这是N = 1的二项分布的特殊情况,称为Bernoulli - 一个简单的“硬币”翻转”。
这是输出:
> print(out, dig = 3)
Inference for Bugs model at "model.txt", fit using WinBUGS,
3 chains, each with 1200 iterations (first 200 discarded), n.thin = 2
n.sims = 1500 iterations saved
mean sd 2.5% 25% 50% 75% 97.5% Rhat n.eff
alpha -0.040 0.221 -0.465 -0.187 -0.037 0.114 0.390 1.001 1500
beta 3.177 0.478 2.302 2.845 3.159 3.481 4.165 1.000 1500
deviance 136.438 2.059 134.500 135.000 135.800 137.200 141.852 1.001 1500
如您所见,参数对应于用于生成数据的参数。此外,如果您与频率解决方案进行比较,您会发现它对应。
编辑:但典型的逻辑(〜二项式)回归会测量一些具有一些上限值N [i]的计数,并且它将允许每个观察的不同N [i]。例如,说明青少年与整个人口的比例(N)。这只需要在模型中为N添加索引:
C[i] ~ dbin(p[i], N[i])
数据生成看起来像:
N = rpois(n = n.site, lambda = 50)
juveniles <- rbinom(n = n.site, size = N, prob = occ.prob)
win.data <- list(X1 = X1, n = length(X1), C = juveniles, N = N)
(编辑结束)
有关人口生态学的更多示例,请参阅books of Marc Kéry(生态学家的WinBUGS简介,尤其是使用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)) # inverse logit
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("tmp_bugs/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(X1 = X1, n = length(X1), C = true.presence, N = 1)
# 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,
working.directory = paste(getwd(), "/tmp_bugs/", sep = ""),
debug = TRUE)
print(out, dig = 3)
# Frequentist approach for comparison
m1 = glm(true.presence ~ X1, family = binomial)
summary(m1)
# normally, you should do it this way, but the above also works:
#m2 = glm(cbind(true.presence, 1 - true.presence) ~ X1, family = binomial)