gpt4 book ai didi

r - R 如何通过回归处理 NA 值与删除的值

转载 作者:行者123 更新时间:2023-12-04 08:08:36 25 4
gpt4 key购买 nike

假设我有一张表,我删除了所有不适用的值,然后进行了回归。如果我在同一张表上运行完全相同的回归,但这次不是删除不适用的值,而是将它们转换为 NA 值,回归是否仍会给出相同的系数?

最佳答案

在进行分析之前,回归将忽略任何 NA 值(即删除任何包含任何预测变量或结果变量中缺失 NA 的行)。您可以通过比较两个模型的自由度和其他统计数据来检查这一点。
这是一个玩具示例:

head(mtcars)

# check the data set size (all non-missings)
dim(mtcars) # has 32 rows

# Introduce some missings
set.seed(5)
mtcars[sample(1:nrow(mtcars), 5), sample(1:ncol(mtcars), 5)] <- NA

head(mtcars)

# Create an alternative where all missings are omitted
mtcars_NA_omit <- na.omit(mtcars)

# Check the data set size again
dim(mtcars_NA_omit) # Now only has 27 rows

# Now compare some simple linear regressions
summary(lm(mpg ~ cyl + hp + am + gear, data = mtcars))
summary(lm(mpg ~ cyl + hp + am + gear, data = mtcars_NA_omit))
比较这两个摘要,您可以看到它们是相同的,唯一的异常(exception)是对于第一个模型,有一条警告消息表明 5 个 csaes 由于缺失而被丢弃,这正是我们在 mtcars_NA_omit 中手动执行的操作。例子。
# First, original model

Call:
lm(formula = mpg ~ cyl + hp + am + gear, data = mtcars)

Residuals:
Min 1Q Median 3Q Max
-5.0835 -1.7594 -0.2023 1.4313 5.6948

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 29.64284 7.02359 4.220 0.000352 ***
cyl -1.04494 0.83565 -1.250 0.224275
hp -0.03913 0.01918 -2.040 0.053525 .
am 4.02895 1.90342 2.117 0.045832 *
gear 0.31413 1.48881 0.211 0.834833
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.947 on 22 degrees of freedom
(5 observations deleted due to missingness)
Multiple R-squared: 0.7998, Adjusted R-squared: 0.7635
F-statistic: 21.98 on 4 and 22 DF, p-value: 2.023e-07

# Second model where we dropped missings manually

Call:
lm(formula = mpg ~ cyl + hp + am + gear, data = mtcars_NA_omit)

Residuals:
Min 1Q Median 3Q Max
-5.0835 -1.7594 -0.2023 1.4313 5.6948

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 29.64284 7.02359 4.220 0.000352 ***
cyl -1.04494 0.83565 -1.250 0.224275
hp -0.03913 0.01918 -2.040 0.053525 .
am 4.02895 1.90342 2.117 0.045832 *
gear 0.31413 1.48881 0.211 0.834833
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.947 on 22 degrees of freedom
Multiple R-squared: 0.7998, Adjusted R-squared: 0.7635
F-statistic: 21.98 on 4 and 22 DF, p-value: 2.023e-07

关于r - R 如何通过回归处理 NA 值与删除的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66094230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com