if (FALSE) {
## 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)
}
## 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")
if (FALSE) {
## 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)"])
}
Run the code above in your browser using DataLab