View source: R/fitted.bayesx.R
fitted.bayesx | R Documentation |
Extractor functions to the fitted values/model residuals of the estimated model with
bayesx
and fitted model term partial effects/residuals.
## S3 method for class 'bayesx'
fitted(object, model = NULL, term = NULL, ...)
## S3 method for class 'bayesx'
residuals(object, model = NULL, term = NULL, ...)
object |
an object of class |
model |
for which model the fitted values/residuals should be provided, either an integer or
a character, e.g. |
term |
if not |
... |
not used. |
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
.
Nikolaus Umlauf, Thomas Kneib, Stefan Lang, Achim Zeileis.
read.bayesx.output
.
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.