lm() function; extract the statiscal significance of the intercept; store in variable

时间:2015-07-28 22:25:18

标签: r lm

In R, If I write:

reg1 <- lm(y ~ x, data = ds)

the regression model information is stored in reg1 which has a structure of a list.

I can write:

value.of.intercept <- reg1$coefficients[1] 

to save the value of the intercept to the variable named "value.of.intercept"

What do I write to save the statistical significance value of the intercept to the variable "p.value.of.intercept"?

I can see the value, in the summary. If I write:

summary(reg1)

the statistical significance value of the intercept is immediately under this text: Pr(>|t|)

2 个答案:

答案 0 :(得分:7)

You can subset the summary object. You can use

coef(summary(reg1))

to get the annotated coefficients and you can get the p-value for the intercept with

coef(summary(reg1))[1, 4]

答案 1 :(得分:5)

如果您使用扫帚包中的整洁功能,您将获得数据框中的所有系数。

library(broom)

lm_model <- lm(mpg ~ ., data = mtcars)
lm_model_coefficients <- tidy(lm_model)
lm_model_coefficients

      term    estimate   std.error  statistic    p.value
1  (Intercept) 12.30337416 18.71788443  0.6573058 0.51812440
2          cyl -0.11144048  1.04502336 -0.1066392 0.91608738
3         disp  0.01333524  0.01785750  0.7467585 0.46348865
4           hp -0.02148212  0.02176858 -0.9868407 0.33495531
5         drat  0.78711097  1.63537307  0.4813036 0.63527790
6           wt -3.71530393  1.89441430 -1.9611887 0.06325215
7         qsec  0.82104075  0.73084480  1.1234133 0.27394127
8           vs  0.31776281  2.10450861  0.1509915 0.88142347
9           am  2.52022689  2.05665055  1.2254035 0.23398971
10        gear  0.65541302  1.49325996  0.4389142 0.66520643
11        carb -0.19941925  0.82875250 -0.2406258 0.81217871