首页 > 解决方案 > 在 R 中使用 ggplot2 绘制包括连续和离散预测变量的多元线性回归

问题描述

我正在为“R”在线课程做一个项目。原始数据集是“copulaData”包中的“NELS88”。根据任务并经过一些验证,我来到数据框“urpub2”和模型“Model10”,包括连续和离散的预测变量(因子)以及一个交互。现在我正在尝试制作类似这样的一系列情节,但仅使用三个单独的模型就可以做到这一点,我猜这是错误的。在我构建的一个模型(Model10)中有什么方法可以做到这一点?我使用的情节和代码如下。

library(copulaData)
library(ggplot2)
library(cowplot)
library(car)
library(dplyr)
data('NELS88')
urpub <- NELS88[NELS88$Urban == 1 & NELS88$Public == 1 & NELS88$Size > 100,]
urpub$Size_factor <- factor(urpub$Size)
urpub2 <- urpub[-19,]
Model10 <- lm(Science ~ Math + Reading + SES + Minority + Female + SES:Female, data = urpub2)

“科学~数学+阅读”的情节

theme_set(theme_bw())
# Science ~ Math + Reading
Incorrect_M1 <- lm(Science ~ Math + Reading, data = urpub2)
new_data1 <- expand.grid(Math = seq(min(urpub2$Math), max(urpub2$Math), length.out = 100), Reading = seq(min(urpub2$Reading), max(urpub2$Reading), length.out = 100))
new_data1$predicted <- predict(Incorrect_M1, newdata = new_data1)
inc_Pl1 <- ggplot(new_data1, aes(x = Math, y = predicted, group = Reading)) + geom_line(aes(color = Reading)) + geom_point(data = urpub2, aes(x = Math, y = Science)) + scale_color_continuous(high = 'red', low = 'yellow') + ylab('Science')

“科学~少数派”的情节

# Science ~ Minority
Incorrect_M2 <- lm(Science ~ Minority, data = urpub2)
new_data2 <- data.frame(Minority = factor(levels(urpub2$Minority), levels = levels(urpub2$Minority)))
new_data2$fit <- predict(Incorrect_M2, newdata = new_data2, se.fit = TRUE)$fit
new_data2$se <- predict(Incorrect_M2, newdata = new_data2, se.fit = TRUE)$se.fit
t_crit2 <- qt(0.975, df = nrow(urpub2) - length(coef(Incorrect_M2)))
new_data2$lwr <- new_data2$fit - t_crit2 * new_data2$se
new_data2$upr <- new_data2$fit + t_crit2 * new_data2$se
inc_Pl2 <- ggplot(new_data2, aes(x = Minority, y = fit)) + geom_bar(stat = 'identity', aes(fill = Minority)) + geom_errorbar(aes(ymin = lwr, ymax = upr), width = 0.2) + scale_x_discrete(labels = c('No', 'Yes')) + guides(fill = 'none') + labs(x = 'Minority', y = 'Science')

“科学〜SES +女性+ SES:女性”的情节

# Science ~ SES + Female + SES:Female
Incorrect_M3 <- lm(Science ~ SES + Female + SES:Female, data = urpub2)
new_data3 <- urpub2 %>% group_by(Female) %>% do(data.frame(SES = seq(min(.$SES), max(.$SES), length.out = 100)))
Predictions3 <- predict(Incorrect_M3, newdata = new_data3, se.fit = TRUE)
new_data3$fit <- Predictions3$fit
new_data3$se <- Predictions3$se.fit
t_crit3 <- qt(0.975, df = nrow(urpub2) - length(coef(Incorrect_M3)))
new_data3$lwr <- new_data3$fit - t_crit3 * new_data3$se
new_data3$upr <- new_data3$fit + t_crit3 * new_data3$se
inc_Pl3 <- ggplot(new_data3, aes(x = SES, y = fit)) + geom_ribbon(alpha = 0.2, aes(ymin = lwr, ymax = upr, group = Female))  + geom_line(aes(colour = Female), size = 1) + geom_point(data = urpub2, aes(x = SES, y = Science, colour = Female)) + scale_colour_discrete(name = 'Sex', labels = c('Male', 'Female')) + labs(y = 'Science')

标签: rggplot2plotlinear-regressionfactors

解决方案


推荐阅读