【问题标题】:Can R visualize the t.test or other hypothesis test results?R 可以可视化 t.test 或其他假设检验结果吗?
【发布时间】:2016-07-30 05:08:30
【问题描述】:

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

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

【问题讨论】:

    标签: r hypothesis-test


    【解决方案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))
    

    【讨论】:

      【解决方案2】:

      我意识到这是一个老问题,但我最近在 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)
      

      【讨论】:

        【解决方案3】:

        这是使用估计值和 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)
        

        【讨论】:

          【解决方案4】:

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

          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)
          

          【讨论】:

            【解决方案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 看起来都会有所不同,如果重复运行,这可能是一个很好的说明。

            【讨论】:

              【解决方案6】:

              我想你正在寻找

              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)
              

              查看所有可用参数。

              【讨论】:

              • 我知道如何获取数据。我正在寻找一种将其可视化的方法。
              猜你喜欢
              • 1970-01-01
              • 2016-04-10
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-06-30
              • 2021-02-16
              • 2017-09-17
              • 1970-01-01
              相关资源
              最近更新 更多