gpt4 book ai didi

r - 使用 R 的加权调查数据中 logit 的边际效应

转载 作者:行者123 更新时间:2023-12-03 15:27:23 31 4
gpt4 key购买 nike

我正在尝试估计 logit 模型的边际效应,其中我有几个二分解释变量。

假设模型估计为

logit<- svyglm ( if_member ~ if_female + dummy_agegroup_2 + dummy_agegroup_3 + dummy_education_2 + dummy_education_3 + dummy_education_4, family = quasibinomial(link = "logit"), design = survey_design)

我知道 margin 调查包中的功能,但我对它不是很熟悉。我在模型中只有二分变量,所以我想知道如何通过这个函数估计边际效应,特别是我不确定 预测 (给出模型中要预测的变量值的数据框)。

最佳答案

您在找边缘效果或边缘 预测 ?

顾名思义,marginpred()函数返回 预测 . predictat 的参数是一个包含控制变量和模型中变量的数据框。让我强调一下:控制变量应该被排除在模型之外。

library("survey")

odds2prob <- function(x) x / (x + 1)
prob2odds <- function(x) x / (1 - x)
expit <- function(x) odds2prob(exp(x))
logit <- function(x) log(prob2odds(x))

set.seed(1)

survey_data <- data.frame(
if_female = rbinom(n = 100, size = 1, prob = 0.5),
agegroup = factor(sample(x = 1:3, size = 100, replace = TRUE)),
education = NA_integer_,
if_member = NA_integer_)
survey_data["agegroup"] <- relevel(survey_data$agegroup, ref = 3)
# Different probabilities between female and male persons
survey_data[survey_data$if_female == 0, "education"] <- sample(
x = 1:4,
size = sum(survey_data$if_female == 0),
replace = TRUE,
prob = c(0.1, 0.1, 0.5, 0.3))
survey_data[survey_data$if_female == 1, "education"] <-sample(
x = 1:4,
size = sum(survey_data$if_female == 1),
replace = TRUE,
prob = c(0.1, 0.1, 0.3, 0.5))
survey_data["if_member"] <- rbinom(n = 100, size = 1, prob =
expit((survey_data$education - 3)/2))
survey_data["education"] <- factor(survey_data$education)
survey_data["education"] <- relevel(survey_data$education, ref = 3)
survey_design <- svydesign(ids = ~ 1, data = survey_data)

logit <- svyglm(if_member ~ if_female + agegroup + education,
family = quasibinomial(link = "logit"),
design = survey_design)
exp(cbind(`odds ratio` = coef(logit), confint(logit)))
newdf <- data.frame(if_female = 0:1, education = c(3, 3), agegroup = = c(3, 3))
# Fails
mp <- marginpred(model = logit, adjustfor = ~ agegroup + education,
predictat = newdf, se = TRUE, type = "response")
logit2 <- svyglm(if_member ~ if_female,
family = quasibinomial(link = "logit"),
design = survey_design)
mp <- marginpred(model = logit2, adjustfor = ~ agegroup + education,
predictat = newdf, se = TRUE, type = "response")
# Probability for male and for female persons controlling for agegroup and education
cbind(prob = mp, confint(mp))

这就是我如何估计边际 效果survey包裹:

# Probability difference between female and male persons
# when agegroup and education are set to 3
svycontrast(full_model, quote(
(exp(`(Intercept)` + if_female) / (exp(`(Intercept)` + if_female) + 1)) -
(exp(`(Intercept)`) / (exp(`(Intercept)`) + 1))))
# Can't use custom functions like expit :_(

可能有更聪明的方法,但我希望它有所帮助。

请注意 marginpred() 预测的概率之间的差异与 svycontrast() 估计的差异不同. marginpred() 预测的概率似乎不受更改控制变量值的影响(例如, education = c(4, 4)而不是 education = c(3, 3) ),但估计来自 svycontrast()受到回归模型暗示的影响。

关于r - 使用 R 的加权调查数据中 logit 的边际效应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38492706/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com