我正在运行所有预测变量的线性回归(我有384个预测变量),但只从汇总中得到373个系数 . 我想知道为什么R不返回所有系数,我怎么能得到所有384个系数?
full_lm <- lm(Y ~ ., data=dat[,2:385]) #384 predictors coef_lm <- as.matrix(summary(full_lm)$coefficients[,4]) #only gives me 373
首先, summary(full_lm)$coefficients[,4] 返回 p-values 而不是系数 . 现在,为了真正回答你的问题,我相信你的一些变量会从估算中退出,因为它们与其他变量完全共线 . 如果运行 summary(full_lm) ,您将看到这些变量的估计在所有字段中返回 NA . 因此,它们不包含在 summary(full_lm)$coefficients 中 . 举个例子:
summary(full_lm)$coefficients[,4]
p-values
summary(full_lm)
NA
summary(full_lm)$coefficients
x<- rnorm(1000) x1<- 2*x x2<- runif(1000) eps<- rnorm(1000) y<- 5+3*x + x1 + x2 + eps full_lm <- lm(y ~ x + x1 + x2) summary(full_lm) #Call: #lm(formula = y ~ x + x1 + x2) # #Residuals: # Min 1Q Median 3Q Max #-2.90396 -0.67761 -0.02374 0.71906 2.88259 # #Coefficients: (1 not defined because of singularities) # Estimate Std. Error t value Pr(>|t|) #(Intercept) 4.96254 0.06379 77.79 <2e-16 *** #x 5.04771 0.03497 144.33 <2e-16 *** #x1 NA NA NA NA #x2 1.05833 0.11259 9.40 <2e-16 *** #--- #Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # #Residual standard error: 1.024 on 997 degrees of freedom #Multiple R-squared: 0.9546, Adjusted R-squared: 0.9545 #F-statistic: 1.048e+04 on 2 and 997 DF, p-value: < 2.2e-16 coef_lm <- as.matrix(summary(full_lm)$coefficients[,1]) coef_lm #(Intercept) 4.962538 #x 5.047709 #x2 1.058327
例如,如果数据中的某些列是其他列的线性组合,则系数将为 NA ,如果您按照自己的方式编制索引,则会自动省略 .
a <- rnorm(100) b <- rnorm(100) c <- rnorm(100) d <- b + 2*c e <- lm(a ~ b + c + d)
给
Call: lm(formula = a ~ b + c + d) Coefficients: (Intercept) b c d 0.088463 -0.008097 -0.077994 NA
但索引......
> as.matrix(summary(e)$coefficients)[, 4] (Intercept) b c 0.3651726 0.9435427 0.3562072
2 回答
首先,
summary(full_lm)$coefficients[,4]
返回p-values
而不是系数 . 现在,为了真正回答你的问题,我相信你的一些变量会从估算中退出,因为它们与其他变量完全共线 . 如果运行summary(full_lm)
,您将看到这些变量的估计在所有字段中返回NA
. 因此,它们不包含在summary(full_lm)$coefficients
中 . 举个例子:例如,如果数据中的某些列是其他列的线性组合,则系数将为
NA
,如果您按照自己的方式编制索引,则会自动省略 .给
但索引......