This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button above a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.
I have created code chunks for you to enter code for each answer. To execute code press the green arrow to run a specific chunk or click knit to run who file. If needed a new code chuck can be added after 3 ` and r in curly braces followed by code you want to execute and another 3 ’, such as:
print('hello world')
## [1] "hello world"
When adding a new code chuck please do not add
echo = FALSE parameter as you will be assessed on both R
code and the output generated.
1a) Using the gapminder dataframe calculate the median life expectancy in each continent. [3 marks]
median_life_expectancy <- gapminder %>%
group_by(continent) %>%
summarize(median_life_expectancy = median(lifeExp, na.rm = TRUE))
print(median_life_expectancy)
## # A tibble: 5 × 2
## continent median_life_expectancy
## <fct> <dbl>
## 1 Africa 47.8
## 2 Americas 67.0
## 3 Asia 61.8
## 4 Europe 72.2
## 5 Oceania 73.7
1b) Using the gapminder dataframe create a boxplot of life expectancy for each continent (colour outliers in a distinctive colour) and label axis appropriately. [4 marks]
ggplot(data=gapminder, mapping=aes(y=lifeExp, x=continent))+geom_boxplot(color="black", fill="lightyellow")+geom_jitter(color="red",shape=16,width=0.3)+labs(title=paste('life expectancy for each continent'),x=paste('Continent'),y=paste('Life Expectancy(years)'))
1c) Create a scatter plot of life expectancy against gross domestic product per capita. [3 marks]
ggplot(data=gapminder, mapping=aes(x=gdpPercap,y=lifeExp))+geom_point()
Q1d) Where and when has the greatest life expectancy? [2 marks]
data(gapminder)
max_life_exp_row <- which.max(gapminder$lifeExp)
max_life_exp_location <- gapminder$country[max_life_exp_row]
max_life_exp_year <- gapminder$year[max_life_exp_row]
cat(max_life_exp_location, max_life_exp_year, gapminder$lifeExp[max_life_exp_row], "years.")
## 67 2007 82.603 years.
Q1e) What is the median life expectancy? Which 4 countries have the closest life expectancy to the median and in what years? [4 marks]
median_life_expectancy <- median(gapminder$lifeExp)
closest_countries <- gapminder[order(abs(gapminder$lifeExp - median_life_expectancy)), ][1:4, ]
closest_years <- closest_countries$year
cat("Median life expectancy:", median_life_expectancy, "\n")
## Median life expectancy: 60.7125
cat("Countries with life expectancies closest to the median:\n")
## Countries with life expectancies closest to the median:
print(closest_countries[, c("country", "lifeExp")])
## # A tibble: 4 × 2
## country lifeExp
## <fct> <dbl>
## 1 Comoros 60.7
## 2 West Bank and Gaza 60.8
## 3 Venezuela 60.8
## 4 Guatemala 60.8
cat(closest_years, "\n")
## 1997 1977 1962 1987
The following data is taken from Table 5.1 of Diekmann, Hesterbeck and Britton’s textbook ‘Mathematical Tools for Understanding Infectious Disease Dynamics’, originally from Becker 1989. I have imputed the hour and minute of symptom onset.
SmallPoxOnset <- data.frame(
day = c( 0, 7, 9, 12, 12, 12, 13, 17, 22, 23, 23, 24, 24, 25, 25, 26, 26,
26, 26, 26, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 29, 29, 29,
29, 29, 30, 30, 30, 30, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 33,
33, 34, 35, 36, 36, 37, 37, 38, 39, 39, 40, 40, 41, 41, 41, 41, 42,
42, 42, 42, 43, 43, 43, 43, 43, 44, 45, 47, 47, 48, 53, 53, 58, 63))
# impute time of day via random samples
set.seed(1525)
SmallPoxOnset$hour <- round(runif(SmallPoxOnset$day, 0, 24), 0)
SmallPoxOnset$min <- round(runif(SmallPoxOnset$day, 0, 60), 0)
Q2) Create histogram of the times of symptom onset, with appropriate axis labels and bin width. Justify the choice of bin width. [4 marks]
ggplot(SmallPoxOnset, aes(x = hour + min/60))+geom_histogram(binwidth = 1,fill = "blue", color = "black")+
labs(title = "Histogram of Smallpox Symptom Onset Times",x = "Time of Day (hours),1 hour bins",y = "Frequency")