Aim of this exercise
An exploratory data analysis is a crucial step in a data analysis to get insight in the nature and distribution of the data, and to assess the assumptions of the downstream data analysis.
In this exercise you will acquired the skills to conduct a data exploration for a two group comparison in R and to interpret the results.
Background
Researchers wanted to study the immune response on pertussis. They have set up an experiments with 40 rats. 16 rats were infected with pertussis and 24 rats received a control treatment. Researchers measured the white blood cell concentration (WBC) in each rat (count per mm\(^3\).
De data consists of two variables:
Load the libraries
Import the dataset
Data path:
https://raw.githubusercontent.com/statOmics/PSLSData/main/wbcon.csv
wbcon <- read_csv("https://raw.githubusercontent.com/statOmics/PSLSData/main/wbcon.csv")
## Rows: 40 Columns: 2
## ── Column specification ──────────────────────────────────────────────
## Delimiter: ","
## chr (1): trt
## dbl (1): WBC
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 40
## Columns: 2
## $ WBC <dbl> 10252, 10467, 10601, 10638, 10901, 11071, 11092, 11371, …
## $ trt <chr> "control", "control", "control", "control", "control", "…
Aim of the study
The overarching goal of this study was to assess if the white blood cell count changes upon pertussis infection. To this end, researchers randomized 40 rats to two treatments: A control treatment and a treatment in which the rat was infected with pertussis.
We will explore the data to get insight on the impact of the pertussis infection on the white blood cell count.
A secondary goal of the data exploration is to assess assumptions that will be required to use a formal statistical test to assess if the white blood cell count is on average different between infected and control rats (see later exercises).
For this test to be valid, we have to assess following assumptions:
The data in each treatment group are normally distributed.
The data from the two treatment groups has the same variance.
Data visualization
A crucial first step in a data analysis is to visualize and to explore the raw data.
Histogram
First, make a histogram of the data. Fill in the missing parts in the chunk of code below:
wbcon %>%
ggplot() +
geom_histogram(aes(x = WBC, fill = trt), color = "black") +
facet_grid(rows = vars(trt)) +
theme_bw() +
xlab("WBC (count/mm3)")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Based on this plot, it seems that the white blood cell counts are higher for rats that were infected with pertussis than for rats that received the control treatment.
Boxplots
However, given the relative small sample size a better option to visualize these data are boxplots
. Histograms get useful in larger datasets, 30 observations per group are a bare minimum.
wbcon %>% ggplot(aes(x = trt, y = WBC, fill = trt)) +
geom_boxplot(outlier.shape = NA) +
geom_point(position = "jitter") +
ylab("WBC (count/mm3)") +
xlab("treatment") +
stat_summary(
fun = mean, geom = "point",
shape = 5, size = 3, color = "black"
)
What do you observe?
Both the mean and variance of the data seems to differ between control rats and rats infected with pertussis.
QQ-plots
To assess the assumption that the data are normally distributed in each treatment group, we will use QQ plots.
wbcon %>%
ggplot(aes(sample = WBC)) +
geom_qq() +
geom_qq_line() +
facet_grid(cols = vars(trt))
What do you observe?
The white blood cell counts appear to be normally distributed in both treatment groups.
Descriptive statistics
Here, we will generate some informative descriptive statistics for the dataset.
We first summarize the data and calculate the mean, standard deviation, number of observations and standard error and store the result in an object wbcSum via ’wbcSum<-`
- We pipe the
wbcon
dataframe to the group_by function to group the data by treatment groep group_by(trt)
- We pipe the result to the
summarize()
function to summarize the “WBC” variable and calculate the mean, standard deviation and the number of observations
- We pipe the result to the
mutate
function to make a new variable in the data frame that is named se
for which we calculate the standard error (\(\sigma / n\))
wbcSum <- wbcon %>%
group_by(trt) %>%
summarize(
mean = mean(WBC, na.rm = TRUE),
sd = sd(WBC, na.rm = TRUE),
n = n()
) %>%
mutate(se = sd / sqrt(n))
wbcSum
This concludes the data exploration. In the next exercise sessions, we will learn how to formally test if the observed difference in WBC between rats that were infected with pertussis and those receiving the control treatment is statistically significant.
LS0tCnRpdGxlOiAiRXhlcmNpc2UgNC4yOiBFeHBsb3JpbmcgdGhlIHBlcnR1c3NpcyBkYXRhc2V0IgphdXRob3I6ICJMaWV2ZW4gQ2xlbWVudCwgYW5kIE1pbGFuIE1hbGZhaXQiCmRhdGU6ICJzdGF0T21pY3MsIEdoZW50IFVuaXZlcnNpdHkgKGh0dHBzOi8vc3RhdG9taWNzLmdpdGh1Yi5pbykiCi0tLQoKIyBBaW0gb2YgdGhpcyBleGVyY2lzZQoKQW4gZXhwbG9yYXRvcnkg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