【问题标题】:How to calculate marginal effects of logit model with fixed effects by using a sample of more than 50 million observations如何使用超过 5000 万个观测值的样本计算具有固定效应的 logit 模型的边际效应
【发布时间】:2022-01-11 16:02:27
【问题描述】:

我有超过 5000 万个观测值的样本。我在 R 中估计以下模型:

model1 <- feglm(rejection~  variable1+ variable1^2 +  variable2+ variable3+ variable4 | city_fixed_effects + year_fixed_effects, family=binomial(link="logit"),  data=database)

根据模型 1 的估计,我计算边际效应:

mfx2 <- marginaleffects(model1)
summary(mfx2)

这行代码还计算了每一个减慢R的固定效应的边际效应。我只需要计算变量1、2和3的平均边际效应。如果我单独计算,使用mfx2计算边际效应

有解决这个问题的办法吗?

【问题讨论】:

    标签: r bigdata marginal-effects logits


    【解决方案1】:

    fixestmarginaleffects 软件包都是最近发布的 更改以提高互操作性。下一个官方 CRAN 发布 将能够做到这一点,但从 2021 年 12 月 8 日起,您可以使用 开发版本。安装:

    library(remotes)
    install_github("lrberge/fixest")
    install_github("vincentarelbundock/marginaleffects")
    

    我建议您先将固定效应变量转换为因子 拟合你的模型:

    library(fixest)
    library(marginaleffects)
    
    dat <- mtcars
    dat$gear <- as.factor(dat$gear)
    
    mod <- feglm(am ~ mpg + mpg^2 + hp + hp^3| gear,
                 family = binomial(link = "logit"),
                 data = dat)
    

    然后,您可以使用marginaleffectssummary 来计算平均值 边际效应:

    mfx <- marginaleffects(mod, variables = "mpg")
    summary(mfx)
    
    ## Average marginal effects 
    ##       type Term Effect Std. Error  z value Pr(>|z|)  2.5 % 97.5 %
    ## 1 response  mpg 0.3352         40 0.008381  0.99331 -78.06  78.73
    ## 
    ## Model type:  fixest 
    ## Prediction type:  response
    

    请注意,计算平均边际效应需要计算 数据集的每一行都有明显的边际效应。这个可以 当您的数据包含数百万 观察。

    相反,您可以计算特定值的边际效应 使用 newdata 参数和 typical 函数的回归器。 有关详细信息,请参阅marginaleffects 文档 那些:

    marginaleffects(mod, 
                    variables = "mpg", 
                    newdata = typical(mpg = 22, gear = 4))
    
    ##   rowid     type term     dydx std.error       hp mpg gear predicted
    ## 1     1 response  mpg 1.068844   50.7849 146.6875  22    4 0.4167502
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      • 2020-05-07
      • 2020-04-26
      • 2020-05-17
      • 1970-01-01
      • 2022-06-15
      相关资源
      最近更新 更多