如何绘制具有连续变量和分类变量的二项式GLM的预测 [英] How to plot predictions of binomial GLM that has both continuous and categorical variables

查看:216
本文介绍了如何绘制具有连续变量和分类变量的二项式GLM的预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有一个二项式GLM,其中有几个连续且明确的预测变量.

I have a binomial GLM in R, with several predictors that are both continuous and categorical.

响应变量是存在",它是二进制(0/1). 长度是一个连续变量,而其他所有变量都是分类变量.

The response variable is "Presence", which is binary (0/1). Length is a continuous variable, while all others are categorical.

我正在尝试为最终模型中的每个变量绘制预测,特别是对于长度",但是我遇到了困难.

I am trying to plot predictions for each of the variables in the final model, particularly for "length", but I'm having difficulties.

我的数据如下:

MyData<-structure(list(site = structure(c(3L, 1L, 3L, 2L, 1L, 4L, 3L, 
4L, 1L, 2L, 4L, 5L, 5L, 1L, 4L, 3L, 2L, 4L, 1L, 4L, 5L, 1L, 5L, 
4L, 3L, 1L, 3L, 5L, 5L, 4L, 4L, 3L, 1L, 5L, 1L, 3L, 1L, 4L, 4L, 
3L, 4L, 4L, 2L, 3L, 1L, 4L, 2L, 1L, 1L, 4L, 4L, 4L, 1L, 3L, 3L, 
2L, 1L, 4L, 2L, 5L, 5L, 3L, 3L, 2L, 5L, 2L, 4L, 5L, 2L, 4L, 4L, 
2L, 5L, 2L, 3L, 5L, 4L, 4L, 5L, 1L, 1L, 3L, 2L, 4L, 3L, 1L, 4L, 
3L, 1L, 4L, 3L, 3L, 4L, 5L, 1L, 3L, 2L, 3L, 2L, 3L, 2L, 1L, 1L, 
5L, 5L, 1L, 5L, 2L, 3L, 4L, 4L, 3L, 2L, 3L, 3L, 5L, 3L, 3L, 3L, 
5L, 1L, 5L, 2L, 3L, 4L, 5L, 5L, 1L, 4L, 2L, 5L, 3L, 2L, 5L, 4L, 
3L, 3L, 3L, 1L, 1L, 4L, 1L, 2L, 4L, 5L, 1L, 1L, 2L, 2L, 5L, 3L, 
4L, 4L, 1L, 5L, 2L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, 4L, 4L, 3L, 4L, 2L, 3L, 4L, 4L, 2L, 3L, 2L, 
4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 2L, 4L, 3L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 3L, 
3L, 4L, 2L, 5L, 3L, 4L, 2L, 4L, 4L, 4L, 3L, 3L, 3L, 4L, 4L, 2L, 
4L, 3L, 4L, 4L, 3L, 4L, 4L, 4L, 3L, 2L, 4L), .Label = c("East", 
"Flat", "North", "South", "West"), class = "factor"), length = c(260L, 
60L, 60L, 40L, 240L, 80L, 30L, 100L, 100L, 200L, 70L, 50L, 60L, 
35L, 120L, 60L, 500L, 40L, 20L, 70L, 250L, 80L, 50L, 130L, 350L, 
170L, 50L, 60L, 90L, 50L, 40L, 110L, 60L, 70L, 70L, 500L, 140L, 
50L, 50L, 360L, 50L, 150L, 60L, 270L, 280L, 130L, 130L, 50L, 
60L, 30L, 70L, 70L, 60L, 400L, 20L, 30L, 70L, 160L, 340L, 100L, 
210L, 60L, 70L, 130L, 50L, 40L, 50L, 80L, 390L, 40L, 110L, 130L, 
40L, 230L, 120L, 70L, 80L, 80L, 90L, 70L, 150L, 120L, 50L, 100L, 
120L, 10L, 40L, 80L, 180L, 160L, 200L, 40L, 70L, 90L, 50L, 40L, 
80L, 80L, 70L, 480L, 90L, 60L, 100L, 140L, 190L, 20L, 70L, 360L, 
70L, 130L, 60L, 50L, 320L, 210L, 130L, 180L, 90L, 20L, 300L, 
90L, 50L, 130L, 70L, 70L, 40L, 40L, 50L, 40L, 100L, 20L, 70L, 
100L, 340L, 70L, 110L, 40L, 230L, 200L, 80L, 35L, 110L, 200L, 
50L, 110L, 100L, 50L, 150L, 110L, 50L, 50L, 40L, 70L, 80L, 60L, 
100L, 90L, 40L, 300L, 140L, 180L, 140L, 40L, 190L, 100L, 170L, 
40L, 120L, 15L, 70L, 340L, 40L, 40L, 70L, 60L, 130L, 140L, 170L, 
120L, 90L, 130L, 210L, 50L, 180L, 120L, 100L, 50L, 90L, 70L, 
360L, 80L, 30L, 170L, 70L, 300L, 40L, 130L, 120L, 90L, 40L, 40L, 
140L, 80L, 400L, 70L, 80L, 60L, 420L, 320L, 200L, 40L, 50L, 70L, 
50L, 80L, 50L, 110L, 100L, 120L, 170L, 20L, 110L, 20L, 20L, 30L, 
30L, 90L, 150L, 80L, 40L, 90L, 300L, 30L, 70L, 50L, 90L, 200L
), sun = structure(c(1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 
3L, 3L, 3L, 3L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 2L, 3L, 1L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 1L, 3L, 
3L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 
3L, 2L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 3L, 3L, 1L, 2L, 1L, 
1L, 3L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 1L, 3L, 3L, 3L, 1L, 3L, 
1L, 3L, 3L, 2L, 1L, 3L, 3L, 1L, 1L, 3L, 1L, 3L, 3L, 1L, 1L, 1L, 
2L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 3L, 2L, 1L, 3L, 1L, 1L, 3L, 3L, 
1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 1L, 1L, 1L, 
3L, 3L, 3L, 1L, 3L, 3L, 1L, 3L, 3L, 1L, 3L, 3L, 1L, 3L, 3L, 3L, 
3L, 1L, 3L, 1L, 3L, 1L, 1L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 1L, 1L, 
1L, 1L, 3L, 3L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 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0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
    0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
    1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L
    )), .Names = c("site", "species", "aspect", "length", "sun", 
"leaf", "Presence"), row.names = c(NA, 236L), class = "data.frame")

(请注意,这是一个简化的数据集,我已经删除了在模型选择期间删除的变量)

(note that this is a reduced dataset, and I have already removed variables that were dropped during model selection)

最佳模型是:

model <- glm(Presence ~ site + species + aspect + length + sun 
                + leaf, data=MyData, family=binomial)

我尝试了以下操作,但是它也想要其他变量,所以出现错误:

I tried the following, but it wants the other variables too, so I get an error:

plot(MyData$length, MyData$Presence)
mydat1 <- data.frame(length = seq(from = 10, to = 500, by = 1)
pred1 <- predict(model, newdata = mydat1, type = "response")
lines(MyData$length, pred1)

所以我尝试指定所有变量,但随后只在存在数据点上划了一条水平线(这意味着我需要指定我认为的因子变量的所有可能组合):

So I tried specifying all variables, but then it only puts a horizontal line through the presence data points (and that means I need to specify all possible combinations of factor variables I suppose):

plot(MyData$length, MyData$Presence)
mydat2 <- data.frame(length = seq(from = 10, to = 500, by = 1), 
                     site = "R1a", 
                     species = "Monogyna",
                     aspect = "Flat", 
                     sun = "Sun", 
                     leaf = "Scarce")
pred2 <- predict(model, newdata = mydat2, type = "response")
lines(MyData$length, pred2)

最后,我尝试了以下代码:

Finally, I tried the following code:

pred <- predict(model, type = "response")
par(mfrow=c(2,2))
for(i in names(MyData)){
   plot(MyData[,i],pred,xlab=i, ylab="Probability")
}

我对最后一个感到困惑,因为我无法获得曲线,再加上输出为我提供了甚至不是最佳模型中变量的预测值.

I am confused by this last one, as I am not able to obtain the curve, plus the output gives me predicted values for variables that are not even in the optimal model.

我认为,在这种模型下,我应该期望的是正弦曲线.但这不是我要得到的.

What I should expect under this model, is a sinusoidal curve, I suppose. But that's not what I'm getting.

我如何得出有意义的预测图?

How can I produce a meaningful plot of predictions?

任何帮助将不胜感激.

推荐答案

对于单个预测变量,我将使用effects包获得一些更简单的结果.方法如下:

I would use the effects package for some easier results for a single predictor. Here is how:

library(effects)
fit <- as.data.frame(effect('length', model, xlevels = 100))

绘图很容易(尽管要注意绘图):

Plotting is easy (although note the overplotting):

plot(MyData$length, MyData$Presence)
lines(fit$length, fit$fit)

或者我们可以使用ggplot2:

library(ggplot2)
ggplot() +
  geom_count(aes(length, Presence), MyData) +
  geom_line(aes(length, fit), fit, size = 1, col = 'red') +
  geom_ribbon(aes(length, ymin = lower, ymax = upper), fit, alpha = 0.15) +
  scale_size_area()

我们可以看到长度的影响不是很令人印象深刻.

We can see that the effect of length is not very impressive.

这篇关于如何绘制具有连续变量和分类变量的二项式GLM的预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆