model_get_n: Get the number of observations

View source: R/model_get_n.R

model_get_nR Documentation

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 'glmerMod'
model_get_n(model)

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

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

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

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

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

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

Arguments

model

(a model object, e.g. glm)
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, exposure time and the number of individuals.

For competing risk regression models (tidycmprsk::crr()), n_event takes into account only the event of interest defined by failcode.

See tidy_add_n() for more details.

The total number of observations (N_obs), of individuals (N_ind), 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

Other model_helpers: model_compute_terms_contributions(), model_get_assign(), model_get_coefficients_type(), model_get_contrasts(), model_get_model(), model_get_model_frame(), model_get_model_matrix(), model_get_nlevels(), model_get_offset(), model_get_pairwise_contrasts(), model_get_response(), model_get_response_variable(), model_get_terms(), model_get_weights(), model_get_xlevels(), model_identify_variables(), model_list_contrasts(), model_list_higher_order_variables(), model_list_terms_levels(), model_list_variables()

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()

## Not run: 
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()

dont
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()

## End(Not run)

broom.helpers documentation built on Sept. 11, 2024, 6:31 p.m.