Linear model 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-08-lm-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 + 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 4465334. logPost -2454954 logLik -2232662 edf 5.0881 eps 0.0321 iteration 1"
#> [2] "AICc 4458447. logPost -2231450 logLik -2229211 edf 12.679 eps 0.0074 iteration 2"
#> [3] "AICc 4456610. logPost -2228818 logLik -2228272 edf 33.194 eps 0.0038 iteration 3"
#> [4] "AICc 4456383. logPost -2228414 logLik -2228146 edf 44.877 eps 0.0015 iteration 4"
#> [5] "AICc 4456381. logPost -2228395 logLik -2228144 edf 46.357 eps 0.0001 iteration 5"
#> [6] "AICc 4456381. logPost -2228395 logLik -2228144 edf 46.352 eps 0.0000 iteration 6"
#> [7] "AICc 4456381. logPost -2228395 logLik -2228144 edf 46.352 eps 0.0000 iteration 6"
#> [8] "elapsed time: 9.68sec"
#> [9] "Starting the sampler..."
#> [10] "Starting the sampler..."
#> [11] "Starting the sampler..."
#> [12] "Starting the sampler..."
#> [13] ""
#> [14] ""
#> [15] "| | | 0% 13.00min | 0% 12.96min"
#> [16] "| | 0% 13.13min"
#> [17] "| | 0% 13.12min"
#> [18] ""
#> [19] ""
#> [20] "|* | 5%|* | 5%|* | 5% 12.08min12.07min12.08min 38.15sec38.12sec38.15sec"
#> [21] "|* | 5% 12.18min 38.45sec"
#> [22] ""
#> [23] "|** | 10% 11.25min 1.25min|** | 10% 11.25min 1.25min"
#> [24] "|** | 10% 11.26min 1.25min"
#> [25] "|** | 10% 11.34min 1.26min"
#> [26] "|*** | 15% 10.50min 1.85min"
#> [27] "|*** | 15% 10.50min 1.85min"
#> [28] "|*** | 15% 10.53min 1.86min"
#> [29] "|*** | 15% 10.55min 1.86min"
#> [30] "|**** | 20% 9.81min 2.45min"
#> [31] "|**** | 20% 9.83min 2.46min"
#> [32] ""
#> [33] "|**** | 20% 9.85min 2.46min|**** | 20% 9.86min 2.46min"
#> [34] "|***** | 25% 9.17min 3.06min"
#> [35] ""
#> [36] "|***** | 25%|***** | 25% 9.21min 9.21min 3.07min 3.07min"
#> [37] "|***** | 25% 9.20min 3.07min"
#> [38] "|****** | 30% 8.55min 3.66min"
#> [39] "|****** | 30% 8.56min 3.67min"
#> [40] "|****** | 30% 8.58min 3.68min"
#> [41] "|****** | 30% 8.57min 3.67min"
#> [42] "|******* | 35% 7.92min 4.26min"
#> [43] "|******* | 35% 7.94min 4.28min"
#> [44] "|******* | 35% 7.94min 4.28min"
#> [45] "|******* | 35% 7.96min 4.28min"
#> [46] "|******** | 40% 7.31min 4.87min"
#> [47] "|******** | 40% 7.32min 4.88min"
#> [48] "|******** | 40% 7.32min 4.88min"
#> [49] "|******** | 40% 7.33min 4.89min"
#> [50] "|********* | 45% 6.70min 5.48min"
#> [51] "|********* | 45% 6.71min 5.49min"
#> [52] "|********* | 45% 6.71min 5.49min"
#> [53] "|********* | 45% 6.72min 5.50min"
#> [54] "|********** | 50% 6.09min 6.09min"
#> [55] "|********** | 50% 6.10min 6.10min"
#> [56] "|********** | 50% 6.10min 6.10min"
#> [57] "|********** | 50% 6.11min 6.11min"
#> [58] "|*********** | 55% 5.49min 6.71min"
#> [59] "|*********** | 55% 5.50min 6.72min"
#> [60] "|*********** | 55% 5.50min 6.72min"
#> [61] "|*********** | 55% 5.50min 6.72min"
#> [62] "|************ | 60% 4.88min 7.32min"
#> [63] "|************ | 60% 4.89min 7.33min"
#> [64] "|************ | 60% 4.89min 7.33min"
#> [65] "|************ | 60% 4.89min 7.34min"
#> [66] "|************* | 65% 4.27min 7.93min"
#> [67] "|************* | 65% "
#> [68] " 4.28min 7.94min|************* | 65% 4.28min 7.94min"
#> [69] "|************* | 65% 4.28min 7.95min"
#> [70] "|************** | 70% 3.66min 8.54min"
#> [71] "|************** | 70% 3.67min 8.55min"
#> [72] "|************** | 70% 3.67min 8.55min"
#> [73] "|************** | 70% 3.67min 8.56min"
#> [74] "|*************** | 75% 3.05min 9.15min"
#> [75] "|*************** | 75% 3.05min 9.16min"
#> [76] "|*************** | 75% 3.05min 9.16min"
#> [77] "|*************** | 75% 3.06min 9.18min"
#> [78] "|**************** | 80% 2.44min 9.76min"
#> [79] "|**************** | 80% 2.44min 9.77min"
#> [80] "|**************** | 80% 2.44min 9.78min"
#> [81] "|**************** | 80% 2.45min 9.79min"
#> [82] "|***************** | 85% 1.83min 10.36min"
#> [83] "|***************** | 85% 1.83min 10.37min"
#> [84] "|***************** | 85% 1.83min 10.39min"
#> [85] "|***************** | 85% 1.83min 10.40min"
#> [86] "|****************** | 90% 1.22min 10.96min"
#> [87] "|****************** | 90% 1.22min 10.98min"
#> [88] "|****************** | 90% 1.22min 10.99min"
#> [89] "|****************** | 90% 1.22min 11.00min"
#> [90] "|******************* | 95% 36.54sec 11.57min"
#> [91] "|******************* | 95% 36.58sec 11.58min"
#> [92] "|******************* | 95% 36.63sec 11.60min"
#> [93] "|******************* | 95% 36.65sec 11.61min"
#> [94] "|********************| 100% 0.00sec 12.18min"
#> [95] ""
#> [96] "|********************| 100% 0.00sec 12.19min"
#> [97] ""
#> [98] "|********************| 100% 0.00sec 12.20min"
#> [99] ""
#> [100] "|********************| 100% 0.00sec 12.21min"
system.time(saveRDS(bamlss_model, file = path_modelled_data))
#> user system elapsed
#> 2.312 0.004 2.317
saveRDS(form, file = path_modelled_form)
Time to execute the task
Only useful when executed with Rscript
.
proc.time()
#> user system elapsed
#> 2987.760 8.275 767.654