## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  fig.width  = 7,
  fig.height = 5
)

## ----data---------------------------------------------------------------------
library(lorbridge)
data(lorbridge_data)
str(lorbridge_data)

## ----blr_continuous-----------------------------------------------------------
res_1a <- blr_continuous(
  outcome   = lorbridge_data$minority,
  predictor = lorbridge_data$VM
)
print(res_1a$summary_table[, c("LOR","OR","OR_lo","OR_hi","p",
                                "Nagelkerke_R2","YuleQ","r_meta")],
      digits = 4)

## ----blr_categorical----------------------------------------------------------
res_1b <- blr_categorical(
  outcome   = lorbridge_data$minority,
  predictor = lorbridge_data$VMbin,
  ref_level = "VM4"
)
print(res_1b$results[, c("Category","LOR","OR","p","YuleQ","r_meta","cos_theta")],
      digits = 4)

## ----sonsca_setup-------------------------------------------------------------
data(tab_IQ)
row_anchor <- "Race2"
col_anchor <- "IQ4"
races      <- setdiff(rownames(tab_IQ), row_anchor)
bins       <- setdiff(colnames(tab_IQ), col_anchor)

## ----sonsca_ccms--------------------------------------------------------------
# Pairwise CCMs for Race1 vs Race2 at IQ1 vs IQ4
sonsca_ccm(tab_IQ, row_k = "Race1", bin_j = "IQ1",
           row_anchor = row_anchor, col_anchor = col_anchor)

## ----sonsca_cosines-----------------------------------------------------------
# SONSCA coordinates and cosine theta matrix
sc  <- sonsca_coords(tab_IQ)
cos <- sonsca_cosines(sc$row_coords, sc$col_coords,
                      row_anchor = row_anchor,
                      col_anchor = col_anchor)
round(cos[races, bins], 3)

## ----inertia------------------------------------------------------------------
pct <- inertia_pct(tab_IQ)
cat(sprintf("Dimension 1: %.1f%%  |  Dimension 2: %.1f%%\n", pct[1], pct[2]))

## ----donsca-------------------------------------------------------------------
data(tab_IQ_VM)
fit <- donsca_fit(tab_IQ_VM)
cos_d <- donsca_cosines(fit, col_anchor_idx = 4, row_anchor_idx = 4)
head(cos_d, 6)

## ----mlr_ccm, message = FALSE-------------------------------------------------
data(lorbridge_data)  # use VM as numeric predictor, VMbin as outcome proxy
# Illustrative: treat VM bins as the outcome and VM numeric as predictor
# (In practice use IQ bins as outcome and VM as predictor per the paper)
data(tab_IQ_VM)

# Build long-format data from tab_IQ_VM for multinomial logit
vm_vals <- c(54,59,62,63,65,67,69,71,73,74,76,78,80,81,82,84,85,86,87,89,
             90,92,93,95,96,98,100,101,103,104,105,107,108,110,112,113,
             115,117,119,121,123,125,126,128,130,132,134,136,138,139,
             143,147,149)
rows6 <- paste0("IQ", 1:6)
# (Full X_wide matrix omitted here for brevity — see unified analysis script)

