Learn R Programming

BayesSIM (version 1.0.0)

fitted.bsim: Extract Fitted Values from BayesSIM

Description

Computes fitted values from a BayesSIM, using either the posterior mean or median of the estimated link function with index values. Fitted values can be returned on the latent scale or on the linear predictor scale.

Usage

# S3 method for bsim
fitted(
  object,
  type = c("latent", "linpred"),
  method = c("mean", "median"),
  ...
)

Value

A numeric vector of fitted values.

Arguments

object

A fitted object of BayesSIM or individual model.

type

Character string indicating the scale on which fitted values are returned. Default is "latent".

  • "latent": fitted response values \(\hat{y} = E(\mathbf{Y}|\mathbf{X})\).

  • "linpred": linear predictor \(X'\theta\).

method

Character string specifying the summary statistic used to compute the fitted values. Options are "mean" or "median". Default is "mean".

...

Additional arguments passed to other methods.

Examples

Run this code
# \donttest{
simdata2 <- data.frame(DATA1$X, y = DATA1$y)

# 1. One tool version
fit_one <- BayesSIM(y ~ ., data = simdata2,
                    niter = 5000, nburnin = 1000, nchain = 1)

# Check median index vector estimates with standard errors
coef(fit_one, method = "median", se = TRUE)

# Fitted index values of median prediction
fitted(fit_one, type = "linpred", method = "median")

# Residuals of median prediction
residuals(fit_one, method = "median")

# Summary of the model
summary(fit_one)

# Convergence diagnostics
nimTraceplot(fit_one)

# Goodness of fit
GOF(fit_one)

# Fitted plot
plot(fit_one)

# Prediction with 95% credible interval at new data
newx <- data.frame(X1 = rnorm(10), X2 = rnorm(10), X3 = rnorm(10), X4 = rnorm(10))
pred <- predict(fit_one, newdata = newx, interval = "credible", level = 0.95)
plot(pred)


# 2. Split version
models <- BayesSIM_setup(y ~ ., data = simdata2)
Ccompile <- compileModelAndMCMC(models)
nimSampler <- get_sampler(Ccompile)
initList <- getInit(models)
mcmc.out <- runMCMC(nimSampler, niter = 5000, nburnin = 1000, thin = 1,
                    nchains = 1, setSeed = TRUE, inits = initList,
                    summary = TRUE, samplesAsCodaMCMC = TRUE)

# "fit_split" becomes exactly the same as the class of "fit_one" object and apply generic functions.
fit_split <- as_bsim(models, mcmc.out)

# }

Run the code above in your browser using DataLab