简体   繁体   English

R:分层数据的贝叶斯逻辑回归

[英]R: Bayesian Logistic Regression for Hierarchical Data

This is a repost from stats.stackexchange where I did not get a satisfactory response. 这是从stats.stackexchange转发的 ,我没有得到满意的答复。 I have two datasets, the first on schools, and the second lists students in each school who have failed in a standardized test (emphasis intentional). 我有两个数据集,第一个数据集在学校中,第二个数据集列出了每所学校中未通过标准化测试 (故意强调)的学生。 Fake datasets can be generated by (thanks to Tharen ): 可以通过以下方式生成伪造的数据集(感谢Tharen ):

#random school data for 30 schools
schools.num = 30
schools.data = data.frame(school_id=seq(1,schools.num)
                         ,tot_white=sample(100:300,schools.num,TRUE)
                         ,tot_black=sample(100:300,schools.num,TRUE)
                         ,tot_asian=sample(100:300,schools.num,TRUE)
                         ,school_rev=sample(4e6:6e6,schools.num,TRUE)
                         )

#total students in each school
schools.data$tot_students = schools.data$tot_white + schools.data$tot_black + schools.data$tot_asian
#sum of all students all schools
tot_students = sum(schools.data$tot_white, schools.data$tot_black, schools.data$tot_asian)
#generate some random failing students
fail.num = as.integer(tot_students * 0.05)

students = data.frame(student_id=sample(seq(1:tot_students), fail.num, FALSE)
                      ,school_id=sample(1:schools.num, fail.num, TRUE)
                      ,race=sample(c('white', 'black', 'asian'), fail.num, TRUE)
                      )

I am trying to estimate P(Fail=1 | Student Race, School Revenue). 我正在尝试估算P(失败= 1 |学生竞赛,学校收入)。 If I run a multinomial discrete choice model on the student dataset, I shall clearly be estimating P(Race | Fail=1). 如果我在学生数据集上运行多项式离散选择模型,则很明显我将估计P(Race | Fail = 1)。 I obviously have to estimate the inverse of this. 我显然必须估计与此相反。 Since all the pieces of information are available in the two datasets (P(Fail), P(Race), Revenue), I see no reason why this can't be done. 由于所有信息都在两个数据集中(P(失败),P(种族),收入)可用,所以我认为没有理由无法做到这一点。 But I am stumped as to actually how to implement in R. Any pointer would be much appreciated. 但是我对如何在R中实现感到困惑。任何指针将不胜感激。 Thanks. 谢谢。

It will be easier if you have a single data.frame. 如果只有一个data.frame,会更容易。

library(reshape2)
library(plyr)
d1 <- ddply(
  students, 
  c("school_id", "race"), 
  summarize,
  fail=length(student_id)
) 
d2 <- with( schools.data, data.frame( 
  school_id = school_id, 
  white = tot_white, 
  black = tot_black, 
  asian = tot_asian, 
  school_rev = school_rev 
) )
d2 <- melt(d2, 
  id.vars=c("school_id", "school_rev"), 
  variable.name="race", 
  value.name="total"
)
d <- merge( d1, d2, by=c("school_id", "race") )
d$pass <- d$total - d$fail

You can then look at the data 然后您可以查看数据

library(lattice)
xyplot( d$fail / d$total ~ school_rev | race, data=d )

Or compute anything you want. 或计算您想要的任何东西。

r <- glm(
  cbind(fail,pass) ~ race + school_rev, 
  data=d, 
  family=binomial() # Logistic regression (not bayesian)
)
summary(r)

(EDIT) If you have more information about the failed students, but only aggregated data for the passed ones, you can recreate a complete dataset as follows. (编辑)如果您有更多有关失败学生的信息,但仅是通过的学生的汇总数据,则可以按以下方式重新创建完整的数据集。

# Unique student_id for the passed students
d3 <- ddply( d, 
  c("school_id", "race"), 
  summarize, student_id=1:pass 
)
d3$student_id <- - seq_len(nrow(d3))
# All students
d3$result <- "pass"
students$result <- "fail"
d3 <- merge( # rather than rbind, in case there are more columns
  d3, students, 
  by=c("student_id", "school_id", "race", "result"), 
  all=TRUE 
)
# Students and schools in a single data.frame
d3 <- merge( d3, schools.data, by="school_id", all=TRUE )
# Check that the results did not change
r <- glm(
  (result=="fail") ~ race + school_rev, 
  data=d3, 
  family=binomial()
)
summary(r)

You'll need a dataset with information on all students. 您将需要一个包含所有学生信息的数据集。 Both failed and passed. 都失败了并通过了。

schools.num = 30
schools.data = data.frame(school_id=seq(1,schools.num)
                          ,tot_white=sample(100:300,schools.num,TRUE)
                          ,tot_black=sample(100:300,schools.num,TRUE)
                          ,tot_asian=sample(100:300,schools.num,TRUE)
                          ,school_rev=sample(4e6:6e6,schools.num,TRUE)
                          )

library(plyr)
fail_ratio <- 0.05
dataset <- ddply(schools.data, .(school_id, school_rev), function(x){
  data.frame(Fail = rbinom(sum(x$tot_white, x$tot_asian, x$tot_black), size = 1, prob = fail_ratio), Race = c(rep("white", x$tot_white), rep("asian", x$tot_asian), rep("black", x$tot_black)))
})
dataset$Race <- factor(dataset$Race)

Then you can use glmer() for the lme4 package for a frequentist approach. 然后,您可以将lmer4软件包的glmer()用于常用方法。

library(lme4)
glmer(Fail ~ school_rev + Race + (1|school_id), data = dataset, family = binomial)

Have a look at the MCMCglmm package if you need Bayesian estimates. 如果需要贝叶斯估计,请看一下MCMCglmm软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM