Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

model_get_n

Get the number of observations


Description

For binomial and multinomial logistic models, will also return the number of events.

Usage

model_get_n(model)

## Default S3 method:
model_get_n(model)

## S3 method for class 'glm'
model_get_n(model)

## S3 method for class 'gam'
model_get_n(model)

## S3 method for class 'glmerMod'
model_get_n(model)

## S3 method for class 'multinom'
model_get_n(model)

## S3 method for class 'coxph'
model_get_n(model)

## S3 method for class 'survreg'
model_get_n(model)

Arguments

model

a model object

Details

For Poisson models, will return the number of events and exposure time (defined with stats::offset()).

For Cox models (survival::coxph()), will return the number of events and exposure time.

See tidy_add_n() for more details.

The total number of observations (N_obs), of events (N_event) and of exposure time (Exposure) are stored as attributes of the returned tibble.

This function does not cover lavaan models (NULL is returned).

See Also

Examples

lm(hp ~ mpg + factor(cyl) + disp:hp, mtcars) %>%
  model_get_n()

mod <- glm(
  response ~ stage * grade + trt,
  gtsummary::trial,
  family = binomial,
  contrasts = list(stage = contr.sum, grade = contr.treatment(3, 2), trt = "contr.SAS")
)
mod %>% model_get_n()

mod <- glm(
  Survived ~ Class * Age + Sex, data = Titanic %>% as.data.frame(),
  weights = Freq, family = binomial
)
mod %>% model_get_n()

d <- dplyr::as_tibble(Titanic) %>%
  dplyr::group_by(Class, Sex, Age) %>%
  dplyr::summarise(
    n_survived = sum(n * (Survived == "Yes")),
    n_dead = sum(n * (Survived == "No"))
  )
mod <- glm(cbind(n_survived, n_dead) ~ Class * Age + Sex, data = d, family = binomial)
mod %>% model_get_n()

mod <- glm(response ~ age + grade * trt, gtsummary::trial, family = poisson)
mod %>% model_get_n()

mod <- glm(
  response ~ trt * grade + offset(ttdeath),
  gtsummary::trial,
  family = poisson
)
mod %>% model_get_n()

df <- survival::lung %>% dplyr::mutate(sex = factor(sex))
mod <- survival::coxph(survival::Surv(time, status) ~ ph.ecog + age + sex, data = df)
mod %>% model_get_n()

mod <- lme4::lmer(Reaction ~ Days + (Days | Subject), lme4::sleepstudy)
mod %>% model_get_n()

mod <- lme4::glmer(response ~ trt * grade + (1 | stage),
  family = binomial, data = gtsummary::trial
)
mod %>% model_get_n()

mod <- lme4::glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
  family = binomial, data = lme4::cbpp
)
mod %>% model_get_n()

broom.helpers

Helpers for Model Coefficients Tibbles

v1.3.0
GPL-3
Authors
Joseph Larmarange [aut, cre] (<https://orcid.org/0000-0001-7097-700X>), Daniel D. Sjoberg [aut] (<https://orcid.org/0000-0003-0862-2018>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.