Full model with flexible prior: 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-11-full-re-p1.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

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

Run the model of interest and save results

{
    sink(path_modelled_sink)
    bamlss_model <- bamlss(
        form, family = "binomial", data = bwdata_model,
        n.iter = 8000, burnin = 0, cores = 4, combine = FALSE, light = TRUE
    )
    sink()
}
readLines(path_modelled_sink)
#>   [1] "AICc 139298.7 logPost -8377857 logLik -69507.4 edf 141.88 eps 0.5483 iteration   1"                                                                  
#>   [2] "AICc 129929.2 logPost -7939268 logLik -64781.3 edf 183.17 eps 0.2223 iteration   2"                                                                  
#>   [3] "AICc 129565.2 logPost -8992485 logLik -64572.1 edf 210.29 eps 0.0380 iteration   3"                                                                  
#>   [4] "AICc 129537.6 logPost -6947691 logLik -64550.5 edf 218.13 eps 0.0079 iteration   4"                                                                  
#>   [5] "AICc 129531.4 logPost -1047376 logLik -64545.1 edf 220.39 eps 0.0029 iteration   5"                                                                  
#>   [6] "AICc 129529.0 logPost -1379598 logLik -64543.1 edf 221.17 eps 0.0014 iteration   6"                                                                  
#>   [7] "AICc 129527.8 logPost -1508622 logLik -64542.2 edf 221.48 eps 0.0008 iteration   7"                                                                  
#>   [8] "AICc 129527.2 logPost -1490440 logLik -64541.8 edf 221.62 eps 0.0005 iteration   8"                                                                  
#>   [9] "AICc 129526.8 logPost -1419587 logLik -64541.5 edf 221.68 eps 0.0003 iteration   9"                                                                  
#>  [10] "AICc 129526.5 logPost -1344386 logLik -64541.3 edf 221.71 eps 0.0003 iteration  10"                                                                  
#>  [11] "AICc 129526.3 logPost -1280304 logLik -64541.2 edf 221.73 eps 0.0002 iteration  11"                                                                  
#>  [12] "AICc 129526.2 logPost -1231166 logLik -64541.2 edf 221.74 eps 0.0001 iteration  12"                                                                  
#>  [13] "AICc 129526.2 logPost -1193967 logLik -64541.1 edf 221.75 eps 0.0001 iteration  13"                                                                  
#>  [14] "AICc 129526.1 logPost -1159101 logLik -64541.1 edf 221.75 eps 0.0001 iteration  14"                                                                  
#>  [15] "AICc 129526.1 logPost -1137510 logLik -64541.1 edf 221.76 eps 0.0001 iteration  15"                                                                  
#>  [16] "AICc 129526.1 logPost -1137510 logLik -64541.1 edf 221.76 eps 0.0001 iteration  15"                                                                  
#>  [17] "elapsed time: 10.18min"                                                                                                                              
#>  [18] "Starting the sampler..."                                                                                                                             
#>  [19] "Starting the sampler..."                                                                                                                             
#>  [20] "Starting the sampler..."                                                                                                                             
#>  [21] "Starting the sampler..."                                                                                                                             
#>  [22] ""                                                                                                                                                    
#>  [23] ""                                                                                                                                                    
#>  [24] ""                                                                                                                                                    
#>  [25] ""                                                                                                                                                    
#>  [26] "||  | |                                                                          |   0%  |   0%  448.76min 448.64min|   0% |   0% 448.53min448.59min"
#>  [27] "|*                   |   5% 424.53min 22.34min"                                                                                                      
#>  [28] "|*                   |   5% 425.23min 22.38min"                                                                                                      
#>  [29] "|*                   |   5% 425.23min 22.38min"                                                                                                      
#>  [30] "|*                   |   5% 425.37min 22.39min"                                                                                                      
#>  [31] "|**                  |  10% 408.50min 45.39min"                                                                                                      
#>  [32] "|**                  |  10% 408.82min 45.42min"                                                                                                      
#>  [33] "|**                  |  10% 408.82min 45.42min"                                                                                                      
#>  [34] "|**                  |  10% 408.88min 45.43min"                                                                                                      
#>  [35] "|***                 |  15% 387.49min 68.38min"                                                                                                      
#>  [36] "|***                 |  15% 387.71min 68.42min"                                                                                                      
#>  [37] "|***                 |  15% 387.71min 68.42min"                                                                                                      
#>  [38] "|***                 |  15% 387.74min 68.43min"                                                                                                      
#>  [39] "|****                |  20% 365.46min 91.37min"                                                                                                      
#>  [40] ""                                                                                                                                                    
#>  [41] "|****                |  20% 365.61min 91.40min|****                |  20% 365.61min 91.40min"                                                        
#>  [42] "|****                |  20% 365.64min 91.41min"                                                                                                      
#>  [43] "|*****               |  25% 343.18min 114.39min"                                                                                                     
#>  [44] "|*****               |  25% 343.30min 114.43min"                                                                                                     
#>  [45] "|*****               |  25% 343.30min 114.43min"                                                                                                     
#>  [46] "|*****               |  25% 343.32min 114.44min"                                                                                                     
#>  [47] "|******              |  30% 320.60min 137.40min"                                                                                                     
#>  [48] ""                                                                                                                                                    
#>  [49] "|******              |  30% 320.69min 137.44min|******              |  30% 320.69min 137.44min"                                                      
#>  [50] "|******              |  30% 320.70min 137.44min"                                                                                                     
#>  [51] "|*******             |  35% 297.86min 160.39min"                                                                                                     
#>  [52] "|*******             |  35% 297.93min 160.42min"                                                                                                     
#>  [53] "|*******             |  35% 297.93min 160.42min"                                                                                                     
#>  [54] "|*******             |  35% 297.94min 160.43min"                                                                                                     
#>  [55] "|********            |  40% 275.06min 183.38min"                                                                                                     
#>  [56] "|********            |  40% 275.12min 183.41min"                                                                                                     
#>  [57] "|********            |  40% 275.12min 183.41min"                                                                                                     
#>  [58] "|********            |  40% 275.13min 183.42min"                                                                                                     
#>  [59] "|*********           |  45% 252.23min 206.37min"                                                                                                     
#>  [60] "|*********           |  45% 252.28min 206.41min"                                                                                                     
#>  [61] "|*********           |  45% 252.28min 206.41min"                                                                                                     
#>  [62] "|*********           |  45% 252.29min 206.42min"                                                                                                     
#>  [63] "|**********          |  50% 229.35min 229.35min"                                                                                                     
#>  [64] ""                                                                                                                                                    
#>  [65] "|**********          |  50% 229.39min 229.39min|**********          |  50% 229.39min 229.39min"                                                      
#>  [66] "|**********          |  50% 229.40min 229.40min"                                                                                                     
#>  [67] "|***********         |  55% 206.42min 252.29min"                                                                                                     
#>  [68] "|***********         |  55% 206.45min 252.33min"                                                                                                     
#>  [69] "|***********         |  55% 206.45min 252.33min"                                                                                                     
#>  [70] "|***********         |  55% 206.46min 252.34min"                                                                                                     
#>  [71] "|************        |  60% 183.54min 275.31min"                                                                                                     
#>  [72] "|************        |  60% 183.57min 275.35min"                                                                                                     
#>  [73] "|************        |  60% 183.57min 275.35min"                                                                                                     
#>  [74] "|************        |  60% 183.57min 275.35min"                                                                                                     
#>  [75] "|*************       |  65% 160.52min 298.11min"                                                                                                     
#>  [76] "|*************       |  65% 160.55min 298.16min"                                                                                                     
#>  [77] "|*************       |  65% 160.55min 298.16min"                                                                                                     
#>  [78] "|*************       |  65% 160.55min 298.16min"                                                                                                     
#>  [79] "|**************      |  70% 137.38min 320.56min"                                                                                                     
#>  [80] "|**************      |  70% 137.41min 320.61min"                                                                                                     
#>  [81] "|**************      |  70% 137.41min 320.61min"                                                                                                     
#>  [82] "|**************      |  70% 137.41min 320.62min"                                                                                                     
#>  [83] "|***************     |  75% 114.35min 343.04min"                                                                                                     
#>  [84] "|***************     |  75% 114.36min 343.09min"                                                                                                     
#>  [85] ""                                                                                                                                                    
#>  [86] "|***************     |  75% 114.36min 343.09min|***************     |  75% 114.36min 343.09min"                                                      
#>  [87] "|****************    |  80% 91.37min 365.48min"                                                                                                      
#>  [88] "|****************    |  80% 91.38min 365.53min"                                                                                                      
#>  [89] ""                                                                                                                                                    
#>  [90] "|****************    |  80% 91.38min 365.53min|****************    |  80% 91.38min 365.53min"                                                        
#>  [91] "|*****************   |  85% 68.48min 388.04min"                                                                                                      
#>  [92] "|*****************   |  85% 68.49min 388.09min"                                                                                                      
#>  [93] "|*****************   |  85% 68.49min 388.09min"                                                                                                      
#>  [94] "|*****************   |  85% 68.49min 388.09min"                                                                                                      
#>  [95] "|******************  |  90% 45.61min 410.48min"                                                                                                      
#>  [96] ""                                                                                                                                                    
#>  [97] "|******************  |  90% 45.61min 410.53min|******************  |  90% 45.61min 410.53min"                                                        
#>  [98] "|******************  |  90% 45.61min 410.53min"                                                                                                      
#>  [99] "|******************* |  95% 22.79min 433.05min"                                                                                                      
#> [100] "|******************* |  95% 22.79min 433.10min"                                                                                                      
#> [101] ""                                                                                                                                                    
#> [102] "|******************* |  95% 22.79min 433.10min|******************* |  95% 22.79min 433.10min"                                                        
#> [103] "|********************| 100%  0.00sec 455.74min"                                                                                                      
#> [104] ""                                                                                                                                                    
#> [105] "|********************| 100%  0.00sec 455.79min"                                                                                                      
#> [106] ""                                                                                                                                                    
#> [107] "|********************| 100%  0.00sec 455.79min"                                                                                                      
#> [108] ""                                                                                                                                                    
#> [109] "|********************| 100%  0.00sec 455.79min"
system.time(saveRDS(bamlss_model, file = path_modelled_data))
#>    user  system elapsed 
#>   6.596   0.040   6.655
saveRDS(form, file = path_modelled_form)

Time to execute the task

Only useful when executed with Rscript.

proc.time()
#>      user    system   elapsed 
#> 110184.76     35.36  28081.39