-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides2.qmd
1385 lines (1066 loc) · 30.9 KB
/
slides2.qmd
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: "Creating data plots for effective decision-making using statistical inference with R"
author: "Dianne Cook <br> Monash University"
format:
revealjs:
theme:
- default
- custom.scss
slide-number: c/t
chalkboard: true
code-line-numbers: false
message: false
highlight-style: pygments
footer: "[https://github.com/StatSocAus/tutorial_effective_data_plots](https://github.com/StatSocAus/tutorial_effective_data_plots)"
---
```{r, include = FALSE}
#| label: libraries-for-participants
library(tidyverse)
library(colorspace)
library(patchwork)
library(broom)
library(palmerpenguins)
library(nullabor)
library(dslabs) # For stars data
```
```{r, include = FALSE}
#| label: code-for-nice-slides
library(DT)
options(width = 200)
knitr::opts_chunk$set(
fig.width = 3,
fig.height = 3,
fig.align = "center",
dev.args = list(bg = 'transparent'),
out.width = "100%",
fig.retina = 3,
echo = FALSE,
warning = FALSE,
message = FALSE,
cache = FALSE
)
theme_set(ggthemes::theme_gdocs(base_size = 12) +
theme(plot.background =
element_rect(fill = 'transparent', colour = NA),
axis.line.x = element_line(color = "black",
linetype = "solid"),
axis.line.y = element_line(color = "black",
linetype = "solid"),
plot.title.position = "plot",
plot.title = element_text(size = 18),
panel.background =
element_rect(fill = 'transparent', colour = "black"),
legend.background =
element_rect(fill = 'transparent', colour = NA),
legend.key =
element_rect(fill = 'transparent', colour = NA)
)
)
```
```{r}
#| echo: false
#| eval: false
# divergingx_hcl(palette="Zissou 1", n=10)
# [1] "#3B99B1" "#40ABA5" "#6FB798" "#9FC095" "#C7C98A"
# [6] "#EABE23" "#E8A419" "#E78802" "#EA6400" "#F5191C"
# specplot(divergingx_hcl(palette="Zissou 1", n=10))
```
## Session 2: Making decisions and inferential statements based on data plots {.center .center-align}
## Outline
```{r}
plan <- tribble(~time, ~topic,
"3:00-3:20", "What is your plot testing?",
"3:20-3:35", "Creating null samples",
"3:35-4:00", "Conducting a lineup test",
"4:00-4:30", "Testing for best plot design")
knitr::kable(plan)
```
## What is your plot testing?
:::: {.columns}
::: {.column width=50%}
<br>
```{r}
#| eval: false
#| echo: true
LM_FIT <- lm(VAR2 ~ VAR1,
data = DATA)
FIT_ALL <- augment(LM_FIT)
ggplot(FIT_ALL, aes(x=.FITTED,
y=.RESID)) +
geom_point()
```
<br>
What will we be assessing using this plot?
:::
::: {.column width=10%}
:::
::: {.column width=40%}
::: {.fragment}
Is the model misspecified?
::: {style="font-size: 90%;"}
- non-linearity
- heteroskedasticity
- outliers/anomalies
- non-normality
- fitted value distribution
:::
:::
:::
::::
## What is your plot testing?
:::: {.columns}
::: {.column width=60%}
```{r}
#| fig-width: 4
#| fig-height: 4
cars_lm <- lm(mpg ~ hp, data = mtcars)
cars_all <- augment(cars_lm)
ggplot(cars_all, aes(x=.fitted, y=.resid)) + geom_point()
```
:::
::: {.column width=40%}
What do you see?
::: {.fragment}
✗ non-linearity <br>
✓ heteroskedasticity <br>
✗ outliers/anomalies <br>
✓ non-normality <br>
✗ fitted value distribution is uniform
:::
::: {.fragment}
<br>
<span style="color: #F5191C;"> Are you sure? </span>
:::
:::
::::
## What is your plot testing?
:::: {.columns}
::: {.column width=50%}
<br>
```{r}
#| eval: false
#| echo: true
ggplot(DATA,
aes(x=VAR1,
y=VAR2,
color=CLASS)) +
geom_point()
```
<br>
What will we be assessing using this plot?
:::
::: {.column width=10%}
:::
::: {.column width=40%}
::: {.fragment}
<br>
Is there a difference between the groups?
- location
- shape
- outliers/anomalies
:::
:::
::::
## What is your plot testing?
:::: {.columns}
::: {.column width=50%}
```{r}
#| fig-width: 4
#| fig-height: 4.5
ggplot(penguins,
aes(x=flipper_length_mm,
y=bill_length_mm,
color=species)) +
geom_point(alpha=0.8) +
scale_color_discrete_divergingx(palette="Zissou 1") +
theme(legend.title = element_blank(),
legend.position = "bottom",
legend.direction = "horizontal",
legend.text = element_text(size="8"))
```
<br>
:::
::: {.column width=10%}
:::
::: {.column width=40%}
What do you see?
::: {.fragment}
There a difference between the groups
✓ location <br>
✗ shape <br>
✓ outliers/anomalies
:::
::: {.fragment}
<br>
<span style="color: #F5191C;"> Are you sure? </span>
:::
:::
::::
::: {.hidden}
```{r}
ebs <- read_csv("data/EBS_faculty_keywords.csv") %>%
mutate(id = 1:n())
theory_stem <- c("infer", "tsa", "statist", "theory", "theori", "bayesian", "infer", "robust", "highdimension", "estim", "nonlinear", "depend", "probabilist", "memori", "risk", "oper", "constrain", "stochast", "minimum", "distanc", "nonparametr", "econometr", "simul", "semiparametr", "mathemat", "actuari","optimisationoper", "test","extrem","causal", "multivari", "econometr", "actuari","cross","section","quantit","uncertainti")
```
:::
## Statistical thinking
- Because the <span style="color: #3B99B1;"> plot </span> is specified using a functional mapping of the variables, it <span style="color: #3B99B1;"> is a statistic</span>.
- The null and alternative hypotheses are indicated from the plot description.
- Applying the function to a dataset provides the observed value.
## Null hypothesis, example 1
:::: {.columns}
::: {.column width=50%}
<br>
```{r}
#| eval: false
#| echo: true
LM_FIT <- lm(VAR2 ~ VAR1,
data = DATA)
FIT_ALL <- augment(LM_FIT)
ggplot(FIT_ALL, aes(x=.FITTED,
y=.RESID)) +
geom_point()
```
<br>
What is the null hypothesis?
::: {.fragment}
*There is no relationship between residuals and fitted values.* This is $H_o$.
:::
:::
::: {.column width=5%}
:::
::: {.column width=45%}
::: {.fragment .f80}
<br><br><br>
**Alternative hypothesis**, $H_a$:
*There is some relationship*, which might be
- non-linearity
- heteroskedasticity
- outliers/anomalies
:::
:::
::::
## Null hypothesis, example 2
:::: {.columns}
::: {.column width=50%}
<br>
```{r}
#| eval: false
#| echo: true
ggplot(DATA,
aes(x=VAR1,
y=VAR2,
color=CLASS)) +
geom_point()
```
<br>
What is the null hypothesis?
::: {.fragment}
*There is no difference between the classes.* This is $H_o$.
:::
:::
::: {.column width=5%}
:::
::: {.column width=45%}
::: {.fragment .f80}
<br><br><br>
**Alternative hypothesis**, $H_a$:
*There is some difference between the classes*, which might be
- location
- shape
- outliers/anomalies
:::
:::
::::
## Exercise
What is being tested in each of these plot descriptions?
:::: {.columns}
::: {.column width=30% .fragment}
```{r}
#| eval: false
#| echo: true
ggplot(DATA,
aes(x=VAR1)) +
geom_histogram()
```
:::
::: {.column width=38% .fragment}
```{r}
#| eval: false
#| echo: true
ggplot(DATA,
aes(x=VAR1,
fill=VAR2)) +
geom_bar(position="fill")
```
:::
::: {.column width=30% .fragment}
```{r}
#| eval: false
#| echo: true
ggplot(DATA,
aes(x=VAR1,
y=VAR2)) +
geom_point() +
geom_smooth()
```
:::
::: {.column width=30% .fragment}
Distribution of VAR1 is ?
:::
::: {.column width=38% .fragment}
There is no relationship between VAR1 and VAR2. More specifically, the proportion of VAR2 in each level of VAR1 is the same.
:::
::: {.column width=30% .fragment .f80}
<br>
There is no relationship between VAR1 and VAR2. Particularly, VAR2 is not dependent on VAR1 and there is no trend.</span>
:::
::::
## Creating null samples {.center}
## Statistical thinking
:::: {.columns}
::: {.column width=50% .f80}
Sampling distribution for a t-statistic. Values expected assuming $H_o$ is true. <span style="color: #F5191C;"> Shaded areas </span> indicate extreme values.
```{r}
#| fig-width: 3
#| fig-height: 2.6
#| out-width: 70%
t_df <- tibble(x=seq(-4, 4, 0.1), y=dt(x, df=5))
t_cr1 <- tibble(x=c(-4, seq(-4, -2.57, 0.05), -2.57),
y=c(0, dt(x[-c(1,31)], df=5), 0))
t_cr2 <- tibble(x=c(2.57, seq(2.57, 4, 0.05), 4),
y=c(0, dt(x[-c(1,31)], df=5), 0))
ggplot() +
geom_hline(yintercept = 0) +
geom_line(data=t_df, aes(x=x, y=y)) +
geom_polygon(data=t_cr1, aes(x=x, y=y), fill="#F5191C", alpha=0.8) +
geom_polygon(data=t_cr2, aes(x=x, y=y), fill="#F5191C", alpha=0.8) +
theme(axis.title = element_blank(),
axis.text = element_blank(),
panel.grid.major = element_blank())
```
:::
::: {.column width=50% .fragment}
<br><br><br><br>
<span style="color: #3B99B1;"> For making comparisons when plotting, draw a number of null samples, and plot them with the same script in the plot description.</span>
:::
::::
## Creating null samples, example 1
:::: {.columns}
::: {.column width=50% .f80}
```{r}
#| eval: false
#| echo: true
ggplot(DATA,
aes(x=VAR1,
y=VAR2,
color=CLASS)) +
geom_point()
```
<br>
$H_o$: *There is no difference between the classes.*
::: {.fragment}
How would you generate null samples?
:::
::: {.fragment}
<br>
Break any association by permuting (scrambling/shuffling/re-sampling) the CLASS variable.
:::
:::
::: {.column width=50%}
::: {.fragment}
```{r}
#| fig-width: 4
#| fig-height: 8
#| out-width: 60%
p1 <- ggplot(penguins,
aes(x=flipper_length_mm,
y=bill_length_mm,
color=species)) +
geom_point(alpha=0.8) +
scale_color_discrete_divergingx(palette="Zissou 1") +
ggtitle("Original") +
theme(legend.position = "none")
set.seed(235)
p2 <- ggplot(penguins,
aes(x=flipper_length_mm,
y=bill_length_mm,
color=sample(species, replace = TRUE))) +
geom_point(alpha=0.8) +
scale_color_discrete_divergingx(palette="Zissou 1") +
ggtitle("Permuted") +
theme(legend.position = "none")
p1/p2
```
:::
:::
::::
## Creating null samples, example 2
:::: {.columns}
::: {.column width=50% .f80}
```{r}
#| eval: false
#| echo: true
LM_FIT <- lm(VAR2 ~ VAR1,
data = DATA)
FIT_ALL <- augment(LM_FIT)
ggplot(FIT_ALL, aes(x=.FITTED,
y=.RESID)) +
geom_point()
```
::: {.fragment}
$H_o$: *There is no relationship between residuals and fitted values.*
:::
::: {.fragment}
How would you generate null samples?
:::
::: {.fragment}
<br>
Break any association by
- permuting residuals,
- or residual rotation,
- or simulate residuals from a normal distribution.
:::
:::
::: {.column width=50%}
::: {.fragment}
```{r}
#| fig-width: 3
#| fig-height: 5.5
#| out-width: 60%
cars_lm <- lm(mpg ~ hp, data = mtcars)
cars_all <- augment(cars_lm)
set.seed(1235)
ggplot(lineup(null_lm(mpg ~ hp, method="rotate"),
cars_all, n=2, pos=1), aes(x=.fitted, y=.resid)) +
geom_point() +
facet_wrap(~.sample, ncol=1) +
theme(axis.title = element_blank(),
axis.text = element_blank())
```
:::
:::
::::
## Conducting a lineup test {.center}
## Steps
1. Create a lineup of $m-1$ null plots + 1 data plot, where the data plot is randomly placed among nulls. Remove any distracting information, like tick labels, titles.
2. Ask uninvolved observer(s) to pick the plot that is most different. (May need to use a crowd-sourcing service.)
3. Compute the probability that the data plot was chosen, assuming it is no different from the null plots. This is the $p$-value.
4. Decide to reject or fail to reject the null.
## Lineup example 1 <span style="font-size: 70%;"> (1/2) </span>
::: {.f60}
```{r}
#| code-fold: true
#| echo: true
#| fig-width: 12
#| fig-height: 8
#| out.width: 80%
set.seed(241)
ggplot(lineup(null_permute("species"), penguins, n=15),
aes(x=flipper_length_mm,
y=bill_length_mm,
color=species)) +
geom_point(alpha=0.8) +
facet_wrap(~.sample, ncol=5) +
scale_color_discrete_divergingx(palette="Zissou 1") +
theme(legend.position = "none",
axis.title = element_blank(),
axis.text = element_blank(),
panel.grid.major = element_blank())
```
:::
## Lineup example 1 <span style="font-size: 70%;"> (2/2) </span>
If 10 people are shown this lineup and all 10 pick plot 2, which is the data plot, the $p$-value will be 0.
Generally, we can compute the probability that the data plot is chosen by $x$ out of $K$ observers, shown a lineup of $m$ plots, using a simulation approach that extends from a binomial distribution, with $p=1/m$.
```{r}
#| echo: true
pvisual(10, 10, 15)
```
This means we would reject $H_o$ and conclude that there is a difference in the distribution of bill length and flipper length between the species of penguins.
## Lineup example 2 <span style="font-size: 70%;"> (1/2) </span>
::: {.f60}
```{r}
#| code-fold: true
#| echo: true
#| fig-width: 9
#| fig-height: 6
#| out.width: 80%
data(wasps)
set.seed(258)
wasps_l <- lineup(null_permute("Group"), wasps[,-1], n=15)
wasps_l <- wasps_l %>%
mutate(LD1 = NA, LD2 = NA)
for (i in unique(wasps_l$.sample)) {
x <- filter(wasps_l, .sample == i)
xlda <- MASS::lda(Group~., data=x[,1:42])
xp <- MASS:::predict.lda(xlda, x, dimen=2)$x
wasps_l$LD1[wasps_l$.sample == i] <- xp[,1]
wasps_l$LD2[wasps_l$.sample == i] <- xp[,2]
}
ggplot(wasps_l,
aes(x=LD1,
y=LD2,
color=Group)) +
geom_point(alpha=0.8) +
facet_wrap(~.sample, ncol=5) +
scale_color_discrete_divergingx(palette="Zissou 1") +
theme(legend.position = "none",
axis.title = element_blank(),
axis.text = element_blank(),
panel.grid.major = element_blank())
```
:::
## Lineup example 2 <span style="font-size: 70%;"> (2/2) </span>
If 10 people are shown this lineup and 1 picked the data plot (position 6), which is the data plot, the $p$-value will be large.
```{r}
#| echo: true
pvisual(1, 10, 15)
```
This means we would NOT reject $H_o$ and conclude that there is NO difference in the distribution of groups.
## What is the $p$-value?
```{r}
#| label: coin
head <- '<img src="images/heads.png" height = "70px" style="vertical-align:middle;">'
tail <- '<img src="images/tails.png" height = "70px" style="vertical-align:middle;">'
```
- Suppose $X$ is the number of nandus out of $n$ independent tosses.
- Let $p$ be the probability of getting a `r head` for this coin.
- **Hypotheses**: $H_0: p = 0.5$ vs. $H_a: p > 0.5$. <br> Alternative $H_a$ is saying we believe that the coin is biased to nandus. <br> Alternative needs to be decided before seeing data.
- **Assumption**: Each toss is independent with equal chance of getting a nandu.
<!--
**Test statistic**: $X \sim B(n, p)$. Recall $E(X\mid H_0) = np_0$.<br> We observe $n, x, \widehat{p}$. Test statistic is $\widehat{p} - p_0$.
- **P-value**: $P(X ~ \geq ~ x\mid H_0)$
- Conclusion**: Reject null hypothesis when the $p$-value is less than<br> some significance level $\alpha$. Usually $\alpha = 0.05$.
-->
## What is the $p$-value?
- Suppose I have a coin that I'm going to flip `r tail` `r head`
- **Experiment 1**: I flipped the coin 10 times and this is the result:
<center>
```{r coin-bias, results='asis'}
set.seed(924)
samp10 <- sample(rep(c(head, tail), c(7, 3)))
cat(paste0(samp10, collapse = ""))
```
</center>
- The result is 7 nandus and 3 tails. So 70% are nandus.
- Do you believe the coin is biased based on this data?
## What is the $p$-value?
- **Experiment 2**: Suppose now I flip the coin 100 times and this is the outcome:
```{r coin-bias100, results='asis'}
samp100 <- sample(rep(c(head, tail), c(70, 30)))
cat(paste0(samp100[1:42], collapse = ""))
```
- We observe 70 nandus and 30 tails. So again 70% are nandus.
- Based on this data, do you think the coin is biased?
# Calculate the $p$-value
:::: {.columns}
::: {.column width=50% .f70}
**Experiment 1 (n=10)**
- We observed $x=7$, or $\widehat{p} = 0.7$.
- Assuming $H_0$ is true, we expect $np=10\times 0.5=5$.
- Calculate the $P(X \geq 7)$
<br>
<br>
```{r echo=TRUE}
sum(dbinom(7:10, 10, 0.5))
```
:::
::: {.column width=50% .f70}
**Experiment 2 (n=100)**
- We observed $x=70$, or $\widehat{p} = 0.7$.
- Assuming $H_0$ is true, we expect $np=100\times 0.5=50$.
- Calculate the $P(X \geq 70)$
<br>
<br>
```{r echo=TRUE}
sum(dbinom(70:100, 100, 0.5))
```
:::
::::
## Lineup $p$-value and power
Suppose $x$ out of $n$ people detected the data plot from a lineup, then
the **visual inference p-value** is given as $P(X \geq x)$ where $X \sim B(n, 1/m)$, but
::: {.fragment}
the assumption of independence is not strictly satisfied, if people are shown the same lineup. So the $p$-value is computed by simulation with
```{r}
#| eval: false
#| echo: true
nullabor::pvisual()
```
:::
::: {.fragment}
<br> and the **power of a lineup** is estimated as $x/n$. We'll use this to compare the signal strengths for different plot designs. *Stay tuned!*
:::
## Lineup example 3 <span style="font-size: 70%;"> (1/2) </span>
:::: {.columns}
::: {.column width=15%}
<br><br>Which plot is the most different?
:::
::: {.column width=85%}
```{r}
#| fig-width: 8
#| fig-height: 5.5
#| out-width: 90%
set.seed(357)
ggplot(lineup(null_dist("temp", "exp",
list(rate = 1 / mean(dslabs::stars$temp))),
stars, n=15),
aes(x=temp)) +
geom_density(fill="black", alpha=0.7) +
facet_wrap(~.sample, ncol=5, scales="free") +
theme(axis.title = element_blank(),
axis.text = element_blank())
```
:::
::::
## Lineup example 3 <span style="font-size: 70%;"> (2/2) </span>
:::: {.columns}
::: {.column width=50%}
Plot description was:
```{r}
#| eval: false
#| echo: true
ggplot(stars, aes(x=temp)) +
geom_density()
```
::: {.fragment}
In particular, the researcher is interested to know if star temperature is a skewed distribution.
:::
::: {.fragment}
$H_o: X\sim exp(\widehat{\lambda})$ <br>
$H_a:$ it has a different distribution.
:::
:::
::: {.column width=50% .fragment}
::: {.fragment}
Generate the lineup with:
```{r}
#| eval: false
#| echo: true
lineup(null_dist("temp", "exp",
list(rate = 1 /
mean(dslabs::stars$temp))),
stars, n=15)
```
:::
::: {.fragment}
<br><br>Compute the $p$-value based on your responses to the lineup (previous slide).
```{r}
#| eval: false
#| echo: true
pvisual(n=??, k=??, m=15)
```
:::
:::
::::
## Lineup example 4 <span style="font-size: 70%;"> (1/2) </span>
:::: {.columns}
::: {.column width=15%}
<br><br>Which row of plots is the most different?
:::
::: {.column width=85%}
```{r}
tb <- read_csv(here::here("data/TB_notifications_2023-08-21.csv"))
tb_aus <- tb %>%
filter(iso3 == "AUS") %>%
filter(year > 1996)
tb_aus_sa <- tb_aus %>%
filter(year > 2012) %>%
select(iso3, year,
newrel_f014:newrel_f65,
newrel_m014:newrel_m65) %>%
pivot_longer(cols=newrel_f014:newrel_m65,
names_to = "sex_age",
values_to = "count") %>%
filter(!is.na(count)) %>%
separate(sex_age, into=c("stuff",
"sex_age")) %>%
mutate(sex = str_sub(sex_age, 1, 1),
age = str_sub(sex_age, 2,
str_length(sex_age))) %>%
mutate(age = case_when(
age == "014" ~ "0-14",
age == "1524" ~ "15-24",
age == "2534" ~ "25-34",
age == "3544" ~ "35-44",
age == "4554" ~ "45-54",
age == "5564" ~ "55-64",
age == "65" ~ "65")) %>%
select(iso3, year, sex, age, count)
```
```{r}
#| fig-width: 8
#| fig-height: 5.5
#| out-width: 90%
l1 <- ggplot(tb_aus_sa,
aes(x=year,
y=count,
fill=sex)) +
geom_col(position="fill") +
facet_wrap(~age, ncol=7) +
scale_fill_discrete_divergingx(palette="ArmyRose") +
theme(legend.position="none",
axis.title = element_blank(),
axis.text = element_blank()) +
ggtitle("sample 1")
set.seed(446)
l2 <- tb_aus_sa %>%
uncount(count) %>%
group_by(age) %>%
mutate(sex = sample(sex, replace = FALSE)) %>%
count(sex, age, year) %>%
ggplot(aes(x=year,
y=n,
fill=sex)) +
geom_col(position="fill") +
facet_wrap(~age, ncol=7) +
scale_fill_discrete_divergingx(palette="ArmyRose") +
theme(legend.position="none",
axis.title = element_blank(),
axis.text = element_blank()) +
ggtitle("sample 2")
l3 <- tb_aus_sa %>%
uncount(count) %>%
group_by(age) %>%
mutate(sex = sample(sex, replace = FALSE)) %>%
count(sex, age, year) %>%
ggplot(aes(x=year,
y=n,
fill=sex)) +
geom_col(position="fill") +
facet_wrap(~age, ncol=7) +
scale_fill_discrete_divergingx(palette="ArmyRose") +
theme(legend.position="none",
axis.title = element_blank(),
axis.text = element_blank()) +
ggtitle("sample 3")
l1 + l2 + l3 + plot_layout(ncol=1)
```
:::
::::
## Lineup example 4 <span style="font-size: 70%;"> (2/2) </span>
:::: {.columns}
::: {.column width=50%}
Plot description was:
```{r}
#| eval: false
#| echo: true
ggplot(tb_aus_sa,
aes(x=year,
y=count,
fill=sex)) +
geom_col(position="fill") +
facet_wrap(~age, ncol=7)
```
::: {.fragment}
$H_o:$ Proportion of males and females is the same for each year, conditional on age group <br>
$H_a:$ it's not
:::
:::
::: {.column width=50% .fragment}
::: {.fragment}