[英]Stratified svyglm in R
I'm looking for advice on how to conduct a weighted logistic regression analysis, stratified by gender, in R.我正在寻找有关如何在 R 中进行按性别分层的加权逻辑回归分析的建议。
For my main, unstratified analysis, I generated inverse probability weights (invp) and ran a weighted logistic regression as follows:对于我的主要非分层分析,我生成了逆概率权重 (invp) 并运行了如下加权逻辑回归:
complete_cases_weighted <- svydesign(id=~1, weights=~invp, data=complete_cases)
svyglm(outcome ~ exposure, design=complete_cases_weighted, family="binomial")
I now want to essentially re-run this analysis twice, once for females only, and once for males only.我现在想基本上重新运行这个分析两次,一次只针对女性,一次只针对男性。
What's the best way of doing this with correct weighting?正确加权的最佳方法是什么? Do I re-do the entire process of generating weights and running a weighted logistic regression having subset my data into males and females?
我是否重新执行生成权重和运行加权逻辑回归的整个过程,将我的数据子集为男性和女性? Or is there some fancy footwork I can do with
svydesign
/ svyglm
to do this?或者我可以用
svydesign
/ svyglm
做一些花哨的步法来做到这一点吗?
Since strata are independent data layers, your approach seems to be correct.由于层是独立的数据层,您的方法似乎是正确的。 You build
svydesign
separately for men and separately for women.您分别为男性和女性分别构建
svydesign
。 Then perform svyglm
separately for them.然后分别为他们执行
svyglm
。 If I understood correctly, then see the example.如果我理解正确,请参阅示例。 The weight coefficients are calculated separately for each stratum.
每个层的权重系数是单独计算的。
library(tidyverse)
library(survey)
library(broom)
df <- mtcars %>%
mutate(weight = ifelse(vs == 1, 3, 4))
df %>%
group_nest(vs) %>%
mutate(design = map(data, ~ svydesign(
ids = ~ 1,
weights = .x$weight,
data = .x
)),
model = map(design, ~ svyglm(disp ~ hp, design = .x) %>% glance)) %>%
unnest(model)
#> # A tibble: 2 x 9
#> vs data design null.deviance df.null AIC BIC deviance df.residual
#> <dbl> <list<ti> <list> <dbl> <int> <dbl> <dbl> <dbl> <dbl>
#> 1 0 [18 x 11] <srvy~ 193780. 17 3.01 1.31e5 131261. 16
#> 2 1 [14 x 11] <srvy~ 42079. 13 1.51 2.68e4 26791. 12
Created on 2021-07-29 by the reprex package (v2.0.0)由reprex 包( v2.0.0 ) 于 2021 年 7 月 29 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.