# Week 6, Group 1. Run sections in order from the project root.
# Packages: dplyr, tibble, ggplot2 and readr are loaded by the helper.
# stats supplies generators and models; the repetition helpers are provided.
source("power-helpers.R")
s <- scenario(1)

# 1. Recall and declare the question (13:00).
# Write your response support, effect scale, unit and detection rule here:
# ...
design <- make_design(16)
print(count(design, room, diet))

# 2. One experiment (13:15): predict before running.
set.seed(6101)
one <- generate_response(design, s)
print(select(one, pen_id, room, diet, eta, mu, response))
result <- analyse_one(one, s)
print(result, width = Inf)
# Interpret beta on the response scale; retain unresolved results and warnings.
# ...

# 3. Twenty repetitions (13:55). Rotate roles.
small <- run_simulations(s, total_pens = 16, B = 20, seed = 6201)
print(select(small, iteration, p, unresolved, detected))
print(summarise_runs(small), width = Inf)
# Manually count detections / ALL attempts, then compare with the summary.
reference <- read_csv("reference/power-summary.csv", show_col_types = FALSE)
print(filter(reference, group == 1, scenario == "Baseline", null), width = Inf)

# 4. One sample size (14:05).
# 200 repetitions concern numerical precision, not 200 experimental pens.
one_size <- run_simulations(s, total_pens = 16, B = 200, seed = 6301)
print(summarise_runs(one_size), width = Inf)

# 5. Complete the curve (14:15–14:40). Reuse the 16-pen batch.
other_sizes <- run_curve(s, sizes = c(32, 48, 64), B = 200, seed = 6401)
baseline_runs <- bind_rows(one_size, other_sizes)
baseline_summary <- summarise_runs(baseline_runs)
print(plot_curve(baseline_summary))
print(baseline_summary, width = Inf)
# What does the interval include? Is any size defensibly adequate?
# ...

# 6. Save BASELINE results before the reports and recommendation.
# Stop computing at 14:40; use supplied Baseline results if needed.
dir.create("group-1-results", showWarnings = FALSE)
write_csv(baseline_summary, "group-1-results/summary.csv")
write_csv(baseline_runs, "group-1-results/iterations.csv")
ggsave("group-1-results/curve.png", plot_curve(baseline_summary),
       width = 8, height = 5, dpi = 160)
# 15:10 report baseline finding; 15:25 write recommendation and PREDICT a change.
# No second curve is required in class. Keep your prediction in the worksheet.

# 7. OPTIONAL after-class computation. Supplied results are sufficient.
# The week 42 revision task includes comparison within its existing time limit.
run_sensitivity <- FALSE
if (run_sensitivity) {
  s_changed <- scenario(1, sensitivity = TRUE)
  changed_runs <- run_curve(s_changed, B = 200, seed = 6501)
  comparison <- bind_rows(baseline_summary, summarise_runs(changed_runs))
  print(plot_curve(comparison))
  print(comparison, width = Inf)
  write_csv(comparison, "group-1-results/sensitivity-summary.csv")
  ggsave("group-1-results/sensitivity-curve.png", plot_curve(comparison),
         width = 8, height = 5, dpi = 160)
}
# For revision, compare supplied Baseline and Sensitivity rows without fitting:
# filter(reference, group == 1, !null)
