-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexperimental-data.qmd
More file actions
1187 lines (895 loc) · 29 KB
/
Copy pathexperimental-data.qmd
File metadata and controls
1187 lines (895 loc) · 29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Working with experimental data in R"
subtitle: "Tables, transformations, and comparisons"
author: "Denis O'Meally"
institute: "City of Hope"
format:
revealjs:
theme:
- default
- brand
slide-number: c/t
transition: none
controls: true
progress: true
hash: true
history: true
center: false
width: 1600
height: 900
margin: 0.08
code-overflow: wrap
embed-resources: true
footer: "BIOSCI 504 · Working with experimental data in R"
execute:
warning: false
message: false
---
## By the end
```{r}
#| label: setup
#| include: false
library(BIOSCI504)
library(tidyverse)
data(mouse_trial, package = "BIOSCI504")
data(course_data_dictionary, package = "BIOSCI504")
illustrative_comparison <- tibble(
comparison_group = rep(c("A", "B"), each = 5),
outcome = c(1, 2, 3, 4, 5, 2.8, 2.9, 3, 3.1, 3.2)
)
illustrative_outcome_plot <- ggplot(
illustrative_comparison,
aes(x = comparison_group, y = outcome, color = comparison_group)
) +
scale_color_manual(values = c(A = "#177BFB", B = "#0757A8")) +
labs(
x = "Illustrative group",
y = "Illustrative outcome",
caption = "Illustration only — not mouse_trial results"
) +
theme_minimal(base_size = 24)
paired_measurements <- tribble(
~mouse, ~measurement, ~weight_g,
"Mouse A", "Baseline", 20,
"Mouse A", "Final", 25,
"Mouse B", "Baseline", 24,
"Mouse B", "Final", 25
) |>
mutate(measurement = factor(measurement, levels = c("Baseline", "Final")))
paired_measurement_plot <- ggplot(
paired_measurements,
aes(x = measurement, y = weight_g, group = mouse, color = mouse)
) +
geom_line(linewidth = 2) +
geom_point(size = 6) +
scale_color_manual(values = c("Mouse A" = "#177BFB", "Mouse B" = "#0757A8")) +
scale_y_continuous(limits = c(18, 27), breaks = seq(18, 26, by = 2)) +
labs(x = NULL, y = "Weight (g)", color = NULL) +
theme_minimal(base_size = 24) +
theme(legend.position = "bottom")
```
You should be able to:
1. state what one row represents;
2. inspect a table before changing it;
3. choose an operation that matches the biological question;
4. inspect a derived variable before comparing it;
5. distinguish a result, its interpretation and the evidence supporting it.
::: {.notes}
- Purpose: prepare the reasoning used in the exercise without completing it.
- Ask: If every line runs, which outcome is already established?
- Answer: none; execution is not verification.
- Build source: `BIOSCI504` commit `3730f8b813668f317689ac56fabb299c9a1b072e`.
:::
## How this session works
```text
LOOK → PREDICT → TRY IN RSTUDIO → DISCUSS → REVEAL
```
- The slides contain the code and precomputed output.
- Short pauses are marked **Now in RStudio**.
- Work with your neighbour; the TAs will circulate around the room.
::: {.notes}
- You present from this deck; no instructor RStudio window is needed.
- Students use RStudio only at the marked pauses.
- Advance to the following slide for the precomputed answer.
:::
## The mouse experiment
:::: {.columns}
::: {.column width="29%"}
### Before
**48 mice**
Baseline weight
:::
::: {.column width="6%"}
### →
:::
::: {.column width="30%"}
### Assigned group
Control
Nutritional treatment
:::
::: {.column width="6%"}
### →
:::
::: {.column width="29%"}
### After
Final weight
:::
::::
`sex` · `cage` · `batch` provide experimental context for each mouse.
**Did treated mice gain more weight than control mice?**
Treatment varies within cages; treatment and cage are not the same grouping.
::: {.notes}
- Context: 48 mice, one assigned group, weights before and after treatment.
- Clarify: treatment varies within cages; cage is context, not the treatment group.
- Ask: What information must the table retain to answer the question?
- Do not discuss the observed treatment result.
:::
## Before seeing the table
Predict:
- What should one row represent?
- Which columns are needed?
- Which column should identify a mouse?
- Where should the unit of weight be recorded?
::: {.notes}
- Take answers before advancing.
- Listen for mouse ID, treatment, baseline weight and final weight.
- Accept sex, cage and batch as useful experimental context.
:::
## Before seeing the table
Predict:
- What should one row represent?
- Which columns are needed?
- Which column should identify a mouse?
- Where should the unit of weight be recorded?
::: {.callout-note appearance="simple" icon=false}
| One row | Identifier | Assigned group | Measurements | Context |
|---|---|---|---|---|
| one mouse | `mouse_id` | `treatment` | baseline and final weight | sex, cage, batch |
One value per cell. Units belong in metadata or the column name.
:::
::: {.notes}
- Reveal this as a defensible answer, not the only imaginable layout.
- Takeaway: table structure should preserve experimental structure.
- Transition: inspect the actual representation.
:::
## Four rows from the real table {.smaller}
```{r}
#| echo: false
mouse_trial |>
slice_head(n = 4) |>
select(
mouse_id,
treatment,
sex,
baseline_weight_g,
final_weight_g,
cage,
batch
) |>
knitr::kable()
```
::: {.notes}
- This is a genuine preview from `mouse_trial`.
- Ask learners to locate the identifier, group, measurements and context.
- A preview does not establish completeness, uniqueness or plausible ranges.
:::
## What does one row represent?
Choose one:
1. one weight measurement;
2. one mouse;
3. one cage;
4. one treatment group.
::: {.notes}
- Ask for a show of hands before advancing.
- Do not resolve the choice on this slide.
:::
## What does one row represent?
Choose one:
1. one weight measurement;
2. one mouse;
3. one cage;
4. one treatment group.
::: {.callout-note appearance="simple" icon=false}
**One mouse.**
The two measurements occupy different columns on the same mouse row.
:::
::: {.notes}
- Answer: one mouse.
- Takeaway: identifiers, duplicates, sample size and change depend on this choice.
- Return to the observational unit after transformations and summaries.
:::
## Rows, columns and cells
| Part | Meaning in this table |
|---|---|
| row | one observed mouse |
| column | one recorded or derived variable |
| cell | one value for one mouse and variable |
| `mouse_id` | the identifier for the observational unit |
One value per cell; units belong in metadata or the column name.
::: {.notes}
- Connect the vocabulary directly to the displayed table.
- Takeaway: rows are observations, columns are variables, cells are values.
- Source: https://datacarpentry.github.io/spreadsheet-ecology-lesson/01-format-data.html
:::
## Variables have different roles
| Role | Variables |
|---|---|
| identifier | `mouse_id` |
| assigned group | `treatment` |
| biological/context variables | `sex`, `cage`, `batch` |
| continuous measurements | `baseline_weight_g`, `final_weight_g` |
The role follows from the experiment, not merely the R storage type.
::: {.notes}
- Connect to Lecture 2: character and double describe storage.
- Treatment, sex, cage and batch are categorical variables stored as text.
- Weight is a continuous measurement.
:::
## The dictionary is part of the data {.smaller}
```{r}
#| echo: false
course_data_dictionary |>
filter(dataset == "mouse_trial") |>
transmute(
variable,
description,
units = replace_na(units, "—"),
allowed_values = replace_na(allowed_values, "—")
) |>
knitr::kable()
```
::: {.notes}
- A name alone is not a complete variable definition.
- Ask: could the R type tell us whether `27500` is a plausible weight?
- Answer: no; the dictionary supplies units and biological meaning.
:::
## Checkpoint: read the representation
For the cell in row 2 under `final_weight_g`:
- What does its row represent?
- What does its column represent?
- What does the value represent?
- Where does its unit come from?
::: {.notes}
- Take answers before advancing.
- Check that row and measurement are no longer being conflated.
:::
## Checkpoint: read the representation
For the cell in row 2 under `final_weight_g`:
- What does its row represent?
- What does its column represent?
- What does the value represent?
- Where does its unit come from?
::: {.callout-note appearance="simple" icon=false}
- **Row:** mouse M002.
- **Column:** post-treatment weight.
- **Cell:** M002's recorded final weight.
- **Unit:** grams, established by `_g` and the data dictionary.
:::
::: {.notes}
- Reveal and correct any mismatch in the room's answers.
- Takeaway: meaning comes from table structure plus metadata.
:::
## Now in RStudio
Run once in the Console:
```r
library(tidyverse)
data(mouse_trial, package = "BIOSCI504")
```
Keep `mouse_trial` unchanged. We will build temporary pipelines from it.
::: {.notes}
- Pause while everyone loads the package data.
- The TAs circulate; confirm that `mouse_trial` appears in the Environment.
- The next slides contain precomputed output if anyone falls behind.
:::
## A preview is not an inspection
```r
head(mouse_trial)
```
can show examples of values.
It cannot establish:
- the total number of records;
- whether every ID is unique;
- missingness elsewhere;
- complete group counts;
- the full range of a measurement.
::: {.notes}
- Ask: what could be wrong in row 40 that `head()` cannot reveal?
- Takeaway: a preview samples rows; inspection checks the complete object.
- Source: https://datacarpentry.github.io/R-ecology-lesson/instructor/how-r-thinks-about-data.html
:::
## Inspection starts with expectations
| Question | Check | Concerning result |
|---|---|---|
| Is the table complete? | dimensions | missing or extra records |
| Does one row equal one mouse? | distinct IDs | duplicates |
| Are measurements present? | missingness | incomplete data |
| Are groups represented? | counts | imbalance or miscoding |
| Are values plausible? | ranges + units | impossible or mis-scaled values |
Inspection is a sequence of questions, not one command.
::: {.notes}
- Ask learners to state an expected result before each check.
- Takeaway: a check is useful when a failure would change the analysis.
:::
## Now in RStudio: inspect structure
Predict what each command will tell you, then run it:
```r
dim(mouse_trial)
names(mouse_trial)
glimpse(mouse_trial)
```
::: {.notes}
- Allow two minutes; pairs can divide the three commands.
- Ask for dimensions and one observation about the schema.
- Advance for the precomputed output; do not open instructor RStudio.
:::
## Now in RStudio: inspect structure {.smaller}
Predict what each command will tell you, then run it:
```{r}
#| echo: true
dim(mouse_trial)
names(mouse_trial)
glimpse(mouse_trial)
```
::: {.notes}
- Answer: 48 rows and seven columns.
- Point out character identifiers/groups and double weight measurements.
- `glimpse()` does not test every data-quality condition.
:::
## Check the observational unit
```r
mouse_trial |>
summarise(
rows = n(),
distinct_mice = n_distinct(mouse_id)
)
```
What relationship do you expect between the two results?
::: {.notes}
- Ask for the expected relationship before students run it.
- Allow one minute, then advance for the precomputed answer.
:::
## The identifiers support the row claim
```{r}
#| echo: true
mouse_trial |>
summarise(
rows = n(),
distinct_mice = n_distinct(mouse_id)
)
```
Rows equal distinct mouse IDs in this table.
::: {.notes}
- Answer: both are 48.
- A mismatch would trigger investigation; it would not identify the cause.
- Do not introduce the challenge fixture here.
:::
## Now in RStudio: groups and missingness
Before running, predict the number of groups and what `0` would mean:
```r
mouse_trial |>
count(treatment)
mouse_trial |>
summarise(
across(
c(baseline_weight_g, final_weight_g),
~ sum(is.na(.x))
)
)
```
::: {.notes}
- Take predictions, then allow two minutes to run both checks.
- Advance for the precomputed output.
:::
## Now in RStudio: groups and missingness
Before running, predict the number of groups and what `0` would mean:
```{r}
#| echo: true
mouse_trial |>
count(treatment)
mouse_trial |>
summarise(
across(
c(baseline_weight_g, final_weight_g),
~ sum(is.na(.x))
)
)
```
::: {.notes}
- Group counts establish denominators before comparison.
- There is one missing baseline weight and one missing final weight.
- These need not occur in the same mouse; the derived change has two missing values.
- Later checks may examine missingness within groups.
:::
## Now in RStudio: check ranges against units
Predict a plausible weight range in grams, then run:
```r
mouse_trial |>
summarise(
across(
ends_with("_weight_g"),
~ list(range(.x, na.rm = TRUE))
)
)
```
The suffix `_g` makes the expected scale testable.
::: {.notes}
- Ask for a rough plausible range before execution.
- Advance for the precomputed output.
:::
## Now in RStudio: check ranges against units
Predict a plausible weight range in grams, then run:
```{r}
#| echo: true
mouse_trial |>
summarise(
across(
ends_with("_weight_g"),
~ list(range(.x, na.rm = TRUE))
)
)
```
::: {.notes}
- Compare the output with the dictionary and biological expectation.
- A valid double can still use the wrong unit.
- Takeaway: plausible storage is not plausible biology.
:::
## Checkpoint: choose the revealing check
Which check is most direct?
| Concern | Candidate check |
|---|---|
| the same mouse appears twice | ? |
| one weight uses another unit | ? |
| final weights are missing mainly in one group | ? |
::: {.notes}
- Ask pairs to supply one check for each concern.
- Require them to name the evidence each check would produce.
:::
## Checkpoint: choose the revealing check
Which check is most direct?
| Concern | Candidate check |
|---|---|
| the same mouse appears twice | ? |
| one weight uses another unit | ? |
| final weights are missing mainly in one group | ? |
::: {.callout-note appearance="simple" icon=false}
- **Duplicate mouse:** compare rows with distinct IDs, then inspect duplicates.
- **Wrong unit:** compare ranges or plots with the documented unit.
- **Group-dependent missingness:** count missing values within treatment.
:::
::: {.notes}
- Reveal these as defensible checks, not magic commands.
- Takeaway: each concern should map to evidence capable of exposing it.
:::
## The question determines the operation
:::: {.columns}
::: {.column width="48%"}
### Final weight
Are treated mice heavier at the end?
:::
::: {.column width="48%"}
### Weight change
Did treated mice gain more weight?
:::
::::
These are different biological questions.
::: {.notes}
- This is the conceptual centrepiece.
- Final weight combines starting weight and change during the study.
- Neither target is inherently invalid; the operation must match the question.
:::
## Same final weight, different change
```{r}
#| echo: false
#| fig-height: 5.2
#| fig-alt: "Two mice both finish at 25 grams. Mouse A rises from 20 to 25 grams, while Mouse B rises from 24 to 25 grams."
paired_measurement_plot
```
Mouse A gained **5 g**. Mouse B gained **1 g**. Both finished at **25 g**.
::: {.notes}
- Ask: which quantity answers the study question?
- Takeaway: paired measurements retain information the final value alone cannot.
- This illustration does not reveal the treatment comparison.
:::
## Valid code can answer the wrong question
Question: **Did treated mice gain more weight?**
```r
mouse_trial |>
group_by(treatment) |>
summarise(
n = sum(!is.na(final_weight_g)),
mean_final_g = mean(final_weight_g, na.rm = TRUE)
)
```
This code runs. Which question does it actually answer?
::: {.notes}
- Ask learners to name the mismatch before advancing.
- The code is valid and its output could look reasonable.
- Do not run or reveal the treatment means.
:::
## Valid code can answer the wrong question
Question: **Did treated mice gain more weight?**
```r
mouse_trial |>
group_by(treatment) |>
summarise(
n = sum(!is.na(final_weight_g)),
mean_final_g = mean(final_weight_g, na.rm = TRUE)
)
```
This code runs. Which question does it actually answer?
::: {.callout-note appearance="simple" icon=false}
It compares **mean final weight** between treatment groups—not weight gain.
“It ran” does not establish that the code implemented the scientific question.
:::
::: {.notes}
- Answer: it asks whether groups differ in final weight.
- Takeaway: compare the specification with the executed operation.
:::
## Name the quantity before coding
For each mouse:
$$
\text{weight change (g)} = \text{final weight (g)} - \text{baseline weight (g)}
$$
Expected sign: positive for gain, negative for loss.
::: {.notes}
- Move from words to arithmetic to an R name.
- Units remain grams because grams are subtracted from grams.
- Ask for the expected sign before showing the transformation.
:::
## Now in RStudio: derive the variable
```r
mouse_trial |>
mutate(
weight_change_g = ____________________
)
```
What should remain unchanged after `mutate()`?
::: {.notes}
- Give pairs one minute to complete the expression.
- Answer: `final_weight_g - baseline_weight_g`.
- Invariant: still 48 rows, each representing one mouse.
:::
## The derived table state {.smaller}
```{r}
#| echo: true
mouse_trial |>
mutate(
weight_change_g = final_weight_g - baseline_weight_g
) |>
select(mouse_id, baseline_weight_g, final_weight_g, weight_change_g) |>
slice_head(n = 6) |>
knitr::kable()
```
`mutate()` adds a column; it does not change what one row represents.
::: {.notes}
- Ask students to verify one subtraction from the displayed values.
- Takeaway: inspect the new table state before continuing.
- Source: https://datacarpentry.github.io/R-ecology-lesson/instructor/working-with-data.html#making-new-columns-with-mutate
:::
## Inspect the new state
```{r}
#| echo: true
mouse_trial |>
mutate(
weight_change_g = final_weight_g - baseline_weight_g
) |>
summarise(
type = typeof(weight_change_g),
missing = sum(is.na(weight_change_g)),
minimum = min(weight_change_g, na.rm = TRUE),
maximum = max(weight_change_g, na.rm = TRUE)
)
```
::: {.notes}
- Ask what each check contributes: type, missingness and plausible range.
- Stop before grouping the real changes by treatment.
- Takeaway: inspect → transform → inspect again.
:::
## Missing inputs propagate
```text
final weight baseline weight change
25.3 g − NA = NA
NA − 24.2 g = NA
```
The mouse remains a row. The derived measurement is missing.
::: {.notes}
- Connect to Lecture 2: arithmetic cannot reconstruct an unobserved input.
- Do not silently delete these rows.
- Record the effective sample and whether missingness differs by group.
:::
## Checkpoint: predict the transformation
For one mouse:
```text
baseline = 24.1 g
final = 25.3 g
```
Before calculating:
- What sign should the change have?
- What is its value and unit?
- What does the row represent after adding the column?
::: {.notes}
- Take answers before advancing.
- Use this as a small-case test of the transformation.
:::
## Checkpoint: predict the transformation
For one mouse:
```text
baseline = 24.1 g
final = 25.3 g
```
Before calculating:
- What sign should the change have?
- What is its value and unit?
- What does the row represent after adding the column?
::: {.callout-note appearance="simple" icon=false}
- **Sign:** positive.
- **Value:** 1.2 g.
- **Row:** still the same mouse.
Adding a derived variable changes columns, not the observational unit.
:::
::: {.notes}
- Reveal the answer and correct sign or unit errors.
- Transition: retain individual observations when comparing groups.
:::
## Plot individual observations first
```{r}
#| echo: false
#| fig-height: 4.8
#| fig-alt: "Two dot plots with five observations each. Group A spans outcomes 1 to 5, while group B is tightly clustered around 3."
illustrative_outcome_plot +
geom_jitter(width = 0.08, size = 5, show.legend = FALSE)
```
::: {.notes}
- Neutral illustration: both groups have the same mean but different spread.
- Ask what is visible before the mean: sample size, values, spread and extremes.
:::
## A summary deliberately removes detail
```{r}
#| echo: false
#| fig-height: 4.8
#| fig-alt: "The same two dot plots with diamond mean markers. Both means equal 3 despite the much wider spread in group A."
illustrative_outcome_plot +
geom_jitter(width = 0.08, size = 5, alpha = 0.45, show.legend = FALSE) +
stat_summary(
fun = mean,
geom = "point",
shape = 18,
size = 8,
show.legend = FALSE
) +
labs(
caption = "Diamonds show group means; illustration only"
)
```
Same mean; different evidence.
::: {.notes}
- Ask what is lost if only the diamonds are shown.
- Takeaway: retain observations beside summaries.
:::
## Predict the grouped summary
```r
tibble(
comparison_group = rep(c("A", "B"), each = 5),
outcome = c(1, 2, 3, 4, 5, 2.8, 2.9, 3, 3.1, 3.2)
) |>
group_by(comparison_group) |>
summarise(
n = n(),
mean = mean(outcome),
.groups = "drop"
)
```
Before running: how many rows should the result contain?
::: {.notes}
- Ask for the row count and new observational unit before advancing.
- Do not substitute the real treatment comparison.
:::
## Predict the grouped summary
```{r}
#| echo: true
tibble(
comparison_group = rep(c("A", "B"), each = 5),
outcome = c(1, 2, 3, 4, 5, 2.8, 2.9, 3, 3.1, 3.2)
) |>
group_by(comparison_group) |>
summarise(
n = n(),
mean = mean(outcome),
.groups = "drop"
)
```
One row now represents one group.
::: {.notes}
- Answer: two rows, one per group.
- Keep `n` beside the mean so the denominator remains visible.
- Source: https://datacarpentry.github.io/R-ecology-lesson/instructor/working-with-data.html#the-split-apply-combine-approach
:::
## Uncertainty belongs with an estimate
| Quantity | What it reports |
|---|---|
| observed difference | size and direction in these data |
| confidence interval | effect sizes compatible with the data and model under the stated procedure |
| p-value | how unusual the result would be if a specified null model were true |
None establishes that the design, data handling or code are correct.
::: {.notes}
- Keep the statistical treatment modest.
- A confidence interval is not the range containing 95% of mice.
- A p-value is neither the probability of the null nor biological importance.
- Do not calculate the exercise's final values.
:::
## Before choosing a test, record the assumptions
- What is the observational unit?
- Are observations independent enough for the proposed comparison?
- How were treatment and measurements assigned or collected?
- Are missing values plausibly ignorable?
- Do spread, shape or unusual values challenge the summary?
- Are all measurements expressed in the documented unit?
::: {.notes}
- Shared cages can challenge a simple independence assumption.
- Cage and batch may contribute variation; do not turn this into a mixed-model lecture.
- Test choice follows the question, design and observed data.
:::
## Visualization is verification evidence
A plot can reveal:
- spread hidden by a mean;
- an observation on the wrong scale;
- a group with few usable values;
- an unexpected relationship between baseline and final weight;
- a transformation whose sign or range is implausible.
The figure should be checked before it is interpreted.
::: {.notes}
- A plot is an analytical result and a diagnostic artifact.
- In the exercise, retain observations and check agreement with numerical results.
:::
## Checkpoint: what does the summary row represent? {.smaller}
After `group_by(treatment)` and `summarise()`:
1. Does one row still represent one mouse?
2. What does `n` establish?
3. Which feature of the individual data can the mean conceal?
4. Which assumption could shared cages challenge?
::: {.callout-note appearance="simple" icon=false style="visibility: hidden;"}
1. One row represents one treatment group—not one mouse.
2. `n` records the denominator used by the summary.
3. A mean can conceal spread, overlap, clusters or outliers.
4. Shared cages can challenge simple independence.
:::
::: {.notes}
- Take answers before advancing.