7

我需要在 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 或其他假设检验结果?

以下是我正在寻找的示例:

在此处输入图像描述

4

6 回答 6

5

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

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)

在此处输入图像描述

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

于 2016-04-08T20:04:06.680 回答
2

还有gginference 包

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))

在此处输入图像描述

于 2020-03-12T15:47:55.020 回答
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)
于 2016-04-08T19:55:29.577 回答
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)
于 2019-04-29T20:40:52.890 回答
0

这是使用估计值和 95% 置信区间可视化许多假设检验结果的一种方法。我直接从TukeyHSD()绘图方法中获取了这个想法,但是用ggplot2. 不幸的是,R 中没有内置的htest结果绘图方法。

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)

在此处输入图像描述

于 2016-04-08T22:46:24.940 回答
-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)

查看所有可用参数。

于 2016-04-08T19:58:49.680 回答