-
Notifications
You must be signed in to change notification settings - Fork 58
[DRAFT] adding support for the {mmrm} package #1000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Currently my test case fails irrespective of me setting or not. I did not open a separate issue in the bug tracker since this is likely a bug caused by my faulty implementation of the {mmrm} interface. |
f28ed19 to
632dfc5
Compare
|
Thanks for taking the time to do this, I really appreciate it! The
Does your example work when you just supply data frame directly: |
|
Thanks, just adding some thoughts here. Does your example work when you just supply data frame directly: newdata=fev_data? The question to me is what the general default strategy regarding missing data should be. The element |
|
Ideally, on my end, I would like to go through Also, looking at the code quickly, I suspect that the reason predictions don't work in the current PR is that It might also be worth checking if you really need all those methods. Sometimes, the defaults work perfectly well. Just try something like: library(marginaleffects)
get_predict.default(fit) |
|
Thanks for the feeback @vincentarelbundock, made some adjustments and starting to wrap my head around this. If I see it correctly we still have two things to address here.
|
|
I was looking at this PR today and noticed that the current version does not return standard errors. The underlying reason for this is that the Jacobian is full of zeros: library(marginaleffects)
library(mmrm)
fit <- mmrm(FEV1 ~ RACE + SEX + ARMCD * AVISIT + us(AVISIT | USUBJID),
data = fev_data,
weights = fev_data$WEIGHTS,
method = "Kenward-Roger")
p <- predictions(fit)
attr(p, "jacobian")The reason for that, is that One possibility is that this is related to the problem with I don't actually know if this is related, but I noticed that both |
|
Thanks @vincentarelbundock and @kkmann and all, just looking at this thread as a bystander - yes, structurally the two packages will work similarly. However I also wonder if in this case, because we use TMB, there would be much better automatic differentiation ways to generate the Jacobian you need. |
|
@danielinteractive Thanks for chiming in! Yes, that would certainly be more efficient. I think it makes sense for very common quantities like predictions. But the issue is that My guess is that this would require a ton of work on the backend. But then again, I don't know TMB at all, so... |
|
PR created at kkmann#1 to fix the issue that the jacobian is 0 reason is that predict in mmrm is always returning a conditional prediction (conditional on observed values) |
|
Sorry I left this PR to linger for so long. I looked into this today, and things mostly seem to work, but there are two lingering issues. The first is probably easy to solve. Not sure about the second. library("mmrm")
fit <- mmrm(
formula = FEV1 ~ RACE + SEX + ARMCD * AVISIT + us(AVISIT | USUBJID),
data = fev_data
)
|
|
Thanks @vincentarelbundock , looking into this now on the |
|
Thanks again @vincentarelbundock! While we will be able to fix the first problem quickly (indeed this is a bug in |
* issue1204 * test fix
* support glmtoolbox::glmgee() * fixup test * test fixup * supported models
* support glmtoolbox::glmgee() * fixup test * test fixup * supported models
|
Cool. Thanks for taking a look and finding a solution for issue 1. I just spend a bunch of time trying to find a place to insert a hack that would append some random string to the subject ID column. Unfortunately, it's very hard. The problem is that this stacking logic is very "deep" in the So in order to support this, I would have to insert complex hacks in a dozen places in the code base, and don't think that's sensible design for software that wants to support 100+ model classes. To be sure, that's less convenient, but maybe it's not the end of the world that users have to define a single contrast they are interested in, rather than get all of the them at once. This can probably be documented. |
|
Hi @vincentarelbundock , I discussed the issue openpharma/mmrm#461 further with @clarkliming , and it seems that a more principled solution to that will likely also fix the issue 2 here. So let us first sort that out and maybe both issues 1 and 2 will be solved in |
|
Oh wow, OK. I really appreciate your help in working on this. It'll be really cool.if we can push this through the fi ish line |
|
Hello @vincentarelbundock , @danielinteractive, I just checked the mmrm repository and it seems that the main issue has been resolved: Does it mean that the integration with marginaleffects is a step closer? |
|
Thanks @adrianolszewski for circling back on this, I am not sure if we solved the second issue, @clarkliming do you remember? Maybe @vincentarelbundock worth to try the code again with the current {mmrm} version? |
|
I merged the I need your help! If you have experience with InstallationFirst, install the development version with mmrm support: remotes::install_github("kkmann/marginaleffects@mmrm-extension")Load packages and fit modellibrary(marginaleffects)
library(mmrm)
# Fit an mmrm model using the built-in fev_data
# This models FEV1 (lung function) with an unstructured covariance matrix
fit <- mmrm(
formula = FEV1 ~ RACE + SEX + ARMCD * AVISIT + us(AVISIT | USUBJID),
data = fev_data
)Test core marginaleffects functionsPredictionsGet unit-level predictions and average predictions: # Unit-level predictions (first few rows)
predictions(fit) |>
head() |>
data.frame()
#> rowid estimate std.error statistic p.value s.value conf.low conf.high df FEV1 RACE SEX ARMCD AVISIT USUBJID
#> 1 1 41.20593 0.7662678 53.77484 0.000000e+00 Inf 39.70407 42.70779 Inf 39.97105 Black or African American Female TRT VIS2 PT1
#> 2 2 52.08639 1.2751130 40.84845 0.000000e+00 Inf 49.58722 54.58557 Inf 20.48379 Black or African American Female TRT VIS4 PT1
#> 3 3 35.61706 0.7636975 46.63766 0.000000e+00 Inf 34.12024 37.11388 Inf 31.45522 Asian Male PBO VIS2 PT2
#> 4 4 41.11959 0.6454274 63.70909 0.000000e+00 Inf 39.85457 42.38460 Inf 36.87889 Asian Male PBO VIS3 PT2
#> 5 5 45.83137 1.2760045 35.91788 1.606758e-282 936.0996 43.33045 48.33230 Inf 48.80809 Asian Male PBO VIS4 PT2
#> 6 6 37.47363 0.7408669 50.58078 0.000000e+00 Inf 36.02155 38.92570 Inf 35.98699 Black or African American Female PBO VIS2 PT3
# Unit-level predictions for an individual with average characteristics
predictions(fit, newdata = "mean")
#>
#> Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 % Df
#> 35.9 0.736 48.8 <0.001 Inf 34.5 37.4 Inf
#>
#> Type: response
# Average predictions across the dataset
avg_predictions(fit)
#>
#> Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 % Df
#> 42.3 0.33 128 <0.001 Inf 41.7 43 Inf
#>
#> Type: responseComparisonsCalculate average marginal comparisons for the SEX variable: avg_comparisons(fit,
variables = "SEX",
newdata = fev_data
)
#>
#> Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
#> 0.326 0.532 0.613 0.54 0.9 -0.717 1.37
#>
#> Term: SEX
#> Type: response
#> Comparison: Female - MaleSlopes (Marginal Effects)Calculate average marginal effects: avg_slopes(fit)
#>
#> Term Contrast Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
#> ARMCD TRT - PBO 3.752 0.668 5.621 <0.001 25.6 2.444 5.06
#> AVISIT VIS2 - VIS1 4.819 0.565 8.534 <0.001 56.0 3.712 5.93
#> AVISIT VIS3 - VIS1 10.004 0.594 16.850 <0.001 209.2 8.840 11.17
#> AVISIT VIS4 - VIS1 15.358 0.926 16.590 <0.001 202.9 13.544 17.17
#> RACE Black or African American - Asian 1.530 0.624 2.451 0.0143 6.1 0.307 2.75
#> RACE White - Asian 5.644 0.666 8.479 <0.001 55.3 4.339 6.95
#> SEX Female - Male 0.326 0.532 0.613 0.5399 0.9 -0.717 1.37
#>
#> Type: response |
|
Thanks a lot @vincentarelbundock ! |
|
Thank you so much @danielinteractive @vincentarelbundock. |
Hi,
this is an attempt to implement support for the {mmrm} package https://github.com/openpharma/mmrm following the steps outlined in the {marginaleffects} documentation https://marginaleffects.com/vignettes/extensions.html#marginaleffects-extension.
I am still unclear about the role of the option setting.
Is it expected that the user triggers this everytime they want to use {marginaleffects} with {mmrm}? This does not seem to be the case for other extensions.
My test case is
Also tagging @lang-benjamin @danielinteractive