One-term additive models: masking out urban with interactions


Simple univariate GAM models to evaluate the relationship between each land category and covariates.

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)

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")

land_cover <- fst::read_fst(file.path(path_processed, "uk_1km_df_model_maskout.fst"))
land_cover <- dplyr::sample_n(land_cover, 5000)

Exploratory model of land classes with covariates

terms <- list(
    ~ s(elev),
    ~ s(slope_nb8),
    ~ s(gdd, bs = "cc"),
    ~ s(min_tasmin,max_tasmax),
    ~ s(smd,sms)
    )

land_types <- tidyselect::vars_select(names(land_cover), matches("^count_[1-5]"))

update_form <- function(x, y) {
    update(x, paste0("cbind(", y, ", count_no_urban - ", y, ") ~ ."))
}

data_model <- tibble::as_tibble(expand.grid(
    formula = map(land_types, ~ map(terms, ~ update_form(.x, .y), .y = .x)) %>%
        unlist(use.names = FALSE),
    gamma = c(1, log(nrow(land_cover)) / 2)
))

data_model <- data_model %>%
    mutate(model = map2(formula, gamma, ~ gam(.x, binomial(), land_cover, gamma = .y)))

saveRDS(data_model, file = file.path(path_modelled, "oneterm_maskout_interact.rds"))

Time to execute the task

Only useful when executed with Rscript.

proc.time()
#>    user  system elapsed 
#>  90.257   0.724  90.956