Assumed diet effect: +45 g/day Estimated diet effect in this dataset: +50.2 g/day
The fitting call uses observations, diet and room. It is not given the assumed effect or the true expected responses.
A second possible experiment
Draw fresh observations and fit again. Keep the design, expected responses and SD fixed.
second <- expectedsecond$response <-rnorm(nrow(second), second$mu, sd_response)fit_second <-glm(response ~ diet + room,family =gaussian(), data = second)coef(fit_second)["dietHigh fibre"]
dietHigh fibre
-21.42496
The assumed effect remains +45 g/day. Compare this output with the first estimate of +50.2 g/day. Even the estimated direction can change.
Across many such experiments, how often would the planned analysis identify a statistically interesting effect?
Reconstruct the workflow
With your group, put these stages in order:
fitted model
design
observation
decision
expected response (\mu)
model score (\eta)
Which quantities stay fixed across repeated experiments?
Pens and repetitions
We simulate 200 possible experiments, each with 16 pens.
A larger real experiment would contain more independently treated pens. More simulated experiments give a more precise estimate of the existing design’s performance.
Discuss: does the proposed real experiment now have 3,200 pens (16 x 200)? What has actually increased?
Four effects, four interpretations
Group
Response
Assumed diet effect
1
Daily gain
+45 g/day
2
Any pig treated
Odds ratio 0.60
3
Treatment events
Count ratio 0.65
4
Feed conversion ratio
Expected FCR ratio 0.92
Groups 2–4 use the logarithm of the ratio as their model coefficient. The same coefficient value would not mean the same scientific effect across families.
The design question
Before simulation, state the effect worth detecting and the assumptions that make the responses plausible.
A statistically detectable difference may be too small to matter from a biological perspective. An effect that matters biologically may still be missed by a noisy experiment.
We investigate a planned design under specified effects.
We do not calculate “observed power” from a realised estimate.
Power under an assumed effect
Imagine repeating the experiment under the same nonzero diet effect and the same assumptions.
Power: the probability that the specified test/model rejects the no-effect hypothesis (\text{H}_0)
It is conditional on the data generating process, covariate / treatment effect, baseline, variation among experimental units, experimental / study design and analysis.
It is not the probability that the alternative hypothesis is true — we assume \text{H}_0 is true when conducting the test!
Why use 80% power?
At a specified relevant effect, 80% power means detecting it in about 4 out of 5 repetitions; about 1 in 5 would miss it.
0.80 is a common planning convention, not a statistical law. It balances the risk of missing an effect against the resources needed for a larger experiment.
A higher target, such as 0.90, may be appropriate when missing an effect is particularly costly. Choose and justify the target before comparing designs.
Power does not mean an 80% probability that the hypothesis is true. The 5% false-positive threshold is a separate choice.
A statistically interesting effect requires a usable comparison with p < \alpha.
\alpha is the Type I error rate; commonly \alpha = 0.05
Both models retain room
Record estimated direction separately; i.e. does diet lead to an increase or decrease in the response.
Rejection does not imply biological importance
Unresolved analyses
Sparse binary responses can produce unreliable fits even when R returns a coefficient.
Our declared procedure flags warnings, convergence or boundary problems, invalid results and extreme binary fits.
Keep every attempt. Report detections / all attempts and unresolved / all attempts separately. Do not keep drawing until a fit looks favourable.
Complete separation
Binomial logit model: response ~ diet + room
16 pens: two per diet in each room. Diet and room together perfectly distinguish 0 from 1.
summary(fit) — coefficient table
Term
Estimate
SE
z
p
Intercept
25.36
119,300
≈0
≈1
High fibre
−50.72
137,700
≈0
≈1
Room 2
−50.72
182,200
≈0
≈1
Room 3
50.72
182,200
≈0
≈1
Room 4
≈0
137,700
≈0
≈1
Residual deviance ≈ 0; 25 iterations. fit$converged is TRUE.
Estimates and test statistics rounded.
Look for: extreme log-odds coefficients, huge SEs and fitted probabilities near 0 or 1. The Wald p-values are unreliable—not evidence of no effect.
R warns: fitted probabilities numerically 0 or 1 occurred.
Power across candidate designs
Return to Gaussian daily gain: +45 g/day, SD 60 g/day, four fixed rooms. Read the supplied results into power_reference; select our baseline scenario as gain_curve.
At 48 pens, estimated power is 72.2%; at 64 pens, it is 82.2%.
Of the four sizes tested, 64 pens is the first to exceed our 80% target under these assumptions.
This does not establish the exact minimum sample size. We have not tested the sizes between 48 and 64, and the assumed effect and variation may be wrong.
More simulation repetitions refine the plotted estimates. More independently treated pens change the experiment’s power.
Would 64 pens still be convincing if the responses were more variable than we assumed?
Package choice and project
We use dplyr, tibble, ggplot2 and readr for the table grammar already used in Week 5 and for ggplot2. R’s supplied stats package handles generators and GLMs.
Open your assigned group-N-student.R in the 06-power-analysis project. Run from that project folder. source() defines the supplied helpers; scenario(3) creates s, the Poisson assumptions for this demonstration.
source("power-helpers.R")s <-scenario(3) # use your assigned group
One complete repetition
make_design() builds the balanced table we constructed earlier. generate_response() adds the responses; analyse_one() fits, tests and returns the result.
# A tibble: 1 × 4
beta p unresolved detected
<dbl> <dbl> <lgl> <lgl>
1 -0.182 0.546 FALSE FALSE
Identify the assumed effect, fitted effect, p-value and decision. Read unresolved before interpreting the result; inspect result$warnings in your script if an analysis is unresolved.
One design, many possible datasets
The design and parameters stay fixed. The response draw, fitted estimate and decision can change.