if (FALSE) ## Simulate data.
set.seed(123)
d <- GAMart(n = 27000, sd = -1)
## Write data to disc.
tf <- tempdir()
write.table(d, file.path(tf, "d.raw"), quote = FALSE, row.names = FALSE, sep = ",")
## Model formula.
f <- list(
y ~ s(x1,k=40) + s(x2,k=40) + s(x3,k=40) + te(lon,lat,k=10),
sigma ~ s(x1,k=40) + s(x2,k=40) + s(x3,k=40) + te(lon,lat,k=10)
)
## Specify 50 batches with 1000 observations.
batch_ids <- c("nobs" = 1000, "nbatch" = 50)
## Note, can also be a list of indices, e.g.
## batch_ids <- lapply(1:50, function(i) { sample(1:nrow(d), size = 1000) })
## Different flavors:
## (1) Using "out-of-sample" aic for smoothing
## variance estimation. Update is only accepted
## if the "out-of-sample" log-likelihood is
## increased. If data is a filepath, the data set is
## read into R using package ff and model and
## design matrices are processed with ff. This may
## take some time depending on the size of the data.
set.seed(1)
b1 <- bamlss(f, data = file.path(tf, "d.raw"),
sampler = FALSE, optimizer = opt_bbfit,
batch_ids = batch_ids, nu = 0.1, aic = TRUE, eps_loglik = -Inf,
always = FALSE)
## Plot estimated effects.
## plot(b1)
## Plot coefficient paths for x3 in mu.
## pathplot(b1, name = "mu.s.s(x3).b")
## (2) Same but always update, this mimics the classic SGD.
## Note, for prediction only the last iteration is
## used in this case. To use more iterations use opt_bbfitp(),
## Then iterations are stored as "mcmc" object and we can
## predict using the burnin argment, e.g.,
## p <- predict(b2, model = "mu", burnin = 35)
set.seed(2)
b2 <- bamlss(f, data = file.path(tf, "d.raw"),
sampler = FALSE, optimizer = opt_bbfit,
batch_ids = batch_ids, nu = 0.1, aic = TRUE, eps_loglik = -Inf,
always = TRUE)
## Plot coefficient paths for x3 in mu.
## pathplot(b2, name = "mu.s.s(x3).b")
## (3) Boosting type flavor, only update model term with
## the largest contribution in the "out-of-sample"
## log-likelihood. In this case, if edf = 0 during
## runtime of the algorithm, no model has an additional
## contribution and the algorithm converges. This
## behavior is controlled by argument eps_loglik, the
## higher eps_loglik, the more restrictive is the
## updating step.
## Initialize intercepts.
set.seed(0)
batch_ids <- lapply(1:400, function(i) { sample(1:nrow(d), size = 1000) })
b0 <- bamlss(y ~ 1, data = d, sampler = FALSE, optimizer = opt_bbfitp,
batch_ids = batch_ids)
## Compute starting values, remove the first
## 10 iterates and compute the mean of the
## remaining iterates.
start <- coef(b0, FUN = mean, burnin = 200)
## Start boosting, only update if change in
## "out-of-sample" log-likelihood is 0.1
## eps_loglik = 0.001.
b3 <- bamlss(f, data = d, sampler = FALSE, optimizer = opt_bbfit,
batch_ids = batch_ids, nu = 0.1, aic = TRUE, eps_loglik = 0.001,
select = TRUE, always = FALSE, start = start)
## Plot log-likelihood contributions.
## contribplot(b3)
## In this case, the algorithm did not converge,
## we need more iterations/batches.
## Note, prediction uses last iterate.
p3 <- predict(b3, model = "mu")
## (4) Use slice sampling under the "out-of-sample"
## log likelihood for estimation of smoothing
## variances. In this case model terms are always
## updated ad parameter paths behave like a MCMC
## chain. Therefore, use opt_bbfitp(), which stores
## parameter paths as "mcmc" objects and we can
## inspect using traceplots. Note nu = 1 if
## slice = TRUE.
set.seed(4)
b4 <- bamlss(f, data = d, sampler = FALSE, optimizer = opt_bbfitp,
batch_ids = batch_ids, aic = TRUE, slice = TRUE)
## plot(b4)
## Plot parameter updates/samples.
## plot(b4, which = "samples")
## Predict with burnin and compute mean
## prediction of the last 20 iterates.
p4 <- predict(b4, model = "mu", burnin = 30, FUN = mean)
Run the code above in your browser using DataLab