resamplefunction: resample function

View source: R/resamplefunction.R

resamplefunctionR Documentation

resample function

Description

Resampling model estimates from glmmADMB fits

Usage

resamplefunction(
  model,
  dat,
  N,
  termsref = NULL,
  useparallel = TRUE,
  stepsalong = 11,
  allcores = FALSE
)

Arguments

model

a model fit from glmmadmb

dat

the data set for the fit (typically a data.frame)

N

the number of (re)samples to take

termsref

character with 'control' variables. These variables will be set to zero (numeric predictors) or their reference level (factors).

useparallel

use the doParallel package?

stepsalong

the resolution of steps along the range of numerical predictors

allcores

should all available cores be used (which might slow down any activity other than R substantially). If FALSE (default), all but one core are assigned to R.

Details

the function needs data sets in which columns have one dimension only (at least on the predictor side). If you get a warning or error, please check unlist(lapply(<yourdata>, ncol)) to find columns that have more than one dimension. This could happen, for example, if you applied scale() to a numerical column. You could use <yourdata>$mycol <- as.numeric(<yourdata>$mycol) to fix this issue. Also, the model has to be refitted after such an operation.

In the case of glmmADMB, if the model fitting fails in one or more of the resampling runs, this would normally break the loop. The function currently uses a form of error catching that returns NA as predicted values for such cases. The function will produce a message informing you that this happend. You might want to increase N so that you actually end up with the number of valid samples intended.

Value

a list with two items:

  • a data.frame with the predictor variables at the values for which the prediction was made

  • a matrix with the same number of rows as the first element of the list, and N columns, i.e. the predicted values for each sample

Examples

## Not run: 
library(glmmADMB)
set.seed(12345)
N <- 100
resp <- rnorm(n = N)
pred1 <- sample(letters[1:2], length(resp), T)
pred2 <- sample(LETTERS[3:4], length(resp), T)
pred3 <- rnorm(n = N)
ref <- sample(letters[15:20], length(resp), T)
xdata <- data.frame(resp, pred1, pred2, pred3, ref)
res <- glmmadmb(resp ~ pred1*pred2 + pred3 + (1|ref), data = xdata, family = "gaussian")
summary(res)

# predict along different values of $pred3
resamplefunction(res, xdata, N = 2)
# keep $pred3 at 0
resamplefunction(res, xdata, N = 2, termsref = "pred3")
# keep $pred2 at its reference level ("C")
resamplefunction(res, xdata, N = 2, termsref = "pred2")
# keep $pred1 at reference and $pred3 at 0
resamplefunction(res, xdata, N = 2, termsref = c("pred1", "pred3"))

epil2$subject <- factor(epil2$subject)
(fm <- glmmadmb(y ~ Base*trt + Age + Visit + (Visit|subject),
                data = epil2, family = "nbinom"))
resamplefunction(model = fm, dat = epil2, N = 3, stepsalong = 3)

library(lme4)
(fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy))
resamplefunction(model = fm1, dat = sleepstudy, N = 3, stepsalong = 3)

(gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
              data = cbpp, family = binomial))
resamplefunction(model = gm1, dat = cbpp, N = 3, stepsalong = 3)

## End(Not run)


gobbios/cfp documentation built on April 11, 2022, 2:22 a.m.