计算 AME、MEM、MER 的最简单方法?
Simplest way to calculate AME, MEM, MER?
计算logit模型的平均边际效应、平均边际效应和代表值边际效应的最简单方法是什么?
我找到了这个例子,但是解释很乱,坦率地说我不明白:
https://cran.r-project.org/web/packages/margins/vignettes/Introduction.html
我正在使用 STATA 数据集 ANES.dta,其中包含 2000 年美国总统大选的信息。这是数据集的内容:
dat <- structure(list(age = c(49, 63, 40, 47, 26, 48, 41, 18, 31, 22
), gender = c(1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L), race = c(1L,
1L, 1L, 1L, 2L, 1L, 1L, 1L, 5L, 1L), education = c(3L, 3L, 3L,
4L, 4L, 2L, 2L, 2L, 3L, 3L), income = c(4L, 3L, 3L, 3L, 4L, 3L,
3L, 3L, 4L, 3L), attendance = c(2L, 5L, 5L, 5L, 4L, 5L, 4L, 1L,
0L, 3L), lib_con = c(59, 49, 94, 24, 29, 19, 39, 49, 79, 49),
pro_choice = c(2L, 4L, 3L, 4L, 4L, 2L, 4L, 1L, 1L, 4L), vote = c(1,
0, 1, 0, 0, 0, 0, 1, 1, 0), black = c(0, 0, 0, 0, 1, 0, 0,
0, 0, 0)),row.names = c(NA, -10L), class = c("data.frame"))
这是数据集的头部:
age gender race education income attendance lib_con pro_choice vote black
1 49 1 1 3 4 2 59 2 1 0
4 63 1 1 3 3 5 49 4 0 0
5 40 2 1 3 3 5 94 3 1 0
8 47 2 1 4 3 5 24 4 0 0
9 26 2 2 4 4 4 29 4 0 1
10 48 2 1 2 3 5 19 2 0 0
这是我的模型的代码:
rm(list=ls())
library(foreign)
dat <- read.dta("ANES.dta", convert.factors = FALSE)
dat_clear <- na.omit(dat)
head(dat_clear)
m1_logit <- glm(vote ~ gender + income + pro_choice ,
data = dat_clear, family = binomial(link = "logit") ,
na.action = na.omit)
summary(m1_logit)
你可以用margins::margins
计算平均边际效应:
library(margins)
margins(m1_logit)
#Average marginal effects
#glm(formula = vote ~ gender + income + pro_choice, family = #binomial(link = "logit"), data = dat, na.action = na.omit)
#
# gender income pro_choice
# -0.08049 0.08049 -0.1607
然后您可以使用 marginal_effects
:
计算对数据或任意值的边际效应
marginal_effects(m1_logit)
# dydx_gender dydx_income dydx_pro_choice
#1 -0.07200314 0.07200315 -0.14380458
#2 -0.08333712 0.08333712 -0.16644077
#3 -0.15510887 0.15510887 -0.30978309
#4 -0.03829122 0.03829123 -0.07647519
#5 -0.08333712 0.08333712 -0.16644077
#6 -0.21028256 0.21028254 -0.41997574
#7 -0.03829122 0.03829123 -0.07647519
#8 -0.07215379 0.07215380 -0.14410546
#9 -0.01377665 0.01377665 -0.02751471
#10 -0.03829122 0.03829123 -0.07647519
计算logit模型的平均边际效应、平均边际效应和代表值边际效应的最简单方法是什么?
我找到了这个例子,但是解释很乱,坦率地说我不明白: https://cran.r-project.org/web/packages/margins/vignettes/Introduction.html
我正在使用 STATA 数据集 ANES.dta,其中包含 2000 年美国总统大选的信息。这是数据集的内容:
dat <- structure(list(age = c(49, 63, 40, 47, 26, 48, 41, 18, 31, 22
), gender = c(1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L), race = c(1L,
1L, 1L, 1L, 2L, 1L, 1L, 1L, 5L, 1L), education = c(3L, 3L, 3L,
4L, 4L, 2L, 2L, 2L, 3L, 3L), income = c(4L, 3L, 3L, 3L, 4L, 3L,
3L, 3L, 4L, 3L), attendance = c(2L, 5L, 5L, 5L, 4L, 5L, 4L, 1L,
0L, 3L), lib_con = c(59, 49, 94, 24, 29, 19, 39, 49, 79, 49),
pro_choice = c(2L, 4L, 3L, 4L, 4L, 2L, 4L, 1L, 1L, 4L), vote = c(1,
0, 1, 0, 0, 0, 0, 1, 1, 0), black = c(0, 0, 0, 0, 1, 0, 0,
0, 0, 0)),row.names = c(NA, -10L), class = c("data.frame"))
这是数据集的头部:
age gender race education income attendance lib_con pro_choice vote black
1 49 1 1 3 4 2 59 2 1 0
4 63 1 1 3 3 5 49 4 0 0
5 40 2 1 3 3 5 94 3 1 0
8 47 2 1 4 3 5 24 4 0 0
9 26 2 2 4 4 4 29 4 0 1
10 48 2 1 2 3 5 19 2 0 0
这是我的模型的代码:
rm(list=ls())
library(foreign)
dat <- read.dta("ANES.dta", convert.factors = FALSE)
dat_clear <- na.omit(dat)
head(dat_clear)
m1_logit <- glm(vote ~ gender + income + pro_choice ,
data = dat_clear, family = binomial(link = "logit") ,
na.action = na.omit)
summary(m1_logit)
你可以用margins::margins
计算平均边际效应:
library(margins)
margins(m1_logit)
#Average marginal effects
#glm(formula = vote ~ gender + income + pro_choice, family = #binomial(link = "logit"), data = dat, na.action = na.omit)
#
# gender income pro_choice
# -0.08049 0.08049 -0.1607
然后您可以使用 marginal_effects
:
marginal_effects(m1_logit)
# dydx_gender dydx_income dydx_pro_choice
#1 -0.07200314 0.07200315 -0.14380458
#2 -0.08333712 0.08333712 -0.16644077
#3 -0.15510887 0.15510887 -0.30978309
#4 -0.03829122 0.03829123 -0.07647519
#5 -0.08333712 0.08333712 -0.16644077
#6 -0.21028256 0.21028254 -0.41997574
#7 -0.03829122 0.03829123 -0.07647519
#8 -0.07215379 0.07215380 -0.14410546
#9 -0.01377665 0.01377665 -0.02751471
#10 -0.03829122 0.03829123 -0.07647519