tinafmills
tinafmills

Reputation: 39

filter lm coefficient summary model

How do you filter the coefficients summary?

model = lm(x ~ a + b + c + d + e, data=ds)
ss <- coef(summary(model3))
df.coef <- as.data.frame( coef(summary(model)) )
filter(df.coef, Pr(>|t|) < 0.05)  

Error: unexpected '>' in "filter(df.coef, Pr(>" `

Upvotes: 1

Views: 597

Answers (2)

Allan Cameron
Allan Cameron

Reputation: 173813

To use filter you need to convert the table to a data frame, and wrap the column name in back-ticks:

mod <- lm(y ~ x, data = data.frame(x = 1:10, y = 1:10 + rnorm(10)))

summary(mod)$coefficients %>% 
as.data.frame() %>% 
filter(`Pr(>|t|)` < 0.05)
#>    Estimate Std. Error t value     Pr(>|t|)
#> 1 0.9862634 0.07084972 13.9205 6.866089e-07

Upvotes: 1

jay.sf
jay.sf

Reputation: 72828

Your ss is a matrix, which you may subset like so:

model <- lm(mpg ~ hp + am + gear, mtcars)
ss <- coef(summary(model))
ss[ss[,"Pr(>|t|)"] < .05,]
#                Estimate  Std. Error   t value     Pr(>|t|)
# (Intercept) 23.18636779 3.898968754  5.946795 2.111900e-06
# hp          -0.05973668 0.007925368 -7.537401 3.283912e-08
# am           3.95611932 1.777326184  2.225883 3.424800e-02

Accordingly after coercing to a dataframe,

df.coef <- as.data.frame(ss)

you would do:

df.coef[df.coef$`Pr(>|t|)` < .05,]
#                Estimate  Std. Error   t value     Pr(>|t|)
# (Intercept) 23.18636779 3.898968754  5.946795 2.111900e-06
# hp          -0.05973668 0.007925368 -7.537401 3.283912e-08
# am           3.95611932 1.777326184  2.225883 3.424800e-02

Note: gear is filtered out of the result, since it's p-value is above 0.05.

Upvotes: 3

Related Questions