Extract p-value from aov
Here:
summary(test)[[1]][["Pr(>F)"]][1]
Extract p-value from anova() from comparison of two linear models in R
You just need quotation marks:
pvalue <- anova(m2,m1)$"Pr(>F)"
You can access the second element of pvalue using normal bracket subsetting:
pvalue[2]
So in your example I believe you'll use
Coop <- anova(M6,Models5[[B]])$"Pr(>F)"[2]
(Although without access to M6, I can't be totally sure).
The str
command is very useful in this sort of situation, to figure out what kind of object you're dealing with:
str(myanova$"Pr(>F)")
How to get p-value from a summary anova test in R?
If you take a look at the structrure of your result (see ?str
function) you'll realize about how to use indexing operator ([
) in order to get Pr(>F)
.
> str(result)
List of 1
$ :Classes ‘anova’ and 'data.frame': 2 obs. of 5 variables:
..$ Df : num [1:2] 2 12
..$ Sum Sq : num [1:2] 35.7 261.3
..$ Mean Sq: num [1:2] 17.9 21.8
..$ F value: num [1:2] 0.82 NA
..$ Pr(>F) : num [1:2] 0.464 NA
- attr(*, "class")= chr [1:2] "summary.aov" "listof"
You can do it this way
> result[[1]][["Pr(>F)"]][1]
[1] 0.4636054
or
> result[[1]][[5]][1]
[1] 0.4636054
How to extract p values from a vector with multiple anova
You can use sapply
to iterate over each anova and extract p-value.
sapply(w3, function(x) summary(x)[[1]][["Pr(>F)"]][[1]])
Using reproducible example with mtcars
w3 <- lapply(split(mtcars, mtcars$cyl), aov, formula=mpg ~ am)
sapply(w3, function(x) summary(x)[[1]][["Pr(>F)"]][[1]])
# 4 6 8
#0.0892 0.2209 0.8662
R- One way anova extracting p_value
Consider using broom
. With tidy()
, you can extract only the p.value
field:
require(broom)
a <- aov(mpg ~ wt, mtcars)
tidy(a)
# term df sumsq meansq statistic p.value
# 1 wt 1 847.7252 847.725250 91.37533 1.293959e-10
# 2 Residuals 30 278.3219 9.277398 NA NA
tidy(a)$p.value
# [1] 1.293959e-10 NA
How to extract p-values at a time and save p-values as csv files?
You could apply anova
on each group and extract p-value from them
vals <- sapply(split(df, df$GeneSymbol), function(i)
anova(lm(Value ~ Label, data = i))$"Pr(>F)"[1])
vals
# A B C
#0.6419426 0.9446151 0.9146334
If you want to write it in csv, you could do
p_data <- data.frame(p_value = vals)
write.csv(p_data, "/path/of/the/file.csv", row.names = FALSE)
Similarly with dplyr
you could do
df %>%
group_split(GeneSymbol) %>%
purrr::map_dbl(~anova(lm(Value ~ Label, data = .))$"Pr(>F)"[1])
#[1] 0.6419426 0.9446151 0.9146334
data
df <- structure(list(GeneSymbol = structure(c(1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L, 1L, 2L, 3L), .Label = c("A", "B", "C"), class = "factor"),
Value = c(0.14, 0.16, 0.01, 0.18, 0.54, 0.18, 0.2, 0.54,
0.2, 0.02, 0.2, 0.02), Label = c(1L, 1L, 1L, 1L, 1L, 1L,
0L, 0L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c("2",
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"))
How to use lapply() with aov() because results returns no p-value and f-value but just a list of Class and Mode ?
I'm not sure your purpose applying those tests.
If code below is your purpose,
cary <- aov(df_list[["1.3.A"]]$Height ~ df_list[["1.3.A"]]$Person)
summary(cary)
and kruskal wallis test that gives you p-value was
lapply(df_list, kruskal.test)
then I'm not sure p-value from code above is correct.
That code will print warning message that
In kruskal.test.default(X[[i]], ...) :
some elements of 'x' are not numeric and will be coerced to numeric
This coerced person's name as numeric.
It's pretty messy but code below may be messy but gives better p-values
lapply(df_list, function(x) (kruskal.test(x$Height ~ x$Person)))
lapply(df_list, function(x) summary(aov(x$Height ~ x$Person)))
EDIT:
If you want to get only p-values,
lapply(df_list, function(x) (kruskal.test(x$Height ~ x$Person)$p.value))
lapply(df_list, function(x) summary(aov(x$Height ~ x$Person))[[1]]$`Pr(>F)`[1])
will do.
EDIT 2(reply to comments)
If you want your tree function work without using $, then you should change code as
tree <- function(df) { aov(Height ~ Person, data = df) }
or
tree <- function(df) { aov(df$Height ~ df$Person) }
and then, to print p-value, instead of using
summary(shrub)
use
lapply(shrub,summary)
Because I use lapply, it fits ANOVA for each data frame of list.
To call p-values from summary result, try fitting code belowx<-summary(aov(Height~Person, df_list[[1]]))
then you'll see x is List of 1 and to call components of x and because there is only one element in x, so i call with [[1]]
And then, x has form of matrix so with
x[[1]]$`Pr(>F)`
it will print
[1] 1.433592e-42 NA
I want to remove NA so i add [1] to the end like
x[[1]]$`Pr(>F)`[1]
Related Topics
Why Would R Use the "L" Suffix to Denote an Integer
"Correct" Way to Specifiy Optional Arguments in R Functions
Convert Data.Frame Column to a Vector
Use Trycatch Skip to Next Value of Loop Upon Error
How to Use R with Google Colaboratory
Check for Installed Packages Before Running Install.Packages()
How to Use Objects from Global Environment in Rstudio Markdown
Remove Multiple Objects with Rm()
Creating a Prompt/Answer System to Input Data into R
How to Install Development Version of R Packages Github Repository
Display a Time Clock in the R Command Line
Adaptive Moving Average - Top Performance in R
Changing Line Colors with Ggplot()