开发者

using chisq.test in R (chi-squared tests)

I am trying to read a csv file and then creating 3 matrices out of each row from the csv file and then apply chi-squared test using the method chisq.test(matrix), but somehow this methods seems to fail.

It gives me the following error:

Error in sum(x) :开发者_如何学JAVA invalid 'type' (list) of argument

On the other hand, if I simply create a matrix passing some numbers then it works fine. I also tried running str on two types of matrices.

  1. That I create using the row, from the csv file. str on that gives:

    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. Matrix created using some numbers. str on that gives:

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

Can someone please tell me what is going on here?


The problems is that your data structure is an array of lists, and for chisq.test() you need an array of numeric values.

One solution is to coerce your data into numeric, using as.numeric(). I demonstrate this below. Another solution would be to convert the results of your read.csv() into numeric first before you create the array.

# 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜