Displaying only the p-value of multiple t.tests
Solution 1
t.test
returns a object of class htest
which is a list containing a number of components including p.value
(which is what you want).
You have a couple of options.
You can save the t.test
results in a list and then extract the p.value
component
# simplify = FALSE to avoid coercion to array
ttestlist <- replicate(1000, t.test(rnorm(10)), simplify = FALSE)
ttest.pval <- sapply(ttestlist, '[[', 'p.value')
Or you could simply only save that component of the t.test
object
pvals <- replicate(1000, t.test(rnorm(10))$p.value)
Solution 2
Here are the steps I'd use to solve your problem. Pay attention to how I broke it down into the smallest component parts and built it up step by step:
#Let's look at the structure of one t.test to see where the p-value is stored
str(t.test(rnorm(10)))
#It is named "p.value, so let's see if we can extract it
t.test(rnorm(10))[["p.value"]]
#Now let's test if its less than your 0.05 value
ifelse(t.test(rnorm(10))[["p.value"]]< 0.05,1,0)
#That worked. Now let's replace the code above in your replicate function:
replicate(1000, ifelse(t.test(rnorm(10))[["p.value"]]< 0.05,1,0))
#That worked too, now we can just take the sum of that:
#Make it reproducible this time
set.seed(42)
sum(replicate(1000, ifelse(t.test(rnorm(10))[["p.value"]]< 0.05,1,0)))
Should yield this:
[1] 54
Heleen Feijen
Updated on August 16, 2022Comments
-
Heleen Feijen over 1 year
I have
replicate(1000, t.test(rnorm(10)))
What it does that it draws a sample of size ten from a normal distribution, performs a
t.test
on it, and does this a 1000 times. But for my assignment I'm only interested in the p-value (the question is: how many times is the null hypothesis rejected). How do I get only the p-values, or can I add something that already says how many times the null hypothesis is rejected(how many times the p-value is smaller than 0.05) -
Heleen Feijen over 11 yearsThank you so much for your help, this was definitely the answer I needed.
-
Heleen Feijen over 11 yearsWow, this is an even better answer. But my teacher is never going to believe that I came up with this myself haha. I'll stick with the method of Brandon and sort the data so I can easily count the ones that are below 0.05.