Full model: fitting


Low birthweight model including covariates at municipality level with linear effects.

Load packages, read data and source custom scripts

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, "lbw-10-full-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

form_aux <- lbw ~ sex + born_race + birth_place +
    marital_status + study_years + consult_num + s(age) +
    s(wk_ini) + s(rivwk_conception, bs = "cc") +
    remoteness + prop_tap_toilet + s(res_muni, bs = "re")
form <- update(form_aux, . ~ . +
                s(neg_mbsi_mean_1wk, pos_mbsi_mean_1wk, k = 70) +
                s(neg_mckee_mean_8wk, pos_mckee_mean_8wk, k = 70) +
                s(neg_ext_mbsi_mean_8wk, pos_ext_mbsi_mean_8wk, k = 70))

Run the model of interest and save results

{
    sink(path_modelled_sink)
    bamlss_model <- bamlss(
        form, family = "binomial", data = bwdata_model,
        n.iter = 3000, burnin = 0, cores = 4, combine = FALSE, light = TRUE
    )
    sink()
}
readLines(path_modelled_sink)
#>   [1] "AICc 139298.7 logPost -69468.9 logLik -69507.4 edf 141.88 eps 0.5483 iteration   1"          
#>   [2] "AICc 129929.2 logPost -65763.4 logLik -64781.3 edf 183.17 eps 0.2223 iteration   2"          
#>   [3] "AICc 129565.2 logPost -65044.3 logLik -64572.1 edf 210.29 eps 0.0380 iteration   3"          
#>   [4] "AICc 129537.6 logPost -64994.8 logLik -64550.5 edf 218.13 eps 0.0079 iteration   4"          
#>   [5] "AICc 129531.4 logPost -65029.6 logLik -64545.1 edf 220.39 eps 0.0029 iteration   5"          
#>   [6] "AICc 129529.0 logPost -65062.6 logLik -64543.1 edf 221.17 eps 0.0014 iteration   6"          
#>   [7] "AICc 129527.8 logPost -65075.5 logLik -64542.2 edf 221.48 eps 0.0008 iteration   7"          
#>   [8] "AICc 129527.2 logPost -65073.9 logLik -64541.8 edf 221.62 eps 0.0005 iteration   8"          
#>   [9] "AICc 129526.8 logPost -65067.0 logLik -64541.5 edf 221.68 eps 0.0003 iteration   9"          
#>  [10] "AICc 129526.5 logPost -65059.7 logLik -64541.3 edf 221.71 eps 0.0003 iteration  10"          
#>  [11] "AICc 129526.3 logPost -65053.5 logLik -64541.2 edf 221.73 eps 0.0002 iteration  11"          
#>  [12] "AICc 129526.2 logPost -65048.7 logLik -64541.2 edf 221.74 eps 0.0001 iteration  12"          
#>  [13] "AICc 129526.2 logPost -65045.0 logLik -64541.1 edf 221.75 eps 0.0001 iteration  13"          
#>  [14] "AICc 129526.1 logPost -65041.7 logLik -64541.1 edf 221.75 eps 0.0001 iteration  14"          
#>  [15] "AICc 129526.1 logPost -65039.5 logLik -64541.1 edf 221.76 eps 0.0001 iteration  15"          
#>  [16] "AICc 129526.1 logPost -65039.5 logLik -64541.1 edf 221.76 eps 0.0001 iteration  15"          
#>  [17] "elapsed time: 10.11min"                                                                      
#>  [18] "Starting the sampler..."                                                                     
#>  [19] "Starting the sampler..."                                                                     
#>  [20] "Starting the sampler..."                                                                     
#>  [21] "Starting the sampler..."                                                                     
#>  [22] ""                                                                                            
#>  [23] "|                    |   0% 175.77min"                                                       
#>  [24] "|                    |   0% 175.91min"                                                       
#>  [25] "|                    |   0% 175.53min"                                                       
#>  [26] "|                    |   0% 177.95min"                                                       
#>  [27] "|*                   |   5% 169.42min  8.92min"                                              
#>  [28] "|*                   |   5% 169.36min  8.91min"                                              
#>  [29] "|*                   |   5% 169.46min  8.92min"                                              
#>  [30] "|*                   |   5% 169.47min  8.92min"                                              
#>  [31] "|**                  |  10% 160.46min 17.83min"                                              
#>  [32] "|**                  |  10% 160.43min 17.83min"                                              
#>  [33] "|**                  |  10% 160.42min 17.82min"                                              
#>  [34] "|**                  |  10% 160.48min 17.83min"                                              
#>  [35] "|***                 |  15% 150.99min 26.65min"                                              
#>  [36] "|***                 |  15% 150.97min 26.64min"                                              
#>  [37] "|***                 |  15% 150.97min 26.64min"                                              
#>  [38] "|***                 |  15% 151.01min 26.65min"                                              
#>  [39] "|****                |  20% 141.25min 35.31min"                                              
#>  [40] "|****                |  20% 141.25min 35.31min"                                              
#>  [41] "|****                |  20% 141.32min 35.33min"                                              
#>  [42] "|****                |  20% 141.28min 35.32min"                                              
#>  [43] "|*****               |  25% 131.81min 43.94min"                                              
#>  [44] ""                                                                                            
#>  [45] "|*****               |  25%|*****               |  25%  131.86min131.81min  43.95min43.94min"
#>  [46] "|*****               |  25% 131.83min 43.94min"                                              
#>  [47] "|******              |  30% 122.72min 52.59min"                                              
#>  [48] "|******              |  30% 122.71min 52.59min"                                              
#>  [49] "|******              |  30% 122.71min 52.59min"                                              
#>  [50] "|******              |  30% 122.76min 52.61min"                                              
#>  [51] "|*******             |  35% 113.87min 61.32min"                                              
#>  [52] "|*******             |  35% 113.86min 61.31min"                                              
#>  [53] "|*******             |  35% 113.86min 61.31min"                                              
#>  [54] "|*******             |  35% 113.90min 61.33min"                                              
#>  [55] "|********            |  40% 105.09min 70.06min"                                              
#>  [56] "|********            |  40% 105.08min 70.05min"                                              
#>  [57] "|********            |  40% 105.08min 70.05min"                                              
#>  [58] "|********            |  40% 105.11min 70.07min"                                              
#>  [59] "|*********           |  45% 96.34min 78.82min"                                               
#>  [60] "|*********           |  45% 96.33min 78.82min"                                               
#>  [61] "|*********           |  45% 96.33min 78.82min"                                               
#>  [62] "|*********           |  45% 96.36min 78.84min"                                               
#>  [63] "|**********          |  50% 87.58min 87.58min"                                               
#>  [64] "|**********          |  50% 87.57min 87.57min"                                               
#>  [65] "|**********          |  50% 87.57min 87.57min"                                               
#>  [66] "|**********          |  50% 87.59min 87.59min"                                               
#>  [67] "|***********         |  55% 78.78min 96.29min"                                               
#>  [68] "|***********         |  55% 78.78min 96.29min"                                               
#>  [69] "|***********         |  55% 78.78min 96.29min"                                               
#>  [70] "|***********         |  55% 78.80min 96.31min"                                               
#>  [71] "|************        |  60% 70.02min 105.04min"                                              
#>  [72] "|************        |  60% 70.02min 105.03min"                                              
#>  [73] "|************        |  60% 70.02min 105.03min"                                              
#>  [74] "|************        |  60% 70.03min 105.05min"                                              
#>  [75] "|*************       |  65% 61.26min 113.78min"                                              
#>  [76] "|*************       |  65% 61.26min 113.77min"                                              
#>  [77] "|*************       |  65% 61.26min 113.77min"                                              
#>  [78] "|*************       |  65% 61.27min 113.79min"                                              
#>  [79] "|**************      |  70% 52.49min 122.49min"                                              
#>  [80] "|**************      |  70% 52.49min 122.48min"                                              
#>  [81] "|**************      |  70% 52.49min 122.48min"                                              
#>  [82] "|**************      |  70% 52.50min 122.50min"                                              
#>  [83] "|***************     |  75% 43.75min 131.26min"                                              
#>  [84] "|***************     |  75% 43.75min 131.25min"                                              
#>  [85] "|***************     |  75% 43.75min 131.26min"                                              
#>  [86] "|***************     |  75% 43.76min 131.27min"                                              
#>  [87] "|****************    |  80% 35.03min 140.13min"                                              
#>  [88] "|****************    |  80% 35.03min 140.12min"                                              
#>  [89] "|****************    |  80% 35.03min 140.13min"                                              
#>  [90] "|****************    |  80% 35.04min 140.14min"                                              
#>  [91] "|*****************   |  85% 26.30min 149.02min"                                              
#>  [92] "|*****************   |  85% 26.30min 149.01min"                                              
#>  [93] "|*****************   |  85% 26.30min 149.02min"                                              
#>  [94] "|*****************   |  85% 26.30min 149.03min"                                              
#>  [95] "|******************  |  90% 17.55min 157.91min"                                              
#>  [96] "|******************  |  90% 17.55min 157.91min"                                              
#>  [97] "|******************  |  90% 17.55min 157.91min"                                              
#>  [98] "|******************  |  90% 17.55min 157.93min"                                              
#>  [99] "|******************* |  95%  8.78min 166.80min"                                              
#> [100] "|******************* |  95%  8.78min 166.79min"                                              
#> [101] "|******************* |  95%  8.78min 166.80min"                                              
#> [102] "|******************* |  95%  8.78min 166.82min"                                              
#> [103] "|********************| 100%  0.00sec 175.46min"                                              
#> [104] ""                                                                                            
#> [105] "|********************| 100%  0.00sec 175.45min"                                              
#> [106] ""                                                                                            
#> [107] "|********************| 100%  0.00sec 175.47min"                                              
#> [108] ""                                                                                            
#> [109] "|********************| 100%  0.00sec 175.46min"
system.time(saveRDS(bamlss_model, file = path_modelled_data))
#>    user  system elapsed 
#>   4.998   0.024   5.082
saveRDS(form, file = path_modelled_form)

Time to execute the task

Only useful when executed with Rscript.

proc.time()
#>      user    system   elapsed 
#> 42919.275    20.108 11256.108