问题
I have a list of 25 columns that I am testing to by group (4 levels) through a Dunnett test. I was able to use the sapply
function to get the Dunnett to work for all the columns by group and am having some trouble pulling the p-values into a table. Below is an example of what I am trying to do using the iris dataset.
iris <- iris
iris$group <- ifelse(iris$Species =='setosa', 1,
ifelse(iris$Species =='versicolor', 2,
ifelse(iris$Species =='virginica', 3,
0)))
iris$group <- as.factor(iris$group)
summary(glht(aov(Sepal.Length ~ group, iris), linfct=mcp(group="Dunnett" )))
test
iris$Species
dunnet_model_iris <- sapply(iris[-c(5,6)], function(y, f) summary(glht(aov(y ~ f, iris), linfct=mcp(f="Dunnett"))), f = iris$Species)
names(dunnet_model_iris[[10]]$pvalues)
p_value <- dunnet_model[[10]]$pvalues
p_value
I am able to get the p-values for each column through dunnet_model[[10]]$pvalues (with each column being 10 rows apart (for example: the second column would be dunnet_model[[20]]$pvalues). In total, my data set has 25 columns so I would pull from 10-250. I would like to create a table like this:
2-1 3-1
Sepal.Length 1.44E-15 2.22E-16
Sepal.Width 1.44E-15 2.22E-16
Petal.Length 1.44E-15 2.22E-16
Question: How do I pull out all the Dunnett Comparison P-values for each column into a single table?
I am having some trouble searching for the answer. If anyone has some suggestions that would be greatly appreciated. I am not expecting any code, just some ideas to help shine some light on my situation.
回答1:
We need to extract the test$pvalues
after keeping the model in the list
with lapply
library(multcomp)
dunnet_model_iris <- lapply(iris[-c(5,6)], function(y, f)
summary(glht(aov(y ~ f, iris), linfct=mcp(f="Dunnett"))), f = iris$Species)
t(sapply(dunnet_model_iris, function(x) x$test$pvalues))
# [,1] [,2]
#Sepal.Length 1.443290e-15 2.220446e-16
#Sepal.Width 5.551115e-16 9.074667e-10
#Petal.Length 1.110223e-16 2.220446e-16
#Petal.Width -2.220446e-16 1.110223e-16
Or using the OP's method of creating the 'dunnet_model_iris'
t(sapply(dunnet_model_iris["test",], `[[`, "pvalues"))
来源:https://stackoverflow.com/questions/48407931/extract-p-values-from-dunnett-test-into-a-table-by-variable