简体   繁体   中英

Replicating marginal effects from logit model example in Gujarati & Porter on R

I am replicating a logit model example from Econometrics book from Gujarati and Porter (Spanish edition). I have no problems with the model estimation, but I can't replicate marginal effects. In book, regression results are the following:

回归结果在书中

In the following lines I show my regression results:

> dat <- data.frame(
+   debito = c(0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 
+              1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 
+              1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1),
+   saldo = c(1756, 748, 1501, 1831, 1622, 1886,  740, 1593, 1169, 2125, 
+             1554, 1474, 1913, 1218, 1006, 2215,  137,  167, 343, 2557, 
+             2276, 1494, 2144, 1995, 1053, 1526, 1120, 1838, 1746, 1616,
+             1958, 634, 580, 1320, 1675, 789, 1735, 1784, 1326, 2051, 1044,
+             1885, 1790, 765, 1645, 32, 1266, 890, 2204, 2409, 1338, 2076,
+             1708, 2138, 2375, 1455, 1487, 1125, 1989, 2156),
+   cajero = c(13, 9, 10, 10, 14, 17, 6, 10, 6, 18, 12, 12, 6, 10, 12, 20, 
+              7, 5, 7, 20, 15, 11, 17, 10, 8, 8, 8, 7, 11, 10, 6, 2, 4, 4,
+              6, 8, 12, 11, 16, 14, 7, 10, 11, 4, 6, 2, 11, 7, 14, 16, 14,
+              12, 13, 18, 12, 9, 8, 6, 12, 14),
+   interes = c(1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
+               0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0)
+ )
> mod <- glm(debito ~ saldo + cajero + interes,data = dat,
+            family = "binomial")
> summary(mod)

Call:
glm(formula = debito ~ saldo + cajero + interes, family = "binomial", 
    data = dat)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-1.6619  -0.9712  -0.6629   1.1440   1.7076  

Coefficients:
              Estimate Std. Error z value Pr(>|z|)  
(Intercept) -0.5749004  0.7857866  -0.732   0.4644  
saldo        0.0012481  0.0006973   1.790   0.0735 .
cajero      -0.1202251  0.0939842  -1.279   0.2008  
interes     -1.3520856  0.6809872  -1.985   0.0471 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 82.108  on 59  degrees of freedom
Residual deviance: 75.500  on 56  degrees of freedom
AIC: 83.5

Number of Fisher Scoring iterations: 4

As you can see, I obtained the same results. My problem is when I try to replicate marginal effects, which in book are the following:

书中的边际效应

I try to replicate thus results with the following method:

> coef(mod) * mean(dlogis(predict(mod, type = "link")))
  (Intercept)         saldo        cajero       interes 
-0.1262098468  0.0002740024 -0.0263934252 -0.2968279711 

As you can see, I failed. I know results in book are obtained from Stata, so I don't know the details of calculation or if there is posible to replicate the calculation in R. There exist a way?

Use the package "margins" for marginal effects.

install.packages("margins")
library("margins")

mod <- glm(debito ~ saldo + cajero + interes, data = dat, family = "binomial")

(m <- margins(mod))

Here are the outputs:

Average marginal effects
glm(formula = debito ~ saldo + cajero + interes, family = "binomial",     data = dat)

    saldo   cajero interes
 0.000274 -0.02639 -0.2968

Are your column names correct?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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