Skip to content
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

Covariates and slopes #114

Open
burrowsk opened this issue May 15, 2023 · 6 comments
Open

Covariates and slopes #114

burrowsk opened this issue May 15, 2023 · 6 comments
Labels
Difficulty: [2] Intermediate Priority: [1] Low Type: Discussion 💬 Issues related to questions, discussions, or comments
Milestone

Comments

@burrowsk
Copy link
Collaborator

Hi!
I'm having trouble marrying up the slopes to the average curve. The lines from one predicted period to another don't fall along the curve as expected.

curve_slopes

The data for the predicted periods and the model fit are the same. I ended up playing with the prediction for the curve by setting my covariate to 0 (my covariate is coded 1/2) and the slopes matched much more closely:

curve_slopes2

Am I being paranoid here, doesn't the predicted periods used for deriving the slopes include covariates in the predictions? I know we specify "filter" in run_eggla_lmm() but I'm having trouble seeing how this is incorporated into predicting the slopes?

Thanks!

@mcanouil
Copy link
Owner

mcanouil commented May 16, 2023

Short answer, the slopes are correct.

Long answer, currently in the function to draw the plots, the average curve is "independent"
Of the covariates while for the slopes, they are predicted from the mixed model.
For instance, you can see the formula to compute "y": https://github.com/mcanouil/eggla/blob/4538ff2d53424f94cca44f188c1006c952fd4929/R/plot_egg_slopes.R#LL70C9-L70C9

As you can see, it does not account for any covariates that could have been included.

For the figure to have curve and slopes to match, we would need to generalise that formula to include covariates effects.

If you feel you can make a PR, please do! 😉

@burrowsk
Copy link
Collaborator Author

Thanks very much! I thought about how the "predicted periods" in compute_slopes():
https://github.com/mcanouil/eggla/blob/4538ff2d53424f94cca44f188c1006c952fd4929/R/compute_slopes.R#LL104C1-L123C4
uses the coef (fixed + random effects) which of course is adjusted for covariates. Unfortunately, that popped into my head at 3:30 this morning :(

I didn't use the plot_slopes() function though you raise a good point about the curve not accounting for covariates so I can see how that would affect them matching up. However I predicted the curve using:

res_f <-readRDS("female-model-object.rds")
pred_f <- expand.grid(egg_ageyears = seq(min(res_f[["data"]][["egg_ageyears"]])+0.05,max(res_f[["data"]][["egg_ageyears"]])-0.05,0.01))
pred_f$sourceb <- 1
pred_f$bmi <-exp(predict(res_f,pred_f,level =0))

sourceb being my covariate. This is what goes towards creating the first plot where the slopes and curve do not match. However when I change to pred_f$sourceb <- 0 (sourceb is a binary variable of 1/2) it is much closer?

Thanks!

@mcanouil
Copy link
Owner

mcanouil commented May 16, 2023

As done internally, it uses predict, but the coordinates for the segments representing the slopes are roughly defined by pred_period + end * slope which here use adjusted "end"/"x", thus does not have the fixed effect of any covariates, which lead to the vertical shift of the segments.
For an interval, we have:

  • "pred_period", the corrected y value at the start of the interval.
  • "end", the x value at the at the end of the interval.
  • "slope" is the predicted slope in the interval.

We don't keep covariates effects in the results, thus, I don't see how from the slopes and predicted values we can put them back.

Note that this is only a visualisation issue.
Also, for the model we defined, the functions include "egg" in their names, such as egg_slopes() and plot_egg_slopes().

@mcanouil
Copy link
Owner

I could try to extract covariates effects and add them to the equation, to ensure the visualisation accounts for those, but I'll have to think about how to do that in a general way (interaction terms, etc.).

@burrowsk
Copy link
Collaborator Author

Sorry for lack of reply!, I'm out of action for the next two days but will pick this up on Friday :)

@mcanouil
Copy link
Owner

No worries, I will be off as well (theoretically).

@mcanouil mcanouil added this to the v1.0.1 milestone Jan 26, 2024
@mcanouil mcanouil added Type: Discussion 💬 Issues related to questions, discussions, or comments Difficulty: [2] Intermediate Priority: [1] Low and removed question labels Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: [2] Intermediate Priority: [1] Low Type: Discussion 💬 Issues related to questions, discussions, or comments
Projects
None yet
Development

No branches or pull requests

2 participants