I am looking to extract the p-value generated from an anova in R.
Here is what I am running:
test <- aov(asq[,9] ~ asq[,187]) summary(test)
Yields:
Df Sum Sq Mean Sq F value Pr(>F) asq[, 187] 1 3.02 3.01951 12.333 0.0004599 *** Residuals 1335 326.85 0.24483 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 12 observations deleted due to missingness
When I look a the structure, this is what I see. I usually can work through lists to get what I need, but I am having a hard time with this one. A Google searched also seemed to reveal much simpler structures than I am getting.
NOTE: ASQ is my data frame.
str(test) List of 13 $ coefficients : Named num [1:2] 0.2862 0.0973 ..- attr(*, "names")= chr [1:2] "(Intercept)" "asq[, 187]" $ residuals : Named num [1:1337] 0.519 0.519 -0.481 -0.481 -0.481 ... ..- attr(*, "names")= chr [1:1337] "1" "2" "3" "4" ... $ effects : Named num [1:1337] -16.19 -1.738 -0.505 -0.505 -0.505 ... ..- attr(*, "names")= chr [1:1337] "(Intercept)" "asq[, 187]" "" "" ... $ rank : int 2 $ fitted.values: Named num [1:1337] 0.481 0.481 0.481 0.481 0.481 ... ..- attr(*, "names")= chr [1:1337] "1" "2" "3" "4" ... $ assign : int [1:2] 0 1 $ qr :List of 5 ..$ qr : num [1:1337, 1:2] -36.565 0.0273 0.0273 0.0273 0.0273 ... .. ..- attr(*, "dimnames")=List of 2 .. .. ..$ : chr [1:1337] "1" "2" "3" "4" ... .. .. ..$ : chr [1:2] "(Intercept)" "asq[, 187]" .. ..- attr(*, "assign")= int [1:2] 0 1 ..$ qraux: num [1:2] 1.03 1.02 ..$ pivot: int [1:2] 1 2 ..$ tol : num 1e-07 ..$ rank : int 2 ..- attr(*, "class")= chr "qr" $ df.residual : int 1335 $ na.action :Class 'omit' Named int [1:12] 26 257 352 458 508 624 820 874 1046 1082 ... .. ..- attr(*, "names")= chr [1:12] "26" "257" "352" "458" ... $ xlevels : list() $ call : language aov(formula = asq[, 9] ~ asq[, 187]) $ terms :Classes 'terms', 'formula' length 3 asq[, 9] ~ asq[, 187] .. ..- attr(*, "variables")= language list(asq[, 9], asq[, 187]) .. ..- attr(*, "factors")= int [1:2, 1] 0 1 .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. ..$ : chr [1:2] "asq[, 9]" "asq[, 187]" .. .. .. ..$ : chr "asq[, 187]" .. ..- attr(*, "term.labels")= chr "asq[, 187]" .. ..- attr(*, "order")= int 1 .. ..- attr(*, "intercept")= int 1 .. ..- attr(*, "response")= int 1 .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> .. ..- attr(*, "predvars")= language list(asq[, 9], asq[, 187]) .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric" .. .. ..- attr(*, "names")= chr [1:2] "asq[, 9]" "asq[, 187]" $ model :'data.frame': 1337 obs. of 2 variables: ..$ asq[, 9] : int [1:1337] 1 1 0 0 0 1 1 1 0 0 ... ..$ asq[, 187]: int [1:1337] 2 2 2 2 2 2 2 2 2 2 ... ..- attr(*, "terms")=Classes 'terms', 'formula' length 3 asq[, 9] ~ asq[, 187] .. .. ..- attr(*, "variables")= language list(asq[, 9], asq[, 187]) .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1 .. .. .. ..- attr(*, "dimnames")=List of 2 .. .. .. .. ..$ : chr [1:2] "asq[, 9]" "asq[, 187]" .. .. .. .. ..$ : chr "asq[, 187]" .. .. ..- attr(*, "term.labels")= chr "asq[, 187]" .. .. ..- attr(*, "order")= int 1 .. .. ..- attr(*, "intercept")= int 1 .. .. ..- attr(*, "response")= int 1 .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> .. .. ..- attr(*, "predvars")= language list(asq[, 9], asq[, 187]) .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric" .. .. .. ..- attr(*, "names")= chr [1:2] "asq[, 9]" "asq[, 187]" ..- attr(*, "na.action")=Class 'omit' Named int [1:12] 26 257 352 458 508 624 820 874 1046 1082 ... .. .. ..- attr(*, "names")= chr [1:12] "26" "257" "352" "458" ... - attr(*, "class")= chr [1:2] "aov" "lm"
If this p-value is less than α = . 05, we reject the null hypothesis of the ANOVA and conclude that there is a statistically significant difference between the means of the three groups. Otherwise, if the p-value is not less than α = .
In short: aov fits a model (as you are already aware, internally it calls lm ), so it produces regression coefficients, fitted values, residuals, etc; It produces an object of primary class "aov" but also a secondary class "lm". So, it is an augmentation of an "lm" object. anova is a generic function.
The summary. aov function returns an object of class "summary. aov", "listof", which contains a list of objects with class "anova", "data. frame", each object contains an ANOVA table.
Here:
summary(test)[[1]][["Pr(>F)"]][1]
since the suggest above didn't work for me this is how i managed to solve it:
sum_test = unlist(summary(test))
then looking at the names with
names(sum_test)
i have"Pr(>F)1" and "Pr(>F)2", when the first it the requested value, so
sum_test["Pr(>F)1"]
will give the requested value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With