Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当R中有两组时,如何使用两个样本的t检验?_R - Fatal编程技术网

当R中有两组时,如何使用两个样本的t检验?

当R中有两组时,如何使用两个样本的t检验?,r,R,我有一个包含水果、成熟度和平均值的数据框架。 我如何创建一个for循环来运行一个ttest来确定每个水果成熟度的平均差异?换句话说,对于苹果,ttest将产生成熟和未成熟苹果之间的平均差异结果。 这方面的示例如下表所示。 类似的方法可以用于返回t检验的p值,在循环遍历数据中出现的独特果实时比较成熟度 ## create a vector of the unique fruit in the data; vector of fruit to be tested fruit<-unique(d

我有一个包含水果、成熟度和平均值的数据框架。 我如何创建一个for循环来运行一个ttest来确定每个水果成熟度的平均差异?换句话说,对于苹果,ttest将产生成熟和未成熟苹果之间的平均差异结果。 这方面的示例如下表所示。
类似的方法可以用于返回t检验的p值,在循环遍历数据中出现的独特果实时比较成熟度

## create a vector of the unique fruit in the data; vector of fruit to be tested
fruit<-unique(data$Fruits)
## iterate through your list of unique fruit, testing as you go
for(i in 1:length(fruit)){
  ## subset your data to include only the current fruit to be tested
  df<-filter(data, Fruits==fruit[i])
  ## let the user know which fruit is being tested
  message(fruit[i])
  ## create a vector of the unique ripeness states of the current fruit to be tested
  ripe<-unique(df$Ripeness)
  ## make sure two means exist; ensure there are both ripe and non-ripe values
  if(length(ripe) < 2){
    ## if only one ripeness, let user know and skip to next unique fruit
    message("only one ripeness")
    next
  }
  ## try testing the fruit and return p-value if success
  tryCatch(
    {
      message(t.test(Mean ~ Ripeness, data = df)$p.value)
    },
    ## if error in t-testing return message that there are "not enough observations"
    error=function(cond) {
      message("not enough observations")
    }
  )    
}

我希望这有帮助

这样的方法可以在循环遍历数据中出现的独特果实时,返回比较成熟度的t检验的p值

## create a vector of the unique fruit in the data; vector of fruit to be tested
fruit<-unique(data$Fruits)
## iterate through your list of unique fruit, testing as you go
for(i in 1:length(fruit)){
  ## subset your data to include only the current fruit to be tested
  df<-filter(data, Fruits==fruit[i])
  ## let the user know which fruit is being tested
  message(fruit[i])
  ## create a vector of the unique ripeness states of the current fruit to be tested
  ripe<-unique(df$Ripeness)
  ## make sure two means exist; ensure there are both ripe and non-ripe values
  if(length(ripe) < 2){
    ## if only one ripeness, let user know and skip to next unique fruit
    message("only one ripeness")
    next
  }
  ## try testing the fruit and return p-value if success
  tryCatch(
    {
      message(t.test(Mean ~ Ripeness, data = df)$p.value)
    },
    ## if error in t-testing return message that there are "not enough observations"
    error=function(cond) {
      message("not enough observations")
    }
  )    
}
我希望这有帮助

假设水果被编码为一个分类变量,即因子,您可以使用sapply按每个水果迭代地对数据子集。在t.test中,我们使用alternative=two.side,只是为了强调它是默认设置

然而,你的数据非常少,香蕉只成熟。因此,我需要一个更大的样本数据集来演示

res <- sapply(levels(dat$fruits), function(x) 
  t.test(mean ~ ripeness, dat[dat$fruits %in% x, ], alternative="two.sided")
)
res
#             Apple                     Banana                    Orange                   
# statistic   0.948231                  0.3432062                 0.4421971                
# parameter   23.38387                  30.86684                  16.47366                 
# p.value     0.3527092                 0.7337699                 0.664097                 
# conf.int    Numeric,2                 Numeric,2                 Numeric,2                
# estimate    Numeric,2                 Numeric,2                 Numeric,2                
# null.value  0                         0                         0                        
# stderr      0.8893453                 1.16548                   1.043739                 
# alternative "two.sided"               "two.sided"               "two.sided"              
# method      "Welch Two Sample t-test" "Welch Two Sample t-test" "Welch Two Sample t-test"
# data.name   "mean by ripeness"        "mean by ripeness"        "mean by ripeness"     
数据:

请注意,将来您应该包括一个最小的自包含示例,其中包含适当格式的数据。请阅读如何执行此操作,以及迄今为止您尝试的所有步骤,因为堆栈溢出不是编码服务。干杯

假设水果被编码为一个分类变量,即因子,您可以使用sapply按每个水果迭代地对数据子集。在t.test中,我们使用alternative=two.side,只是为了强调它是默认设置

然而,你的数据非常少,香蕉只成熟。因此,我需要一个更大的样本数据集来演示

res <- sapply(levels(dat$fruits), function(x) 
  t.test(mean ~ ripeness, dat[dat$fruits %in% x, ], alternative="two.sided")
)
res
#             Apple                     Banana                    Orange                   
# statistic   0.948231                  0.3432062                 0.4421971                
# parameter   23.38387                  30.86684                  16.47366                 
# p.value     0.3527092                 0.7337699                 0.664097                 
# conf.int    Numeric,2                 Numeric,2                 Numeric,2                
# estimate    Numeric,2                 Numeric,2                 Numeric,2                
# null.value  0                         0                         0                        
# stderr      0.8893453                 1.16548                   1.043739                 
# alternative "two.sided"               "two.sided"               "two.sided"              
# method      "Welch Two Sample t-test" "Welch Two Sample t-test" "Welch Two Sample t-test"
# data.name   "mean by ripeness"        "mean by ripeness"        "mean by ripeness"     
数据:


请注意,将来您应该包括一个最小的自包含示例,其中包含适当格式的数据。请阅读如何执行此操作,以及迄今为止您尝试的所有步骤,因为堆栈溢出不是编码服务。干杯

谢谢你的意见!我试着插上这个,但它似乎不适用于只有一种成熟度的水果。是否有一种方法可以解决这个问题,同时也适用于具有两种成熟度类别的水果?上述答案已更新,以包括一项检查,以确保在比较平均值之前有两种成熟度!最美好的祝福!再次感谢!我遇到了另一个问题,我希望你能提供一些意见。当没有足够的观察值时,t.test参数似乎会出错。是否可以使用if语句,以便在遇到错误时跳过该特定结果,并转到下一个有效的t.test?我更新的代码将t.test包装在tryCatch函数中。tryCatch功能可用于捕获错误和警告消息。可以找到一个很好的tryCatch解释。关于这个for循环的结果,我有一个后续问题。假设我在for循环之外初始化了一个名为finalOut的数据帧,那么如何存储t-test的结果,使finalOut有一列名为Fruits,另一列名为t-test的p值?谢谢您的输入!我试着插上这个,但它似乎不适用于只有一种成熟度的水果。是否有一种方法可以解决这个问题,同时也适用于具有两种成熟度类别的水果?上述答案已更新,以包括一项检查,以确保在比较平均值之前有两种成熟度!最美好的祝福!再次感谢!我遇到了另一个问题,我希望你能提供一些意见。当没有足够的观察值时,t.test参数似乎会出错。是否可以使用if语句,以便在遇到错误时跳过该特定结果,并转到下一个有效的t.test?我更新的代码将t.test包装在tryCatch函数中。tryCatch功能可用于捕获错误和警告消息。可以找到一个很好的tryCatch解释。关于这个for循环的结果,我有一个后续问题。假设我在for循环外初始化了一个名为finalOut的数据帧,那么如何存储t-test的结果,使finalOut有一列名为Fruits,另一列名为t-test的p值?