...
...
...
...
...
...
...
...
...
...
...
...
...
...
Table of Contents |
---|
Reading the data
We have simulated some data from the ALSPAC study (see: Avraam, Wilson and Burton. 2018 for data synthesis details) located in your work folder as alspac-simulated.csv
. The data dictionary for this simulated dataset is in the panel below.
...
- start a new script and save it as a .R file in an appropriate location
- comment in some header information: what is the script for? who is it written by? what data set is being used? etc
- read the dataset into R and assign it the variable
sim.alspac
using theread.csv
function - look up the
colnames
function in the help file and apply it tosim.alspac
to list all the column headings in the data. - look up the
dim
function in the help file and apply it to tosim.alspac
to get the dimensions of the dataset. Number of columns is the number of variables, number of rows is the number of participants.
Selecting and subsetting
Selecting variables can be done a number of ways including selection by column number or column name. It is best practice to use the column name as the column number may vary between datasets.
...
Code Block | ||
---|---|---|
| ||
subset.4<-subset(dataframe, x < 5) #subset of the whole dataframe where x < 5 subset.4<-subset(dataframe, x == 5) #subset of the whole dataframe where x = 5 |
- create a subset of
sim.alspac
for males calledsubset.male
and for females calledsubset.female
- How many participants are female and how many are male? HINT: Use
dim
to check the dimensions ofsubset.male
andsubset.female
.
Exploring the data
- Get object summary statistics by using the
summary
function onsubset.male
andsubset.female
- Use the
boxplot
function to plot BMI at age 7 against gender. HINT: You will only need to use the argumentsformula=
anddata=
- Output your boxplot as a .png file using the
png
function. - Use the
hist
function to plot histograms of BMI age 7 for females and males. HINT: You can layer graphs over one another by using the argumentadd=T
in the second histogram. Line colour of the histogram can be set using the argument e.g.border="red"
- Make the plot more readable by using the
legend
to add an appropriate key. - Output your histogram as a .png file using the
png
function. - Use the
plot
function to create a scatter plot of height and weight age 7 for males. - Use
lm
function to generate a linear model calledlm1
for the two variables. HINT: R uses formula notation in formula argument e.g.formula=y~x
- Use the
summary
function on lm1 to get the coefficients. - You can add your regression line to the scatterplot by running the
abline
function on lm1 after yourplot
function
Modelling
- Apply a generalised linear model (glm) using the
glm
function to investigate the relationships between the variables
...
Info | ||
---|---|---|
| ||
Your R script should be similar to the example R answer script. Try uploading your own dataset and repeat the practical. |
...