문제

This is easy at first glance, but I didn't know how to compute it when I started to work on it. The question is to calculate the odds ratio of pass comparing female with male in each school, and the data is constructed like this:

set.seed(1000)
female = sample(c(0,1),size=20,replace=TRUE)
school = factor(sample(c(0:2),size=20,replace=TRUE),
            labels=c("A school","B school","C school"))
school = sort(school)
pass = sample(c(0,1),size=20,replace=TRUE)
data = data.frame(female,school,pass)

Thank you very much!

도움이 되었습니까?

해결책

You can compute this using split-apply-combine, using the split function to break down your data by school, using lapply to run a function that computes the odds ratio, and using unlist to combine the results:

unlist(lapply(split(data, data$school), function(x) {
  (sum(x$female == 1 & x$pass == 1) / sum(x$female == 1)) /
  (sum(x$female == 0 & x$pass == 1) / sum(x$female == 0))
}))
#  A school  B school  C school 
# 1.5000000 2.0000000 0.6666667 

What I've computed here is actually a risk ratio, since for your dataset the odds ratios are all either infinite or 0.

다른 팁

table and prop.table give what you want for each school. by will call the function on each school in the data frame. As a fan of package functional, I will use that to create the function:

> require(functional)
> by(data[c('female', 'pass')], data$school,
       Compose(table, Curry(prop.table, margin=1))
    )
data$school: A school
      pass
female         0         1
     0 0.3333333 0.6666667
     1 0.0000000 1.0000000
------------------------------------------------------------------------------------------------ 
data$school: B school
      pass
female   0   1
     0 0.5 0.5
     1 0.0 1.0
------------------------------------------------------------------------------------------------ 
data$school: C school
      pass
female         0         1
     0 0.0000000 1.0000000
     1 0.3333333 0.6666667

Note that Compose(table, Curry(prop.table, margin=1)) is equivalent to function(x) prop.table(table(x), margin=1)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top