在数据框中分离逻辑模型

时间:2016-05-21 21:56:43

标签: r logistic-regression

我在16个不同的国家/地区拥有3240个观测数据集。我想为每个国家运行16个独立的逻辑模型,因为我预计我的预测变量的影响会因国家而异。

Data <- data.frame(
X = sample(1:100),
Y = sample(c("yes", "no"), 100, replace=TRUE),     
country=sample(c("UK","USA","Denmark","Norway","Iceland","Ireland","Sweden","Italy","France","Germany","Luxembourg","Belgium","Netherlands","Spain","Portugal","Greece"),100, replace=TRUE))

如何在R?

中执行此操作

1 个答案:

答案 0 :(得分:2)

当然可以。根据您的基岩问题,其他方法可能更合适(如混合效果模型)。

以下两个例子是等效的。您可以添加肉(例如,提取系数)。

sapply(Data$country, FUN = function(ctry) {
  summary(glm(Y ~ X, data = Data, family = binomial, subset = country == ctry))
}, simplify = FALSE) # if simplify = FALSE, it is coerced to a matrix

for (ctry in Data$country) {
  print( # print has to be called explicitly inside a for loop
    summary(glm(Y ~ X, data = Data, family = binomial, subset = country == ctry))
  )
}