vincentarelbundock / marginaleffects

R package to compute and plot predictions, slopes, marginal means, and comparisons (contrasts, risk ratios, odds, etc.) for over 100 classes of statistical and ML models. Conduct linear and non-linear hypothesis tests, or equivalence tests. Calculate uncertainty estimates using the delta method, bootstrapping, or simulation-based inference
https://marginaleffects.com
Other
433 stars 45 forks source link

Support: `oohbchoice` #852

Closed donaldkip77 closed 1 year ago

donaldkip77 commented 1 year ago

How can you support Models of class "data.frame"

library(DCchoice)
library(marginaleffects)
library(margins)
data(oohbsyn)
oohb1 <- oohbchoice(R1 + R2 ~ 1 | log(BL) + log(BH), data = oohbsyn)
oohb2 <- oohbchoice(R1 + R2 ~ age + gender | log(BL) + log(BH), data = oohbsyn)
summary(oohb2)
marginaleffects(oohbsyn)
Models of class "data.frame" are not supported. Supported
  model classes include:

  afex_aov, betareg, bglmerMod, bife, bigglm, biglm, blmerMod,
  bracl, brglmFit, brmsfit, brnb, clm, clogit, coxph, crch,
  fixest, gam, Gam, gamlss, geeglm, glimML, glm, glmerMod,
  glmmPQL, glmmTMB, glmrob, glmx, gls, hetprob, hurdle, hxlr,
  iv_robust, ivpml, ivreg, lm, lm_robust, lme, lmerMod,
  lmerModLmerTest, lmrob, lmRob, loess, lrm, mblogit, mclogit,
  MCMCglmm, mhurdle, mira, mlogit, multinom, negbin, ols, orm,
  phyloglm, phylolm, plm, polr, Rchoice, rlmerMod, rq, scam,
  selection, speedglm, speedlm, stanreg, tobit, tobit1,
  truncreg, zeroinfl

  New modeling packages can usually be supported by
  `marginaleffects` if they include a working `predict()`
  method. If you believe that this is the case, please file a
  feature request on Github:
  https://github.com/vincentarelbundock/marginaleffects/issues
vincentarelbundock commented 1 year ago

I think I was able to add support for this model. However, I don’t know this package at all, and I was not able to find a benchmark to compare the results with. I would appreciate if you could check if the results are correct.

Install the development version of the package:

remotes::install_github("vincentarelbundock/marginaleffects")

Restart R completely, then:

library(DCchoice)
library(marginaleffects)
data(oohbsyn)
mod <- oohbchoice(R1 + R2 ~ age + gender | log(BL) + log(BH), data = oohbsyn)

avg_slopes(mod)
# 
#    Term      Contrast  Estimate Std. Error       z Pr(>|z|)    S    2.5 %   97.5 %
#  age    dY/dX         -0.000706    0.00405  -0.174    0.862  0.2 -0.00865  0.00724
#  gender male - female  0.035993    0.08024   0.449    0.654  0.6 -0.12128  0.19326
#  BL     dY/dX         -0.090310    0.00832 -10.848   <0.001 88.7 -0.10663 -0.07399
#  BH     dY/dX          0.000000         NA      NA       NA   NA       NA       NA
# 
# Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high

avg_slopes(mod, type = "utility")
# 
#    Term      Contrast Estimate Std. Error      z Pr(>|z|)    S  2.5 % 97.5 %
#  age    dY/dX         -0.00399      0.023 -0.174    0.862  0.2 -0.049  0.041
#  gender male - female  0.20337      0.454  0.448    0.654  0.6 -0.686  1.093
#  BL     dY/dX         -0.66793      0.137 -4.888   <0.001 19.9 -0.936 -0.400
#  BH     dY/dX          0.00000         NA     NA       NA   NA     NA     NA
# 
# Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high

predictions(mod, by = "gender")
# 
#  gender Estimate Std. Error    z Pr(>|z|)     S 2.5 % 97.5 %
#  female    0.625     0.0588 10.6   <0.001  85.3 0.510  0.740
#  male      0.653     0.0564 11.6   <0.001 100.5 0.543  0.764
# 
# Columns: gender, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
donaldkip77 commented 1 year ago

Thank you so much for the feedback. I tried to run the command but it couldn't even after restarting R several times.

Error: Models of class "oohbchoice" are not supported. Supported model classes include:

afex_aov, betareg, bglmerMod, bife, bigglm, biglm, blmerMod, bracl, brglmFit, brmsfit, brnb, clm, clogit, coxph, crch, fixest, gam, Gam, gamlss, geeglm, glimML, glm, glmerMod, glmmPQL, glmmTMB, glmrob, glmx, gls, hetprob, hurdle, hxlr, iv_robust, ivpml, ivreg, lm, lm_robust, lme, lmerMod, lmerModLmerTest, lmrob, lmRob, loess, lrm, mblogit, mclogit, MCMCglmm, mhurdle, mira, mlogit, multinom, negbin, ols, orm, phyloglm, phylolm, plm, polr, Rchoice, rlmerMod, rq, scam, selection, speedglm, speedlm, stanreg, tobit, tobit1, truncreg, zeroinfl

New modeling packages can usually be supported by marginaleffects if they include a working predict() method. If you believe that this is the case, please file a feature request on Github: https://github.com/vincentarelbundock/marginaleffects/issues

On Tue, 1 Aug 2023 at 16:54, Vincent Arel-Bundock @.***> wrote:

I think I was able to add support for this model. However, I don’t know this package at all, and I was not able to find a benchmark to compare the results with. I would appreciate if you could check if the results are correct.

Install the development version of the package:

remotes::install_github("vincentarelbundock/marginaleffects")

Restart R completely, then:

library(DCchoice) library(marginaleffects) data(oohbsyn)mod <- oohbchoice(R1 + R2 ~ age + gender | log(BL) + log(BH), data = oohbsyn)

avg_slopes(mod)# # Term Contrast Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %# age dY/dX -0.000706 0.00405 -0.174 0.862 0.2 -0.00865 0.00724# gender male - female 0.035993 0.08024 0.449 0.654 0.6 -0.12128 0.19326# BL dY/dX -0.090310 0.00832 -10.848 <0.001 88.7 -0.10663 -0.07399# BH dY/dX 0.000000 NA NA NA NA NA NA# # Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high

avg_slopes(mod, type = "utility")# # Term Contrast Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %# age dY/dX -0.00399 0.023 -0.174 0.862 0.2 -0.049 0.041# gender male - female 0.20337 0.454 0.448 0.654 0.6 -0.686 1.093# BL dY/dX -0.66793 0.137 -4.888 <0.001 19.9 -0.936 -0.400# BH dY/dX 0.00000 NA NA NA NA NA NA# # Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high

predictions(mod, by = "gender")# # gender Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %# female 0.625 0.0588 10.6 <0.001 85.3 0.510 0.740# male 0.653 0.0564 11.6 <0.001 100.5 0.543 0.764# # Columns: gender, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high

— Reply to this email directly, view it on GitHub https://github.com/vincentarelbundock/marginaleffects/issues/852#issuecomment-1660383524, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6N6FY6T7QX457IODUMJWITXTEDALANCNFSM6AAAAAA27VX2YQ . You are receiving this because you authored the thread.Message ID: @.***>

vincentarelbundock commented 1 year ago

Please make sure you are running version 0.13.0.9010 or later: ˋpackageVersion("marginaleffects")`

donaldkip77 commented 1 year ago

It's working! after changing the package, thank you. I will compare the results with that from the LIMDEP software. I will let you know.

On Thu, 3 Aug 2023 at 15:52, Vincent Arel-Bundock @.***> wrote:

Please make sure you are running version 0.13.0.9010 or later: ˋpackageVersion("marginaleffects")`

— Reply to this email directly, view it on GitHub https://github.com/vincentarelbundock/marginaleffects/issues/852#issuecomment-1663929016, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6N6FY53XQWCJGXCGQ5BSILXTONHFANCNFSM6AAAAAA27VX2YQ . You are receiving this because you authored the thread.Message ID: @.***>

donaldkip77 commented 1 year ago

What is the difference between avg_slopes(mod) and avg_slopes(mod, type = "utility") because both provide results as the marginal effects?

Regards,

Donald

On Tue, 1 Aug 2023 at 16:54, Vincent Arel-Bundock @.***> wrote:

I think I was able to add support for this model. However, I don’t know this package at all, and I was not able to find a benchmark to compare the results with. I would appreciate if you could check if the results are correct.

Install the development version of the package:

remotes::install_github("vincentarelbundock/marginaleffects")

Restart R completely, then:

library(DCchoice) library(marginaleffects) data(oohbsyn)mod <- oohbchoice(R1 + R2 ~ age + gender | log(BL) + log(BH), data = oohbsyn)

avg_slopes(mod)# # Term Contrast Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %# age dY/dX -0.000706 0.00405 -0.174 0.862 0.2 -0.00865 0.00724# gender male - female 0.035993 0.08024 0.449 0.654 0.6 -0.12128 0.19326# BL dY/dX -0.090310 0.00832 -10.848 <0.001 88.7 -0.10663 -0.07399# BH dY/dX 0.000000 NA NA NA NA NA NA# # Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high

avg_slopes(mod, type = "utility")# # Term Contrast Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %# age dY/dX -0.00399 0.023 -0.174 0.862 0.2 -0.049 0.041# gender male - female 0.20337 0.454 0.448 0.654 0.6 -0.686 1.093# BL dY/dX -0.66793 0.137 -4.888 <0.001 19.9 -0.936 -0.400# BH dY/dX 0.00000 NA NA NA NA NA NA# # Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high

predictions(mod, by = "gender")# # gender Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %# female 0.625 0.0588 10.6 <0.001 85.3 0.510 0.740# male 0.653 0.0564 11.6 <0.001 100.5 0.543 0.764# # Columns: gender, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high

— Reply to this email directly, view it on GitHub https://github.com/vincentarelbundock/marginaleffects/issues/852#issuecomment-1660383524, or unsubscribe https://github.com/notifications/unsubscribe-auth/A6N6FY6T7QX457IODUMJWITXTEDALANCNFSM6AAAAAA27VX2YQ . You are receiving this because you authored the thread.Message ID: @.***>

vincentarelbundock commented 1 year ago

The type argument is passed to predict(), and slopes() takes the derivative of the output.