HOMEWORK 1

Hypothesis testing

Research question: Are there differences in average birth weight of children between the ones whose mothers smoked during the pregnancy and the ones whose mothers did not smoked during pregnancy?
#install.packages("MASS")
library(MASS)

mydata <- force(birthwt)

head(mydata, 10)
##    low age lwt race smoke ptl ht ui ftv  bwt
## 85   0  19 182    2     0   0  0  1   0 2523
## 86   0  33 155    3     0   0  0  0   3 2551
## 87   0  20 105    1     1   0  0  0   1 2557
## 88   0  21 108    1     1   0  0  1   2 2594
## 89   0  18 107    1     1   0  0  1   0 2600
## 91   0  21 124    3     0   0  0  0   0 2622
## 92   0  22 118    1     0   0  0  0   1 2637
## 93   0  17 103    3     0   0  0  0   1 2637
## 94   0  29 123    1     1   0  0  0   1 2663
## 95   0  26 113    1     1   0  0  0   0 2665

Description:

  • low - indicator of birth weight less than 2.5 kg. (1= yes, 0 = no)

  • age - mother’s age in years

  • lwt - mother’s weight in pounds at last menstrual period

  • race - mother’s race (1 = white, 2 = black, 3 = other)

  • smoke - smoking status during pregnancy ( 1= yes, 0= no)

  • ptl - number of previous premature labours

  • ht - history of hypertension ( 1= yes, 0= no)

  • ui -presence of uterine irritability ( 1= yes, 0= no)

  • ftv - number of physician visits during the first trimester

  • bwt - birth weight in grams

unit of observation: 1 child

number of observation: 189

Source:

Hosmer, D.W. and Lemeshow, S. (1989) Applied Logistic Regression. New York: Wiley

References:

Venables, W. N. and Ripley, B. D. (2002) Modern Applied Statistics with S. Fourth edition. Springer.

#since the data was in int we changed the ones we are going to test in numeric and one in factor: 
mydata$bwt<- as.numeric(mydata$bwt)

mydata$SmokingF<- factor(mydata$smoke,
                           levels= c(0,1),
                           labels= c("NoSmoking", "YesSmoking"))

summary(mydata[c("bwt", "SmokingF")])
##       bwt             SmokingF  
##  Min.   : 709   NoSmoking :115  
##  1st Qu.:2414   YesSmoking: 74  
##  Median :2977                   
##  Mean   :2945                   
##  3rd Qu.:3487                   
##  Max.   :4990
#install.packages("psych")
library(psych)
describeBy(mydata$bwt, mydata$SmokingF)
## 
##  Descriptive statistics by group 
## group: NoSmoking
##    vars   n   mean     sd median trimmed    mad  min  max range  skew kurtosis
## X1    1 115 3055.7 752.66   3100 3086.14 816.91 1021 4990  3969 -0.28    -0.32
##       se
## X1 70.19
## ------------------------------------------------------------ 
## group: YesSmoking
##    vars  n    mean     sd median trimmed   mad min  max range  skew kurtosis
## X1    1 74 2771.92 659.63 2775.5 2782.55 630.1 709 4238  3529 -0.28     0.23
##       se
## X1 76.68

For the description of the data we can observe that birth weight of child from the mother that did not smoked during the pregnancy is 3055.70 grams on average, while the birth weight of the child which mother smoked during the pregnancy is 2771.92 on average. There are in total 115 mothers that did not smoked during the pregnancy and 74 that did. Minimal weight of the child was 709 grams and it was born from mother that smoked during the pregnancy and the highest weight measured was 4990 grams from the mother that was not smoking during the pregnancy.

For the further investigation, we need to check the assumptions before we preform the parametric test -> independent t-test with Welch correction: First assumptions is the variable we are comparing is numerical, variances between groups is assumed (var.equal = FALSE) and the normality needs to be examined of the variables between groups-

#install.packages("ggplot2")
library(ggplot2)
## 
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha
#install.packages("ggpubr")
library(ggpubr)
NoSmoking_t <- ggplot(mydata[mydata$SmokingF == "NoSmoking", ], aes(x = bwt)) +
  theme_linedraw()+
  geom_bar(fill = "coral")+
  ylab("Frequency") +
  xlab("Weight of baby") +
  ggtitle("NoSmoking")

YesSmoking_t <- ggplot(mydata[mydata$SmokingF == "YesSmoking", ], aes(x = bwt)) +
  theme_linedraw()+
  geom_bar(fill = "darkblue" ) +
  ylab("Frequency") +
  xlab("Weight of baby") +
  ggtitle("YesSmoking")

ggarrange(NoSmoking_t, YesSmoking_t,
          ncol = 2, nrow = 1)

#install.packages(stats)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:MASS':
## 
##     select
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(rstatix)
## 
## Attaching package: 'rstatix'
## The following object is masked from 'package:MASS':
## 
##     select
## The following object is masked from 'package:stats':
## 
##     filter
library(stats)
mydata %>%
  group_by(SmokingF) %>% 
  shapiro_test(bwt)
## # A tibble: 2 × 4
##   SmokingF   variable statistic     p
##   <fct>      <chr>        <dbl> <dbl>
## 1 NoSmoking  bwt          0.987 0.334
## 2 YesSmoking bwt          0.983 0.419

Based on the sample data we cannot reject null hypothesis for neither of groups (p>0.05). If we assume that there are variances between groups (var.equal = FALSE), we can preform the independent t-test with Welch correction.

t.test(mydata$bwt ~ mydata$SmokingF,
       paired = FALSE,
       var.equal = FALSE,
       alternative = "two.sided")
## 
##  Welch Two Sample t-test
## 
## data:  mydata$bwt by mydata$SmokingF
## t = 2.7299, df = 170.1, p-value = 0.007003
## alternative hypothesis: true difference in means between group NoSmoking and group YesSmoking is not equal to 0
## 95 percent confidence interval:
##   78.57486 488.97860
## sample estimates:
##  mean in group NoSmoking mean in group YesSmoking 
##                 3055.696                 2771.919

Welch correction:

H0: true difference in means between the baby birth weight in pregnancies where mothers smoked and in the ones where they did not smoked is equal to 0;

H1: true difference in means between the baby birth weight in pregnancies where mothers smoked and in the ones where they did not smoked is NOT equal to 0.

effectsize::cohens_d(mydata$bwt ~ mydata$SmokingF)
## Cohen's d |       95% CI
## ------------------------
## 0.40      | [0.10, 0.69]
## 
## - Estimated using pooled SD.
#install.packages("effectsize")
library(effectsize)
## 
## Attaching package: 'effectsize'
## The following objects are masked from 'package:rstatix':
## 
##     cohens_d, eta_squared
## The following object is masked from 'package:psych':
## 
##     phi
interpret_cohens_d(0.40, rules = "sawilowsky2009")
## [1] "small"
## (Rules: sawilowsky2009)

Based on the sample data we can reject H0 at p-value = 0.007. We can say that there are differences on average between the baby birth weight in pregnancies where mothers smoked and in the ones where they did not smoked, the birth weigh of babies which mother smoked during pregnancy is lower on average. Effect size is small.

For the nonparametric/ our alternative test we are going to do Wilcoxon rank sum test. We will check robustness and check results one more time

wilcox.test(mydata$bwt ~ mydata$SmokingF,
            paired = FALSE,
            correct = FALSE,
            exact = FALSE,
            alternative = "less")
## 
##  Wilcoxon rank sum test
## 
## data:  mydata$bwt by mydata$SmokingF
## W = 5249.5, p-value = 0.9966
## alternative hypothesis: true location shift is less than 0

H0: The locations of distributions are the same,

H1: The location of distributions are NOT the same, true location shift is less than 0

effectsize(wilcox.test(mydata$bwt ~ mydata$SmokingF,
                       paired = FALSE,
            correct = FALSE,
            exact = FALSE,
            alternative = "less"))
## r (rank biserial) |        95% CI
## ---------------------------------
## 0.23              | [-1.00, 0.36]
## 
## - One-sided CIs: lower bound fixed at [-1.00].

Based on the sample data, we cannot reject H0 (P = 0.9966) and we cannot claim that location of distributions are not the same. Effect size is medium based on “funder2019” (Funder & Ozer 2019).