Can R visualize tttest test results or other hypotheses?

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 a way to visualize t.test test results or other hypotheses?

Below is an example of what I'm looking for:

enter image description here

+6
source share
5 answers

There are many things you can do. Here is just one case when I draw a random sample from the standard normal distribution, then do a t-test, plot the observed t and t, necessary to reject the null hypothesis that the average value is 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 values ​​will look different every time you restart this code, which can serve as a good illustration if you run it again.

+5
source

Here is one way. You can change the schedule according to 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 do not like these vertical lines, you can try this:

 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) 
+1
source

Here is one way to visualize the results of many hypothesis tests using estimated values ​​and 95% confidence intervals. I took the idea directly from the TukeyHSD() build method, but implemented it with ggplot2 . Unfortunately, there is no built-in htest method for htest results in R.

 library(MASS) library(ggplot2) h = na.omit(survey$Height) pop.mean = mean(h) n_reps = 20 sample_size = 30 res_list = list() for (i in 1:n_reps) { h.sample = sample(h, sample_size) res_list[[i]] = t.test(h.sample, mu=pop.mean) } dat = data.frame(id=seq(length(res_list)), estimate=sapply(res_list, function(x) x$estimate), conf_int_lower=sapply(res_list, function(x) x$conf.int[1]), conf_int_upper=sapply(res_list, function(x) x$conf.int[2])) p = ggplot(data=dat, aes(x=estimate, y=id)) + geom_vline(xintercept=pop.mean, color="red", linetype=2) + geom_point(color="grey30") + geom_errorbarh(aes(xmin=conf_int_lower, xmax=conf_int_upper), color="grey30", height=0.4) ggsave("CI_plot.png", plot=p, height=4, width=6, units="in", dpi=150) 

enter image description here

0
source

I understand this is an old question, but I recently created an R package on CRAN to solve this problem. The code below produces the desired schedule:

 library(MASS) library(mcStats) h=na.omit(survey$Height) pop.mean=mean(h) h.sample = sample(h,30) showT.Test(h.sample,mu=pop.mean) 
0
source

I think you are looking

 t <- t.test(h.sample,mu=pop.mean) t$conf.int[2] # the t-statistic value (pink circle in your image) t$p.value 

using

 str(t) 

to see all available options.

-one
source

All Articles