使用R
,我构建了一个模型来查看在 1992 年总统选举中对布什的支持。这是数据的预览:
presvote_intent presvote age_discrete female educ1 white ideo region state prevote
1 2 2 2 1 2 0 5 2 34 1
2 2 2 4 1 3 1 5 3 44 1
3 NA NA 2 1 2 1 5 3 43 NA
4 1 1 4 1 2 0 1 2 24 0
5 2 2 1 0 4 1 1 3 44 1
6 1 1 4 1 2 0 1 2 34 0
statevote bush
1 0.3636364 1
2 0.3684211 1
3 0.3783784 NA
4 0.4680851 0
5 0.3684211 1
6 0.3636364 0
这是模型:
m2 <- glmer(bush ~ age_discrete + female + educ1 + white + ideo + region + statevote + (1 | state), data=nes, family=binomial("logit"))
鉴于与我的模型相关的线性预测变量,我想创建投票给布什的概率图。最终结果应如下所示:
该图来自 Gelman 和 Hill 的Data Analysis Using Regression and Multilevel/Hierarchical Models。Gelman 和 Hill 做R
/BUGS
来制作这个数字。他们本质上是从模型中的所有相关自变量创建一个线性预测器:
这是他们用来执行此操作的代码:
# create linear predictors
attach.bugs (M2.bugs)
linpred <- rep (NA, n)
for (i in 1:n){
linpred[i] <- mean (b.0 + b.female*female[i] + b.black*black[i] +
b.female.black*female[i]*black[i] + a.age[,age[i]] + a.edu[,edu[i]] +
a.age.edu[,age[i],edu[i]])
}
# plot the 8 states
par (mfrow=c(2,4))
y.jitter <- y + ifelse (y==0, runif (n, 0, .1), runif (n, -.1, 0))
state.name.all <- c(state.name[1:8], "District of Columbia", state.name[9:50])
for (j in c(2,3,4,8,6,7,5,9)) {
plot (0, 0, xlim=range(linpred), ylim=c(0,1), yaxs="i", pch=20,
xlab="linear predictor", ylab="Pr (support Bush)",
main=state.name.all[j], type="n")
for (s in 1:20){
curve (invlogit (a.state[s,j] + x), lwd=.5, add=TRUE, col="gray20")}
curve (invlogit (median (a.state[,j]) + x), lwd=2, add=TRUE)
if (sum(state==j)>0) points (linpred[state==j], y.jitter[state==j])
}
总之,我想为我上面指定的模型做这个。我想这样做R
(即,没有BUGS
)。任何帮助将不胜感激。