Robert Kubrick
Robert Kubrick

Reputation: 8753

R: extracting lm() attributes

I want to extract the fstatistic value from summary(lm()). So far the only way I've found is

summary(lm(this_vector ~ that_vector))["fstatistic"][[1]][1]

Is there a less verbose way to get that cell value? The question is a bit pedantic but I thought the answer might provide some interesting info on how to use R lists.

Upvotes: 2

Views: 1639

Answers (2)

Helix123
Helix123

Reputation: 3697

The package broom contains functions to convenietly extract estimates and model fit statistics from various models (among them lm). For extracting the F statistic, use broom's glance() function. See the example mentioned here https://github.com/dgrtwo/broom.

In your case, that would be

glance(lm(this_vector ~ that_vector))$statistic

Upvotes: 0

Aaron - mostly inactive
Aaron - mostly inactive

Reputation: 37824

Try either of these:

summary(lm(this_vector ~ that_vector))$fstatistic[1]
summary(lm(this_vector ~ that_vector))[["fstatistic"]][1]

["fstatistic"] returns a list with elements that have names that match what's inside the single brackets, so you need the [[1]] to get the first element. Double brackets return the element itself, as does using the $ notation.

Upvotes: 4

Related Questions