Sunday, 15 March 2015

statistics - Three factor contingency table in R -



statistics - Three factor contingency table in R -

i looking build contingency table 3 main effects. these crime, gender , prior conviction. response variable whether or not lenient sentence granted.

this best have far.

offense gender priorconv yes no 1 shoplifting men n 24 1 2 other theft acts men n 52 9 3 shoplifting women n 48 3 4 other theft acts women n 22 2 5 shoplifting men p 17 6 6 other theft acts men p 60 34 7 shoplifting women p 15 6 8 other theft acts women p 4 3

which created next code

table1<-expand.grid(crime=factor(c("shoplifting","other theft acts")),gender=factor(c("men","women")), priorconv=factor(c("n","p"))) table1<-data.frame(table1,yes=c(24,52,48,22,17,60,15,4),no=c(1,9,3,2,6,34,6,3))

unfortunately not elegant , wondering if there way nowadays info more clearly.

thank you.

for contingency can utilize sample operator , set within function alter number of strings like

factory <- function(i) { offense <- sample(c("shoplifting","other theft acts"),i, replace = true) gender <- sample(c("men","women"),i,replace = true) priorconv <- sample(c("p","n"),i, replace = true) table <- data.frame(crime,gender,priorconv) return(table) } table1 <- factory(20)

result:

offense gender priorconv 1 shoplifting men n 2 shoplifting women p 3 other theft acts men p 4 shoplifting men p 5 other theft acts women n 6 shoplifting women n 7 shoplifting women p 8 shoplifting men p 9 other theft acts women p 10 shoplifting men p 11 other theft acts men n 12 other theft acts men p 13 shoplifting men p 14 shoplifting women n 15 other theft acts men n 16 other theft acts men p 17 other theft acts women p 18 shoplifting women p 19 other theft acts men n 20 shoplifting women n

r statistics

No comments:

Post a Comment