Author: Yaren Yılmaz
#Importing data by reading the csv file with the headers which is separated by comma and has no numbers with decimals
mydata <- read.table("./babies.csv",
header = TRUE,
sep = ",")
#Displaying the first 6 rows of the data
head(mydata)
## case bwt gestation parity age height weight smoke
## 1 1 120 284 0 27 62 100 0
## 2 2 113 282 0 33 64 135 0
## 3 3 128 279 0 28 64 115 1
## 4 4 123 NA 0 36 69 190 0
## 5 5 108 282 0 23 67 125 1
## 6 6 136 286 0 25 62 93 0
The data set is obtained from https://www.kaggle.com/. Here is the direct link to the web page from which the data is obtained: https://www.kaggle.com/datasets/debjeetdas/babies-birth-weight.
One pregnancy case between 1960 and 1967 among women in the Kaiser Foundation Health Plan in the San Francisco East Bay area.
#Changing the variable parity into a factor
mydata$parityF <- factor(mydata$parity,
levels = c(0,1),
labels = c("First","Not First"))
#Changing the variable smoke into a factor
mydata$smokeF <- factor(mydata$smoke,
levels = c(0,1),
labels = c("Non-Smoker","Smoker"))
head(mydata)
## case bwt gestation parity age height weight smoke parityF smokeF
## 1 1 120 284 0 27 62 100 0 First Non-Smoker
## 2 2 113 282 0 33 64 135 0 First Non-Smoker
## 3 3 128 279 0 28 64 115 1 First Smoker
## 4 4 123 NA 0 36 69 190 0 First Non-Smoker
## 5 5 108 282 0 23 67 125 1 First Smoker
## 6 6 136 286 0 25 62 93 0 First Non-Smoker
#Showing descriptive statistics excluding variables case, parity, and smoke (first, fourth, and eighth columns of the data set)
summary(mydata[ , c(-1,-4,-8)])
## bwt gestation age height
## Min. : 55.0 Min. :148.0 Min. :15.00 Min. :53.00
## 1st Qu.:108.8 1st Qu.:272.0 1st Qu.:23.00 1st Qu.:62.00
## Median :120.0 Median :280.0 Median :26.00 Median :64.00
## Mean :119.6 Mean :279.3 Mean :27.26 Mean :64.05
## 3rd Qu.:131.0 3rd Qu.:288.0 3rd Qu.:31.00 3rd Qu.:66.00
## Max. :176.0 Max. :353.0 Max. :45.00 Max. :72.00
## NA's :13 NA's :2 NA's :22
## weight parityF smokeF
## Min. : 87.0 First :921 Non-Smoker:742
## 1st Qu.:114.8 Not First:315 Smoker :484
## Median :125.0 NA's : 10
## Mean :128.6
## 3rd Qu.:139.0
## Max. :250.0
## NA's :36
There are 13 pregnancy cases that do not have gestation length information, 2 pregnancy cases without age information of mothers, 22 cases without height information of mothers, 36 cases without weight information of mothers, and 10 cases where mothers did not indicate their smoking behavior.
Data Cleaning
#Creating a new data frame with removing units with missing data
library(tidyr)
mydata2 <- drop_na(mydata)
#Showing descriptive statistics for the new data frame excluding variables case, parity, and smoke (first, fourth, and eighth columns of the data set)
summary(mydata2[ , c(-1,-4,-8)])
## bwt gestation age height
## Min. : 55.0 Min. :148.0 Min. :15.00 Min. :53.00
## 1st Qu.:108.0 1st Qu.:272.0 1st Qu.:23.00 1st Qu.:62.00
## Median :120.0 Median :280.0 Median :26.00 Median :64.00
## Mean :119.5 Mean :279.1 Mean :27.23 Mean :64.05
## 3rd Qu.:131.0 3rd Qu.:288.0 3rd Qu.:31.00 3rd Qu.:66.00
## Max. :176.0 Max. :353.0 Max. :45.00 Max. :72.00
## weight parityF smokeF
## Min. : 87.0 First :866 Non-Smoker:715
## 1st Qu.:114.2 Not First:308 Smoker :459
## Median :125.0
## Mean :128.5
## 3rd Qu.:139.0
## Max. :250.0
Some explanations regarding descriptive statistics above:
Mean value of each variable:
Descriptive statistics by group:
#Descriptive statistics by group
library(psych)
describeBy(mydata2$bwt, mydata2$smokeF)
##
## Descriptive statistics by group
## group: Non-Smoker
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 715 123.09 17.42 123 123.22 16.31 55 176 121 -0.16 1.01 0.65
## ------------------------------------------------------------
## group: Smoker
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 459 113.82 18.3 115 113.78 19.27 58 163 105 -0.02 -0.07 0.85
Average birth weight of a baby whose mother is non-smoker is 123.09 ounces while the average birth weight of a smoker mother’s baby is 113.82 ounces. So, a non-smoker mother’s baby weighted 9.27 ounces more on average, in comparison to a smoker mother’s baby.
Graphing the distribution of pregnancy age with histogram
hist(mydata2$age, #Variable
ylab = "Number of Cases", #Naming of y-axis
xlab = "Pregnancy Age", #Naming of x-axis
main = "Distribution of Age in Pregnancy Cases", #Heading
breaks = seq(15, 45, 5), #Breaks for histogram
col = "pink" #color of bars
)
The age distribution of pregnant women is uni modal and seems to be asymmetrical with a slight skew to the right. Skewness can be calculated as below:
#Calculating skewness of the pregnancy age
skew(mydata2$age)
## [1] 0.5964838
Since it is a positive number, it can be claimed that the pregnancy age distribution is right-skewed, as it was also predicted by looking at the histogram above.
Graphing the distribution of pregnancy age with histogram, separated by smoking behavior of mothers
library(ggplot2)
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
ggplot(mydata2, aes(x = age)) + #Name of data frame and defining axis
geom_histogram(binwidth = 5, colour="black", fill= "plum1") + #Type of graph
facet_wrap(~smokeF, ncol = 1) + #Adding factor
ylab("Number of Cases") + #Naming y-axis
xlab("Pregnancy Age") + #Naming x-axis
theme_dark() #Dark theme
The distribution of pregnancy age is almost the same for smoker and non-smoker mothers. Both are uni modal and seem to be asymmetrical slightly to the right.
Examining correlation between variables using scatterplot
#Drawing scatterplot
library(car)
## Zorunlu paket yükleniyor: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
scatterplot(y = mydata$bwt, #Defining x-axis
x = mydata$weight, #Defining y-axis
ylab = "Birth weight of baby (in ounces)", #Naming y-axis
xlab = "Weight of mother (in pounds)", #Naming x-axis
smooth = FALSE)
There seems to be a positive correlation between the birth weight of a baby and its mother’s weight.