【问题标题】:using chisq.test in R (chi-squared tests)在 R 中使用 chisq.test(卡方检验)
【发布时间】:2011-03-09 07:04:16
【问题描述】:

我正在尝试读取 csv 文件,然后从 csv 文件的每一行中创建 3 个矩阵,然后使用方法 chisq.test(matrix) 应用卡方检验,但不知何故,这种方法似乎失败了。

它给了我以下错误:

sum(x) 中的错误:参数的“类型”(列表)无效

另一方面,如果我只是创建一个传递一些数字的矩阵,那么它就可以正常工作。 我还尝试在两种类型的矩阵上运行 str

  1. 我使用 csv 文件中的行创建的。 str 给出:

    List of 12
     $ : int 3
     $ : int 7
     $ : int 3
     $ : int 1
     $ : int 7
     $ : int 3
     $ : int 1
     $ : int 1
     $ : int 1
     $ : int 0
     $ : int 2
     $ : int 0
     - attr(*, "dim")= int [1:2] 4 3
    
  2. 使用一些数字创建的矩阵。 str 给出:

    num [1:2, 1:3] 1 2 3 4 5 6
    

谁能告诉我这里发生了什么?

【问题讨论】:

标签: r statistics chi-squared


【解决方案1】:

问题在于你的数据结构是一个列表数组,而对于 chisq.test() 你需要一个数值数组。

一种解决方案是使用 as.numeric() 将数据强制转换为数字。我在下面演示这一点。另一种解决方案是在创建数组之前先将 read.csv() 的结果转换为数字。

# Recreate data
x <- structure(array(list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)), dim=c(3,4))
str(x)

List of 12
 $ : num 1
 $ : num 2
 $ : num 3
 $ : num 4
 $ : num 5
 $ : num 6
 $ : num 7
 $ : num 8
 $ : num 9
 $ : num 10
 $ : num 11
 $ : num 12
 - attr(*, "dim")= int [1:2] 3 4

# Convert to numeric array
x <- array(as.numeric(x), dim=dim(x))
str(x)

num [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ...

chisq.test(x)

    Pearson's Chi-squared test

data:  x 
X-squared = 0.6156, df = 6, p-value = 0.9961

Warning message:
In chisq.test(x) : Chi-squared approximation may be incorrect

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 2017-01-03
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    相关资源
    最近更新 更多