################################################################################
######### L Godin - Assignment 5 ##############################################
################################################################################
##### Set working directory and install packages ###############################
setwd("C:/Users/laura/OneDrive - University of Calgary/Classes/MDCH 610 - Bioinformatics/Assignments")
getwd()
## [1] "C:/Users/laura/OneDrive - University of Calgary/Classes/MDCH 610 - Bioinformatics/Assignments"
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(pwr)
## Warning: package 'pwr' was built under R version 4.4.3
library(DescTools)
## Warning: package 'DescTools' was built under R version 4.4.3
library(haven)
## Warning: package 'haven' was built under R version 4.4.3
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.4.3
library(BSDA)
## Warning: package 'BSDA' was built under R version 4.4.3
## Loading required package: lattice
##
## Attaching package: 'BSDA'
## The following object is masked from 'package:datasets':
##
## Orange
library(effectsize)
## Warning: package 'effectsize' was built under R version 4.4.3
########################## PART A ##############################################
################################################################################
# Categorizing individuals by 'race' is problematic for several reasons.
# One of these reasons is that racial classes are poorly correlated to the
# underlying genetic profiles of individuals. This results in more uncertainty
# surrounding the 'true' variable they represent, and this can result in the study
# having decreased power. Unfortunately, self-reported 'race' is often the only
# measure of an individual's genetic profile available within administrative
# datasets (especially historical datasets).
# If you are using R, you can import the "lbw.csv" file.
# Within the lbw dataset individuals are classified as
# 'white', 'black', or 'other'.
### Import data ###
read.csv('lbw.csv')
## id low age lwt race smoke ptl ht ui ftv bwt
## 1 85 0 19 182 Black Nonsmoker 0 0 1 0 2523
## 2 86 0 33 155 Other Nonsmoker 0 0 0 3 2551
## 3 87 0 20 105 White Smoker 0 0 0 1 2557
## 4 88 0 21 108 White Smoker 0 0 1 2 2594
## 5 89 0 18 107 White Smoker 0 0 1 0 2600
## 6 91 0 21 124 Other Nonsmoker 0 0 0 0 2622
## 7 92 0 22 118 White Nonsmoker 0 0 0 1 2637
## 8 93 0 17 103 Other Nonsmoker 0 0 0 1 2637
## 9 94 0 29 123 White Smoker 0 0 0 1 2663
## 10 95 0 26 113 White Smoker 0 0 0 0 2665
## 11 96 0 19 95 Other Nonsmoker 0 0 0 0 2722
## 12 97 0 19 150 Other Nonsmoker 0 0 0 1 2733
## 13 98 0 22 95 Other Nonsmoker 0 1 0 0 2750
## 14 99 0 30 107 Other Nonsmoker 1 0 1 2 2750
## 15 100 0 18 100 White Smoker 0 0 0 0 2769
## 16 101 0 18 100 White Smoker 0 0 0 0 2769
## 17 102 0 15 98 Black Nonsmoker 0 0 0 0 2778
## 18 103 0 25 118 White Smoker 0 0 0 3 2782
## 19 104 0 20 120 Other Nonsmoker 0 0 1 0 2807
## 20 105 0 28 120 White Smoker 0 0 0 1 2821
## 21 106 0 32 121 Other Nonsmoker 0 0 0 2 2835
## 22 107 0 31 100 White Nonsmoker 0 0 1 3 2835
## 23 108 0 36 202 White Nonsmoker 0 0 0 1 2836
## 24 109 0 28 120 Other Nonsmoker 0 0 0 0 2863
## 25 111 0 25 120 Other Nonsmoker 0 0 1 2 2877
## 26 112 0 28 167 White Nonsmoker 0 0 0 0 2877
## 27 113 0 17 122 White Smoker 0 0 0 0 2906
## 28 114 0 29 150 White Nonsmoker 0 0 0 2 2920
## 29 115 0 26 168 Black Smoker 0 0 0 0 2920
## 30 116 0 17 113 Black Nonsmoker 0 0 0 1 2920
## 31 117 0 17 113 Black Nonsmoker 0 0 0 1 2920
## 32 118 0 24 90 White Smoker 1 0 0 1 2948
## 33 119 0 35 121 Black Smoker 1 0 0 1 2948
## 34 120 0 25 155 White Nonsmoker 0 0 0 1 2977
## 35 121 0 25 125 Black Nonsmoker 0 0 0 0 2977
## 36 123 0 29 140 White Smoker 0 0 0 2 2977
## 37 124 0 19 138 White Smoker 0 0 0 2 2977
## 38 125 0 27 124 White Smoker 0 0 0 0 2922
## 39 126 0 31 215 White Smoker 0 0 0 2 3005
## 40 127 0 33 109 White Smoker 0 0 0 1 3033
## 41 128 0 21 185 Black Smoker 0 0 0 2 3042
## 42 129 0 19 189 White Nonsmoker 0 0 0 2 3062
## 43 130 0 23 130 Black Nonsmoker 0 0 0 1 3062
## 44 131 0 21 160 White Nonsmoker 0 0 0 0 3062
## 45 132 0 18 90 White Smoker 0 0 1 0 3076
## 46 133 0 18 90 White Smoker 0 0 1 0 3076
## 47 134 0 32 132 White Nonsmoker 0 0 0 4 3080
## 48 135 0 19 132 Other Nonsmoker 0 0 0 0 3090
## 49 136 0 24 115 White Nonsmoker 0 0 0 2 3090
## 50 137 0 22 85 Other Smoker 0 0 0 0 3090
## 51 138 0 22 120 White Nonsmoker 0 1 0 1 3100
## 52 139 0 23 128 Other Nonsmoker 0 0 0 0 3104
## 53 140 0 22 130 White Smoker 0 0 0 0 3132
## 54 141 0 30 95 White Smoker 0 0 0 2 3147
## 55 142 0 19 115 Other Nonsmoker 0 0 0 0 3175
## 56 143 0 16 110 Other Nonsmoker 0 0 0 0 3175
## 57 144 0 21 110 Other Smoker 0 0 1 0 3203
## 58 145 0 30 153 Other Nonsmoker 0 0 0 0 3203
## 59 146 0 20 103 Other Nonsmoker 0 0 0 0 3203
## 60 147 0 17 119 Other Nonsmoker 0 0 0 0 3225
## 61 148 0 17 119 Other Nonsmoker 0 0 0 0 3225
## 62 149 0 23 119 Other Nonsmoker 0 0 0 2 3232
## 63 150 0 24 110 Other Nonsmoker 0 0 0 0 3232
## 64 151 0 28 140 White Nonsmoker 0 0 0 0 3234
## 65 154 0 26 133 Other Smoker 2 0 0 0 3260
## 66 155 0 20 169 Other Nonsmoker 1 0 1 1 3274
## 67 156 0 24 115 Other Nonsmoker 0 0 0 2 3274
## 68 159 0 28 250 Other Smoker 0 0 0 6 3303
## 69 160 0 20 141 White Nonsmoker 2 0 1 1 3317
## 70 161 0 22 158 Black Nonsmoker 1 0 0 2 3317
## 71 162 0 22 112 White Smoker 2 0 0 0 3317
## 72 163 0 31 150 Other Smoker 0 0 0 2 3321
## 73 164 0 23 115 Other Smoker 0 0 0 1 3331
## 74 166 0 16 112 Black Nonsmoker 0 0 0 0 3374
## 75 167 0 16 135 White Smoker 0 0 0 0 3374
## 76 168 0 18 229 Black Nonsmoker 0 0 0 0 3402
## 77 169 0 25 140 White Nonsmoker 0 0 0 1 3416
## 78 170 0 32 134 White Smoker 1 0 0 4 3430
## 79 172 0 20 121 Black Smoker 0 0 0 0 3444
## 80 173 0 23 190 White Nonsmoker 0 0 0 0 3459
## 81 174 0 22 131 White Nonsmoker 0 0 0 1 3460
## 82 175 0 32 170 White Nonsmoker 0 0 0 0 3473
## 83 176 0 30 110 Other Nonsmoker 0 0 0 0 3475
## 84 177 0 20 127 Other Nonsmoker 0 0 0 0 3487
## 85 179 0 23 123 Other Nonsmoker 0 0 0 0 3544
## 86 180 0 17 120 Other Smoker 0 0 0 0 3572
## 87 181 0 19 105 Other Nonsmoker 0 0 0 0 3572
## 88 182 0 23 130 White Nonsmoker 0 0 0 0 3586
## 89 183 0 36 175 White Nonsmoker 0 0 0 0 3600
## 90 184 0 22 125 White Nonsmoker 0 0 0 1 3614
## 91 185 0 24 133 White Nonsmoker 0 0 0 0 3614
## 92 186 0 21 134 Other Nonsmoker 0 0 0 2 3629
## 93 187 0 19 235 White Smoker 0 1 0 0 3629
## 94 188 0 25 95 White Smoker 3 0 1 0 3637
## 95 189 0 16 135 White Smoker 0 0 0 0 3643
## 96 190 0 29 135 White Nonsmoker 0 0 0 1 3651
## 97 191 0 29 154 White Nonsmoker 0 0 0 1 3651
## 98 192 0 19 147 White Smoker 0 0 0 0 3651
## 99 193 0 19 147 White Smoker 0 0 0 0 3651
## 100 195 0 30 137 White Nonsmoker 0 0 0 1 3699
## 101 196 0 24 110 White Nonsmoker 0 0 0 1 3728
## 102 197 0 19 184 White Smoker 0 1 0 0 3756
## 103 199 0 24 110 Other Nonsmoker 1 0 0 0 3770
## 104 200 0 23 110 White Nonsmoker 0 0 0 1 3770
## 105 201 0 20 120 Other Nonsmoker 0 0 0 0 3770
## 106 202 0 25 241 Black Nonsmoker 0 1 0 0 3790
## 107 203 0 30 112 White Nonsmoker 0 0 0 1 3799
## 108 204 0 22 169 White Nonsmoker 0 0 0 0 3827
## 109 205 0 18 120 White Smoker 0 0 0 2 3856
## 110 206 0 16 170 Black Nonsmoker 0 0 0 4 3860
## 111 207 0 32 186 White Nonsmoker 0 0 0 2 3860
## 112 208 0 18 120 Other Nonsmoker 0 0 0 1 3884
## 113 209 0 29 130 White Smoker 0 0 0 2 3884
## 114 210 0 33 117 White Nonsmoker 0 0 1 1 3912
## 115 211 0 20 170 White Smoker 0 0 0 0 3940
## 116 212 0 28 134 Other Nonsmoker 0 0 0 1 3941
## 117 213 0 14 135 White Nonsmoker 0 0 0 0 3941
## 118 214 0 28 130 Other Nonsmoker 0 0 0 0 3969
## 119 215 0 25 120 White Nonsmoker 0 0 0 2 3983
## 120 216 0 16 95 Other Nonsmoker 0 0 0 1 3997
## 121 217 0 20 158 White Nonsmoker 0 0 0 1 3997
## 122 218 0 26 160 Other Nonsmoker 0 0 0 0 4054
## 123 219 0 21 115 White Nonsmoker 0 0 0 1 4054
## 124 220 0 22 129 White Nonsmoker 0 0 0 0 4111
## 125 221 0 25 130 White Nonsmoker 0 0 0 2 4153
## 126 222 0 31 120 White Nonsmoker 0 0 0 2 4167
## 127 223 0 35 170 White Nonsmoker 1 0 0 1 4174
## 128 224 0 19 120 White Smoker 0 0 0 0 4238
## 129 225 0 24 116 White Nonsmoker 0 0 0 1 4593
## 130 226 0 45 123 White Nonsmoker 0 0 0 1 4990
## 131 4 1 28 120 Other Smoker 1 0 1 0 709
## 132 10 1 29 130 White Nonsmoker 0 0 1 2 1021
## 133 11 1 34 187 Black Smoker 0 1 0 0 1135
## 134 13 1 25 105 Other Nonsmoker 1 1 0 0 1330
## 135 15 1 25 85 Other Nonsmoker 0 0 1 0 1474
## 136 16 1 27 150 Other Nonsmoker 0 0 0 0 1588
## 137 17 1 23 97 Other Nonsmoker 0 0 1 1 1588
## 138 18 1 24 128 Black Nonsmoker 1 0 0 1 1701
## 139 19 1 24 132 Other Nonsmoker 0 1 0 0 1729
## 140 20 1 21 165 White Smoker 0 1 0 1 1790
## 141 22 1 32 105 White Smoker 0 0 0 0 1818
## 142 23 1 19 91 White Smoker 2 0 1 0 1885
## 143 24 1 25 115 Other Nonsmoker 0 0 0 0 1893
## 144 25 1 16 130 Other Nonsmoker 0 0 0 1 1899
## 145 26 1 25 92 White Smoker 0 0 0 0 1928
## 146 27 1 20 150 White Smoker 0 0 0 2 1928
## 147 28 1 21 200 Black Nonsmoker 0 0 1 2 1928
## 148 29 1 24 155 White Smoker 1 0 0 0 1936
## 149 30 1 21 103 Other Nonsmoker 0 0 0 0 1970
## 150 31 1 20 125 Other Nonsmoker 0 0 1 0 2055
## 151 32 1 25 89 Other Nonsmoker 2 0 0 1 2055
## 152 33 1 19 102 White Nonsmoker 0 0 0 2 2082
## 153 34 1 19 112 White Smoker 0 0 1 0 2084
## 154 35 1 26 117 White Smoker 1 0 0 0 2084
## 155 36 1 24 138 White Nonsmoker 0 0 0 0 2100
## 156 37 1 17 130 Other Smoker 1 0 1 0 2125
## 157 40 1 20 120 Black Smoker 0 0 0 3 2126
## 158 42 1 22 130 White Smoker 1 0 1 1 2187
## 159 43 1 27 130 Black Nonsmoker 0 0 1 0 2187
## 160 44 1 20 80 Other Smoker 0 0 1 0 2211
## 161 45 1 17 110 White Smoker 0 0 0 0 2225
## 162 46 1 25 105 Other Nonsmoker 1 0 0 1 2240
## 163 47 1 20 109 Other Nonsmoker 0 0 0 0 2240
## 164 49 1 18 148 Other Nonsmoker 0 0 0 0 2282
## 165 50 1 18 110 Black Smoker 1 0 0 0 2296
## 166 51 1 20 121 White Smoker 1 0 1 0 2296
## 167 52 1 21 100 Other Nonsmoker 1 0 0 4 2301
## 168 54 1 26 96 Other Nonsmoker 0 0 0 0 2325
## 169 56 1 31 102 White Smoker 1 0 0 1 2353
## 170 57 1 15 110 White Nonsmoker 0 0 0 0 2353
## 171 59 1 23 187 Black Smoker 0 0 0 1 2367
## 172 60 1 20 122 Black Smoker 0 0 0 0 2381
## 173 61 1 24 105 Black Smoker 0 0 0 0 2381
## 174 62 1 15 115 Other Nonsmoker 0 0 1 0 2381
## 175 63 1 23 120 Other Nonsmoker 0 0 0 0 2395
## 176 65 1 30 142 White Smoker 1 0 0 0 2410
## 177 67 1 22 130 White Smoker 0 0 0 1 2410
## 178 68 1 17 120 White Smoker 0 0 0 3 2414
## 179 69 1 23 110 White Smoker 1 0 0 0 2424
## 180 71 1 17 120 Black Nonsmoker 0 0 0 2 2438
## 181 75 1 26 154 Other Nonsmoker 1 1 0 1 2442
## 182 76 1 20 106 Other Nonsmoker 0 0 0 3 2450
## 183 77 1 26 190 White Smoker 0 0 0 0 2466
## 184 78 1 14 101 Other Smoker 1 0 0 0 2466
## 185 79 1 28 95 White Smoker 0 0 0 2 2466
## 186 81 1 14 100 Other Nonsmoker 0 0 0 2 2495
## 187 82 1 23 94 Other Smoker 0 0 0 0 2495
## 188 83 1 17 142 Black Nonsmoker 0 1 0 0 2495
## 189 84 1 21 130 White Smoker 0 1 0 3 2495
lbw <- read.csv('lbw.csv')
lbw$race <- as.factor(lbw$race); lbw$smoke <- as.factor(lbw$smoke)
glimpse(lbw)
## Rows: 189
## Columns: 11
## $ id <int> 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101…
## $ low <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ age <int> 19, 33, 20, 21, 18, 21, 22, 17, 29, 26, 19, 19, 22, 30, 18, 18, …
## $ lwt <int> 182, 155, 105, 108, 107, 124, 118, 103, 123, 113, 95, 150, 95, 1…
## $ race <fct> Black, Other, White, White, White, Other, White, Other, White, W…
## $ smoke <fct> Nonsmoker, Nonsmoker, Smoker, Smoker, Smoker, Nonsmoker, Nonsmok…
## $ ptl <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0…
## $ ht <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ ui <int> 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1…
## $ ftv <int> 0, 3, 1, 2, 0, 0, 1, 1, 1, 0, 0, 1, 0, 2, 0, 0, 0, 3, 0, 1, 2, 3…
## $ bwt <int> 2523, 2551, 2557, 2594, 2600, 2622, 2637, 2637, 2663, 2665, 2722…
########################## QUESTION 1-A ########################################
# The birthweight of an infant has been hypothesized to be associated with the
# genetic phenotype of the mother. An imperfect test of this hypothesis can be
# completed by using self-reported race to group individuals and compare the
# mean birthweights in each group. Using STATA/R, perform a hypothesis test
# assessing if there are differences in the mean birthweights
# among the three groups.
# Step 1: State your null and alternative hypotheses
print('Null hypothesis: µ1 = µ2 = µ3')
## [1] "Null hypothesis: µ1 = µ2 = µ3"
print('Alternative hypothesis: Not all µi are equal')
## [1] "Alternative hypothesis: Not all µi are equal"
# Step 2: State your significance level
print ('Alpha = 0.05 for a One-Way ANOVA')
## [1] "Alpha = 0.05 for a One-Way ANOVA"
## Calculate normality ##
shapiro.test(lbw$bwt)
##
## Shapiro-Wilk normality test
##
## data: lbw$bwt
## W = 0.99251, p-value = 0.4435
# p-value = 0.4435
# p = 0.4435 < 0.05, so data is normally distributed
# Step 3: Calculate the test statistic assuming the null hypothesis is true
## One Way ANOVA ##
anovaRace <- aov(bwt ~ race, data = lbw); anova(anovaRace)
## Analysis of Variance Table
##
## Response: bwt
## Df Sum Sq Mean Sq F value Pr(>F)
## race 2 5048361 2524181 4.949 0.008052 **
## Residuals 186 94866938 510037
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Analysis of Variance Table
#
# Response: bwt
# Df Sum Sq Mean Sq F value Pr(>F)
# race 2 5048361 2524181 4.949 0.008052 **
# Residuals 186 94866938 510037
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
anova(anovaRace)$'F value'[1]
## [1] 4.949012
# [1] 4.949012
round(anova(anovaRace)$'F value'[1],2)
## [1] 4.95
# [1] 4.95
# The Value of the F ratio F-ratio tells you how much bigger the variation
# between group means is compared to random variation within groups
# A large F (>>>1) means group differences are unlikely due to chance alone.
print('The F ratio is 4.95, which is unlikely to be due to chance alone')
## [1] "The F ratio is 4.95, which is unlikely to be due to chance alone"
# Step 4: Describe the sampling distribution of the test statistic assuming
# the null hypothesis is true
qf(0.95, df1= 2,df2 = 186)
## [1] 3.044504
# [1] 3.044504
round(qf(0.95, df1= 2,df2 = 186),2)
## [1] 3.04
# [1] 3.04
print('If the null hypothesis is true, with an alpha significance level of 0.05, the F critical value based on 2/186 degrees of freedom is 3.04')
## [1] "If the null hypothesis is true, with an alpha significance level of 0.05, the F critical value based on 2/186 degrees of freedom is 3.04"
# Step 5: Draw your conclusion
anova(anovaRace)$'Pr(>F)'[1]
## [1] 0.008052094
# [1] 0.008052094
## Interpretation ##
# A one-way ANOVA revealed a significant effect of race on birth weight,
# F(2,186) = 4.95, p = 0.008. Therefore, we reject the null hypothesis that
# all group means are equal and conclude that birth weight differs
# significantly across racial groups.
########################## QUESTION 1-B ########################################
# The F-ratio represents the ratio of the variation between group means to the
# variation within groups, showing how much greater the differences among groups
# are compared to random variation within them. In this study,
# the calculated F-ratio of 4.95 indicates that the variation in mean
# birth weights between racial groups is nearly five times larger than what
# would be expected by chance alone. Since this value exceeds the
# critical F of 3.04 and the associated p-value (0.008) is less than the
# significance level (0.05), we conclude that there is a statistically
# significant difference in mean birth weight among the racial groups.
# This suggests that race has a significant effect on birth weight.
########################## QUESTION 1-C ########################################
# Use Scheffé's procedure in STATA/R to test for all differences between
# each pair of groups.
# Summarize and interpret the findings from this test.
ScheffeTest(anovaRace)
##
## Posthoc multiple comparisons of means: Scheffe Test
## 95% family-wise confidence level
##
## $race
## diff lwr.ci upr.ci pval
## Other-Black 84.32262 -322.862264 491.5075 0.8777
## White-Black 383.31811 -6.293484 772.9297 0.0550 .
## White-Other 298.99549 18.455338 579.5356 0.0335 *
##
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Posthoc multiple comparisons of means: Scheffe Test
# 95% family-wise confidence level
#
# $race
# diff lwr.ci upr.ci pval
# Other-Black 84.32262 -322.862264 491.5075 0.8777
# White-Black 383.31811 -6.293484 772.9297 0.0550 .
# White-Other 298.99549 18.455338 579.5356 0.0335 *
#
# ---
# Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Interpretation ##
# Since the one-way ANOVA revealed a significant effect of race on the outcome
# variable, a post-hoc comparison using Scheffé’s procedure was applied,
# to identify group differences while controlling for family-wise error.
# The results indicated that mean scores differed significantly between the
# White and Other groups (p = 0.0335), with the White group showing higher
# values on average.
# The difference between the White and Black groups approached significance
# (p = 0.0550), suggesting a possible trend, whereas no significant difference
# was observed between the Black and Other groups (p = 0.8777).
# These findings suggest that racial group membership may be associated
# with differences in the outcome,
# particularly between White and Other participants.
########################## QUESTION 1-D ########################################
# Use the Bonferroni Approach in STATA/R to test for all differences
# between each pair of groups. Summarize and interpret
# the findings from this test.
# > pairwise.t.test(lbw$bwt, lbw$race, p.adjust.method = 'bonferroni')
#
# Pairwise comparisons using t tests with pooled SD
#
# data: lbw$bwt and lbw$race
#
# Black Other
# Other 1.000 -
# White 0.048 0.028
#
# P value adjustment method: bonferroni
## Interpretation ##
# After applying the Bonferroni correction to control for multiple comparisons,
# significant differences in mean birth weight were found between
# the White and Black groups (p = 0.048) and between the White
# and Other groups (p = 0.028).
# There was no significant difference between the
# Black and Other groups (p = 1.000).
# These results suggest that infants born to White mothers had significantly
# higher mean birth weights compared to both Black and Other groups,
# while the latter two groups did not differ from each other.
########################## QUESTION 1-E ########################################
# How do the Bonferroni results compare to Scheffé's results?
#
# Both the Bonferroni and Scheffé post-hoc tests identified significant
# differences involving the White group. The Bonferroni approach detected
# significant differences between White–Black (p = 0.048)
# and White–Other (p = 0.028) pairs, whereas the Scheffé test found only
# the White–Other comparison to be significant (p = 0.0335) and the
# White–Black difference to be marginal (p = 0.0550).
# This pattern reflects the greater conservativeness of Scheffé’s procedure,
# which applies a stricter adjustment for multiple comparisons.
# Overall, both methods indicate that birth weight differs significantly by race,
# with infants of White mothers having higher mean birth weights
# than those of Other or Black mothers.
########################## PART B ##############################################
################################################################################
# It is often the case that researchers forget to test whether the pre-specified
# analysis plan matches the characteristics of the data that
# was ultimately collected.
# It is important to do these checks to ensure
# that your inferences can be trusted.
########################## QUESTION 2-A ########################################
# List the assumptions of a one-way ANOVA
# In no particular order:
# 1. Data must be independent: The data for each group are collected from
# independent subjects or experimental units, and further, no individual's
# measure should influence another.
# 2. The dependent variable is continuous (interval or ratio)
# 3. Equal group variances: The variance of the dependent variable
# should be roughly equal across all groups.
# 4. Normality: each group has data that is approximately normally distributed
########################## QUESTION 2-B ########################################
# Assess the assumptions of the one-way ANOVA you completed for question 1
# 1. Independence
#1. Independance cannot be tested using R , so it is assumed if data were
# randomly sampled or randomly assigned;
# each infant’s birth weight is independent of others.
# 2. Continuos dependant variable
# Birth weight is a continuous variable, satisfying this assumption
# 3. Equal group variances
# If Levene’s test p > 0.05, variances are equal across groups.
# Residuals can be plotted vs fitted, and if equal variances, then
# esiduals should scatter randomly around 0 without a pattern.
## Fit ANOVA
anovaRace <- aov(bwt ~ race, data = lbw)
## Predict residuals
residuals_anova <- residuals(anovaRace)
fitted_anova <- fitted(anovaRace)
## Plot residuals vs fitted values (to check homogeneity of variance)
plot(fitted_anova, residuals_anova,
xlab = "Fitted values", ylab = "Residuals",
main = "Residuals vs Fitted")
abline(h = 0, col = "red")

## Levene’s test for equality of variances
library(car)
## Warning: package 'car' was built under R version 4.4.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.4.3
##
## Attaching package: 'carData'
## The following objects are masked from 'package:BSDA':
##
## Vocab, Wool
##
## Attaching package: 'car'
## The following object is masked from 'package:DescTools':
##
## Recode
## The following object is masked from 'package:dplyr':
##
## recode
leveneTest(bwt ~ race, data = lbw)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 0.4669 0.6277
## 186
# Levene's Test for Homogeneity of Variance (center = median)
# Df F value Pr(>F)
# group 2 0.4669 0.6277
# 186
# 4. Normality
# If points roughly follow the 45° line, residuals are approximately normal.
## Q-Q plot for normality
qqnorm(residuals_anova)
qqline(residuals_anova, col = "red")

# Conclusions: The assumptions of the one-way ANOVA were assessed as follows.
# Residuals were extracted from the model and plotted against fitted values to
# visually inspect homogeneity of variances; no obvious pattern
# or funnel shape was observed, suggesting variances are roughly equal.
# A Q-Q plot of residuals indicated that residuals were approximately
# normally distributed, with points following the 45° line.
# Levene’s test confirmed equality of variances (p > 0.05).
# Independence of observations was assumed based on random sampling,
# and birth weight is a continuous variable, satisfying the other assumptions.