Kirstin Kuchler
Kirstin Kuchler

Reputation: 9

Chi-square from tables separated in output

I tried to create a table with three variables. Now I want to run a Chi-square on both of the outputs. How do I run a Chi-square on the output , , = 1 and again on output , , = 2?

> emphasis<-table(Pilot$emphasis.GI, Pilot$emphasis.race, Pilot$required.learning)
> emphasis
, ,  = 1

   
     2  3  4
  2 11  5  0
  3  2  8  0
  4  0  0  0

, ,  = 2

   
     2  3  4
  2  0  0  0
  3  0  2  0
  4  0  0  1

Upvotes: 0

Views: 45

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 102770

You can try asplit over the third dimension and run chisq.test with Map

Map(chisq.test,asplit(emphasis, 3))

Upvotes: 1

akrun
akrun

Reputation: 887951

It is a 3D array. We can use apply with MARGIN = 3 and apply the test

apply(emphasis, 3, chisq.test)

Or use a for loop

out <- vector('list', dim(emphasis)[3])
for(i in seq_along(out)) out[[i]] <- chisq.test(emphasis[,, i])

Upvotes: 3

Related Questions