fitted.bayesx: Extract BayesX Fitted Values and Residuals

View source: R/fitted.bayesx.R

fitted.bayesxR Documentation

Extract BayesX Fitted Values and Residuals

Description

Extractor functions to the fitted values/model residuals of the estimated model with bayesx and fitted model term partial effects/residuals.

Usage

## S3 method for class 'bayesx'
fitted(object, model = NULL, term = NULL, ...)

## S3 method for class 'bayesx'
residuals(object, model = NULL, term = NULL, ...)

Arguments

object

an object of class "bayesx".

model

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

term

if not NULL, the function will search for the term fitted values/residuals specified here, either an integer or a character, eg term = "sx(x)".

...

not used.

Value

For fitted.bayesx, either the fitted linear predictor and mean or if e.g. term = "sx(x)", an object with class "xx.bayesx", where "xx" is depending of the type of the term. In principle the returned term object is simply a data.frame containing the covariate(s) and its effects, depending on the estimation method, e.g. for MCMC estimated models, mean/median fitted values and other quantities are returned. Several additional informations on the term are provided in the attributes of the object. For all types of terms plotting functions are provided, see function plot.bayesx.

Using residuals.bayesx will either return the mean model residuals or the mean partial residuals of a term specified in argument term.

Author(s)

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

See Also

read.bayesx.output.

Examples

## Not run: 
## generate some data
set.seed(121)
n <- 500

## regressors
dat <- data.frame(x = runif(n, -3, 3), z = runif(n, 0, 1),
  w = runif(n, 0, 3))

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

## estimate model
b1 <- bayesx(y ~ sx(x) + z + w, data = dat)

## extract fitted values
fit <- fitted(b1)
hist(fit, freq = FALSE)

## now extract 1st model term
## and plot it
fx <- fitted(b1, term = "sx(x)")
plot(fx)

## extract model residuals
hist(residuals(b1))

## extract partial residuals for sx(x)
pres <- residuals(b1, term = "sx(x)")
plot(fx, ylim = range(pres[, 2]))
points(pres)

## End(Not run)

## now another example with
## use of read.bayesx.output
## load example data from
## package R2BayesX
dir <- file.path(find.package("R2BayesX"), "examples", "ex01")
b2 <- read.bayesx.output(dir)

## extract fitted values
hist(fitted(b2))

## extract model term of x
## and plot it
fx <- fitted(b2, term = "sx(x)")
plot(fx)


## have a look at the attributes
names(attributes(fx))

## extract the sampling path of the variance
spv <- attr(fx, "variance.sample")
plot(spv, type = "l")


## Not run: 
## combine model objects
b <- c(b1, b2)

## extract fitted terms for second model
fit <- fitted(b, model = 2, term = 1:2)
names(fit)
plot(fit["sx(id)"])

## End(Not run)

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