Reputation: 321
I would like to access some elements of an Anova summary in R. I've been trying things like in this question Access or parse elements in summary() in R.
When I convert the summary to a string it shows something like this:
str(summ)
List of 1
$ :Classes 'anova' and 'data.frame': 2 obs. of 5 variables:
..$ Df : num [1:2] 3 60
..$ Sum Sq : num [1:2] 0.457 2.647
..$ Mean Sq: num [1:2] 0.1523 0.0441
..$ F value: num [1:2] 3.45 NA
..$ Pr(>F) : num [1:2] 0.022 NA
- attr(*, "class")= chr [1:2] "summary.aov" "listof"
How can I access the F value?
I've been trying things like summ[c('F value')]
and I still can't get it to work.
Any help would be greatly appreciated!
Upvotes: 5
Views: 9572
Reputation: 16121
As an addition to the answer above I'd recommend to start using the broom
package when you want to access/use various elements of a model object.
First, by using the str
command you don't convert the summary into a string, but you just see the structure of your summary, which is a list. So, str
means "structure".
The broom
package enables you to save the info of your model object as a data frame, which is easier to manipulate. Check my simple example:
library(broom)
fit <- aov(mpg ~ vs, data = mtcars)
# check the summary of the ANOVA (not possible to access info/elements)
fit2 = summary(fit)
fit2
# Df Sum Sq Mean Sq F value Pr(>F)
# vs 1 496.5 496.5 23.66 3.42e-05 ***
# Residuals 30 629.5 21.0
# create a data frame of the ANOVA
fit3 = tidy(fit)
fit3
# term df sumsq meansq statistic p.value
# 1 vs 1 496.5279 496.52790 23.66224 3.415937e-05
# 2 Residuals 30 629.5193 20.98398 NA NA
# get F value (or any other values)
fit3$statistic[1]
#[1] 23.66224
I think for the specific example you provided you don't really need to use the broom
method, but if it happens to deal with more complicated model objects it will be really useful to try it.
Upvotes: 3
Reputation: 596
You have the anova object inside a list (first line of str output is List of 1). So you need to get the "F value" of this single element, like:
summm[[1]][["F value"]]
Upvotes: 6