logistic: Binary logistic regression

View source: R/models.R

logisticR Documentation

Binary logistic regression

Description

Fits binary logistic regression using formula syntax or compact R4VN syntax. Compact syntax avoids the need to type ~ and +.

Usage

logistic(
  y,
  ...,
  vars = NULL,
  data = NULL,
  event = NULL,
  or = FALSE,
  exp = FALSE,
  noconstant = FALSE,
  vce = c("model", "robust", "cluster"),
  cluster = NULL,
  weights = NULL,
  subset = NULL,
  ref = NULL,
  gof = FALSE,
  groups = 10,
  classification = FALSE,
  cutoff = 0.5,
  vif = FALSE,
  diagnosis = FALSE,
  level = 0.95,
  digits = 3,
  p_digits = 3,
  show = TRUE,
  console = FALSE
)

Arguments

y

Formula or binary outcome variable.

...

Predictors or model terms when y is not a formula.

vars

Optional model terms written as vars(...). The expression is captured without evaluating the public table-oriented vars() parser, so interactions are allowed.

data

Data frame or NULL for active data.

event

Event level for a simple named outcome.

or

Add an odds-ratio table while retaining coefficients.

exp

Display odds ratios only.

noconstant

Fit without an intercept.

vce

Model-based, HC1 robust, or cluster-robust covariance.

cluster

Cluster variable.

weights

Optional non-negative weights.

subset

Optional logical subset.

ref

Optional named list of factor reference levels.

gof

Show a Hosmer-Lemeshow test.

groups

Number of groups for the Hosmer-Lemeshow test.

classification

Show a classification table.

cutoff

Classification cutoff.

vif

Show coefficient-level VIFs.

diagnosis

Logical; if TRUE, append calibration/goodness-of-fit, discrimination, residual, influence, and collinearity diagnostics appropriate for binary logistic regression. Default FALSE.

level

Confidence level.

digits, p_digits

Decimal places.

show

Logical; open the formatted result in the Viewer. Default TRUE.

console

Logical; also print the traditional result in the Console. Default FALSE.

Details

Compact model syntax:

  • x: use the variable as stored in the data.

  • c.x: force x to be continuous.

  • i.x: force x to be categorical.

  • b2.x, b3.x, ...: categorical with the corresponding factor-level position as reference.

  • ib0.x, ib1.x, ib2.x, ...: categorical with the requested value/level as reference. If that literal level is unavailable, a positive integer can fall back to the corresponding factor-level position.

  • i.a*i.b: main effects for a and b plus their interaction.

  • i.a:i.b: interaction only.

  • c.x*i.a: continuous and categorical main effects plus interaction.

Thus logistic(y, ib2.occupation*i.treatment, c.age) fits occupation, treatment, occupation-by-treatment interaction, and age without requiring formula operators ~ or +.

The fitted glm object is stored in result$raw$model, so nested models can be compared directly with lrtest().

Value

An object of class r4vn_stat, returned invisibly. Its sections component contains the formatted model summary, coefficient and/or odds- ratio tables, and any requested goodness-of-fit, classification, or VIF tables. In raw, model is the fitted binomial glm object, vcov is the covariance matrix, coefficients contains coefficient-level estimates and tests, logLik and null.logLik are model log likelihoods, pseudo.r2 is McFadden-style pseudo-R-squared, event records the modeled outcome level, and vce and model.terms record the covariance estimator and fitted terms.

See Also

lrtest(), poisson()

Examples

set.seed(2026)
d <- data.frame(
  outcome = factor(rbinom(200, 1, .35), levels = 0:1,
                   labels = c("No", "Yes")),
  age = rnorm(200, 45, 12),
  occupation = factor(sample(c("Office", "Worker", "Other"), 200, TRUE)),
  treatment = factor(sample(c("No", "Yes"), 200, TRUE))
)

m1 <- logistic(
  outcome,
  c.age,
  i.occupation,
  i.treatment,
  data = d,
  event = "Yes",
  show = FALSE
)

m2 <- logistic(
  outcome,
  c.age,
  ib2.occupation*i.treatment,
  data = d,
  event = "Yes",
  show = FALSE
)

m3 <- logistic(
  outcome,
  vars = vars(c.age, ib2.occupation*i.treatment),
  data = d,
  event = "Yes",
  show = FALSE
)

lrtest(m1, m2, show = FALSE)

# Request a complete diagnostic panel
logistic(outcome, c.age, i.occupation, data = d, event = "Yes",
         diagnosis = TRUE, show = FALSE)


R4VN documentation built on Sept. 30, 2026, 5:13 p.m.