Linear model: 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-04-covs-lm.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 ~ remoteness + prop_tap_toilet

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 4458381. logPost -2229223 logLik -2229186 edf 4.0000 eps 0.0299 iteration   1"        
#>  [2] "AICc 4458381. logPost -2229223 logLik -2229186 edf 4.0000 eps 0.0000 iteration   2"        
#>  [3] "AICc 4458381. logPost -2229223 logLik -2229186 edf 4.0000 eps 0.0000 iteration   2"        
#>  [4] "elapsed time:  0.38sec"                                                                    
#>  [5] "Starting the sampler..."                                                                   
#>  [6] "Starting the sampler..."                                                                   
#>  [7] "Starting the sampler..."                                                                   
#>  [8] "Starting the sampler..."                                                                   
#>  [9] ""                                                                                          
#> [10] "|                    |   0%  4.32min"                                                      
#> [11] "|                    |   0%  4.34min"                                                      
#> [12] "|                    |   0%  4.34min"                                                      
#> [13] "|                    |   0%  4.35min"                                                      
#> [14] "|*                   |   5%  4.49min 14.19sec"                                             
#> [15] "|*                   |   5%  4.50min 14.20sec"                                             
#> [16] "|*                   |   5%  4.50min 14.21sec"                                             
#> [17] "|*                   |   5%  4.50min 14.20sec"                                             
#> [18] "|**                  |  10%  4.25min 28.33sec"                                             
#> [19] ""                                                                                          
#> [20] "|**                  |  10%|**                  |  10%   4.25min 4.25min  28.31sec28.31sec"
#> [21] "|**                  |  10%  4.25min 28.33sec"                                             
#> [22] "|***                 |  15%  3.99min 42.29sec"                                             
#> [23] "|***                 |  15%  4.00min 42.38sec"                                             
#> [24] "|***                 |  15%  4.00min 42.38sec"                                             
#> [25] "|***                 |  15%  4.03min 42.67sec"                                             
#> [26] "|****                |  20%  3.75min 56.19sec"                                             
#> [27] "|****                |  20%  3.77min 56.56sec"                                             
#> [28] "|****                |  20%  3.78min 56.65sec"                                             
#> [29] "|****                |  20%  3.78min 56.65sec"                                             
#> [30] "|*****               |  25%  3.52min  1.17min"                                             
#> [31] "|*****               |  25%  3.53min  1.18min"                                             
#> [32] "|*****               |  25%  3.54min  1.18min"                                             
#> [33] "|*****               |  25%  3.54min  1.18min"                                             
#> [34] "|******              |  30%  3.29min  1.41min"                                             
#> [35] "|******              |  30%  3.29min  1.41min"                                             
#> [36] "|******              |  30%  3.29min  1.41min"                                             
#> [37] "|******              |  30%  3.30min  1.41min"                                             
#> [38] "|*******             |  35%  3.05min  1.64min"                                             
#> [39] "|*******             |  35%  3.06min  1.65min"                                             
#> [40] "|*******             |  35%  3.07min  1.65min"                                             
#> [41] "|*******             |  35%  3.07min  1.65min"                                             
#> [42] "|********            |  40%  2.83min  1.88min"                                             
#> [43] "|********            |  40%  2.83min  1.89min"                                             
#> [44] "|********            |  40%  2.83min  1.89min"                                             
#> [45] "|********            |  40%  2.83min  1.89min"                                             
#> [46] "|*********           |  45%  2.60min  2.13min"                                             
#> [47] "|*********           |  45%  2.60min  2.13min"                                             
#> [48] "|*********           |  45%  2.60min  2.13min"                                             
#> [49] "|*********           |  45%  2.60min  2.13min"                                             
#> [50] "|**********          |  50%  2.37min  2.37min"                                             
#> [51] "|**********          |  50%  2.37min  2.37min"                                             
#> [52] "|**********          |  50%  2.37min  2.37min"                                             
#> [53] "|**********          |  50%  2.37min  2.37min"                                             
#> [54] "|***********         |  55%  2.13min  2.60min"                                             
#> [55] "|***********         |  55%  2.13min  2.61min"                                             
#> [56] "|***********         |  55%  2.14min  2.61min"                                             
#> [57] "|***********         |  55%  2.14min  2.62min"                                             
#> [58] "|************        |  60%  1.89min  2.84min"                                             
#> [59] "|************        |  60%  1.90min  2.84min"                                             
#> [60] "|************        |  60%  1.90min  2.85min"                                             
#> [61] "|************        |  60%  1.90min  2.85min"                                             
#> [62] "|*************       |  65%  1.66min  3.08min"                                             
#> [63] "|*************       |  65%  1.66min  3.09min"                                             
#> [64] "|*************       |  65%  1.66min  3.09min"                                             
#> [65] "|*************       |  65%  1.67min  3.10min"                                             
#> [66] "|**************      |  70%  1.43min  3.33min"                                             
#> [67] "|**************      |  70%  1.43min  3.33min"                                             
#> [68] "|**************      |  70%  1.43min  3.34min"                                             
#> [69] "|**************      |  70%  1.43min  3.34min"                                             
#> [70] "|***************     |  75%  1.19min  3.57min"                                             
#> [71] "|***************     |  75%  1.19min  3.57min"                                             
#> [72] "|***************     |  75%  1.19min  3.58min"                                             
#> [73] "|***************     |  75%  1.19min  3.58min"                                             
#> [74] "|****************    |  80% 57.10sec  3.81min"                                             
#> [75] "|****************    |  80% 57.13sec  3.81min"                                             
#> [76] "|****************    |  80% 57.21sec  3.81min"                                             
#> [77] "|****************    |  80% 57.29sec  3.82min"                                             
#> [78] "|*****************   |  85% 42.83sec  4.04min"                                             
#> [79] "|*****************   |  85% 42.93sec  4.05min"                                             
#> [80] "|*****************   |  85% 42.98sec  4.06min"                                             
#> [81] "|*****************   |  85% 43.03sec  4.06min"                                             
#> [82] "|******************  |  90% 28.61sec  4.29min"                                             
#> [83] "|******************  |  90% 28.64sec  4.30min"                                             
#> [84] "|******************  |  90% 28.71sec  4.31min"                                             
#> [85] "|******************  |  90% 28.72sec  4.31min"                                             
#> [86] "|******************* |  95% "                                                              
#> [87] "14.31sec  4.53min|******************* |  95% 14.31sec  4.53min"                            
#> [88] "|******************* |  95% 14.34sec  4.54min"                                             
#> [89] "|******************* |  95% 14.36sec  4.55min"                                             
#> [90] "|********************| 100%  0.00sec  4.77min"                                             
#> [91] ""                                                                                          
#> [92] "|********************| 100%  0.00sec  4.78min"                                             
#> [93] ""                                                                                          
#> [94] "|********************| 100%  0.00sec  4.78min"                                             
#> [95] ""                                                                                          
#> [96] "|********************| 100%  0.00sec  4.78min"
system.time(saveRDS(bamlss_model, file = path_modelled_data))
#>    user  system elapsed 
#>    0.21    0.00    0.21
saveRDS(form, file = path_modelled_form)

Time to execute the task

Only useful when executed with Rscript.

proc.time()
#>     user   system  elapsed 
#> 1183.748    1.649  303.356