# LTP from field recordings, knockout vs wild-type: fEPSP slope as percent of each slice's baseline, the mean of minutes
# 51-60 after HFS, with slices or mice as n, and a t-test at every post-HFS minute. R 4.3.3, base R.
d <- read.csv("fepsp.csv")                           # one row per slice and minute: genotype, mouse, slice, minute (-19 to 60), slope
f2 <- function(p) formatC(signif(p, 2), format = "g")
d$sl <- paste(d$genotype, d$mouse, d$slice, sep = "-")
base <- tapply(d$slope[d$minute <= 0], d$sl[d$minute <= 0], mean)
d$pct <- 100 * d$slope / base[d$sl]                  # percent of the slice's own 20-min baseline
late <- d[d$minute >= 51 & d$minute <= 60, ]
s <- aggregate(pct ~ genotype + mouse + sl, data = late, FUN = mean)                     # LTP per slice
m <- aggregate(pct ~ genotype + mouse, data = s, FUN = mean)                             # LTP per mouse
for (g in sort(unique(m$genotype))) { x <- m[m$genotype == g, ]
  cat(sprintf("%s: %d mice, %d slices; LTP (minutes 51-60) %.0f%% of baseline (mouse range %.0f to %.0f%%)\n", g, nrow(x), sum(s$genotype == g), mean(x$pct), min(x$pct), max(x$pct))) }
k <- s$pct[s$genotype == "knockout"]; w <- s$pct[s$genotype == "wild-type"]; t <- t.test(k, w)
cat(sprintf("A. Slices as n (%d vs %d), Welch t-test: difference %.1f points, p = %s\n", length(k), length(w), mean(k) - mean(w), f2(t$p.value)))
post <- d[d$minute > 0, ]
p <- sapply(1:60, function(mi) { x <- post[post$minute == mi, ]; t.test(x$pct[x$genotype == "knockout"], x$pct[x$genotype == "wild-type"])$p.value })
cat(sprintf("B. A Welch t-test at each of %d post-HFS minutes, slices as n: %d with p < 0.05; smallest p = %s\n", length(p), sum(p < 0.05), f2(min(p))))
k <- m$pct[m$genotype == "knockout"]; w <- m$pct[m$genotype == "wild-type"]; t <- t.test(k, w)
cat(sprintf("C. Mice as n (%d vs %d), mean of each mouse's slices, Welch t-test: difference %.1f points (95%% CI %.1f to %.1f), p = %s\n", length(k), length(w), mean(k) - mean(w), t$conf.int[1], t$conf.int[2], f2(t$p.value)))
