R可以将t.test或其他假设检验结果可视化吗?

时间:2016-04-08 19:40:34

标签: r hypothesis-test

我需要在R中进行许多假设检验并提供结果。这是一个例子:

> 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 

有没有什么方法可以直观地显示t.test或其他假设检验结果?

以下是我正在寻找的一个例子:

enter image description here

6 个答案:

答案 0 :(得分:5)

你可以做很多事情。这里只是我从标准正态分布中抽取一个随机样本,然后进行t检验,观察到的t和t所需的曲线,以拒绝平均值等于0的零假设。 / p>

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

当然,每次重新运行此代码时,您观察到的t都会有所不同,如果反复运行,这可能会很好地说明。

答案 1 :(得分:1)

这是一种方法。您可以修改情节以满足您的需求:

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)

如果你不喜欢这些垂直线,你可以试试这个:

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)

答案 2 :(得分:1)

还有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

答案 3 :(得分:0)

这是使用估计值和95%置信区间可视化许多假设检验结果的一种方法。我直接从TukeyHSD()绘图方法中获取了这个想法,但是使用ggplot2实现了它。遗憾的是,htest没有内置的绘图方法导致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

答案 4 :(得分:0)

我意识到这是一个老问题,但是我最近在CRAN上创建了R包来解决此问题。下面的代码生成所需的图形:

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)

答案 5 :(得分:-1)

我认为你正在寻找

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

使用

str(t)

查看所有可用参数。