【问题标题】:performing a T-test in R with categorical variables使用分类变量在 R 中执行 T 检验
【发布时间】:2018-08-02 10:34:40
【问题描述】:

嘿,伙计们,我正在尝试进行 t 检验,但看起来有问题... 数据如下:

pot pair    type    height
I   1   Cross   23,5
I   1   Self    17,375
I   2   Cross   12
I   2   Self    20,375

我进行了 t 检验:

    darwin <- read.table("darwin.txt", header=T)
    plot(darwin$type, darwin$height, ylab="Height")
    darwin.no.outlier = subset(darwin, height>13)
    tapply(darwin.no.outlier$height, darwin.no.outlier$type, var) 
    t.test(darwin$height ~ darwin$type)

R 给我的错误如下:

错误

if (stderr < 10 * .Machine$double.eps * max(abs(mx), abs(my))) stop("data are essentially constant") : 
  missing value where TRUE/FALSE needed

另外:警告信息:

1:在 mean.default(x) 中:argument is not numeric or logical: returning NA

2:在 var(x) 中:

Calling var(x) on a factor x is deprecated and will become an error.
  Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.

3:在 mean.default(y) 中:argument is not numeric or logical: returning NA

4:在 var(y) 中:

Calling var(x) on a factor x is deprecated and will become an error.
  Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.

【问题讨论】:

  • 确保您的 height 变量是数字。好像不是。
  • 试试t.test(height ~ type, data=darwin)
  • @Jimbou 我想他会得到同样的错误。 height 中的逗号表明它不是数字变量。更多信息:stackoverflow.com/questions/3247178/…

标签: r t-test


【解决方案1】:

问题在于您的小数位,在您的列height 中是逗号而不是点。由于小数的逗号分隔符,您的列被转换为因子,因此您得到错误。

导入数据时,在read.table 中插入"dec = ","(文件中用于小数点的字符)。所以我的例子是你的数据:

darwin <- read.table(text = "pot pair    type    height
I   1   Cross   23,5
           I   1   Self    17,375
           I   2   Cross   12
           I   2   Self    20,375", header = TRUE, dec = ",")

然后是

的输出
t.test(darwin$height ~ darwin$type)

这是:

    Welch Two Sample t-test

data:  darwin$height by darwin$type
t = -0.18932, df = 1.1355, p-value = 0.878
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -58.34187  56.09187
sample estimates:
mean in group Cross  mean in group Self 
             17.750              18.875 

【讨论】:

  • 很高兴为您提供帮助。如果答案解决了您的问题,请随时接受答案(答案旁边的复选标记)。
猜你喜欢
  • 2017-02-02
  • 2020-06-25
  • 1970-01-01
  • 2023-01-13
  • 2022-07-20
  • 1970-01-01
  • 2022-12-11
  • 1970-01-01
  • 2015-09-17
相关资源
最近更新 更多