examples.Rmd
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(bamlssAPI)
#>
#> Attaching package: 'bamlssAPI'
#> The following object is masked from 'package:bamlss':
#>
#> parameters
This is the first example from ?bamlss
:
set.seed(1337)
d <- GAMart()
f <- num ~ s(x1) + s(x2) + s(x3) + te(lon, lat)
b <- bamlss(f, data = d, sampler = FALSE, verbose = FALSE)
b <- apify(b, propose = "iwlsC_gp")
nsamp <- 500
samples <- matrix(
data = NA_real_,
nrow = nsamp,
ncol = length(parameters(b, "mu", "s(x1)")),
dimnames = list(NULL, names(parameters(b, "mu", "s(x1)")))
)
for (i in 1:nsamp) {
prop <- propose(b, "mu", "s(x1)")
if (log(runif(1)) <= prop$alpha) b <- accept(b, "mu", "s(x1)", prop)
samples[i,] <- parameters(b, "mu", "s(x1)")
}
par(mfrow = c(ncol(samples), 2))
plot(as.mcmc(samples), auto.layout = FALSE)
Some more API examples:
predictors(b)
#> [1] "mu" "sigma"
smooths(b, predictor = "mu")
#> [1] "s(x1)" "s(x2)" "s(x3)" "te(lon,lat)" "p"
parameters(b, "mu", "s(x1)")
#> b1 b2 b3 b4 b5
#> -0.0067264089 -0.0029561932 -0.0003810828 0.0079394603 0.0017411598
#> b6 b7 b8 b9 tau21
#> 0.0090295349 0.0001365967 -0.0175519081 -0.1597753593 0.0022726896
b <- update_logpost(b)
logpost(b)
#> [1] 229.6844
#> attr(,"outdated")
#> [1] FALSE
b <- set_parameters(b, "mu", "s(x1)", parameters(b, "mu", "s(x1)") + 0.01)
outdated(fx(b, "mu", "s(x1)"))
#> [1] TRUE
outdated(eta(b, "mu"))
#> [1] TRUE
outdated(logpost(b))
#> [1] TRUE
b <- update_logpost(b)
logpost(b)
#> [1] 190.7844
#> attr(,"outdated")
#> [1] FALSE
grad_logpost(b, "mu", "s(x1)")
#> [1] -994.198941 153.131874 -947.441555 -141.396974 -1190.468594
#> [6] -423.074531 -1271.468653 -1.201107 -1061.496652 -45.103787
hess_logpost(b, "mu", "s(x1)")
#> [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,] 20101.928 -1995.9642 16496.3887 1025.81638 18225.440 3140.46362
#> [2,] -1995.964 3835.7844 -1799.5426 -1269.69972 -2439.421 -613.94683
#> [3,] 16496.389 -1799.5426 20808.6831 946.72821 17385.198 3132.54623
#> [4,] 1025.816 -1269.6997 946.7282 5850.55190 1392.192 -17.89478
#> [5,] 18225.440 -2439.4214 17385.1982 1392.19192 30965.252 3575.27065
#> [6,] 3140.464 -613.9468 3132.5462 -17.89478 3575.271 13291.09983
#> [7,] 18739.899 -2709.6234 17859.9605 1585.12122 19880.860 3724.11362
#> [8,] -2324.002 -2737.7247 -2873.3659 2905.59741 -2601.433 3894.92587
#> [9,] 20136.782 -2178.5037 18648.3658 961.32257 20561.405 3048.84505
#> [,7] [,8] [,9]
#> [1,] 18739.899 -2324.002 20136.7817
#> [2,] -2709.623 -2737.725 -2178.5037
#> [3,] 17859.961 -2873.366 18648.3658
#> [4,] 1585.121 2905.597 961.3226
#> [5,] 19880.860 -2601.433 20561.4052
#> [6,] 3724.114 3894.926 3048.8451
#> [7,] 45004.966 -2421.137 21132.2253
#> [8,] -2421.137 5268.296 -2787.2009
#> [9,] 21132.225 -2787.201 21750.8277