# Sholl analysis: the mixed-model alternative to analysis C, on total intersections per neuron with a random
# intercept per mouse. R 4.3.3 with lme4 and lmerTest (Satterthwaite degrees of freedom). R only.
suppressPackageStartupMessages(library(lmerTest))
d <- read.csv("sholl.csv")
tot <- aggregate(intersections ~ genotype + mouse + neuron, data = d, FUN = sum)       # total per neuron
tot$genotype <- factor(tot$genotype, levels = c("wild-type", "knockout"))
fit <- lmer(intersections ~ genotype + (1 | mouse), data = tot)
s <- coef(summary(fit))["genotypeknockout", ]; ci <- s[["Estimate"]] + c(-1, 1) * qt(0.975, s[["df"]]) * s[["Std. Error"]]   # t-based CI
cat(sprintf("Mixed model, total intersections per neuron, random intercept per mouse: knockout - wild-type %.1f (95%% CI %.1f to %.1f), t(%.1f) = %.2f, p = %s\n",
            s[["Estimate"]], ci[1], ci[2], s[["df"]], s[["t value"]], formatC(signif(s[["Pr(>|t|)"]], 2), format = "g")))
