samples: Extract Samples of Coefficients and Variances

View source: R/samples.R

samplesR Documentation

Extract Samples of Coefficients and Variances

Description

Function to extract the samples generated with Markov chain Monte Carlo simulation.

Usage

samples(object, model = NULL, term = NULL, coda = TRUE, acf = FALSE, ...)

Arguments

object

an object of class "bayesx".

model

for which model the samples should be provided, either an integer or a character, e.g. model = "mcmc.model".

term

character or integer, the term for which samples should be extracted. Also samples of linear effects may be returned if available and term = "linear-samples", or of the variance if term = "var-samples". If set to NULL, the samples of the linear effects will be returned.

acf

if set to TRUE, the autocorrelation function of the samples will be provided.

coda

if set to TRUE the function will return objects of class "mcmc" or "mcmc.list" as provided in the coda package.

...

further arguments passed to function acf, e.g. argument lag.max if acf = TRUE.

Value

A data.frame or an object of class "mcmc" or "mcmc.list", if argument coda = TRUE.

Author(s)

Nikolaus Umlauf, Thomas Kneib, Stefan Lang, Achim Zeileis.

See Also

bayesx.

Examples

## Not run: 
## generate some data
set.seed(111)
n <- 200

## regressor
dat <- data.frame(x = runif(n, -3, 3))

## response 
dat$y <- with(dat, 1.5 + sin(x) + rnorm(n, sd = 0.6))

## estimate model
b <- bayesx(y ~ sx(x), data = dat)

## extract samples for the P-spline
sax <- samples(b, term = "sx(x)")
colnames(sax)

## plotting
plot(sax)

## linear effects samples
samples(b, term = "linear-samples")

## for acf, increase lag
sax <- samples(b, term = c("linear-samples", "var-samples", "sx(x)"),
  acf = TRUE, lag.max = 200, coda = FALSE)
names(sax)
head(sax)


## plot maximum autocorrelation 
## of all parameters
sax <- samples(b, term = c("linear-samples", "var-samples", "sx(x)"), 
  acf = TRUE, lag.max = 50, coda = FALSE)
names(sax)
matplot(y = apply(sax, 1, max), type = "h", 
  ylab = "ACF", xlab = "lag")

## example using multiple chains
b <- bayesx(y ~ sx(x), data = dat, chains = 3)
sax <- samples(b, term = "sx(x)")
plot(sax)

## End(Not run)

R2BayesX documentation built on Oct. 20, 2023, 9:11 a.m.