add_predicted_sims.glm: Generate fitted simulations from a glm model

View source: R/stats_glm_predict_sims.R

add_predicted_sims.glmR Documentation

Generate fitted simulations from a glm model

Description

Generate simulations from a glm model incorporating either error in fitted error. Simulations explore the possible space of what a model might predict rather than an interval for use in comparison to Bayesian posteriors for non-Bayesian models. The output format and functions draw inspiration from the tidybayes::tidybayes() library and merTools::predictInterval()

Usage

## S3 method for class 'glm'
add_predicted_sims(
  newdata,
  mod,
  n_sims = 1000,
  seed = NULL,
  weights = 1,
  type = c("predict", "fit", "linpred"),
  ...
)

Arguments

newdata

a data.frame of new data to predict

mod

An lm model to simulate from.

n_sims

number of simulation samples to construct

seed

numeric, optional argument to set seed for simulations

weights

numeric, optional argument for binomial models that need a number of trials

type

Character defining if we are looking at fit or predict intervals.

...

Unused dots for compatibility with generic functions.

Value

A tibble::tibble with information about simulate values.

See Also

Other glm: add_fitted_sims.glm()

Examples

# Gamma
clotting <- data.frame(
   u = c(5,10,15,20,30,40,60,NA,100),
   lot1 = c(118,58,42,35,27,25,21,19,18),
   lot2 = c(69,35,26,21,18,16,13,12,12))

mod <- glm(lot1 ~ log(u) + lot2, data = clotting, family = Gamma)

sims_pred <- add_predicted_sims(clotting, mod)

head(sims_pred)

# Binomial
# example from Venables and Ripley (2002, pp. 190-2.)
ldose <- rep(0:5, 2)
numdead <- c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16)
sex <- factor(rep(c("M", "F"), c(6, 6)))
SF <- cbind(numdead, numalive = 20-numdead)
budworm.lg <- glm(SF ~ sex*ldose, family = binomial)

dat <- data.frame(sex = factor(c("M", "F", "M", "F")),
                  ldose = c(0,0,5,5))
sims_pred_b <- add_predicted_sims(dat, budworm.lg, weights = 20)

head(sims_pred_b)

jebyrnes/sinterval documentation built on March 18, 2023, 5:05 a.m.