Linear tap water with random effects: fitting


Birthweight model including covariates at municipality level with linear effects.

Load packages, read data and source custom scripts

Paths are defined relative to the git repository location.

rm(list = ls())
library(bamlss)
#> Loading required package: coda
#> Loading required package: colorspace
#> Loading required package: mgcv
#> Loading required package: nlme
#> This is mgcv 1.8-31. For overview type 'help("mgcv-package")'.
#> 
#> Attaching package: 'bamlss'
#> The following object is masked from 'package:mgcv':
#> 
#>     smooth.construct
library(gamlss.dist)
#> Loading required package: MASS
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")

path_modelled_data <- file.path(path_modelled, "bw-muni-14-water-re.rds")
path_modelled_sink <- gsub("\\.rds$", "\\.txt", path_modelled_data)
path_modelled_form <- gsub("(\\.rds)$", "-form\\1", path_modelled_data)

bwdata_model <- fst::read_fst(file.path(path_processed, "bwdata_41_model.fst"))

Define formula for our model

Now we define the same models as in the previous study.

form_sigma <- sigma ~ 1

form_mu <- born_weight ~ prop_tap_toilet + s(res_muni, bs = "re")

form <- list(form_mu, form_sigma)

Run the model of interest and save results

{
    sink(path_modelled_sink)
    bamlss_model <- bamlss(
        form, data = bwdata_model,
        n.iter = 1000, burnin = 0, cores = 4, combine = FALSE, light = TRUE
    )
    sink()
}
readLines(path_modelled_sink)
#>   [1] "AICc 4465554. logPost -2455057 logLik -2232773 edf 4.0881 eps 0.0322 iteration   1"
#>   [2] "AICc 4458520. logPost -2231484 logLik -2229248 edf 11.674 eps 0.0076 iteration   2"
#>   [3] "AICc 4456619. logPost -2228826 logLik -2228277 edf 32.192 eps 0.0040 iteration   3"
#>   [4] "AICc 4456381. logPost -2228409 logLik -2228146 edf 43.877 eps 0.0016 iteration   4"
#>   [5] "AICc 4456379. logPost -2228388 logLik -2228144 edf 45.376 eps 0.0001 iteration   5"
#>   [6] "AICc 4456379. logPost -2228387 logLik -2228144 edf 45.371 eps 0.0000 iteration   6"
#>   [7] "AICc 4456379. logPost -2228387 logLik -2228144 edf 45.371 eps 0.0000 iteration   6"
#>   [8] "elapsed time:  9.62sec"                                                            
#>   [9] "Starting the sampler...Starting the sampler..."                                    
#>  [10] ""                                                                                  
#>  [11] "Starting the sampler..."                                                           
#>  [12] "Starting the sampler..."                                                           
#>  [13] ""                                                                                  
#>  [14] ""                                                                                  
#>  [15] "||                                        |   0% |   0% 12.80min12.80min"          
#>  [16] "|                    |   0% 12.79min"                                              
#>  [17] "|                    |   0% 12.83min"                                              
#>  [18] "|*                   |   5% 11.73min 37.05sec"                                     
#>  [19] "|*                   |   5% 11.85min 37.41sec"                                     
#>  [20] "|*                   |   5% 11.85min 37.41sec"                                     
#>  [21] "|*                   |   5% 12.03min 37.98sec"                                     
#>  [22] "|**                  |  10% 11.08min  1.23min"                                     
#>  [23] "|**                  |  10% 11.10min  1.23min"                                     
#>  [24] "|**                  |  10% 11.14min  1.24min"                                     
#>  [25] "|**                  |  10% 11.20min  1.24min"                                     
#>  [26] "|***                 |  15% 10.65min  1.88min"                                     
#>  [27] "|***                 |  15% 10.65min  1.88min"                                     
#>  [28] "|***                 |  15% 10.70min  1.89min"                                     
#>  [29] "|***                 |  15% 10.75min  1.90min"                                     
#>  [30] "|****                |  20% 10.12min  2.53min"                                     
#>  [31] "|****                |  20% 10.12min  2.53min"                                     
#>  [32] "|****                |  20% 10.15min  2.54min"                                     
#>  [33] "|****                |  20% 10.24min  2.56min"                                     
#>  [34] "|*****               |  25%  9.83min  3.28min"                                     
#>  [35] "|*****               |  25%  9.82min  3.27min"                                     
#>  [36] "|*****               |  25%  9.83min  3.28min"                                     
#>  [37] "|*****               |  25%  9.91min  3.30min"                                     
#>  [38] "|******              |  30%  9.21min  3.95min"                                     
#>  [39] "|******              |  30%  9.20min  3.94min"                                     
#>  [40] "|******              |  30%  9.23min  3.96min"                                     
#>  [41] "|******              |  30%  9.28min  3.98min"                                     
#>  [42] "|*******             |  35%  8.62min  4.64min"                                     
#>  [43] "|*******             |  35%  8.62min  4.64min"                                     
#>  [44] "|*******             |  35%  8.65min  4.66min"                                     
#>  [45] "|*******             |  35%  8.69min  4.68min"                                     
#>  [46] "|********            |  40%  7.96min  5.31min"                                     
#>  [47] "|********            |  40%  7.97min  5.31min"                                     
#>  [48] "|********            |  40%  8.00min  5.33min"                                     
#>  [49] "|********            |  40%  8.02min  5.34min"                                     
#>  [50] "|*********           |  45%  7.28min  5.95min"                                     
#>  [51] "|*********           |  45%  7.29min  5.97min"                                     
#>  [52] "|*********           |  45%  7.31min  5.98min"                                     
#>  [53] "|*********           |  45%  7.33min  5.99min"                                     
#>  [54] "|**********          |  50%  6.57min  6.57min"                                     
#>  [55] "|**********          |  50%  6.59min  6.59min"                                     
#>  [56] "|**********          |  50%  6.60min  6.60min"                                     
#>  [57] "|**********          |  50%  6.62min  6.62min"                                     
#>  [58] "|***********         |  55%  5.90min  7.22min"                                     
#>  [59] "|***********         |  55%  5.91min  7.23min"                                     
#>  [60] "|***********         |  55%  5.93min  7.24min"                                     
#>  [61] "|***********         |  55%  5.94min  7.26min"                                     
#>  [62] "|************        |  60%  5.23min  7.84min"                                     
#>  [63] "|************        |  60%  5.23min  7.85min"                                     
#>  [64] "|************        |  60%  5.24min  7.87min"                                     
#>  [65] "|************        |  60%  5.25min  7.88min"                                     
#>  [66] "|*************       |  65%  4.56min  8.47min"                                     
#>  [67] "|*************       |  65%  4.57min  8.48min"                                     
#>  [68] "|*************       |  65%  4.57min  8.50min"                                     
#>  [69] "|*************       |  65%  4.58min  8.51min"                                     
#>  [70] "|**************      |  70%  3.89min  9.08min"                                     
#>  [71] "|**************      |  70%  3.90min  9.09min"                                     
#>  [72] "|**************      |  70%  3.90min  9.11min"                                     
#>  [73] "|**************      |  70%  3.91min  9.12min"                                     
#>  [74] "|***************     |  75%  3.23min  9.69min"                                     
#>  [75] "|***************     |  75%  3.24min  9.71min"                                     
#>  [76] "|***************     |  75%  3.24min  9.73min"                                     
#>  [77] "|***************     |  75%  3.25min  9.76min"                                     
#>  [78] "|****************    |  80%  2.58min 10.31min"                                     
#>  [79] "|****************    |  80%  2.58min 10.33min"                                     
#>  [80] "|****************    |  80%  2.59min 10.35min"                                     
#>  [81] "|****************    |  80%  2.59min 10.37min"                                     
#>  [82] "|*****************   |  85%  1.93min 10.93min"                                     
#>  [83] "|*****************   |  85%  1.93min 10.95min"                                     
#>  [84] "|*****************   |  85%  1.94min 10.97min"                                     
#>  [85] "|*****************   |  85%  1.94min 10.99min"                                     
#>  [86] "|******************  |  90%  1.28min 11.55min"                                     
#>  [87] "|******************  |  90%  1.28min 11.56min"                                     
#>  [88] "|******************  |  90%  1.29min 11.58min"                                     
#>  [89] "|******************  |  90%  1.29min 11.61min"                                     
#>  [90] "|******************* |  95% 38.45sec 12.18min"                                     
#>  [91] "|******************* |  95% 38.49sec 12.19min"                                     
#>  [92] "|******************* |  95% 38.56sec 12.21min"                                     
#>  [93] "|******************* |  95% 38.64sec 12.24min"                                     
#>  [94] "|********************| 100%  0.00sec 12.79min"                                     
#>  [95] ""                                                                                  
#>  [96] "|********************| 100%  0.00sec 12.80min"                                     
#>  [97] ""                                                                                  
#>  [98] "|********************| 100%  0.00sec 12.82min"                                     
#>  [99] ""                                                                                  
#> [100] "|********************| 100%  0.00sec 12.83min"
system.time(saveRDS(bamlss_model, file = path_modelled_data))
#>    user  system elapsed 
#>    2.21    0.00    2.21
saveRDS(form, file = path_modelled_form)

Time to execute the task

Only useful when executed with Rscript.

proc.time()
#>     user   system  elapsed 
#> 3130.705    9.988  804.350