请向我提供有关如何在R中进行嵌套逻辑回归的详细(尽可能)步骤.我是R的新手,所以如果我能得到详细的答案,它会对我有很大的帮助.
我们测试了渔民退出渔业的决定如何受到不同社会经济因素的影响.因变量:0 - 停留; 1 - 退出预测因子:年龄(连续); 教育(分类); 儿童人数(连续)等
我们的受访者来自不同的城镇.我们的论文评论员指示我们通过使用嵌套逻辑回归来说明受访者所在的城镇.
非常感激您的帮忙.非常感谢.
Bra*_*sen 11
install.packages("mlogit")
library(mlogit)
my.data <- YOUR.DATA
nested.logit <- mlogit(stay.exit~ age + education + children , my.data,
shape='long', alt.var='town.list', nests=list(town.list))
Run Code Online (Sandbox Code Playgroud)
有关嵌套logit模型调用的示例,请参阅mlogit手册的第19页.您必须自己查看文档,以确保您在选项方面获得所需.http://cran.r-project.org/web/packages/mlogit/mlogit.pdf
Segue:在查看嵌套模型之前,我通常希望通过town.list查看所有模型:
注意:如果您的分类变量未被分解,则必须在模型公式中使用as.factor(变量)将它们包围起来
# Show a little love for plyr
library(plyr)
## RNG
set.seed(123454321)
## Create a list object to store your models
my.models <- list()
## import your data
my.data <- YOUR.DATA
## Create a loop that runs by the list of towns
for(x in 1:length(mydata$town.list) {
## subset data in each step by the town
dat <- subset(my.data, town == town.list[x])
## Save the model to it's own place in the list, identified by town
my.models[[town.list[x]]] <- glm(formula = stay.exit ~ age + education + children,
family = binomial(link = "logit"),
data=dat)
}
## View summaries for all models
llply(my.models, summary)
## Access specific models
my.models$<TOWN NAME>
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,你想要一个不同城镇的截距模型,即分层模型?如果是这样,只需使用 lme4 包。
这是一个例子。假设您的数据框中有一个名为城镇的变量(因子),并且您的数据框称为“鱼”,只需运行:
library(lme4)
library(arm) # to use the function display, much better than summary
nest.reg <- glmer(decision ~ age + education + children + (1|town), family = binomial, data = fish)
coef(nest.reg) # this will give the estimated coeficients by town (in this case, only the intercepts will vary).
fixef(nest.reg) # this will give the model averaging over all towns.
ranef(nest.reg) # the errors (specificity) at the town level. If you sum fixef with ranef you will get coef results
Run Code Online (Sandbox Code Playgroud)
最后,比较城镇内部和城镇之间的估计变化很重要
display(nest.reg) # this will show you, among other things, the estimated residual variatio at the town and individual level. It's the error terms by town and by individual (Residual). The ratio between them will tell you how much information the is between town and within each town.
Run Code Online (Sandbox Code Playgroud)
有关使用 lme4 进行多级回归的更多信息,请查看 Gelman 和 Hill 的书的最新版本。
Ps.:也可以按城镇包括不同的坡度。如果这就是您需要的,请在评论中提问。