簡體   English   中英

按名稱選擇回歸系數

[英]Select regression coefs by name

運行回歸后,如何選擇變量名稱和相應的參數估計?

例如,運行以下回歸后,我得到:

set.seed(1)
n=1000
x=rnorm(n,0,1)
y=.6*x+rnorm(n,0,sqrt(1-.6)^2)
(reg1=summary(lm(y~x)))

Call:
lm(formula = y ~ x)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.2994 -0.2688 -0.0055  0.3022  1.4577 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) -0.006475   0.013162  -0.492    0.623    
x            0.602573   0.012723  47.359   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.4162 on 998 degrees of freedom
Multiple R-squared:  0.6921,    Adjusted R-squared:  0.6918 
F-statistic:  2243 on 1 and 998 DF,  p-value: < 2.2e-16

我希望能夠通過變量名稱選擇系數(例如(Intercept) -0.006475

我嘗試了以下方法,但沒有任何效果...

attr(reg1$coefficients,"terms")
names(reg1$coefficients) 

注意:這可以使用reg1$coefficients[1,1]但我希望能夠通過名稱而不是行/列來調用它。

軟件包掃帚很好地整理了很多回歸模型。

require(broom)
set.seed(1)
n=1000
x=rnorm(n,0,1)
y=.6*x+rnorm(n,0,sqrt(1-.6)^2)
model = lm(y~x)
tt <- tidy(model, conf.int=TRUE)

subset(tt,term=="x")
##   term estimate  std.error statistic       p.value  conf.low conf.high
## 2    x 0.602573 0.01272349  47.35908 1.687125e-257 0.5776051 0.6275409

with(tt,tt[term=="(Intercept)","estimate"])
## [1] -0.006474794

因此,您的代碼不會像您所擁有的那樣運行。 我改變了一點:

set.seed(1)
n=1000
x=rnorm(n,0,1)
y=.6*x+rnorm(n,0,sqrt(1-.6)^2)
model = lm(y~x)

現在,我可以調用coef(model)["x"]coef(model)["(Intercept)"]獲取值。

> coef(model)["x"]
       x 
0.602573 
> coef(model)["(Intercept)"]
 (Intercept) 
-0.006474794 

暫無
暫無

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

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