0

am trying to loop through columns and compute a chi square test of the following data set using the code below

result=numeric(length(client-1))
for (i in 1:length(client-1)) {
result=chisq.test(table(client[[i]], client[[i+1]]))
}

a sample of my data set is here

       sex                mar_st                                    education
1 Female         c) Cohabiting           e) Secondary form 3 to form 4
2 Female a) Married monogamous                       c) Primary 5 to 8
3 Female         c) Cohabiting                       c) Primary 5 to 8
4   Male a) Married monogamous         a) None (never attended school)
5   Male a) Married monogamous           e) Secondary form 3 to form 4
6 Female             d) Single f) Higher/tertiary (college/university)
7   Male             d) Single f) Higher/tertiary (college/university)
8 Female a) Married monogamous           e) Secondary form 3 to form 4
                work                   Q6
1  d) Skilled manual                     
2      h) Unemployed                     
3  d) Skilled manual                     
4     g) Agriculture                     
5 i) Other (specify) Business man        
6      h) Unemployed                     
7      h) Unemployed                     
8      h) Unemployed  

I get the following error/warning...

 #Error in .subset2(x, i, exact = exact) : subscript out of bounds
 #In addition: Warning messages:
 #1: In Ops.factor(left, right) : - not meaningful for factors

can someone help

Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
jonestats
  • 349
  • 1
  • 3
  • 10
  • What is the question?! – Simon O'Hanlon Oct 28 '13 at 08:33
  • Am not able to get a list of the chi square values from the loop and am also getting many warnings and an error – jonestats Oct 28 '13 at 08:35
  • And what are those error messages/warnings? Come on, it's on you to provide a complete question. Don't make your readers do extra work! – Simon O'Hanlon Oct 28 '13 at 08:36
  • Please help us help you by providing us with a reproducible example (i.e. code and example data), see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for details. – Paul Hiemstra Oct 28 '13 at 08:37
  • I should imagine that for starters you *probably* meant `( length(client) - 1 )`. The outer parentheses are needed due to the precedence of the `:` operator over `-`, but it is unclear if `client` is a vector or a `data.frame`. If it's a `df` you will get the number of columns. `i + 1` will refer to a column not in your data at present (`client - 1` will attempt to substract 1 from every value in your `dataframe`, hence the warning. – Simon O'Hanlon Oct 28 '13 at 08:39

1 Answers1

1

It seems to me that this is what you want to do:

result=numeric(length(client)-1)
for (i in 1:(length(client)-1)) {
  chi <- chisq.test(table(client[[i]], client[[i+1]]))
  result[i] <- chi[["statistic"]]
}
result
shadow
  • 21,823
  • 4
  • 63
  • 77