我正在尝试重现R中二项式glm
的结果。
考虑来自此处的数据http://www.ats.ucla.edu/stat/r/dae/logit.htm
mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
我可以使用以下方式拟合模型:
model <- glm(admit ~ gre + gpa + rank, data = mydata, family = "binomial")
并且,仅使用对象重现模型:
model_r <- glm(as.numeric(model$y)~0+model.matrix(model), family = binomial)
cbind(coef(model), coef(model_r))
## [,1] [,2]
## (Intercept) -3.44954840 -3.44954840
## gre 0.00229396 0.00229396
## gpa 0.77701357 0.77701357
## rank -0.56003139 -0.56003139
现在假设专栏admit
是该专栏中n
篇论文中成功的数量:
mydata$n <- 1 + rbinom(n = 400, size = 2, prob = 0.5)
现在我必须使用以下方式拟合模型:
model <- glm(cbind(admit, n-admit) ~ gre + gpa + rank, data = mydata,
family = "binomial")
如何使用模型对象重现此模型?我问这个是因为R只保留model$y
中的成功率。
答案 0 :(得分:6)
您可以通过以下方式重现模型:
model_r <- glm(formula(model), model$data, family = family(model))
比较:
cbind(coef(model), coef(model_r))
# [,1] [,2]
# (Intercept) -3.693688931 -3.693688931
# gre 0.001855502 0.001855502
# gpa 0.584915067 0.584915067
# rank -0.450051862 -0.450051862
或者(类似于您的方法):
model_r2 <- glm(model.frame(model)[[1]] ~ 0 + model.matrix(model),
family = family(model))
cbind(coef(model), coef(model_r2))
# [,1] [,2]
# (Intercept) -3.693688931 -3.693688931
# gre 0.001855502 0.001855502
# gpa 0.584915067 0.584915067
# rank -0.450051862 -0.450051862