kosukeimai / mediation

R package mediation
58 stars 29 forks source link

GAM interaction demo code no longer runs #8

Open aolney opened 6 years ago

aolney commented 6 years ago

The demo for code here for gam interactions no longer runs and gives error "Error in predict.gam(new.fit.Y, type = "response", newdata = pred.data.t) : 0 not in original fit". This appears to be an error that happens when predict.gam is given unknown factor levels.

Strangely if I comment out control as below it runs or if I set "control=treat" it runs. However I get different results in with these two methods. The commenting out method gives a summary result that is closer to the old vignette:

model.m <- lm(job_seek ~ treat + depress1 + econ_hard + sex + age + occp + marital
                 + nonwhite + educ + income, data = jobs)

model.y <- gam(depress2 ~ treat + s(job_seek, by = treat) + s(job_seek, by = control)
               + depress1 + econ_hard + sex + age + occp + marital + nonwhite + educ + income,
               data = jobs)

out.5 <- mediation::mediate(model.m, model.y, sims = 1000, boot = TRUE,
                            treat = "treat", mediator = "job_seek",
                            #control = "control"
                       )
summary(out.5)

However the result isn't close enough for me to feel confident that this is correct (e.g. within .01).

Both the paper referenced in mediate's documentation for gam and the old vignette imply that the by variables should be indicator variables. The current jobs data does not have this form: treat is binary but control is a factor. If I create a new variable jobs$control.1 <- 1 - jobs$treat I can then set the by options sensibly, but again I don't get the values from the old vignette. I've also tried just using the spline by factor control since the gam documentation suggests that creates an interaction by itself anyways. Altogether I've explored a number of alternatives, and the closest to the old vignette seems to be the "comment out control" listing above.