【问题标题】:Grouping factors in a pooled 2 sample t-test汇总 2 个样本 t 检验中的分组因素
【发布时间】:2020-06-18 17:43:23
【问题描述】:

我有一个 2*2 表,包含 7 名男性和 11 名女性的体重(保存为 weights_gender.csv),旨在执行合并 t 检验。我已将 CSV 文件指定为 weight = read.csv("weights_gender.csv"),但每当我尝试运行 t.test(weight$men~weight$women, var.equal=TRUE) 时,它都会继续打印此消息:

分组因子必须正好有 2 个级别。

有什么问题?

【问题讨论】:

  • 您好,您能提供一个代表吗?我猜你有缺失值,尝试检查 levels()
  • 没有看到您的数据结构,这只是一个猜测,但我相信您使用的 t.test 格式不正确。以上是公式形式,而您需要逗号形式。试试看:t.test(men, women, var.equal=TRUE)
  • 我已经更新了更多细节。问题是否来自 2 种性别的数据数量不平衡?
  • 我之前的评论站着,你需要用逗号来比较第1列和第2列,类似这个问题:stackoverflow.com/questions/62045080/…

标签: r


【解决方案1】:

试试...

t.test(x = weight$men, y = weight$women, var.equal = TRUE)

您指定命令的方式认为您想要按女性分组的男性体重,这当然不是您想要的。

结果...


    Two Sample t-test

data:  weight$men and weight$women
t = 5.9957, df = 16, p-value = 1.867e-05
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 15.26250 31.95828
sample estimates:
mean of x mean of y 
 77.42857  53.81818 

数据

weight <- data.frame(
   men = c(88, 90, 78, 75, 70, 72, 69, NA, NA, NA, NA),
   women = c(45, 57, 54, 62, 60, 59, 44, 43, 67, 50, 51)
)

【讨论】:

    【解决方案2】:

    你的问题有点“理论”,所以我会更具体

    在这里,我制作了两个数据框,其中包含有关男性和女性体重的数据,并对其进行标记。

    df_m <- tibble(weight = 170 + 30*rnorm(7), sex = "Male")
    df_f <- tibble(weight = 130 + 30*rnorm(11), sex = "Female")
    

    接下来我们结合数据并将sex设置为因子变量

    df_all <- rbind(df_m, df_f)
    df_all[, 'sex'] <- lapply(df_all[, 'sex'], as.factor)
    

    最后我们应用 t 检验。

    t.test(weight ~ sex, data = df_all, var.equal = TRUE)
    

    我的结果是

        Two Sample t-test
    
    data:  weight by sex
    t = -5.2104, df = 16, p-value = 8.583e-05
    alternative hypothesis: true difference in means is not equal to 0
    95 percent confidence interval:
     -89.84278 -37.87810
    sample estimates:
    mean in group Female   mean in group Male 
                120.2316             184.0921
    

    【讨论】:

      猜你喜欢
      • 2017-12-20
      • 2014-05-01
      • 1970-01-01
      • 2015-06-07
      • 2020-04-09
      • 1970-01-01
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多