簡體   English   中英

R 如何使用回歸處理 NA 值與刪除值

[英]how does R handle NA values vs deleted values with regressions

假設我有一張表,我刪除了所有不適用的值,然后進行了回歸。 如果我在同一張表上運行完全相同的回歸,但這次不是刪除不適用的值,而是將它們變成 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))

比較這兩個摘要,您可以看到它們是相同的,除了第一個 model 的一個例外,有一條警告消息指出 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM