dens: Evaluate data against the implied density from a fitted 'glm'...

View source: R/sim_tools.R

densR Documentation

Evaluate data against the implied density from a fitted glm object based on possibly new covariate values, with automatic detection of family and link.

Description

Evaluate data against the implied density from a fitted glm object based on possibly new covariate values, with automatic detection of family and link.

Usage

dens(x, model, newdata, binomial_n = 1)

Arguments

x

vector of values at which to evaluate the density

model

a fitted glm object

newdata

data frame with values of the covariates based on which to simulate the response, similar to predict.glm. nrow(newdata) must equal length(x).

binomial_n

int length nrow(newdata). If model was fit with link=binomial, you can optionally pass a vector of group sizes to simulate aggregate binomial data.

Value

A vector of length(x) of density estimates.

Examples

n  = 100
df = tibble::tibble(x = rnorm(n), y = rpois(n, lambda = exp(x)))
mod = glm(y~x, poisson('log'), df)
dens_y = dens(df$y, mod, newdata=df)

# equivalent to
dpois(df$y, lambda = predict(mod, type='response'))


audreyrenson/didgformula documentation built on Oct. 9, 2022, 11:45 a.m.