gasparrini / dlnm

R package dlnm
68 stars 13 forks source link

Using dlnm with targets package -- does dlnm require that objects live in the global environment? #11

Closed Lfrueh closed 11 months ago

Lfrueh commented 11 months ago

I'm not sure this is the best place to ask this--but a targets package maintainer suggested I raise this issue here.

When I'm using a targets pipeline with dlnm, I end up with an issue where once there are two crossbases in my model (defined by upstream targets), the model can't find them and I get a coef/vcov error. However, if I collapse all my targets into one function (one 'target'), it works. This suggests that dlnm requires that objects live in the global environment. Is there any way around this?

Here is a reprex to demonstrate the issue: https://github.com/ropensci/targets/discussions/1095#discussioncomment-6533184

Thank you in advance!

gasparrini commented 11 months ago

The issue is related to the name of the crossbasis object, which must match the one used in the model formula. In your code, you use 'cb.pm' in the model formula, but then you use 'cb' as the argument 'basis' in the following step. Here crosspred() cannot distinguish between the two crossbasis terms 'cb.pm' and 'cb.tmin'.

Replace the function related to prediction with:

pred_model <- function(cb.pm, model) { crosspred( basis = cb.pm, model = model, from = 0, to = 70, cen = 8, bylag = 1, by = 0.1 ) }

and it is should work.

best -Antonio Gasparrini