如何在R中提取check_model生成的残差正态性图表对应数据?
获取
check_model残差正态性图表的数据集方法 方法一:直接用performance包内置函数提取
check_model的残差正态性图依赖于check_normality()函数计算的数据,直接调用该函数即可获取完整的绘图数据集:
# 先运行原模型代码得到model对象 library(tidyverse) library(performance) library(see) model <- lm(mpg ~ wt + am + gear + vs * cyl, data = mtcars) # 提取残差正态性数据 norm_data <- check_normality(model) %>% as_tibble() # 查看数据结构 glimpse(norm_data)
这个数据框包含标准化残差(Residuals列)、理论正态分位数(Theoretical列)等核心字段,完全对应check_model生成的Q-Q图数据。
方法二:手动计算底层数据
如果需要更灵活的控制,也可以手动计算绘图所需的核心变量:
# 提取模型的标准化残差 std_residuals <- rstandard(model) # 计算对应样本量的理论正态分位数 theoretical_quantiles <- qnorm(ppoints(length(std_residuals))) # 整合成数据框 norm_data <- tibble( std_residuals = std_residuals, theoretical_quantiles = theoretical_quantiles )
复现残差正态性图
用上述数据集,结合ggplot2可以复现和check_model风格一致的图表:
ggplot(norm_data, aes(x = theoretical_quantiles, y = std_residuals)) + geom_point(size = 2, alpha = 0.7) + geom_abline(slope = 1, intercept = 0, color = "#E64B35", linetype = "dashed") + labs( x = "理论正态分位数", y = "标准化残差", title = "残差正态性检验" ) + theme_classic()
内容的提问来源于stack exchange,提问作者Laura
相关产品推荐
相关产品推荐

