如何使用" facet_wrap"绘制预测概率的置信区间在R中使用

时间:2017-04-21 20:16:59

标签: r probability confidence-interval data-visualization

我正在使用以下套餐:

library(aod)
library(MASS)
library(ggplot2)

我正在关注以下链接中的示例R代码: http://stats.idre.ucla.edu/r/dae/logit-regression/

以下是我的GLMM的代码

str(data1)
flocation <- factor(data1$location)
fID <- factor(data1$ID)
GLMM1 <- glmmPQL(presence ~ water + location + turbidity + temperature + 
sp.cond, random = ~ 1|fID, family = binomial, data = data1)
summary(GLMM1)

我根据不同的位置和水位进行预测,同时保持温度和涡轮常数

newdata1 <- with(data1, 
                 data.frame(water = water, 
                            temperature = mean(temperature), 
                            turbidity = mean(turbidity),
                            sp.cond = mean(sp.cond),
                            flocation = flocation))
newdata1$water.levelPred <- predict(GLMM1, type = "response") 
newdata1

为了获得置信区间,我使用了以下代码

newdata2 <- cbind(newdata1, predict(GLMM1, newdata = newdata1, type = "link",
                                    se = TRUE))
newdata2 <- within(newdata2, {
  PredictedProb <- plogis(fit)
  LL <- plogis(fit - (1.96 * se.fit))
  UL <- plogis(fit + (1.96 * se.fit))
})

运行置信区间代码后出现以下错误:

  

predict.lme中的错误(object,newdata,level = level,na.action =   na.action):无法在“新数据”

上评估所需级别的群组      

plogis(fit)中的错误:object&#39; fit&#39;找不到

为什么会发生这种情况?

因为我无法通过这一步骤,所以我无法使用下面的代码来预测CI的预测概率:

在ggplot2中绘图

ggplot(newdata2, aes(x = water, y = water.levelProb)) + geom_ribbon(aes(ymin = LL, ymax = UL, fill = flocation), alpha = 0.2) + geom_line(aes(colour = flocation),size = 1)+facet_wrap(~flocation)+xlab("Water Depth (m)")+ylab("Predicted Probability")+theme_bw()

0 个答案:

没有答案