[R] Extracting elements of model output
Rui Barradas
ruipbarradas at sapo.pt
Mon Oct 14 20:22:57 CEST 2013
Hello,
Inline.
Em 14-10-2013 18:01, David C. Howell escreveu:
> I am having difficulty extracting specific results from the model
> object. The following code shows where I am stuck.
>
> I want to run resamplings of a data set. For each I want to extract a
> particular F for the contrasts. If I run a very simple model
> (e.g. model1 <- aov(time~group) ) I can get the relevant coefficients,
> for example, by using "model1$coefficients". That's fine. But I don't
> want the coefficients of the model, I want the contrasts. So I tried
> model2=summary(model1, split = list(group = list("1&3 vs 2"=1, "1 vs 3"
> = 2)))
> This gives me the printout summary table that I want. i.e.
>
> model2
> Df Sum Sq Mean Sq F value Pr(>F)
> group 2 4090 2045 8.081 0.000682 ***
> group: 1&3 vs 2 1 1483 1484 5.863 0.017988 *
> group: 1 vs 3 1 2606 2606 10.300 0.001987 **
> Residuals 72 18219 253
>
> That's the summary table that I want, but I want to extract, for example
> the F value of 5.863.
>
> Looking at the structure of that object I get
>
> str(model2)
> List of 1
> $ :Classes ‘anova’ and 'data.frame': 4 obs. of 5 variables:
> ..$ Df : Named num [1:4] 2 1 1 72
> .. ..- attr(*, "names")= chr [1:4] "" "1&3 vs 2" "1 vs 3" ""
> ..$ Sum Sq : Named num [1:4] 4090 1483 2606 18219
> .. ..- attr(*, "names")= chr [1:4] "" "1&3 vs 2" "1 vs 3" ""
> ..$ Mean Sq: Named num [1:4] 2045 1483 2606 253
> .. ..- attr(*, "names")= chr [1:4] "" "1&3 vs 2" "1 vs 3" ""
> ..$ F value: Named num [1:4] 8.08 5.86 10.3 NA
> .. ..- attr(*, "names")= chr [1:4] "" "1&3 vs 2" "1 vs 3" ""
> ..$ Pr(>F) : Named num [1:4] 0.000682 0.017988 0.001987 NA
>
> What I would like to do is to request result$"F value[2]"
> but that just gives "NULL."
>
> I note that if I request the structure for model 1, the first element is
> $Df.
No, it's not. The first element is a data.frame, whose 4th column is the
column you want. Using the first example in ?aov,
sm <- summary(aov(yield ~ block + N*P*K, npk))
sm[[1]]$`F value` # note the backticks
# equivalent
sm[[1]][[4]]
Hope this helps,
Rui Barradas
But when I ask for the structure of model2, the first element is
> ..$Df. I assume that those two dots represent an ellipsis, but nothing
> is ?str, ?attr, or other help screens tell me how to interpret and use
> ..Df or any of the other elements.
>
> Any help?
>
> Thanks,
> Dave Howell
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list