Skip to contents

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

Examples

lm(hp ~ mpg + factor(cyl) + disp:hp, mtcars) |>
  model_get_n()
#> # A tibble: 6 × 2
#>   term         n_obs
#>   <chr>        <dbl>
#> 1 (Intercept)     32
#> 2 mpg             32
#> 3 factor(cyl)6     7
#> 4 factor(cyl)8    14
#> 5 hp:disp         32
#> 6 factor(cyl)4    11

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()
#> # A tibble: 16 × 3
#>    term          n_obs n_event
#>    <chr>         <dbl>   <dbl>
#>  1 (Intercept)     193      61
#>  2 stage1           52      18
#>  3 stage2           52      13
#>  4 stage3           40      15
#>  5 grade1           67      21
#>  6 grade3           63      21
#>  7 trtDrug A        95      28
#>  8 stage1:grade1    17       6
#>  9 stage2:grade1    17       4
#> 10 stage3:grade1    18       7
#> 11 stage1:grade3    13       6
#> 12 stage2:grade3    19       5
#> 13 stage3:grade3    13       4
#> 14 stage4           49      15
#> 15 grade2           63      19
#> 16 trtDrug B        98      33

if (FALSE) { # \dontrun{
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()
} # }