Municipality one-term models: visualization


Visualize relationship between covariates and response variables.

Load packages, read data and source custom scripts

rm(list = ls())
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(mgcv)
#> Loading required package: nlme
#> 
#> Attaching package: 'nlme'
#> The following object is masked from 'package:dplyr':
#> 
#>     collapse
#> This is mgcv 1.8-31. For overview type 'help("mgcv-package")'.
library(purrr)
library(ggplot2)

path_proj <- day2day::git_path()
path_data <- file.path(path_proj, "data")
path_processed <- file.path(path_data, "processed")
path_modelled <- file.path(path_data, "modelled")

source(file.path(path_proj, "src", "55-mgcv-visualize.R"))

model_data <- readRDS(file.path(path_modelled,  "bw-explore-smooths-muni.rds"))
bwdata_model <- fst::read_fst(file.path(path_processed, "bwdata_41_model.fst"))

Visualize exploratory models of birthweight with municipality covariates

covar_names <- c(
    age = "mother age",
    wk_ini = "conception date",
    rivwk_conception = "river week of conception",
    remoteness = "remoteness",
    rur_prop = "proportion of rural population",
    prop_tap_toilet = "proportion of population with tap water"
)

data_gams <- model_data %>%
    dplyr::select(- formula) %>%
    get_data_gams()

data_gams$data_eff <- data_gams$data_eff %>%
    mutate(gamma = paste0("gamma: ", round(gamma, 3)))

data_gams <- map(data_gams, ~ mutate(., covar = covar_names[covar]))

plot_gam_1d(data_gams) +
    scale_fill_manual(values = "gray60") +
    scale_colour_manual(values = "red") +
    facet_grid(gamma ~ covar, scales = "free") +
    theme(legend.position = "none")

Time to execute the task

Only useful when executed with Rscript.

proc.time()
#>    user  system elapsed 
#>   5.137   0.210   5.363