Created
August 18, 2020 15:51
-
-
Save dill/61599d96628b7b82e9dfc61cda3195c1 to your computer and use it in GitHub Desktop.
gratia overplotting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# put all yr models in a list | |
model_list <- list(b_norm, b_term, b_term_sel) | |
# name them for nice labels | |
names(model_list) <- c("No selection", "Shrinkage smoother", "Extra penalty") | |
# all the terms (there's probably a gratia built-in for this) | |
term_list <- c("s(Depth)", "s(Bottom)", "s(Surface)") | |
library(gratia) | |
# pre-storage | |
plot_dat <- c() | |
# loop over terms and models | |
for(this_term in term_list){ | |
for(i in seq_along(model_list)){ | |
# evaluate this term for this model | |
this_smoo <- evaluate_smooth(model_list[[i]], this_term) | |
# add identifier columns | |
this_smoo$model <- names(model_list)[i] | |
this_smoo$term <- this_term | |
# rename the column with the covariate values | |
this_smoo[["covar"]] <- this_smoo[[sub("s\\((.+)\\)", "\\1", this_term)]] | |
# remove old column | |
this_smoo[[sub("s\\((.+)\\)", "\\1", this_term)]] <- NULL | |
# squidge it together | |
plot_dat <- rbind.data.frame(plot_dat, this_smoo) | |
} | |
} | |
# adhoc plot | |
p_shrinky <- ggplot(plot_dat, aes(x=covar, group=model, fill=model)) + | |
geom_ribbon(aes(ymin=est-2*se, ymax=est+2*se), alpha=0.4) + | |
geom_line(aes(y=est)) + | |
facet_wrap(~term, scale="free") + | |
theme_minimal() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
produces something like this: