########################
# R Course WS 2019-20
########################
# Solutions to Exercise sheet 7 - Algorithmics in R
# Last updated by NoĆ©mie 17/02/2020
### Exercise 1
## Read the dataset
heartbeats <- read.table("data/heartbeats.txt")
## Overview of the structure of the dataset
head(heartbeats)
## Means of weight increase
mean(heartbeats$wghtincr) # all new borns
## Gives the mean weight increase for each treatment
tapply(heartbeats$wghtincr, heartbeats$treatment, mean)
# or - indivdually:
mean(heartbeats[heartbeats$treatment == 0,]$wghtincr) # controls ie. treatment = 0
mean(heartbeats[heartbeats$treatment == 1,]$wghtincr) # heartbeat group ie. treatment = 1
# By weight class
tapply(heartbeats[heartbeats$treatment == 0,]$wghtincr, as.factor(heartbeats[heartbeats$treatment == 0,]$wghtcls), mean)
tapply(heartbeats[heartbeats$treatment == 1,]$wghtincr, as.factor(heartbeats[heartbeats$treatment == 1,]$wghtcls), mean)
# Standard deviation
sd(heartbeats$wghtincr) # all new borns
# The mean is higher in the heartbeat group as a whole and for each weight
# class. To know if it is significant we would need a T test.
### Exercise 2
A <- 5
B <- 6
C <- A
A <- B
B <- C
A; B # values exchanged!
# Remark:
# in R you do not need to specify the varaibles and their types beforehand
str(A) # we see it is a numerical variable
### Exercise 3
birdnests <- read.table("data/birdnests.txt", header = TRUE)
# Here gender is automoatically considered as a factor because it is a character
# variable. This is fine for us as it is indeed a factor (M or F)
# Define column nest as all TRUE in the first place
birdnests$nest <- TRUE
# Now nodify it for each case where it should be FALSE
# Younger than 1 year (both sexes)
birdnests[birdnests$age < 1,]$nest <- FALSE
# Males older than 6 years
birdnests[birdnests$gender == "M" & birdnests$age >= 6,]$nest <- FALSE
# Could also be achieved with a loop:
for (i in 1:length(birdnests$age)){
if ((birdnests$age[i] < 1) | (birdnests$gender[i] == "M" & birdnests$age[i] >= 6))
{
birdnests$nest[i] <- FALSE
}
else {
birdnests$nest[i] <- TRUE
}
}
# Or with ifelse (does not need to define birdnests$nest in the first place)
birdnests$nest <- ifelse(birdnests$age <1 |
(birdnests$gender == "M" & birdnests$age >= 6),
FALSE, TRUE)
# Subset of the data with only birds making a nest
yesnest <- birdnests[birdnests$nest == TRUE,]
# Assign number of eggs
yesnest$eggs <- NA
for (i in 1: length(yesnest$age)){
# Female nests
if (yesnest$gender[i] == "F"){
yesnest$eggs[i] <- sample(1:5, 1)
}
# Male nests
else {
# Will a female come?
if.female <- rbinom(n=1, size=1, prob=1/2)
if (if.female==1){ # Female came
yesnest$eggs[i] <- sample(3:5, 1)
}
else { # No female
yesnest$eggs[i] <- 0
}
}
}
# The rbinom command here is used to determine whether the male nest will have a
# female or not. The value can be 0 or 1.
# Total number of eggs
tot.eggs <- sum(yesnest$eggs)
# Repeat 100 times
egg.nb <- rep(NA,100)
# We erase the vector yesnest$eggs at each iteration and record only the sum.
for (j in 1:100){
# Assign number of eggs
yesnest$eggs <- NA
for (i in 1: length(yesnest$age)){
# Female nests
if (yesnest$gender[i] == "F"){
yesnest$eggs[i] <- sample(1:5, 1)
}
# Male nests
else {
# Will a female come?
if.female <- rbinom(n=1, size=1, prob=1/2)
if (if.female==1){ # Female came
yesnest$eggs[i] <- sample(3:5, 1)
}
else { # No female
yesnest$eggs[i] <- 0
}
}
}
egg.nb[j] <- sum(yesnest$eggs)
}
# Compute mean and standard deviation for the egg numbers
mean(egg.nb)
sd(egg.nb)