# Reference values from R for Scintria's statistics (pack 157).
# Run in a UTF-8 locale (LC_ALL=C.UTF-8 on Linux) so the method names match the page exactly.
# Runs the same published data sets through base R and the survival, survey, lme4 packages and writes ref-r.json,
# keyed "method|quantity" exactly as on the /validation page. Run from this folder:  Rscript r_reference.R
suppressPackageStartupMessages({ library(jsonlite); library(survival); library(survey); library(lme4) })
out <- list(); soft <- list()
put <- function(method, quantity, value, software, tol) { out[[paste(method, quantity, sep = "|")]] <<- list(value = unname(value), software = software, tol = tol) }
RV <- paste0("R ", getRversion())

core <- read.csv("core.csv", stringsAsFactors = FALSE)
core$arm <- relevel(factor(core$arm), "A"); core$sex <- relevel(factor(core$sex), "F")
F_ <- core$sbp[core$sex == "F"]; M_ <- core$sbp[core$sex == "M"]
w <- t.test(F_, M_, var.equal = FALSE)
m <- "Welch t-test (unequal variances)"; s <- paste(RV, "t.test(var.equal = FALSE)")
put(m, "t", w$statistic, s, 1e-6); put(m, "degrees of freedom", w$parameter, s, 1e-6); put(m, "p", w$p.value, s, 1e-6)
put(m, "95% CI, lower", w$conf.int[1], s, 1e-6); put(m, "95% CI, upper", w$conf.int[2], s, 1e-6)
mw <- suppressWarnings(wilcox.test(core$score[core$sex == "F"], core$score[core$sex == "M"], exact = FALSE, correct = TRUE))
m <- "Mann–Whitney U (ties, continuity correction)"; s <- paste(RV, "wilcox.test(exact = FALSE, correct = TRUE)")
put(m, "U", mw$statistic, s, 0); put(m, "p", mw$p.value, s, 1e-6)
a <- summary(aov(sbp ~ arm, data = core))[[1]]
m <- "One-way ANOVA"; s <- paste(RV, "aov"); put(m, "F", a$`F value`[1], s, 1e-9); put(m, "p", a$`Pr(>F)`[1], s, 1e-6)
k <- kruskal.test(score ~ arm, data = core)
m <- "Kruskal–Wallis (tie correction)"; s <- paste(RV, "kruskal.test"); put(m, "H", k$statistic, s, 1e-9); put(m, "p", k$p.value, s, 1e-6)
ch <- chisq.test(table(core$arm, core$cured), correct = FALSE)
m <- "Chi-square test, 3 × 2 table"; s <- paste(RV, "chisq.test(correct = FALSE)"); put(m, "χ²", ch$statistic, s, 1e-9); put(m, "p", ch$p.value, s, 1e-6)
cells <- function(d) { a <- sum(d$sex == "M" & d$cured == 1); b <- sum(d$sex == "M" & d$cured == 0); cc <- sum(d$sex == "F" & d$cured == 1); dd <- sum(d$sex == "F" & d$cured == 0); matrix(c(a, b, cc, dd), 2, byrow = TRUE) }
fi <- fisher.test(cells(core))
put("Fisher's exact test, 2 × 2", "p (two-sided)", fi$p.value, paste(RV, "fisher.test"), 1e-8)
arr <- array(unlist(lapply(c("North", "East", "West"), function(st) t(cells(core[core$site == st, ])))), dim = c(2, 2, 3))
arr <- aperm(arr, c(2, 1, 3)) # back to rows = exposure (M, F), columns = outcome (cured, not)
mh <- mantelhaen.test(arr, correct = FALSE)
m <- "Mantel–Haenszel odds ratio, 3 strata"; s <- paste(RV, "mantelhaen.test")
put(m, "pooled odds ratio", mh$estimate, s, 1e-9); put(m, "95% CI, lower (Robins–Breslow–Greenland)", mh$conf.int[1], s, 1e-9); put(m, "95% CI, upper", mh$conf.int[2], s, 1e-9)
pe <- cor.test(core$age, core$sbp)
m <- "Pearson correlation"; s <- paste(RV, "cor.test")
put(m, "r", pe$estimate, s, 1e-6); put(m, "p", pe$p.value, s, 1e-6); put(m, "95% CI, lower (Fisher z)", pe$conf.int[1], s, 1e-6); put(m, "95% CI, upper", pe$conf.int[2], s, 1e-6)
sp <- suppressWarnings(cor.test(core$age, core$score, method = "spearman", exact = FALSE))
m <- "Spearman correlation (ties)"; s <- paste(RV, "cor.test(method = \"spearman\", exact = FALSE)"); put(m, "ρ", sp$estimate, s, 1e-9); put(m, "p", sp$p.value, s, 1e-6)

lab <- c("(Intercept)" = "Intercept", age = "age", armB = "arm[T.B]", armC = "arm[T.C]", sexM = "sex[T.M]")
coefs <- function(method, fit, software, tol) { cf <- summary(fit)$coefficients; for (n in rownames(cf)) { put(method, paste(lab[[n]], "coefficient"), cf[n, 1], software, tol); put(method, paste(lab[[n]], "standard error"), cf[n, 2], software, tol) } }
li <- lm(sbp ~ age + arm + sex, data = core); s <- paste(RV, "lm")
coefs("Linear regression", li, s, 1e-8)
put("Linear regression", "R²", summary(li)$r.squared, s, 1e-9); put("Linear regression", "adjusted R²", summary(li)$adj.r.squared, s, 1e-9)
put("Linear regression", "F", summary(li)$fstatistic[1], s, 1e-8); put("Linear regression", "log-likelihood", as.numeric(logLik(li)), s, 1e-9)
lo <- glm(cured ~ age + arm + sex, data = core, family = binomial, control = glm.control(epsilon = 1e-12, maxit = 100)); s <- paste(RV, "glm(binomial)")
coefs("Logistic regression", lo, s, 1e-6)
put("Logistic regression", "log-likelihood", as.numeric(logLik(lo)), s, 1e-9); put("Logistic regression", "null log-likelihood", -lo$null.deviance / 2, s, 1e-9)
po <- glm(visits ~ arm + age + offset(log(months)), data = core, family = poisson, control = glm.control(epsilon = 1e-12, maxit = 100)); s <- paste(RV, "glm(poisson) with offset")
coefs("Poisson regression, person-time offset", po, s, 1e-6)
put("Poisson regression, person-time offset", "log-likelihood", as.numeric(logLik(po)), s, 1e-9)

su <- read.csv("surv.csv", stringsAsFactors = FALSE)
su$arm <- relevel(factor(su$arm), "A"); su$sex <- relevel(factor(su$sex), "F")
km <- survfit(Surv(time, status) ~ arm, data = su); sm <- summary(km, times = c(12, 24), extend = TRUE)
s <- paste0(RV, " survival ", packageVersion("survival"), " survfit"); m <- "Kaplan–Meier, 3 arms, heavy ties"
med <- summary(km)$table[, "median"]
for (g in c("A", "B", "C")) {
  st <- paste0("arm=", g)
  for (tp in c(12, 24)) put(m, sprintf("S(%d) in arm %s", tp, g), sm$surv[sm$strata == st & sm$time == tp], s, 1e-9)
  put(m, sprintf("median survival, arm %s", g), med[[st]], s, 0)
}
lr <- survdiff(Surv(time, status) ~ arm, data = su); s <- paste0(RV, " survival ", packageVersion("survival"), " survdiff")
put("Log-rank test", "χ²", lr$chisq, s, 1e-9); put("Log-rank test", "p", pchisq(lr$chisq, length(lr$n) - 1, lower.tail = FALSE), s, 1e-8)
cx <- coxph(Surv(time, status) ~ arm + sex + age, data = su, ties = "efron", control = coxph.control(eps = 1e-12, iter.max = 100))
s <- paste0(RV, " survival ", packageVersion("survival"), " coxph(ties = \"efron\")"); m <- "Cox proportional hazards (Efron ties)"
cl <- c(armB = "arm_B", armC = "arm_C", sexM = "sex_M", age = "age"); cs <- summary(cx)$coefficients
for (n in rownames(cs)) { put(m, paste(cl[[n]], "coefficient"), cs[n, "coef"], s, 1e-8); put(m, paste(cl[[n]], "standard error"), cs[n, "se(coef)"], s, 1e-8) }
put(m, "log partial likelihood", cx$loglik[2], s, 1e-9)

ph <- read.csv("posthoc.csv", stringsAsFactors = FALSE); tk <- TukeyHSD(aov(y ~ grp, data = ph))$grp
for (r in rownames(tk)) { g <- strsplit(r, "-")[[1]]; put("Tukey–Kramer, unbalanced groups with unequal variances", sprintf("adjusted p, %s vs %s", g[2], g[1]), tk[r, "p adj"], paste(RV, "TukeyHSD"), 1e-6) }

P <- fromJSON("ref.json")$paired
pt <- t.test(P$a, P$b, paired = TRUE); s <- paste(RV, "t.test(paired = TRUE)")
put("Paired t-test", "t", pt$statistic, s, 1e-9); put("Paired t-test", "p", pt$p.value, s, 1e-8)
wx <- suppressWarnings(wilcox.test(P$a, P$b, paired = TRUE, exact = FALSE, correct = TRUE))
put("Wilcoxon signed-rank (ties, zero differences)", "p", wx$p.value, paste(RV, "wilcox.test(paired = TRUE, exact = FALSE, correct = TRUE)"), 1e-8)
fr <- friedman.test(as.matrix(P$M)); s <- paste(RV, "friedman.test")
put("Friedman test", "χ²", fr$statistic, s, 1e-9); put("Friedman test", "p", fr$p.value, s, 1e-8)
Mx <- as.matrix(P$M); long <- data.frame(y = as.vector(t(Mx)), subj = factor(rep(seq_len(nrow(Mx)), each = ncol(Mx))), cond = factor(rep(seq_len(ncol(Mx)), nrow(Mx))))
rm <- summary(aov(y ~ cond + Error(subj / cond), data = long))[["Error: subj:cond"]][[1]]
put("Repeated-measures ANOVA", "F", rm$`F value`[1], paste(RV, "aov(Error(subject/condition))"), 1e-9)
put("McNemar test", "exact p", binom.test(9, 9 + 17)$p.value, paste(RV, "binom.test on discordant pairs"), 1e-9)
put("McNemar test", "χ² p (continuity correction)", mcnemar.test(matrix(c(41, 17, 9, 33), 2), correct = TRUE)$p.value, paste(RV, "mcnemar.test(correct = TRUE)"), 1e-8)

mx <- read.csv("mixed.csv", stringsAsFactors = FALSE); mx$arm <- relevel(factor(mx$arm), "A")
ml <- c("(Intercept)" = "Intercept", time = "time", armB = "arm[T.B]", age = "age")
for (spec in list(list(f = y ~ time + arm + age + (1 | id), name = "random intercept"), list(f = y ~ time + arm + age + (1 + time | id), name = "random intercept and slope"))) {
  fit <- lmer(spec$f, data = mx, REML = TRUE, control = lmerControl(optimizer = "bobyqa", optCtrl = list(maxfun = 1e5)))
  m <- sprintf("Linear mixed model (REML): %s, 35 unbalanced clusters", spec$name); s <- paste0(RV, " lme4 ", packageVersion("lme4"), " lmer(REML = TRUE)")
  cf <- summary(fit)$coefficients
  for (n in rownames(cf)) { put(m, paste(ml[[n]], "coefficient"), cf[n, 1], s, 1e-5); put(m, paste(ml[[n]], "standard error"), cf[n, 2], s, 1e-4) }
  vc <- as.data.frame(VarCorr(fit))
  put(m, "REML log-likelihood", as.numeric(logLik(fit)), s, 1e-7)
  put(m, "random-intercept variance", vc$vcov[vc$grp == "id" & vc$var1 == "(Intercept)" & is.na(vc$var2)], s, 1e-3)
  put(m, "residual variance", vc$vcov[vc$grp == "Residual"], s, 1e-4)
}

sy <- read.csv("survey.csv", stringsAsFactors = FALSE); sy$g <- relevel(factor(sy$g), "a")
des <- svydesign(ids = ~psu, strata = ~stratum, weights = ~w, data = sy, nest = TRUE)
sg <- svyglm(y ~ x + g, design = des); s <- paste0(RV, " survey ", packageVersion("survey"), " svyglm"); m <- "Weighted linear model, 4 strata of 2–5 clusters"
sl <- c("(Intercept)" = "(Intercept)", x = "x", gb = "g_b", gc = "g_c"); cf <- summary(sg)$coefficients
for (n in rownames(cf)) { put(m, paste(sl[[n]], "coefficient"), cf[n, 1], s, 1e-9); put(m, paste(sl[[n]], "standard error"), cf[n, 2], s, 1e-9) }
mn <- svymean(~y, des); s <- paste0(RV, " survey ", packageVersion("survey"), " svymean")
put("Weighted mean", "mean", coef(mn)[[1]], s, 1e-10); put("Weighted mean", "standard error", SE(mn)[[1]], s, 1e-9)

writeLines(toJSON(list(r = as.character(getRversion()), checks = out), auto_unbox = TRUE, digits = NA, pretty = FALSE), "ref-r.json")
cat("wrote", length(out), "R reference values\n")
