generated from UtrechtUniversity/simple-r-project
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPart_4_Outcomes.qmd
1530 lines (1174 loc) · 40 KB
/
Part_4_Outcomes.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: "Preoperative Atelectasis"
subtitle: "Part 4: Outcomes"
author: "Javier Mancilla Galindo"
date: "`r Sys.Date()`"
execute:
echo: false
warning: false
toc: true
toc_float: true
format:
html:
embed-resources: true
pdf:
documentclass: scrartcl
editor: visual
---
\pagebreak
# Setup
#### Packages used
```{r}
#| echo: true
if (!require("pacman", quietly = TRUE)) {
install.packages("pacman")
}
pacman::p_load(
tidyverse, # Used for basic data handling and visualization.
table1, #Used to add lables to variables.
RColorBrewer, #Color palettes for data visualization.
gridExtra, #Used to arrange multiple ggplots in a grid.
grid, #Used to arrange multiple ggplots in a grid.
mgcv, #Used to model non-linear relationships with a general additive model.
ggmosaic, #Used to create mosaic plots.
car, #Used assess distribution of continuous variables (stacked Q-Q plots).
simpleboot, boot, # Used to calculate mean atelectasis coverage and
# 95%CI through bootstrapping.
gt, #Used to present tables in html format.
report #Used to cite packages used in this session.
)
```
##### Session and package dependencies
```{r}
# Credits chunk of code: Alex Bossers, Utrecht University ([email protected])
# remove clutter
session <- sessionInfo()
session$BLAS <- NULL
session$LAPACK <- NULL
session$loadedOnly <- NULL
# write log file
writeLines(
capture.output(print(session, locale = FALSE)),
paste0("sessions/",lubridate::today(), "_session_Part_4.txt")
)
session
```
Set seed (for reproducibility of bootstrapping) as the current year 2023:
```{r}
#| echo: true
seed <- 2023
```
```{r}
#| include: false
# Create directories for sub-folders
figfolder <- "../results/output_figures"
dir.create(figfolder, showWarnings = FALSE)
```
```{r}
#| include: false
# Load dataset
data <- read.csv("../data/processed/atelectasis_included.csv",
na.strings="NA",
row.names = NULL)
# Recode variables
source("scripts/variable_names.R")
```
\pagebreak
# Outcome variable
```{r}
#| include: false
attach(data)
```
Corroborate that atelectasis(Yes/No) matches atelectasis percent equal or different to 0%:
```{r}
table(atelectasis,atelectasis_percent)
```
Yes, these do match.
## Prevalence of atelectasis
```{r}
frequencies <- table(atelectasis)
percent <- round((prop.table(frequencies)*100),1)
total <- rbind(frequencies,percent)
total
```
Prevalence of atelectasis with 95% confidence interval
```{r}
prev_atelectasis <- prop.test(frequencies, correct=FALSE)
confint <- round((prev_atelectasis$conf.int*100),2)
prev_atelectasis
```
> The prevalence of atelectasis was **`r percent[1]` (95%CI: `r confint`)**.
```{r}
#| include: false
rm(frequencies, percent, prev_atelectasis, total, confint)
```
## Atelectasis - obesity class
Mean expected frequency:
```{r}
mean_exp <- data %>%
drop_na(type_obesity, atelectasis) %>%
summarize(mean_expected_freq = n()/(nlevels(type_obesity)*nlevels(atelectasis)))
mean_exp
```
Frequencies:
```{r}
frequencies <- table(type_obesity, atelectasis)
frequencies
```
Percentage:
```{r}
round(prop.table(frequencies,1),4)*100
```
Mosaic Plot
```{r, fig.height=3, fig.width=5}
data %>%
mutate(atelectasis = fct_relevel(atelectasis, "No", "Yes")) %>%
ggplot() +
geom_mosaic(
aes(x = product(atelectasis,type_obesity),
fill=atelectasis),
na.rm = TRUE
) +
scale_fill_manual(values=c("grey95","lightsteelblue4")) +
labs(
y = "Atelectasis",
x = "Obesity class"
) +
theme_mosaic() +
theme(axis.text.x=element_text(size=rel(0.8)))
```
```{r}
chi <- chisq.test(frequencies, correct=FALSE)
chi
```
```{r}
#| include: false
rm(mean_exp,frequencies, chi)
```
#### Atelectasis location by obesity class
Mean expected frequency:
```{r}
mean_exp <- data %>%
drop_na(type_obesity, atelectasis_location) %>%
summarize(mean_expected_freq = n()/(nlevels(type_obesity)*nlevels(atelectasis_location)))
mean_exp
```
Mean expected frequency is greater than 5.0, so chi-squared without continuity correction is adequate.
Frequencies:
```{r}
frequencies <- table(type_obesity, atelectasis_location)
frequencies
```
Percentage:
```{r}
round(prop.table(frequencies,1),4)*100
```
Mosaic Plot
```{r, fig.height=3, fig.width=6}
ggplot(data = data) +
geom_mosaic(
aes(x = product(type_obesity,atelectasis_location),
fill=type_obesity),
na.rm = TRUE
) +
scale_fill_manual(values=c("seagreen1","seagreen3","seagreen4")) +
labs(
y = "Obesity class",
x = "Atelectasis location"
) +
theme_mosaic() + guides(fill="none")
```
```{r}
#| warning: false
chi <- chisq.test(frequencies, correct=FALSE)
chi
```
Prevalence of atelectasis with 95% confidence intervals calculated with sourced script ***Prevalence_atelectasis.R***
```{r}
#| include: false
source("scripts/Prevalence_atelectasis.R", local = knitr::knit_global())
```
> The prevalence of atelectasis was greater in higher obesity classes: class 1, n=`r atelectasis$n[3]` (`r atelectasis$prev[3]`%, 95%CI:`r atelectasis$confint[3]`); class 2, n=`r atelectasis$n[5]` (`r atelectasis$prev[5]`%, 95%CI:`r atelectasis$confint[5]`); and class 3, n=`r atelectasis$n[7]` (`r atelectasis$prev[7]`%, 95%CI:`r atelectasis$confint[7]`) (p\<0.001).
> Of those who had atelectasis, the most frequent presentation was the right lung base predominance n=`r atelectasis$Unilateral[1]`, compared to bilateral lung bases n=`r atelectasis$Bilateral[1]`. When examining this by obesity class, the observed distribution was not significantly different for those with class 1, 2, and 3 obesity categories (n=`r atelectasis$Unilateral[3]`, n=`r atelectasis$Unilateral[5]`, and n=`r atelectasis$Unilateral[7]`, respectively) (p=`r round(chi$p.value,3)`).
```{r}
#| include: false
rm(mean_exp, frequencies, chi, location, atelectasis_obesity, atelectasis_total, atelectasis_total_location, atelectasis_obesity_location, atelectasis)
```
#### Atelectasis Percent
```{r}
#| include: false
## Change 'false' for 'true' above to show plot.
# Assess distribution if assumed to be a numeric variable:
data %>%
mutate(atelectasis_percent = as.numeric(atelectasis_percent)) %>%
ggplot(aes(x = atelectasis_percent)) +
geom_histogram(colour = "black") +
facet_grid(type_obesity ~ .)
```
```{r}
#| include: false
## Change 'false' for 'true' above to show plot.
# Distribution excluding 0:
data %>%
mutate(atelectasis_percent = as.numeric(atelectasis_percent)) %>%
filter(atelectasis_percent >0) %>%
ggplot(aes(x = atelectasis_percent)) +
geom_histogram(colour = "black") +
facet_grid(type_obesity ~ .)
```
##### Mean atelectasis percentage
The following would be the mean atelectasis percentage coverage if a normal distribution were assumed, which is what has been done in some prior studies:
```{r}
data %>% summarize(
mean = mean(atelectasis_percent),
sd = sd(atelectasis_percent)
)
```
And by obesity class:
```{r}
data %>% group_by(type_obesity) %>%
summarize(
mean = mean(atelectasis_percent),
sd = sd(atelectasis_percent)
)
```
As is evident from these numbers, assuming normality causes standard deviation to capture negative values, which is impossible in reality for this variable.
Thus, bootstrapping the mean and 95%CI is expected to lead to more appropriate estimates.
Mean by bootstrapping for the total sample:
```{r}
set.seed(seed)
boot_atel <- one.boot(data$atelectasis_percent, mean, R=10000)
mean_boot <- mean(boot_atel$t)
mean_boot
```
Bootstrap 95% confidence intervals:
```{r}
#| warning: false
boot_ci <- boot.ci(boot_atel)
boot_ci
```
The bias-corrected and accelerated (BCa) bootstrap interval is known to lead to more stable intervals with better coverage. Will report this. However, it is a good thing that here 95%CI through different methods do not lead to widely different results.
Now, I will calculate this for different BMI categories:
```{r}
data_class_1 <- data %>% filter(type_obesity=="Class 1 Obesity")
data_class_2 <- data %>% filter(type_obesity=="Class 2 Obesity")
data_class_3 <- data %>% filter(type_obesity=="Class 3 Obesity")
```
###### Class 1
Mean:
```{r}
set.seed(seed)
boot_class1 <- one.boot(data_class_1$atelectasis_percent, mean, R=10000)
mean_boot_class1 <- mean(boot_class1$t)
mean_boot_class1
```
95% CI:
```{r}
#| warning: false
boot_ci_class1 <- boot.ci(boot_class1)
boot_ci_class1
```
###### Class 2
Mean:
```{r}
set.seed(seed)
boot_class2 <- one.boot(data_class_2$atelectasis_percent, mean, R=10000)
mean_boot_class2 <- mean(boot_class2$t)
mean_boot_class2
```
95% CI:
```{r}
#| warning: false
boot_ci_class2 <- boot.ci(boot_class2)
boot_ci_class2
```
###### Class 3
Mean:
```{r}
set.seed(seed)
boot_class3 <- one.boot(data_class_3$atelectasis_percent, mean, R=10000)
mean_boot_class3 <- mean(boot_class3$t)
mean_boot_class3
```
95% CI:
```{r}
#| warning: false
boot_ci_class3 <- boot.ci(boot_class3)
boot_ci_class3
```
> The mean atelectasis percentage coverage in the sample was `r round(mean_boot,2)`% (95%CI:`r round(boot_ci$bca[1,4],2)`-`r round(boot_ci$bca[1,5],2)`) and according to obesity categories: class 1 (`r round(mean_boot_class1,2)`%, 95%CI:`r round(boot_ci_class1$bca[1,4],2)`-`r round(boot_ci_class1$bca[1,5],2)`), class 2 (`r round(mean_boot_class2,2)`%, 95%CI:`r round(boot_ci_class2$bca[1,4],2)`-`r round(boot_ci_class2$bca[1,5],2)`), and class 3 (`r round(mean_boot_class3,2)`%, 95%CI:`r round(boot_ci_class3$bca[1,4],2)`-`r round(boot_ci_class3$bca[1,5],2)`).
What could happen if I took random subsamples between n=20 and n=30 similar to what has been done in other studies and calculate mean BMI and mean atelectasis percentage assuming normal distributions?
Random sample
```{r}
set.seed(seed)
random_sample_1 <- sample_n(data,20)
random_sample_1 %>% summarize(
mean_BMI = mean(BMI),
mean_atelectasis = mean(atelectasis_percent)
)
```
```{r}
set.seed(seed)
random_sample_2 <- sample_n(data,25)
random_sample_2 %>% summarize(
mean_BMI = mean(BMI),
mean_atelectasis = mean(atelectasis_percent)
)
```
```{r}
set.seed(seed)
random_sample_3 <- sample_n(data,30)
random_sample_3 %>% summarize(
mean_BMI = mean(BMI),
mean_atelectasis = mean(atelectasis_percent)
)
```
```{r}
#| include: false
rm(list=setdiff(ls(pattern = "boot"), lsf.str()))
rm(list=setdiff(ls(pattern = "class"), lsf.str()))
rm(list=setdiff(ls(pattern = "sample"), lsf.str()))
```
##### Atelectasis percentage by obesity class
Now, I will continue assessing atelectasis percentage if assumed to be categorical ordinal:
Mean expected frequency:
```{r}
mean_exp <- data %>%
mutate(
atelectasis_percent=factor(atelectasis_percent)) %>%
summarize(
mean_expected_freq = n()/(nlevels(type_obesity)*nlevels(atelectasis_percent))
)
mean_exp
```
Mean expected frequency is greater than 5.0, so chi-squared without continuity correction is adequate.
Frequencies:
```{r}
frequencies <- table(atelectasis_percent,type_obesity)
frequencies
```
Percentage by obesity class
```{r}
prop_fig2a <- prop.table(frequencies,margin=2)
round(prop_fig2a*100,2)
```
Barplot of absolute frequencies:
```{r}
barplot(frequencies,beside=TRUE)
```
```{r}
#| warning: false
chi <- chisq.test(frequencies, correct=FALSE)
chi
```
##### Barplot of atelectasis percentage by obesity class category
```{r, fig.height=5, fig.width=8}
barplot(prop_fig2a,beside=TRUE,ylim=c(0,1),ylab="Relative frequency",
col=brewer.pal(9,"Blues"),
legend.text=c("0%","2.5%","5%","7.5%","10%","12.5%","15%","17.5%","27.5%"),
space = c(0.2, 1.5)
)
Figure2a <- recordPlot()
```
```{r}
#| include: false
rm(mean_exp,frequencies,percent,frequencies,chi,Figure2a,prop_fig2a)
```
##### Smooth term?
```{r}
plot(atelectasis_percent~BMI,
main="Scatterplot",
xlab="Body mass index (kg/m²)",
ylab="Atelectasis percent (%)"
)
```
Atelectasis percent seems to increase as BMI increases. However, relationship is not linear.
```{r}
#| include: false
## Change 'false' for 'true' above to show plot.
# Would a smooth term be more useful to model atel_percent? Fit with loess:
ggplot(data, aes(BMI,atelectasis_percent)) +
geom_point(size=0.6,color="gray40") +
geom_smooth(method="loess", color="darkblue") +
ylab("Atelectasis percent (%)") +
xlab("Body mass index (kg/m²)") +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
axis.text.x = element_text(size=rel(1.2)),
axis.text.y = element_text(size=rel(1.2))
)
```
Models evaluated with the accompanying sourced script ***nonlinear_BMI_Atelectasis.R***
```{r}
#| include: false
source("scripts/nonlinear_BMI_Atelectasis.R", local = knitr::knit_global())
```
All models are significantly better than linear. Thus, using a smooth term for BMI to predict atelectasis percent is better than modelling a linear relationship.
Best AIC:
```{r}
data.frame(
model = c("k=2","k=4","k=6","k=8"),
AIC = round(c(AIC_k2,AIC_k4,AIC_k6,AIC_k8),1)
) %>% gt()
```
Regarding AIC, greatest improvement in AIC is k=6. Will model with k=5 and k=7 to compare
Best AIC:
```{r}
data.frame(
model = c("k=5","k=6","k=7"),
AIC = round(c(AIC_k5,AIC_k6,AIC_k7),1)
) %>% gt()
```
k=6 offers the lowest AIC. Will keep k=6 to model.
```{r}
fig1a
```
Positive non-monotonic relationship since atelectasis increases as BMI increases only after \~BMI equal to 42.
Will assess Spearman's correlation again only to have a rough idea (will not report this in the paper since the relationship is not monotonic):
```{r}
spearman <- cor.test(spo2_VPO,atelectasis_percent,
method="spearman",
exact=FALSE
)
spearman
```
> Atelectasis percent exhibited a negative non-linear non-monotonic relationship with SpO2 (**Figure 1A**, rho= `r round(spearman$estimate,3)`, p\<0.001).
Note that this p-value refers to the smooth term vs linear as assessed in GAM models.
Interestingly, this figure is almost a mirror image of the priorly created plot for SpO2 \~ BMI.
```{r}
#| include: false
rm(AIC_k2,AIC_k4,AIC_k5,AIC_k6,AIC_k7,AIC_k8,model,spearman,fig1a)
```
## Atelectasis - age
```{r}
boxplot(age~atelectasis,
ylab="Age",
xlab="Atelectasis"
)
```
Assess distribution of age by atelectasis (yes/no):
```{r}
data_age <- data %>% group_by(atelectasis)
```
```{r}
#| include: false
## Change 'false' for 'true' above to show plot.
#Assess distribution of age by atelectasis (yes/no):
ggplot(data_age,aes(x = age)) +
geom_histogram(fill = "lightsteelblue4", colour = "black") +
facet_grid(atelectasis ~ .)
```
```{r}
#| include: false
## Change 'false' for 'true' above to show plot.
qqPlot(age ~ atelectasis, data=data_age)
```
Distribution near-normal, will assess mean and variance for further testing:
```{r}
mean_age <- data_age %>%
summarise(n=n(),
age_mean = round(mean(age),2),
sd = round(sd(age),2),
variance = round(var(age),4)
)
mean_age %>% gt()
```
Variances near-similar, but group sizes differ. Welch's t-test more suitable:
```{r}
t_test <- t.test(age ~ atelectasis, data = data_age)
t_test
```
> Age was similarly distributed among patients without atelectasis (`r round(mean_age$age_mean[2],1)`, sd:`r round(mean_age$sd[2],1)`) and those with atelectasis (`r round(mean_age$age_mean[1],1)`, sd:`r round(mean_age$sd[1],1)`) (p=`r round(t_test$p.value,3)`).
```{r}
#| include: false
rm(data_age,mean_age,t_test)
```
## Atelectasis - sex
Mean expected frequency:
```{r}
mean_exp <- data %>%
drop_na(sex, atelectasis) %>%
summarize(mean_expected_freq = n()/(nlevels(sex)*nlevels(atelectasis)))
mean_exp
```
Mean expected frequency is greater than 5.0, so chi-squared without continuity correction is adequate.
Frequencies:
```{r}
frequencies <- table(sex, atelectasis)
frequencies
```
Percentage:
```{r}
percent <- round((prop.table(frequencies, 1)*100),2)
percent
```
Mosaic Plot
```{r, fig.height=3, fig.width=5}
data %>%
mutate(atelectasis = fct_relevel(atelectasis, "No", "Yes")) %>%
ggplot() +
geom_mosaic(
aes(x = product(atelectasis,sex),
fill=atelectasis),
na.rm = TRUE
) +
scale_fill_manual(values=c("grey95","lightsteelblue4")) +
labs(
y = "Atelectasis",
x = "Sex"
) +
theme_mosaic() +
theme(axis.text.x=element_text(size=rel(0.8)))
```
```{r}
chi <- chisq.test(frequencies, correct=FALSE)
chi
```
> There were no significant differences in atelectasis ocurrence between men (`r round(percent[2,1],1)`%) and women (`r round(percent[1,1],1)`%) (p=`r round(chi$p.value,3)`).
```{r}
#| include: false
rm(mean_exp,frequencies,percent,chi)
```
## Atelectasis - OSA
Mean expected frequency:
```{r}
mean_exp <- data %>%
drop_na(sleep_apnea, atelectasis) %>%
summarize(mean_expected_freq = n()/(nlevels(sleep_apnea)*nlevels(atelectasis)))
mean_exp
```
Mean expected frequency is greater than 5.0, so chi-squared without continuity correction is adequate.
Frequencies:
```{r}
frequencies <- table(sleep_apnea, atelectasis)
frequencies
```
Percentage:
```{r}
percent <- round((prop.table(frequencies, 1)*100),2)
percent
```
Mosaic Plot
```{r, fig.height=3, fig.width=5}
data %>%
mutate(atelectasis = fct_relevel(atelectasis, "No", "Yes")) %>%
ggplot() +
geom_mosaic(
aes(x = product(atelectasis,sleep_apnea),
fill=atelectasis),
na.rm = TRUE
) +
scale_fill_manual(values=c("grey95","lightsteelblue4")) +
labs(
y = "Atelectasis",
x = "Obstructive sleep apnea"
) +
theme_mosaic() +
theme(axis.text.x=element_text(size=rel(0.8)))
```
```{r}
chi <- chisq.test(frequencies, correct=FALSE)
chi
```
> Patients with a diagnosis of obstructive sleep apnea had atelectasis more frequently (`r round(percent[2,1],1)`%) than those without the diagnosis (`r round(percent[1,1],1)`%) (p\<0.001).
```{r}
#| include: false
rm(mean_exp,frequencies, percent, chi)
```
#### Atelectasis location by OSA
Mean expected frequency:
```{r}
mean_exp <- data %>%
drop_na(sleep_apnea, atelectasis_location) %>%
summarize(mean_expected_freq = n()/(nlevels(sleep_apnea)*nlevels(atelectasis_location)))
mean_exp
```
Mean expected frequency is greater than 5.0, so chi-squared without continuity correction is adequate.
Frequencies:
```{r}
frequencies <- table(sleep_apnea, atelectasis_location)
frequencies
```
Percentage:
```{r}
percent <- round((prop.table(frequencies, 1)*100),2)
percent
```
Mosaic Plot
```{r, fig.height=3, fig.width=5}
ggplot(data = data) +
geom_mosaic(aes(
x = product(atelectasis_location,sleep_apnea),
fill=atelectasis_location),
na.rm = TRUE
) +
scale_fill_manual(values=c("seagreen1","seagreen4")) +
labs(
y = "Atelectasis location",
x = "Obstructive sleep apnea"
) +
theme_mosaic() + guides(fill="none")
```
```{r}
chi <- chisq.test(frequencies, correct=FALSE)
chi
```
> The location of atelectasis was not different among patients with and without OSA (p=`r round(chi$p.value,3)`).
```{r}
#| include: false
rm(mean_exp,frequencies, percent, chi)
```
## Atelectasis - Asthma
Mean expected frequency:
```{r}
mean_exp <- data %>%
drop_na(asthma, atelectasis) %>%
summarize(mean_expected_freq = n()/(nlevels(asthma)*nlevels(atelectasis)))
mean_exp
```
Mean expected frequency is greater than 5.0, so chi-squared without continuity correction is adequate.
Frequencies:
```{r}
frequencies <- table(asthma, atelectasis)
frequencies
```
Percentage:
```{r}
percent <- round((prop.table(frequencies, 1)*100),2)
percent
```
Mosaic Plot
```{r, fig.height=3, fig.width=5}
data %>%
mutate(atelectasis = fct_relevel(atelectasis, "No", "Yes")) %>%
ggplot() +
geom_mosaic(
aes(x = product(atelectasis,asthma),
fill=atelectasis),
na.rm = TRUE
) +
scale_fill_manual(values=c("grey95","lightsteelblue4")) +
labs(
y = "Atelectasis",
x = "Asthma"
) +
theme_mosaic() +
theme(axis.text.x=element_text(size=rel(0.8)))
```
```{r}
chi <- chisq.test(frequencies, correct=FALSE)
chi
```
> Patients with a diagnosis of asthma did not have atelectasis more frequently (`r round(percent[2,1],1)`%) than those without the diagnosis (`r round(percent[1,1],1)`%) (p=`r round(chi$p.value,3)`).
```{r}
#| include: false
rm(mean_exp,frequencies, percent, chi)
```
#### Atelectasis location by asthma status
Mean expected frequency:
```{r}
mean_exp <- data %>%
drop_na(asthma, atelectasis_location) %>%
summarize(mean_expected_freq = n()/(nlevels(asthma)*nlevels(atelectasis_location)))
mean_exp
```
Mean expected frequency is greater than 5.0, so chi-squared without continuity correction is adequate.
Frequencies:
```{r}
frequencies <- table(asthma, atelectasis_location)
frequencies
```
Percentage:
```{r}
percent <- round((prop.table(frequencies, 1)*100),2)
percent
```
Mosaic Plot
```{r, fig.height=3, fig.width=5}
ggplot(data = data) +
geom_mosaic(aes(
x = product(atelectasis_location,asthma),
fill=atelectasis_location),
na.rm = TRUE
) +
scale_fill_manual(values=c("seagreen1","seagreen4")) +
labs(
y = "Atelectasis location",
x = "Asthma"
) +
theme_mosaic() + guides(fill="none")
```
```{r}
chi <- chisq.test(frequencies, correct=FALSE)
chi
```
> The location of atelectasis was not different among patients with and without asthma (p=`r round(chi$p.value,3)`).
```{r}
#| include: false
rm(mean_exp,frequencies, percent, chi)
```
## Atelectasis - COPD
Mean expected frequency:
```{r}
mean_exp <- data %>%
drop_na(COPD, atelectasis) %>%
summarize(mean_expected_freq = n()/(nlevels(COPD)*nlevels(atelectasis)))
mean_exp
```
Mean expected frequency is greater than 5.0, so chi-squared without continuity correction is adequate.
Frequencies:
```{r}
frequencies <- table(COPD, atelectasis)
frequencies
```
Percentage:
```{r}
percent <- round((prop.table(frequencies, 1)*100),2)
percent
```
Mosaic Plot
```{r, fig.height=3, fig.width=5}
data %>%
mutate(atelectasis = fct_relevel(atelectasis, "No", "Yes")) %>%
ggplot() +
geom_mosaic(
aes(x = product(atelectasis,COPD),
fill=atelectasis),
na.rm = TRUE
) +
scale_fill_manual(values=c("grey95","lightsteelblue4")) +
labs(
y = "Atelectasis",
x = "COPD"
) +
theme_mosaic() +
theme(axis.text.x=element_text(size=rel(0.8)))
```
```{r}
chi <- chisq.test(frequencies, correct=FALSE)
chi
```
> Patients with a diagnosis of COPD had atelectasis more frequently (`r round(percent[2,1],1)`%) than those without the diagnosis (`r round(percent[1,1],1)`%) (p\<0.001).
```{r}
#| include: false
rm(mean_exp,frequencies, percent, chi)
```