jmpsteen / medflex

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

neEffdecomp and plot.nemodel fail on scaled variables #21

Closed sophiemoullin closed 3 years ago

sophiemoullin commented 3 years ago

Hi there, I'm just sharing an issue that I already resolved in code, just in case it helps others using this package save time. Scaled or mean-standardized variables as covariates return an error in the neEffdecomp and associated plot functions. The ne.model stores $scale within the list. But, you can simply return them to numeric variables and the problem is solved!

Example:

data(UPBdata)
UPBdata$std_age <- scale(UPBdata$age) # using scale or similar scaling eg. from the 'psych' R package

impData <- neImpute(UPB ~ att + gender + std_age, family = binomial, data = UPBdata)
neMod <- neModel(UPB ~ att0 + att1 + std_age, family = binomial, expData = impData, se = "robust")
neEffdecomp(neMod) # fails 
# returns error: "Error in names(dat1) <- all.vars(formula) : 
  'names' attribute [4] must be the same length as the vector [3]"
plot(neMod) # same error
# returning scaled variable to numeric
UPBdata$std_age <- as.numeric(UPBdata$std_age) 
# repeating models as before 
impData <- neImpute(UPB ~ att + gender + std_age, family = binomial, data = UPBdata)
neMod <- neModel(UPB ~ att0 + att1 + std_age, family = binomial, expData = impData, se = "robust")
neEffdecomp(neMod) # works fine! 
plot(neMod) # also works