簡體   English   中英

在模型擬合摘要中提取虛擬變量的根

[英]Extract root of dummy variable in model fit summary

在以下示例中, gender被編碼為與類別對應的虛擬變量。

fit <- lm(mass ~ height + gender, data=dplyr::starwars)
summary(fit)

# Call:
#   lm(formula = mass ~ height + gender, data = dplyr::starwars)
# 
# Residuals:
#   Min      1Q  Median      3Q     Max 
# -41.908  -6.536  -1.585   1.302  55.481 
# 
# Coefficients:
#   Estimate Std. Error t value Pr(>|t|)    
# (Intercept)          -46.69901   12.67896  -3.683 0.000557 ***
#   height                 0.59177    0.06784   8.723  1.1e-11 ***
#   genderhermaphrodite 1301.13951   17.37871  74.870  < 2e-16 ***
#   gendermale            22.39565    5.82763   3.843 0.000338 ***
#   gendernone            68.34530   17.49287   3.907 0.000276 ***
#   ---
#   Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 16.57 on 51 degrees of freedom
# (31 observations deleted due to missingness)
# Multiple R-squared:  0.9915,  Adjusted R-squared:  0.9909 
# F-statistic:  1496 on 4 and 51 DF,  p-value: < 2.2e-16

有沒有辦法提取虛擬變量名稱的根? 例如,對於gendernonegendermalegenderhermaphrodite ,根將是gender ,對應於dplyr::starwars數據中的原始列名。

從公式中獲取變量名稱並檢查哪一個與輸入匹配:

input <- c("gendermale", "height")

v <- all.vars(formula(fit))
v[sapply(input, function(x) which(pmatch(v, x) == 1))]
## [1] "gender" "height"

暫無
暫無

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

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