jmpsteen / medflex

Flexible mediation analysis using natural effect models in R
23 stars 6 forks source link

Using VGAM::vgam with special terms such as s() in formula throws an error #12

Closed jmpsteen closed 7 years ago

jmpsteen commented 7 years ago

Using the vgam function with special terms such as s() in its formula argument, for instance, in the examples below, throws an error.

library(VGAM)

## vgam for exposure model
fitX <- vgam(attcat ~ gender + educ + s(age), 
             family = multinomial(), data = UPBdata)
expData <- neImpute(UPB ~ attcat * negaff + gender + educ + age, 
                    family = binomial, data = UPBdata)
neMod <- neModel(UPB ~ attcat0 * attcat1, 
                 family = binomial, expData = expData, xFit = fitX, nBoot = 4)

## vgam for mediator model
UPBdata$negaff3 <- cut(UPBdata$negaff, breaks = 3, labels = c("low", "moderate", "high"))
expData <- neWeight(negaff3 ~ att + gender + educ + s(age), 
                    family = multinomial(), data = UPBdata, FUN = vgam)
neMod <- neModel(UPB ~ att0 * att1 + gender + educ + age, 
                 family = binomial, expData = expData, nBoot = 4)

## vgam for imputation model
expData <- neImpute(UPB ~ att * negaff + gender + educ + s(age), 
                    family = binomialff, data = UPBdata, FUN = vgam)
neMod <- neModel(UPB ~ att0 * att1, family = binomial("logit"), 
                 expData = expData, nBoot = 4)
jmpsteen commented 7 years ago

This is now fixed by the internal wrapFUN function which constructs an adapted model.frame (including special terms) for vgam objects in order to make predictions (for observations in extended datasets) from either exposure (xFit argument in neModel), mediator (neWeight) or imputation (neImpute) models.