简体   繁体   English

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

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

Please provide me with detailed (as possible) steps on how to do nested logistic regression in R. I'm new to R so it would help me a lot if i can get a detailed answer. 请向我提供有关如何在R中进行嵌套逻辑回归的详细(尽可能)步骤。我是R的新手,所以如果我能得到详细的答案,它会对我有很大的帮助。

We tested how fisher's decision to exit the fishery is affected by different socioeconomic factors. 我们测试了渔民退出渔业的决定如何受到不同社会经济因素的影响。 Dependent variable: 0 - stay; 因变量:0 - 停留; 1 - exit Predictors: Age (continuous); 1 - 退出预测因子:年龄(连续); Education (categorical); 教育(分类); number of children (continuous), etc. 儿童人数(连续)等

Our respondents were from different towns. 我们的受访者来自不同的城镇。 A reviewer of our paper instructed us to account for the town where the respondent comes from by using nested logistic regression. 我们的论文评论员指示我们通过使用嵌套逻辑回归来说明受访者所在的城镇。

Your help is very much appreciated. 非常感激您的帮忙。 Many thanks. 非常感谢。

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

See page 19 of the mlogit manual for examples of nested logit model calls. 有关嵌套logit模型调用的示例,请参阅mlogit手册的第19页。 You'll have to review the documentation yourself to ensure you're getting at what you need in terms of options. 您必须自己查看文档,以确保您在选项方面获得所需。 http://cran.r-project.org/web/packages/mlogit/mlogit.pdf http://cran.r-project.org/web/packages/mlogit/mlogit.pdf

Segue: I usually like to have a peek at all models by town.list before I look at nested models: Segue:在查看嵌套模型之前,我通常希望通过town.list查看所有模型:

Note: if your categorical variables are not factored you'll have to surround them with as.factor(variable) in your model formula 注意:如果您的分类变量未被分解,则必须在模型公式中使用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>

If I understand correctly, you want a varying-intercept model by town, ie, a hierarchical model? 如果我理解正确,你想要一个城镇的变量拦截模型,即分层模型? If that's right, just use lme4 package. 如果这是对的,只需使用lme4包。

Here is an example. 这是一个例子。 Assuming you have in your data frame a variable (factor) called town, and that your data frame is called "fish", just run: 假设你的数据框中有一个名为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, it's important to compare the estimated within and between town variation 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. 

Take a look at the last edition of Gelman and Hill's book for more information about multilevel regression using lme4. 有关使用lme4进行多级回归的更多信息,请查看上一版的Gelman和Hill的书。

Ps.: It's possible to include varying-slope by town as well. Ps:也可以包括城镇的变坡度。 If that's what you need, just ask in the comments. 如果这就是您所需要的,请在评论中提问。

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

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