## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 5, fig.alt = "Map of North Carolina counties shaded by Bayesian surprise values." ) ## ----setup-------------------------------------------------------------------- library(bayesiansurpriser) library(sf) library(ggplot2) ## ----basic-sf----------------------------------------------------------------- # Load North Carolina SIDS data nc <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) # Compute surprise: observed SIDS cases vs expected (births) result <- surprise(nc, observed = SID74, expected = BIR74) # View the result print(result) ## ----plot-ggplot-------------------------------------------------------------- # Plot surprise values with ggplot2 ggplot(result) + geom_sf(aes(fill = surprise)) + scale_fill_surprise() + labs(title = "Bayesian Surprise: NC SIDS Data (1974)") ## ----plot-signed-------------------------------------------------------------- # Plot signed surprise (shows direction of deviation) ggplot(result) + geom_sf(aes(fill = signed_surprise)) + scale_fill_surprise_diverging() + labs(title = "Signed Surprise: Over/Under-representation") ## ----output-details----------------------------------------------------------- # Access surprise values directly get_surprise(result, "surprise")[1:5] # Access the model space get_model_space(result) ## ----custom-models------------------------------------------------------------ # Create custom model space custom_space <- model_space( bs_model_uniform(), bs_model_baserate(nc$BIR74), bs_model_gaussian(), prior = c(0.2, 0.5, 0.3) # Custom prior weights ) result_custom <- surprise(nc, observed = SID74, expected = BIR74, models = custom_space) print(result_custom)