绘制R中多变量逻辑回归模型的结果

Luk*_*uke 5 plot regression r

我想绘制多变量逻辑回归分析(GLM)的结果,用于调整的特定独立变量(即独立于模型中包含的混杂因素)与结果(二元)的关系.

我已经看过使用predict后面的命令推荐以下方法的帖子curve,这是一个例子;

x     <- data.frame(binary.outcome, cont.exposure)
model <- glm(binary.outcome ~ cont.exposure, family=binomial, data=x)
plot(cont.exposure, binary.outcome, xlab="Temperature",ylab="Probability of Response") 
curve(predict(model, data.frame(cont.exposure=x), type="resp"), add=TRUE, col="red")
Run Code Online (Sandbox Code Playgroud)

然而,这似乎不适用于多元回归模型.当我添加'age'(任意 - 可以是任何相同长度的变量)作为混杂变量时,我得到以下错误;

> x     <- data.frame(binary.outcome, cont.exposure, age)
> model <- glm(binary.outcome ~ cont.exposure + age, family=binomial, data=x)
> plot(cont.exposure, binary.outcome, xlab="Temperature",ylab="Probability of Response") 
> curve(predict(model, data.frame(cont.exposure=x), type="resp"), add=TRUE, col="red")
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : 
  variable lengths differ (found for 'age')
In addition: Warning message:
  'newdata' had 101 rows but variable(s) found have 698 rows 
Run Code Online (Sandbox Code Playgroud)

上面的模型是我想要运行的模型的简化版本,但原理是相同的; 我想绘制二元结果变量和连续曝光之间的关系,而不受混杂因素的影响..

获得上述解决方法或者查看我感兴趣的关系的替代方法会很棒.非常感谢.

Thi*_*rry 8

set.seed(12345)
dataset <- expand.grid(Temp = rnorm(30), Age = runif(10))
dataset$Truth <- with(dataset, plogis(2 * Temp - 3 * Age))
dataset$Sample <- rbinom(nrow(dataset), size = 1, prob = dataset$Truth)
model <- glm(Sample ~ Temp + Age, data = dataset, family = binomial)
newdata <- expand.grid(
  Temp = pretty(dataset$Temp, 20), 
  Age = pretty(dataset$Age, 5))
newdata$Sample <- predict(model, newdata = newdata, type = "response")
library(ggplot2)
ggplot(newdata, aes(x = Temp, y = Sample)) + geom_line() + facet_wrap(~Age)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

ggplot(newdata, aes(x = Temp, y = Sample, colour = Age, group = Age)) + 
  geom_line()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述