Reputation: 99
I am estimating a gravity trade model using the sampleSelection
package and having trouble calculating marginal effects. I would like to use the marginaleffects
package. The model converges with results below.
The data is available here: (I would make it smaller, but I cannot figure out how to reduce the size and keep the problem).
The model was having trouble converging so I followed the instructions to this two-step process.
selec_lm_1206_nums <-
bin_1206 ~ pct_lag_1_yr_of_ukr_sunflowers + log(total_world_exports_1206) + member_wto_d + agree_pta_goods + covid_year + deaths + events + deaths * agree_pta_goods + events * agree_pta_goods
outcom_lm_1206_nums <-
log(netweight_kg_1206) ~ pct_lag_1_yr_of_ukr_sunflowers + log(total_world_exports_1206) + member_wto_d + agree_pta_goods + covid_year + deaths + events + deaths * agree_pta_goods + events * agree_pta_goods + month_february + month_march + month_april + month_may + month_june + month_july + month_august + month_september + month_october + month_november + month_december + partner_armenia + partner_australia + partner_austria + partner_azerbaijan + partner_bangladesh + partner_belarus + partner_belgium + partner_bulgaria + partner_china + partner_czech_rep + partner_denmark + partner_egypt + partner_estonia + partner_finland + partner_france + partner_georgia + partner_germany + partner_hungary + partner_india + partner_iraq + partner_italy + partner_lebanon + partner_morocco + partner_netherlands + partner_pakistan + partner_poland + partner_portugal + partner_rep_of_moldova + partner_romania + partner_russian_federation + partner_spain + partner_sweden + partner_switzerland + partner_turkey + partner_united_kingdom
selec_1206_nums_2S <- selection(selection = selec_lm_1206_nums, outcome = outcom_lm_1206_nums, data = ukr_exports_analysis_sunflowers, method = "ml",maxMethod="SANN", parscale = 0.001)
selec_1206_nums <- selection(selection = selec_lm_1206_nums, outcome = outcom_lm_1206_nums, data = ukr_exports_analysis_sunflowers, method = "ml", start = coef(selec_1206_nums_2S))
summary(selec_1206_nums)
> summary(selec_1206_nums)
--------------------------------------------
Tobit 2 model (sample selection model)
Maximum Likelihood estimation
Newton-Raphson maximisation, 8 iterations
Return code 2: successive function values within tolerance limit (tol)
Log-Likelihood: -6585.88
4194 observations (2297 censored and 1897 observed)
68 free parameters (df = 4126)
Probit selection equation:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.879e+01 4.317e+00 4.352 1.38e-05 ***
pct_lag_1_yr_of_ukr_sunflowers 2.864e-02 2.618e-03 10.938 < 2e-16 ***
log(total_world_exports_1206) -8.678e-01 1.955e-01 -4.438 9.31e-06 ***
member_wto_d1 -1.166e-01 5.973e-02 -1.952 0.0510 .
agree_pta_goods1 5.668e-01 6.893e-02 8.223 2.63e-16 ***
covid_year1 1.320e-01 7.484e-02 1.764 0.0779 .
deaths 4.391e-05 2.100e-04 0.209 0.8344
events -3.939e-03 3.351e-03 -1.175 0.2399
agree_pta_goods1:deaths 2.469e-04 3.932e-04 0.628 0.5301
agree_pta_goods1:events 1.666e-03 5.520e-03 0.302 0.7627
Outcome equation:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 33.9286921 10.5381573 3.220 0.001294 **
pct_lag_1_yr_of_ukr_sunflowers 0.0289553 0.0081910 3.535 0.000412 ***
log(total_world_exports_1206) -1.0689663 0.4786482 -2.233 0.025582 *
member_wto_d1 -1.5041634 0.4674271 -3.218 0.001301 **
agree_pta_goods1 0.5015109 0.2095119 2.394 0.016723 *
covid_year1 0.2456088 0.1737226 1.414 0.157496
deaths 0.0013708 0.0005869 2.336 0.019560 *
events -0.0376453 0.0088565 -4.251 2.18e-05 ***
month_february1 -0.0758067 0.1989450 -0.381 0.703191
month_march1 -0.1663013 0.1959581 -0.849 0.396121
month_april1 0.1921004 0.2014316 0.954 0.340304
month_may1 -0.0005563 0.2059650 -0.003 0.997845
month_june1 -0.1007850 0.2146457 -0.470 0.638708
month_july1 -0.1424848 0.2174332 -0.655 0.512309
month_august1 -0.6256302 0.2279437 -2.745 0.006083 **
month_september1 0.0350345 0.2076408 0.169 0.866020
month_october1 0.4296219 0.1991467 2.157 0.031039 *
month_november1 0.4897676 0.2045072 2.395 0.016671 *
month_december1 -0.0395080 0.1948822 -0.203 0.839358
partner_armenia1 0.6667811 0.4272304 1.561 0.118670
partner_australia1 2.0320576 1.0742590 1.892 0.058616 .
partner_austria1 -4.4739967 0.3911026 -11.439 < 2e-16 ***
partner_azerbaijan1 0.0777747 0.4583665 0.170 0.865272
partner_bangladesh1 3.3733063 1.2954631 2.604 0.009249 **
partner_belarus1 -2.0996295 0.4959205 -4.234 2.35e-05 ***
partner_belgium1 2.2120262 0.4485863 4.931 8.50e-07 ***
partner_bulgaria1 1.7822446 0.3461572 5.149 2.75e-07 ***
partner_china1 1.1775708 0.4552847 2.586 0.009731 **
partner_czech_rep1 0.4901475 0.4240523 1.156 0.247803
partner_denmark1 0.3459561 0.3971738 0.871 0.383781
partner_egypt1 1.2197630 0.3712942 3.285 0.001028 **
partner_estonia1 0.1315394 0.3839395 0.343 0.731913
partner_finland1 1.7183312 0.3989766 4.307 1.69e-05 ***
partner_france1 -5.1335809 0.3603393 -14.247 < 2e-16 ***
partner_georgia1 1.2181676 0.3618550 3.366 0.000768 ***
partner_germany1 2.1981464 0.3376699 6.510 8.43e-11 ***
partner_hungary1 -0.7684804 0.4972521 -1.545 0.122313
partner_india1 1.4954587 0.3779445 3.957 7.72e-05 ***
partner_iraq1 0.0907309 0.4449478 0.204 0.838431
partner_italy1 0.4845864 0.3986407 1.216 0.224208
partner_lebanon1 -0.2231801 0.4473566 -0.499 0.617886
partner_morocco1 2.9440561 0.8725557 3.374 0.000748 ***
partner_netherlands1 1.6506210 0.3821066 4.320 1.60e-05 ***
partner_pakistan1 2.5107821 0.7899278 3.178 0.001491 **
partner_poland1 1.6749368 0.3356626 4.990 6.29e-07 ***
partner_portugal1 1.1662223 0.5492737 2.123 0.033796 *
partner_rep_of_moldova1 0.8918645 0.3736330 2.387 0.017031 *
partner_romania1 1.4345824 0.3438745 4.172 3.08e-05 ***
partner_russian_federation1 2.2659206 0.4644291 4.879 1.11e-06 ***
partner_spain1 0.2770208 0.3671673 0.754 0.450603
partner_sweden1 0.8975827 0.4214778 2.130 0.033263 *
partner_switzerland1 1.9644726 0.7822877 2.511 0.012070 *
partner_turkey1 2.8141320 0.4212573 6.680 2.70e-11 ***
partner_united_kingdom1 -0.0482882 0.4040543 -0.120 0.904878
agree_pta_goods1:deaths -0.0001057 0.0009334 -0.113 0.909866
agree_pta_goods1:events 0.0251029 0.0133068 1.886 0.059301 .
Error terms:
Estimate Std. Error t value Pr(>|t|)
sigma 2.17716 0.08559 25.44 <2e-16 ***
rho 0.65668 0.05969 11.00 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
--------------------------------------------
When I attempt to find the marginal effects:
> mfx_s <- marginaleffects(selec_1206_trends)
Error: Unable to extract the data from model of class `selection`. This can happen in a variety of cases, such as when a `marginaleffects` package function is called from inside a user-defined function. Please supply a data frame explicitly via the `newdata` argument.
> mfx_s <- marginaleffects(selec_1206_trends, newdata = datagrid())
Error: object of type 'symbol' is not subsettable
In addition: Warning message:
Could not get model data.
> mfx_s <- marginaleffects(selec_1206_trends, newdata = ukr_exports_analysis_sunflowers)
Error: object of type 'symbol' is not subsettable
> marginaleffects(selec_1206_nums, newdata = datagrid(newdata = ukr_exports_analysis_sunflowers))
Error: object of type 'symbol' is not subsettable
I am not certain what data to give it. I have seen means and medians and using the original data. I have tried giving it the original data in a couple of ways shown above. Really interestingly, if I give the marginal effects function the initial guess the function says the selection models are not supported, but the documentation says they are.
> marginaleffects(selec_1206_nums_2S, newdata = datagrid(newdata = ukr_exports_analysis_sunflowers))
Error: Models of class "selection" are not supported.
I would like to generate a conditional marginal effects plot. Any ideas?
Upvotes: 0
Views: 389
Reputation: 17725
The original poster shared data and code with me, and it helped me identify a bug which is now fixed. In principle, installing the development versions of the insight
and marginaleffects
packages should solve this problem:
library(remotes)
install_github("easystats/insight")
install_github("vincentarelbundock/marginaleffects")
Make sure you restart R
completely. Then,
library("sampleSelection")
library("marginaleffects")
data("Mroz87", package = "sampleSelection")
Mroz87$kids <- (Mroz87$kids5 + Mroz87$kids618 > 0)
dat <- Mroz87
mod <- selection(lfp ~ age + I( age^2 ) + faminc + kids + educ,
wage ~ exper + I( exper^2 ) + educ + city,
data = dat)
mfx <- marginaleffects(mod)
summary(mfx)
#> Average marginal effects
#> Term Contrast Effect Std. Error z value Pr(>|z|) 2.5 %
#> 1 age dY/dX -7.759e-03 2.901e-03 -2.675 0.0074752 -1.344e-02
#> 2 faminc dY/dX 2.120e-06 7.637e-07 2.775 0.0055131 6.228e-07
#> 3 kids TRUE - FALSE -1.622e-01 9.040e-02 -1.794 0.0728393 -3.393e-01
#> 4 educ dY/dX 3.556e-02 2.135e-02 1.666 0.0958117 -6.287e-03
#> 97.5 %
#> 1 -2.074e-03
#> 2 3.617e-06
#> 3 1.502e-02
#> 4 7.740e-02
#>
#> Model type: selection
#> Prediction type: response
Upvotes: 1