Aims
In this exercise, you will revisit the basics of statistical hypothesis testing.
You will acquire the skills
- to assess the assumptions of a one-sample and a paired t-test in a data exploration.
- to conduct a one-sample t-test in R and to interpret the results.
- to conduct a paired t-test in R and to interpret the results.
The diabetes dataset
The diabetes
dataset holds information on a small experiment with 8 patients that are subjected to a glucose tolerance test.
Patients had to fast for eight hours before the test. When the patients entered the hospital their baseline glucose level was measured (mmol/l).
Patients then had to drink 250 ml of a syrupy glucose solution containing 100 grams of sugar. Two hours later, their blood glucose level was measured again.
The data consist of three variables:
before
: glucose concentration upon 8 hours of fasting (mmol/l)
after
: glucose concentration 2 hours after drinking glucose solution (mmol/l).
patient
: identifier for the patient
Research questions
The goal is to answer two research questions;
Is the average glucose level after the sugar intake different from the average glucose level before sugar intake?
Is the average glucose level of the patients two hours after the sugar intake higher than 7.8 mmol/L?
Import the data
First, load the required R libraries:
Data exploration
We will start with a data exploration. Have a first look at the raw data. How is the data structured? Is this data tidy?
Question 1
Is the glucose level after 8 hours of fasting on average different from the glucose level two hours after intake of 100g of glucose?
As the data are paired, we can expect that the measurements before and after the glucose intake are correlated. We can illustrate this with a scatterplot.
Check the assumptions
State the assumptions that you have to check and include the diagnostic plots to assess the assumption.
Hypothesis test
If all assumptions are met, we may continue with performing the paired two-sample t-test.
paired_t <- t.test(..., ..., paired = ...)
paired_t
Conclusion
Formulate a conclusion based on the output.
Alternative solution: One-sample t-test on the difference
Since the data is paired, we can also simply calculate the differences in glucose level before and after sugar intake for each patient. We can then perform a one-sample t-test on these differences, testing whether they are significantly different from zero. This is equivalent to the paired t-test we performed above.
We can verify this with the analysis below
t.test(... ~ 1, data = ..., mu = ...)
Question 2
Is the average glucose level two hours after sugar intake higher than the threshold of 7.8 mmol/l for pre-diabetes?
We can test this hypothesis using a one-sample t-test. Indeed we are interested to compare the average glucose level to a known threshold for pre-diabetes.
Assess the assumptions
Before we can perform a one sample t-test, we must check that the required assumptions are met!
- The observations are independent
- The glucose levels two hours after the treatment are normally distributed
## First filter the data on the desired group
diabetes_after <- ... %>%
filter(... == "...")
... %>%
...() +
geom_qq() +
geom_qq_line()
What do you observe?
Hypothesis test
Here, we will test if mean glucose level 2 hours after sugar intake is significantly higher than the threshold for pre-diabetes of 7.8 mmol/l. More specifically, we will test the null hypothesis;
\(H_0:\) …
versus the alternative hypothesis;
\(H_1:\) …
t_test_after <- t.test(... ~ 1,
data = ...,
mu = ...,
alternative = "...",
conf.level = ...
)
t_test_after
Conclusion
When writing a conclusion on your research hypothesis, it is very important to be precise, concise, and complete.
…
LS0tCnRpdGxlOiAiRXhlcmNpc2UgNS4xOiBIeXBvdGhlc2lzIHRlc3Rpbmcgb24gdGhlIGRpYWJldGVzIGV4YW1wbGUiCmF1dGhvcjogIkxpZXZlbiBDbGVtZW50LCBKZXJvZW4gR2lsaXMgYW5kIE1pbGFuIE1hbGZhaXQiCmRhdGU6ICJzdGF0T21pY3MsIEdoZW50IFVuaXZlcnNpdHkgKGh0dHBzOi8vc3RhdG9taWNzLmdpdGh1Yi5pbykiCi0tLQoKIyBBaW1zCgpJbiB0aGlzIGV4ZXJjaXNlLCB5b3Ugd2lsbCByZXZpc2l0IHRoZSBiYXNpY3Mgb2Ygc3RhdGlzdGljYWwgIGh5cG90aGVzaXMgdGVzdGluZy4KCllvdSB3aWxsIGFjcXVpcmUgdGhlIHNraWxscwoKMS4gdG8gYXNzZXNzIHRoZSBhc3N1bXB0aW9ucyBvZiBhIG9uZS1zYW1wbGUgYW5kIGEgcGFpcmVkIHQtdGVzdCBpbiBhIGRhdGEgZXhwbG9yYXRpb24uCjIuIHRvIGNvbmR1Y3QgYSBvbmUtc2FtcGxlIHQtdGVzdCBpbiBSIGFuZCB0byBpbnRlcnByZXQgdGhlIHJlc3VsdHMuCjMuIHRvIGNvbmR1Y3QgYSBwYWlyZWQgdC10ZXN0IGluIFIgYW5kIHRvIGludGVycHJldCB0aGUgcmVzdWx0cy4KCiMgVGhlIGRpYWJldGVzIGRhdGFzZXQKClRoZSBgZGlhYmV0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