library(APCtools)
library(mgcv)
data(travel)
# variant A: plot observed mean structures
# observed heatmap
plot_APCheatmap(dat = travel, y_var = "mainTrip_distance",
bin_heatmap = FALSE, y_var_logScale = TRUE)
# with binning
plot_APCheatmap(dat = travel, y_var = "mainTrip_distance",
bin_heatmap = TRUE, y_var_logScale = TRUE)
# variant B: plot some smoothed, estimated mean structure
model <- gam(mainTrip_distance ~ te(age, period) + residence_region +
household_size + s(household_income), data = travel)
# plot the smooth tensor product surface
plot_APCheatmap(dat = travel, model = model, bin_heatmap = FALSE, plot_CI = FALSE)
# ... same plot including the confidence intervals
plot_APCheatmap(dat = travel, model = model, bin_heatmap = FALSE)
# the APC dimensions can be flexibly assigned to the x-axis and y-axis
plot_APCheatmap(dat = travel, model = model, dimensions = c("age","cohort"),
bin_heatmap = FALSE, plot_CI = FALSE)
# add some reference lines
plot_APCheatmap(dat = travel, model = model, bin_heatmap = FALSE, plot_CI = FALSE,
markLines_list = list(cohort = c(1910,1939,1955,1980)))
# default binning of the tensor product surface in 5-year-blocks
plot_APCheatmap(dat = travel, model = model, plot_CI = FALSE)
# manual binning
manual_binning <- list(period = seq(min(travel$period, na.rm = TRUE) - 1,
max(travel$period, na.rm = TRUE), by = 5),
cohort = seq(min(travel$period - travel$age, na.rm = TRUE) - 1,
max(travel$period - travel$age, na.rm = TRUE), by = 10))
plot_APCheatmap(dat = travel, model = model, plot_CI = FALSE,
bin_heatmapGrid_list = manual_binning)
Run the code above in your browser using DataLab