Skip to content

Instantly share code, notes, and snippets.

@dill
Created August 18, 2020 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dill/61599d96628b7b82e9dfc61cda3195c1 to your computer and use it in GitHub Desktop.
Save dill/61599d96628b7b82e9dfc61cda3195c1 to your computer and use it in GitHub Desktop.
gratia overplotting
# 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()
@dill
Copy link
Author

dill commented Aug 18, 2020

produces something like this:

Screenshot 2020-08-18 at 16 52 09

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment