Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can R visualize the t.test or other hypothesis test results?

I need to work with many hypothesis tests in R and present the results. Here is an example:

> library(MASS)
> h=na.omit(survey$Height)
> 
> pop.mean=mean(h)
> h.sample = sample(h,30)
> 
> t.test(h.sample,mu=pop.mean)

    One Sample t-test

data:  h.sample
t = -0.0083069, df = 29, p-value = 0.9934
alternative hypothesis: true mean is not equal to 172.3809
95 percent confidence interval:
 168.8718 175.8615
sample estimates:
mean of x 
 172.3667 

Is there any way that we visualize the t.test or other hypothesis test results?

Below is an example of what I am looking for:

enter image description here

like image 466
Allan Xu Avatar asked Apr 08 '16 19:04

Allan Xu


People also ask

How do you visually represent t-test?

The most commonly used way to visualize t-test-like comparison is to use boxplots.

How do you interpret a hypothesis test in R?

It can be interpreted in the following way: A small p-value (typically ≤ 0.05) indicates strong evidence against the null hypothesis, so you reject it. A large p-value (> 0.05) indicates weak evidence against the null hypothesis, so you fail to reject it.


3 Answers

There is a lot of things you can do. Here is just one where I draw a random sample from the standard normal distribution, then do a t-test, the plot the observed t and the t's needed to reject the null hypothesis that the mean is equal to 0.

N=20 #just chosen arbitrarily
samp=rnorm(N)
myTest=t.test(samp)
tcrit=qt(0.025, df=(N-1))

dum=seq(-3.5, 3.5, length=10^4)#For the plot

plot(dum, dt(dum, df=(N-1)), type='l', xlab='t', ylab='f(t)')
abline(v=myTest$statistic, lty=2)
abline(v=tcrit, col='red', lty=2)
abline(v=-tcrit, col='red', lty=2)

enter image description here

Of course your observed t will look different every time you re-run this code, which might make a good illustration if ran repeatedly.

like image 193
justin1.618 Avatar answered Oct 18 '22 22:10

justin1.618


There is also gginference package.

library(MASS)
h=na.omit(survey$Height)
pop.mean=mean(h)
h.sample = sample(h,30)
t.test(h.sample,mu=pop.mean)

library(gginference)
ggttest(t.test(h.sample,mu=pop.mean))

enter image description here

like image 37
akis Avatar answered Oct 18 '22 21:10

akis


Here is one way. you can modify the plot to suit your needs:

library(ggplot2)
x <- seq(mean(h) - 4 * sd(h), mean(h) + 4 * sd(h), 0.01)
df <- data.frame(x = x, d = dnorm(x, mean(h), sd(h)))
ggplot(df, aes(x = x, y = d)) + geom_line() + theme_bw() + geom_vline(xintercept = c(mean(h) + 3 * sd(h), mean(h) - 3 * sd(h)), col = 'red') + xlim(120, 240)

If you don't like those vertical lines, you can try this instead:

ggplot(df, aes(x = x, y = d)) + geom_line() + theme_bw() + geom_segment(aes(x = mean(h) - 3 * sd(h), xend = mean(h) - 3 * sd(h), y = 0, yend = dnorm(mean(h) - 3 * sd(h), mean(h), sd(h)), col = 'red')) + geom_segment(aes(x = mean(h) + 3 * sd(h), xend = mean(h) + 3 * sd(h), y = 0, yend = dnorm(mean(h) + 3 * sd(h), mean(h), sd(h)), col = 'red')) + xlim(120, 240) + ylim(-0.001, 0.041)
like image 35
Gopala Avatar answered Oct 18 '22 22:10

Gopala