繁体   English   中英

关于如何在R中运行嵌套逻辑回归的一步一步的过程

[英]Step by step procedure on how to run nested logistic regression in R

请向我提供有关如何在R中进行嵌套逻辑回归的详细(尽可能)步骤。我是R的新手,所以如果我能得到详细的答案,它会对我有很大的帮助。

我们测试了渔民退出渔业的决定如何受到不同社会经济因素的影响。 因变量:0 - 停留; 1 - 退出预测因子:年龄(连续); 教育(分类); 儿童人数(连续)等

我们的受访者来自不同的城镇。 我们的论文评论员指示我们通过使用嵌套逻辑回归来说明受访者所在的城镇。

非常感激您的帮忙。 非常感谢。

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))

有关嵌套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>

如果我理解正确,你想要一个城镇的变量拦截模型,即分层模型? 如果这是对的,只需使用lme4包。

这是一个例子。 假设你的数据框中有一个名为town的变量(factor),并且你的数据框被称为“fish”,只需运行:

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

Finnaly,重要的是比较城镇内部和城镇之间的差异变化

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. 

有关使用lme4进行多级回归的更多信息,请查看上一版的Gelman和Hill的书。

Ps:也可以包括城镇的变坡度。 如果这就是您所需要的,请在评论中提问。

暂无
暂无

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

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