08-02 · Design and tool consolidation

Use a yes/no model and plan a future pen trial with simglm.

After-class slides · Binary script · Power script

After-class route · 75 minutes

Use the after-class slides and the direct calls in after-class-student.R and power-planning.R. No recording is required. Required packages are readr, dplyr, tidyr, tibble, lme4, performance, marginaleffects, simglm and purrr. Use glmer() for the yes/no model and lmer() for simulated daily gains. simglm reads coefficients directly from these fits. Install through setup.R if needed.

Review random effects, designs and fit checks · 15 minutes

Read (1 | pen) and (1 + week_c | calf) aloud. Explain why the forty-line plots differ and what a centred intercept describes. Use the existing script’s Deep-minus-Standard comparison to describe the bedding change within one diet. Compare the pen model with a model treating animals separately, using the estimates and intervals printed by student-starter.R. Read check_convergence(), check_singularity() and VarCorr(); successful computation does not establish design assumptions. Review the split-plot and Latin-square layouts. State which pigs, pen sections or trays the researchers assign each treatment to and one limitation. Then explain how unequal counts could arise in either example. Review the worked output rather than learning design names without a study context.

A yes/no outcome · 15 minutes

Run after-class-student.R. A new individual welfare screen is recorded as 0/1 at the end of follow-up. It is not the earlier pen-level “any animal treated” response. Diet still belongs to pens. family = binomial() fits a yes/no model.

Read the fitting checks. These invented outcomes give a small non-zero fitted pen spread, and check_singularity() returns FALSE. The example still has only sixteen pens and limited yes/no information within each, so the pen spread is uncertain.

Read the average predicted probabilities and the High fibre minus Control probability difference. Rooms count equally and pen differences are set to zero. For this yes/no model that is generally different from averaging probabilities over a distribution of varying pens. Explain the target in words; do not interpret a difference in probabilities as an odds ratio.

Plan a future trial with simglm · 30 minutes

Run power-planning.R one numbered section at a time. Its comments explain how map2() pairs each pen count with its SD and how map() repeats the simulated trials. It builds a future study with six animals per pen and half the pens assigned each diet. It omits rooms and section treatments deliberately; it is not a power calculation for the full split-plot experiment. The assumed worthwhile diet change is 35 g/day, the Control mean 700 g/day, individual SD 35 g/day, and shared pen SD either 40 or 60 g/day. An SD describes typical spread; simglm takes its square as a variance. These are provisional planning choices, not estimates selected from today’s result.

First inspect one simulated study: verify the number of pens per diet and that diet is constant within pen. replicate_simulation() then repeats the simulation and lmer() fit with REML. compute_statistics() counts the fraction detecting the assumed diet change using a two-sided approximate normal test at 0.05. That fraction is power for this assumed study and analysis.

Read outputs/power-plans.csv. Compare sixteen and 32 independently assigned pens under both spread assumptions. State which change adds assignments and which makes pen outcomes more variable. Do not choose a size from one curve without justifying the assumptions and checking cost and feasibility.

The script counts unusable final fits and reports bounds treating each failure first as missed detection and then as detection. Do not replace failed simulations with fresh draws. An optimiser can warn during its search even when final fitting checks succeed; inspect both warnings and final checks. The current supplied run had 100 usable final fits in each scenario and no warnings. Its output is retained in reference/power-output.txt.

One hundred simulated trials per scenario give only a rough demonstration: near power 0.5 the simulation SE is about 0.05. That is uncertainty from running a finite number of simulations. A real planning decision needs many more repetitions, justified effect/spread assumptions and checks that the simulation and fitted test match the proposed design.

Try larger pen counts · 15 minutes

The manager wants at least 0.8 power to detect a diet improvement of 35 g/day, with six pigs per pen. Try to answer the manager’s question: “How many pens should we use, and does the answer change if the between-pen SD is 40 or 60 g/day?”

Check the existing power estimates against 0.8. In section 4 of power-planning.R, add pen counts above 32 to pens = c(16, 32), keeping both SD values and six pigs per pen. Rerun the script from the start. Use the new results to recommend a pen count, allowing for the uncertainty from only 100 simulated trials per plan.

Software references: lme4, performance, marginaleffects, simglm simulation and simglm power.