bauer-alex / APCtools

Descriptive and model-based tools for Age-Period-Cohort analyses
Other
13 stars 3 forks source link

plot_marginalAPCeffects with variables conversions in model formula #14

Closed ghost closed 1 year ago

ghost commented 2 years ago

plot_marginalAPCeffects cannot handle models where a variable is converted in the model formula (e.g. through as.factor), and throws out an error. plot_smoothEffects and plot_linearEffects do not have this kind of problem.

bauer-alex commented 2 years ago

Do you have a minimal reproducible example for this?

ghost commented 1 year ago
library(APCtools)
library(mgcv)
data(travel)

# Model formula with conversion:
travel$residence_region <- as.character(travel$residence_region)
model <- gam(mainTrip_distance ~ te(age, period) + as.factor(residence_region) +
               household_size + s(household_income), data = travel)
plot_marginalAPCeffects(model, dat = travel, variable = "age")
plot_linearEffects(model)

# Model formula without conversion:
travel$residence_region <- as.factor(travel$residence_region)
model2 <- gam(mainTrip_distance ~ te(age, period) + residence_region +
               household_size + s(household_income), data = travel)
plot_marginalAPCeffects(model2, dat = travel, variable = "age")
plot_linearEffects(model2)

Sorry for the delay. I haven't had this issue in mind anymore. The first call of plot_marginalAPCeffects leads to an error. This is caused by extracting covariate names from the model formula. A column named as.factor(residence_region) cannot be found. I'll try to find a simple fix.

ghost commented 1 year ago

@bauer-alex I fixed this issue by removing the text outside the brackets. If this ok for you, please close the issue.