Linear remoteness 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-12-remote-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 ~ remoteness + 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 4466503. logPost -2455531 logLik -2233247 edf 4.0881 eps 0.0326 iteration   1"                          
#>   [2] "AICc 4458268. logPost -2231120 logLik -2229122 edf 11.652 eps 0.0097 iteration   2"                          
#>   [3] "AICc 4456589. logPost -2228764 logLik -2228262 edf 32.198 eps 0.0038 iteration   3"                          
#>   [4] "AICc 4456380. logPost -2228401 logLik -2228146 edf 43.877 eps 0.0014 iteration   4"                          
#>   [5] "AICc 4456379. logPost -2228385 logLik -2228144 edf 45.290 eps 0.0001 iteration   5"                          
#>   [6] "AICc 4456379. logPost -2228385 logLik -2228144 edf 45.285 eps 0.0000 iteration   6"                          
#>   [7] "AICc 4456379. logPost -2228385 logLik -2228144 edf 45.285 eps 0.0000 iteration   6"                          
#>   [8] "elapsed time:  9.64sec"                                                                                      
#>   [9] "Starting the sampler...Starting the sampler...Starting the sampler..."                                       
#>  [10] ""                                                                                                            
#>  [11] ""                                                                                                            
#>  [12] "Starting the sampler..."                                                                                     
#>  [13] ""                                                                                                            
#>  [14] ""                                                                                                            
#>  [15] ""                                                                                                            
#>  [16] "|||                                                            |   0% |   0% |   0% 12.35min12.35min12.35min"
#>  [17] "|                    |   0% 13.34min"                                                                        
#>  [18] "|*                   |   5% 11.27min 35.58sec"                                                               
#>  [19] "|*                   |   5% 11.27min 35.59sec"                                                               
#>  [20] "|*                   |   5% 11.43min 36.10sec"                                                               
#>  [21] "|*                   |   5% 11.52min 36.36sec"                                                               
#>  [22] "|**                  |  10% 10.80min  1.20min"                                                               
#>  [23] "|**                  |  10% 10.80min  1.20min"                                                               
#>  [24] "|**                  |  10% 10.89min  1.21min"                                                               
#>  [25] "|**                  |  10% 10.97min  1.22min"                                                               
#>  [26] ""                                                                                                            
#>  [27] "|***                 |  15%|***                 |  15%  10.22min10.22min   1.80min 1.80min"                  
#>  [28] "|***                 |  15% 10.31min  1.82min"                                                               
#>  [29] "|***                 |  15% 10.34min  1.83min"                                                               
#>  [30] "|****                |  20%  9.60min  2.40min"                                                               
#>  [31] "|****                |  20%  9.61min  2.40min"                                                               
#>  [32] "|****                |  20%  9.70min  2.42min"                                                               
#>  [33] "|****                |  20%  9.70min  2.42min"                                                               
#>  [34] "|*****               |  25%  8.99min  3.00min"                                                               
#>  [35] "|*****               |  25%  9.01min  3.00min"                                                               
#>  [36] "|*****               |  25%  9.07min  3.02min"                                                               
#>  [37] "|*****               |  25%  9.08min  3.03min"                                                               
#>  [38] "|******              |  30%  8.39min  3.59min"                                                               
#>  [39] "|******              |  30%  8.39min  3.60min"                                                               
#>  [40] "|******              |  30%  8.44min  3.62min"                                                               
#>  [41] "|******              |  30%  8.45min  3.62min"                                                               
#>  [42] "|*******             |  35%  7.78min  4.19min"                                                               
#>  [43] "|*******             |  35%  7.78min  4.19min"                                                               
#>  [44] "|*******             |  35%  7.82min  4.21min"                                                               
#>  [45] "|*******             |  35%  7.83min  4.22min"                                                               
#>  [46] "|********            |  40%  7.18min  4.79min"                                                               
#>  [47] "|********            |  40%  7.19min  4.79min"                                                               
#>  [48] "|********            |  40%  7.22min  4.81min"                                                               
#>  [49] "|********            |  40%  7.23min  4.82min"                                                               
#>  [50] "|*********           |  45%  6.59min  5.39min"                                                               
#>  [51] "|*********           |  45%  6.59min  5.39min"                                                               
#>  [52] "|*********           |  45%  6.62min  5.41min"                                                               
#>  [53] "|*********           |  45%  6.63min  5.42min"                                                               
#>  [54] "|**********          |  50%  5.99min  5.99min"                                                               
#>  [55] "|**********          |  50%  5.99min  5.99min"                                                               
#>  [56] "|**********          |  50%  6.02min  6.02min"                                                               
#>  [57] "|**********          |  50%  6.03min  6.03min"                                                               
#>  [58] "|***********         |  55%  5.39min  6.59min"                                                               
#>  [59] "|***********         |  55%  5.40min  6.60min"                                                               
#>  [60] "|***********         |  55%  5.42min  6.62min"                                                               
#>  [61] "|***********         |  55%  5.43min  6.63min"                                                               
#>  [62] "|************        |  60%  4.80min  7.20min"                                                               
#>  [63] "|************        |  60%  4.80min  7.20min"                                                               
#>  [64] "|************        |  60%  4.82min  7.23min"                                                               
#>  [65] "|************        |  60%  4.82min  7.24min"                                                               
#>  [66] "|*************       |  65%  4.20min  7.80min"                                                               
#>  [67] "|*************       |  65%  4.20min  7.81min"                                                               
#>  [68] "|*************       |  65%  4.22min  7.84min"                                                               
#>  [69] "|*************       |  65%  4.22min  7.84min"                                                               
#>  [70] "|**************      |  70%  3.60min  8.40min"                                                               
#>  [71] "|**************      |  70%  3.60min  8.41min"                                                               
#>  [72] "|**************      |  70%  3.62min  8.44min"                                                               
#>  [73] "|**************      |  70%  3.62min  8.44min"                                                               
#>  [74] "|***************     |  75%  3.00min  9.01min"                                                               
#>  [75] "|***************     |  75%  3.01min  9.02min"                                                               
#>  [76] "|***************     |  75%  3.01min  9.04min"                                                               
#>  [77] "|***************     |  75%  3.02min  9.05min"                                                               
#>  [78] "|****************    |  80%  2.40min  9.62min"                                                               
#>  [79] "|****************    |  80%  2.41min  9.62min"                                                               
#>  [80] "|****************    |  80%  2.41min  9.65min"                                                               
#>  [81] "|****************    |  80%  2.41min  9.66min"                                                               
#>  [82] "|*****************   |  85%  1.80min 10.22min"                                                               
#>  [83] "|*****************   |  85%  1.80min 10.23min"                                                               
#>  [84] "|*****************   |  85%  1.81min 10.26min"                                                               
#>  [85] "|*****************   |  85%  1.81min 10.26min"                                                               
#>  [86] "|******************  |  90%  1.20min 10.83min"                                                               
#>  [87] "|******************  |  90%  1.20min 10.84min"                                                               
#>  [88] "|******************  |  90%  1.21min 10.87min"                                                               
#>  [89] "|******************  |  90%  1.21min 10.88min"                                                               
#>  [90] "|******************* |  95% 36.07sec 11.42min"                                                               
#>  [91] "|******************* |  95% 36.11sec 11.44min"                                                               
#>  [92] "|******************* |  95% 36.18sec 11.46min"                                                               
#>  [93] "|******************* |  95% 36.22sec 11.47min"                                                               
#>  [94] "|********************| 100%  0.00sec 12.02min"                                                               
#>  [95] ""                                                                                                            
#>  [96] "|********************| 100%  0.00sec 12.03min"                                                               
#>  [97] ""                                                                                                            
#>  [98] "|********************| 100%  0.00sec 12.05min"                                                               
#>  [99] ""                                                                                                            
#> [100] "|********************| 100%  0.00sec 12.05min"
system.time(saveRDS(bamlss_model, file = path_modelled_data))
#>    user  system elapsed 
#>   2.213   0.008   2.226
saveRDS(form, file = path_modelled_form)

Time to execute the task

Only useful when executed with Rscript.

proc.time()
#>     user   system  elapsed 
#> 2947.190    8.916  757.571