In this tutorial, you will learn how to import, tidy, wrangle and visualize data yourself!
The FEV dataset
The FEV, which is an acronym for forced expiratory volume
, is a measure of how much air a person can exhale (in liters) during a forced breath. In this dataset, the FEV of 606 children, between the ages of 6 and 17, were measured. The dataset also provides additional information on these children: their age
, their height
, their gender
and, most importantly, whether the child is a smoker or a non-smoker.
The goal of this experiment was to find out whether or not smoking has an effect on the FEV of children.
Note: to analyse this dataset properly, we will need some relatively advanced modeling techniques. At the end of this week, you will have seen all three required steps to analyse such a dataset! For now, we will limit ourselves to exploring the data.
Load libraries
If you do not have these libraries installed, make sure to install them first by using the install.packages()
function with missing the package name inside the parentheses (and using quotation marks, like install.packages("car")
)
library(readr)
library(dplyr)
library(tidyverse)
library(ggplot2)
library(car)
Import the data
Note: fev.txt
is a tab-separated file: make sure to select the correct readr
function!
Have a first look at the data
There are a few things in the formatting of the data that can be improved upon:
Both gender
and smoking
can be transformed to factors.
The height
variable is written in inches. Assuming that this audience is mainly Portuguese/Belgian, inches are hard to interpret. Let’s add a new column, height_cm
, with the values converted to centimeters.
That’s better!
Now, let’s make a first explorative plot, showing only the FEV for both smoking categories.
Which type of plot do you suggest? Generate a good-looking, informative representation of the data.
Did you expect these results?
Maybe there is something else going on in the data. By taking more of the information in the dataset into account, can you provide a more detailed/accurate visualizition of the variables that effect the FEV?
...
## Try to get a visualization that describes the data as good as possible!!
...
LS0tCnRpdGxlOiAiRXhlcmNpc2UgNC40OiBFeHBsb3JpbmcgdGhlIEZFViBkYXRhc2V0IgphdXRob3I6ICJMaWV2ZW4gQ2xlbWVudCBhbmQgSmVyb2VuIEdpbGlzIgpkYXRlOiAic3RhdE9taWNzLCBHaGVudCBVbml2ZXJzaXR5IChodHRwczovL3N0YXRvbWljcy5naXRodWIuaW8pIiAgCm91dHB1dDoKICAgIGh0bWxfZG9jdW1lbnQ6CiAgICAgIGNvZGVfZG93bmxvYWQ6IHRydWUgICAgCiAgICAgIHRoZW1lOiBjb3NtbwogICAgICB0b2M6IHRydWUK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