R:在Ggplot2中绘制Logistic回归图[重复]

2hh7jdfx  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(250)
    • 此问题在此处已有答案**:

how to Plot the results of a logistic regression model using base R and ggplot(1个答案)
13小时前关门了。
我正在使用R编程语言。
我有以下数据框:

library(ggplot2)

library(tidyverse)
set.seed(123)

my_data1 = data.frame(Weight =  rnorm(500,100,100), asthma = sample(c(0,1), prob = c(0.7,0.3), replace=TRUE, size= 500))
my_data2 = data.frame(Weight = rnorm(500, 200, 50),  asthma = sample(c(0,1), prob = c(0.3,0.7), replace=TRUE, size= 500))
my_data = rbind(my_data1, my_data2)

我将逻辑回归模型拟合到该数据:

# fit the logistic regression model
model <- glm(asthma ~ Weight, data = my_data, family = binomial())

然后,我创建了一个数据框架,其中包含不同权重值的预测值和相应的置信区间:

# create a data frame with the predicted values and confidence intervals
preds <- data.frame(Weight = seq(min(my_data$Weight), max(my_data$Weight), length.out = 100))
preds$pred <- predict(model, preds, type = "response")
preds$upper <- predict(model, preds, type = "response", se.fit = TRUE)$fit + 1.96 * predict(model, preds, type = "response", se.fit = TRUE)$se.fit
preds$lower <- predict(model, preds, type = "response", se.fit = TRUE)$fit - 1.96 * predict(model, preds, type = "response", se.fit = TRUE)$se.fit

最后,我试着把一切都画出来:

# plot the data and the model
ggplot(my_data, aes(x = Weight, y = asthma)) +
  geom_point(alpha = 0.5) +
  geom_line(data = preds, aes(x = Weight, y = pred), color = "red") +
  geom_ribbon(data = preds, aes(x = Weight, ymin = lower, ymax = upper), alpha = 0.2) +
  ggtitle("Logistic Regression Model with Confidence Bands")

但这给了我以下错误:

Error in `geom_ribbon()`:
! Problem while computing aesthetics.
i Error occurred in the 3rd layer.
Caused by error in `FUN()`:
! object 'asthma' not found
Run `rlang::last_error()` to see where the error occurred.

"有人知道我做错了什么吗"
我可以清楚地看到数据中有一个名为"asthma"的变量--而且,下面这行代码运行时没有出现错误:

ggplot(my_data, aes(x = Weight, y = asthma)) +
    geom_point(alpha = 0.5) +
    geom_line(data = preds, aes(x = Weight, y = pred), color = "red")

话虽如此,为什么我的代码没有运行?
谢谢!

qhhrdooz

qhhrdooz1#

您可以在使用preds的几何中使用inherit.aes=FALSE

library(tidyverse)
set.seed(123)

my_data1 = data.frame(Weight =  rnorm(500,100,100), asthma = sample(c(0,1), prob = c(0.7,0.3), replace=TRUE, size= 500))
my_data2 = data.frame(Weight = rnorm(500, 200, 50),  asthma = sample(c(0,1), prob = c(0.3,0.7), replace=TRUE, size= 500))
my_data = rbind(my_data1, my_data2)

model <- glm(asthma ~ Weight, data = my_data, family = binomial())
preds <- data.frame(Weight = seq(min(my_data$Weight), max(my_data$Weight), length.out = 100))
preds$pred <- predict(model, preds, type = "response")
preds$upper <- predict(model, preds, type = "response", se.fit = TRUE)$fit + 1.96 * predict(model, preds, type = "response", se.fit = TRUE)$se.fit
preds$lower <- predict(model, preds, type = "response", se.fit = TRUE)$fit - 1.96 * predict(model, preds, type = "response", se.fit = TRUE)$se.fit

ggplot(my_data, aes(x = Weight, y = asthma)) +
  geom_point(alpha = 0.5) +
  geom_line(data = preds, aes(x = Weight, y = pred), color = "red", inherit.aes = FALSE) +
  geom_ribbon(data = preds, aes(x = Weight, ymin = lower, ymax = upper), alpha = 0.2, inherit.aes = FALSE) +
  ggtitle("Logistic Regression Model with Confidence Bands")

reprex package(v2.0.1)于2023年1月26日创建

相关问题