【问题标题】:How to perform a t-test on variables within the same category on r?如何在 r 上对同一类别中的变量进行 t 检验?
【发布时间】:2017-05-19 01:49:53
【问题描述】:

我想对被捕时男女的平均年龄进行 t 检验。但是,我的数据是这样排列的:

Sex: Age:
M    21
F    31
F    42
M    43

有没有办法将性别类别分为两个单独的类别(男性和女性)以执行我的 t 检验?或者在一个类别中执行 t 检验?已经提出了类似的问题,但似乎没有一个适用于我的数据集。感谢您提供的任何指导!

【问题讨论】:

  • 欢迎来到本站!在发布问题之前,请阅读编写最小、完整的可验证代码示例的指南 (stackoverflow.com/help/mcve)。目前,您的问题似乎与统计有关,而不是与编程有关。你使用什么语言?你试过什么了?有一个统计问题网站 (stats.stackexchange.com),但 stackoverflow.com 的存在是为了另一个目的。详情请见stackoverflow.com/help/on-topic
  • 抱歉含糊不清-我第一次将“R”用于 HS 统计项目(我希望能解释我的最小细节)。到目前为止,我已经尝试过:men
  • 我也尝试在stackoverflow.com/questions/41442344/…这个链接上使用答案,但不确定如何将其应用于我自己的数据。
  • 两个答案都是正确的。只要确保你明白你要求 R 在每一个中做什么。有很多方法可以对数据进行排序。我选择我的答案来支持您已经使用数据框的方式,因为它似乎可以帮助您了解如何到达您的目标......但两者都同样有效。

标签: r t-test


【解决方案1】:

首先,很好的第一个问题,很高兴看到高中生学习统计编程!

第二:你自己正在寻找答案的路上,这应该可以帮助你到达那里。

我在做一些假设:

  1. prof 是您的数据框的名称 2 您希望在 t 检验中比较 prof 的性别年龄

你的逻辑是正确的。我在prof 数据框中添加了一些额外的观察结果,但它应该是这样工作的:
# this is a comment in the code, not code, but it explains the reasoning, it always starts with hash tag

women<-prof[which(prof$Sex=="F"),] #notice the comma after parenthesis
men<-prof[which(prof$Sex=="M"),] #notice the comma after parenthesis here too 

逗号左侧选择具有该数据 == “某物”的行。逗号右边告诉你哪些列,留空告诉 r 包括所有列。

head(men);head(women) # shows you first 6 rows of each new frame
# you can see below that the data is still in a data frame

   Sex Age
1    M  21
4    M  43
5    M  12
6    M  36
7    M  21
10   M  23
   Sex Age
2    F  31
3    F  42
8    F  52
9    F  21
11   F  36

所以要对年龄进行 t-test,您必须按名称和带有年龄的列询问数据框,例如:men$Age

t.test(women$Age, men$Age) #this is the test

 # results below

Welch Two Sample t-test

data:  women$Age and men$Age
t = 0.59863, df = 10.172, p-value = 0.5625
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:

 -11.93964  20.73964
sample estimates:
mean of x mean of y 
     36.4      32.0 

在 R 中几乎总是有不止一种方法。有时初始排序更复杂,但处理数据更容易。因此,如果您不想从数据框中解决年龄问题,您可以要求初始子集中的列

women<-prof[which(prof$Sex=="F"),"Age"] #set women equal to just the ages where Sex is 'F'
men<-prof[which(prof$Sex=="M"), "Age"]#set men equal to just the ages where Sex is 'M'

再次查看您的数据,这次只是每个变量的年龄向量:

head(women); head(men)
[1] 31 42 52 21 36
[1] 21 43 12 36 21 23

那么你的 t-test 就是一个简单的比较:

t.test(women,men)
 # notice same results

    Welch Two Sample t-test

data:  women and men
t = 0.59863, df = 10.172, p-value = 0.5625
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -11.93964  20.73964
sample estimates:
mean of x mean of y 
     36.4      32.0 

看来您的问题出在代码中的三个位置:

  1. 当列名为Sex: 时使用gender=="F"
  2. [,] 中不使用逗号来指定行和列
  3. 不处理 t.test 中的 $Age 列(如果它确实仍然存在) 两列

上面的代码应该可以让你到达你需要的地方。

【讨论】:

  • 你太棒了!!非常感谢,这无济于事(现在一切都说得通了!)
【解决方案2】:

比较男性和女性年龄的 t 检验可以像这样进行:

df = data.frame(
    gender = c("M", "F", "F", "M"),
    age = c(21, 31, 42, 43)
)

t.test(age ~ gender, data = df)

根据您的问题,这是最相关的测试。

当您说“在一个类别中执行 t 检验”时,我不确定您的意思:您可以将一组值与某个已知参考值(例如 0)进行比较,但我不确定是什么这可以告诉你(除了你样本中的男性不是 0 岁)。

【讨论】:

  • 我认为她的意思是按因素(分类数据)对Sex 进行排序,以使用 t.test 评估每个级别的年龄
【解决方案3】:

你可以试试这个代码:

t.test(Age ~ Sex, paired = FALSE, data = datasetName)

它应该给您相同的结果,而无需创建更多子集。

【讨论】:

    猜你喜欢
    • 2023-01-13
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 2017-05-17
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    相关资源
    最近更新 更多