为预测概率图构建由多个预测变量组成的单个线性预测变量

问题描述

使用 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"))

我想绘制在给定线性预测因子的情况下为布什投票的概率图 与我的模型相关联。最终结果应如下所示:

enter image description here

此图来自 Gelman 和 Hill 的使用回归和多级/分层模型的数据分析。 Gelman 和 Hill 在 R/BUGS 中做了这个数字。他们本质上是根据模型中的所有相关自变量创建一个线性预测变量:

enter image description here

这是他们用来执行此操作的代码

# 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,.1),-.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,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)。任何帮助将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)