Find quantities of interest from generalized linear models

Share:

Description

Find quantities of interest from generalized linear models

Usage

1
2
qi_builder(obj, newdata, FUN, ci = 0.95, nsim = 1000, slim = FALSE,
  b_sims, large_computation = FALSE, original_order = FALSE, ...)

Arguments

obj

a fitted model object from which to base coefficient simulations on.

newdata

an optional data frame of fitted values with column names corresponding to variable names in b_sims. Note that variabless not included in newdata will be fitted at 0. If missing then observations used to fit the model in obj will be used.

FUN

a function for calculating how to find the quantity of interest from a vector of the fitted linear systematic component. It must return a numeric vector. If missing then a normal linear regression model is assumed and the predicted values are returned (i.e. the fitted linear systematic component from linear_systematic).

ci

the proportion of the central interval of the simulations to return. Must be in (0, 1] or equivalently (0, 100].

nsim

number of simulations to draw.

slim

logical indicating whether to (if FALSE) return all simulations in the central interval specified by ci for each fitted scenario or (if TRUE) just the minimum, median, and maxium values. See qi_slimmer for more details.

b_sims

an optional data frame created by b_sim of simulated coefficients. Only used if obj is not supplied.

large_computation

logical. If newdata is not supplied, whether to allow > 100000 simulated quantities of interest to be found.

original_order

logical whether or not to keep the original scenario order when slim = TRUE. Choosing FALSE can imporove computation time.

...

arguments to pass to linear_systematic.

Value

If slimmer = FALSE a data frame of fitted values supplied in newdata and associated simulated quantities of interest for all simulations in the central interval specified by ci. The quantities of interest are in a column named qi_.

If slimmer = TRUE a data frame of fitted values supplied in newdata and the minimum, median, and maximum values of the central interval specified by ci for each scenario are returned in three columns named qi_min, qi_median, and qi_max, respectively.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
library(car)

## Normal linear model
m1 <- lm(prestige ~ education + type, data = Prestige)

# Using observed data as scenarios
linear_qi_obs <- qi_builder(m1)

# Create fitted values
fitted_df_1 <- expand.grid(education = 6:16, typewc = 1)

linear_qi <- qi_builder(m1, newdata = fitted_df_1)

## Logistic regression
# Download data
URL <- 'http://www.ats.ucla.edu/stat/data/binary.csv'
Admission <- read.csv(URL)
Admission$rank <- as.factor(Admission$rank)

# Estimate model
m2 <- glm(admit ~ gre + gpa + rank, data = Admission, family = 'binomial')

# Specify fitted values
m2_fitted <- expand.grid(gre = seq(220, 800, by = 10), gpa = c(2, 4),
                         rank = '4')

# Function to find predicted probabilities from logistic regression models
pr_function <- function(x) 1 / (1 + exp(-x))

# Find quantity of interest
logistic_qi_1 <- qi_builder(m2, m2_fitted, FUN = pr_function)

logistic_qi_2 <- qi_builder(m2, m2_fitted, FUN = pr_function,
                         slim = TRUE)