This function is intended to be used on the right hand side of the
formula
argument to create_sampler
or
generate_data
. It creates an additive regression term in the
model's linear predictor. By default, the prior for the regression
coefficients is improper uniform. If b0
or Q0
are specified
the prior becomes normal with mean b0
(default 0) and variance
(matrix) sigma_^2 Q0^-1
where sigma_^2
is the overall scale
parameter of the model, if any.
reg(
formula = ~1,
remove.redundant = FALSE,
sparse = NULL,
X = NULL,
Q0 = NULL,
b0 = NULL,
R = NULL,
r = NULL,
S = NULL,
s = NULL,
lower = NULL,
upper = NULL,
name = "",
perm = NULL,
debug = FALSE,
e = parent.frame()
)
a formula specifying the predictors to be used in the model,
in the same way as the right hand side of the formula
argument of
R's lm
function. Variable names are looked up in the data frame
passed as data
argument to create_sampler
or
generate_data
, or in environment(formula)
.
whether redundant columns should be removed from the
design matrix. Default is FALSE
. But note that treatment contrasts are
automatically applied to all factor variables in formula
.
whether the model matrix associated with formula
should
be sparse. The default is to base this on a simple heuristic.
a (possibly sparse) design matrix can be specified directly, as an
alternative to the creation of one based on formula
. If X
is
specified formula
is ignored.
prior precision matrix for the regression effects. The default is a zero matrix corresponding to a noninformative improper prior. It can be specified as a scalar value, as a numeric vector of appropriate length, or as a matrix object.
prior mean for the regression effect. Defaults to a zero vector. It can be specified as a scalar value or as a numeric vector of appropriate length.
optional constraint matrix for equality restrictions R'x = r where
x
is the vector of regression effects.
right hand side for the equality constraints.
optional constraint matrix for inequality constraints S'x >= s where x is the vector of regression effects.
right hand side for the inequality constraints.
as an alternative to s
, lower
and upper
may be specified
for two-sided constraints lower <= S'x <= upper.
as an alternative to s
, lower
and upper
may be specified
for two-sided constraints lower <= S'x <= upper.
the name of the model component. This name is used in the output of the
MCMC simulation function MCMCsim
. By default the name will be 'reg'
with the number of the model term attached.
whether permutation should be used in the Cholesky decomposition used for updating the model component's coefficient. Default is based on a simple heuristic.
if TRUE
a breakpoint is set at the beginning of the posterior
draw function associated with this model component. Mainly intended for developers.
for internal use only.
an object with precomputed quantities and functions for sampling from prior or conditional posterior distributions for this model component. Only intended for internal use by other package functions.
# NOT RUN {
data(iris)
# default: flat priors on regression coefficients
sampler <- create_sampler(Sepal.Length ~
reg(~ Petal.Length + Species, name="beta"),
data=iris
)
sim <- MCMCsim(sampler, burnin=100, n.iter=400)
summary(sim)
# (weakly) informative normal priors on regression coefficients
sampler <- create_sampler(Sepal.Length ~
reg(~ Petal.Length + Species, Q0=1e-2, name="beta"),
data=iris
)
sim <- MCMCsim(sampler, burnin=100, n.iter=400)
summary(sim)
# binary regression
sampler <- create_sampler(Species == "setosa" ~
reg(~ Sepal.Length, Q=0.1, name="beta"),
family="binomial", data=iris)
sim <- MCMCsim(sampler, burnin=100, n.iter=400)
summary(sim)
pred <- predict(sim)
str(pred)
# example with equality constrained regression effects
n <- 500
df <- data.frame(x=runif(n))
df$y <- rnorm(n, 1 + 2*df$x)
R <- matrix(1, 2, 1)
r <- 3
sampler <- create_sampler(y ~ reg(~ 1 + x, R=R, r=r, name="beta"), data=df)
sim <- MCMCsim(sampler)
summary(sim)
plot(sim, "beta")
summary(transform_dc(sim$beta, fun=function(x) crossprod_mv(R, x) - r))
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab