-
Notifications
You must be signed in to change notification settings - Fork 1
/
exercise_6_solution.Rmd
162 lines (112 loc) · 5.55 KB
/
exercise_6_solution.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
---
title: 'Exercises'
output:
html_document:
toc: false
code_folding: hide
---
```{r setup, echo=FALSE, purl = FALSE}
knitr::opts_chunk$set(echo=TRUE, message = FALSE, warning = FALSE, eval = FALSE, cache = FALSE)
SOLUTIONS <- TRUE
```
\
## Exercise 6: Basic programming in R
\
Read [Chapter 7](https://intro2r.com/prog_r.html) to help you complete the questions in this exercise.
\
1\. Create a function to calculate the area of a circle. Test the function by finding the area of a circle with a diameter of 3.4 cm. Can you use it on a vector of data?
```{r Q1, echo=SOLUTIONS}
# area of a circle
# the equation to calculate the area of a circle is pi * radius^2
circle.area <- function(d){
pi * (d/2)^2
}
# to use your new function
circle.area(10)
# [1] 78.53982
# to test on a vector of diameters
# first create a vector with diameters ranging from 0 to 50 in steps of 10
cir.diam <- seq(from = 0, to = 50, by = 10)
# test your function
circle.area(cir.diam)
# [1] 0.00000 78.53982 314.15927 706.85835 1256.63706 1963.49541
```
\
2\. Write a function to convert farenheit to centegrade (oC = (oF - 32) x 5/9). Get your function to print out your result in the following format: "Farenheit : *value of oF* is equivalent to *value oC* centigrade."
```{r Q2, echo=SOLUTIONS, tidy = TRUE}
far.cent <- function(a){
val <- (a-32)*5/9
print(paste("Fahrenheit: ", round(a, digits = 3), "oF",sep = " "), quote = FALSE)# round 3dp
print(paste("Centigrade: ", round(val, digits = 3), "oC", sep = " "), quote = FALSE)# round 3dp
}
# alternative Fahrenheit to centigrade using cat function
far.cent2 <- function(a){
val <- (a - 32) * 5/9 #calculation
cat("Fahrenheit: ", round(a, digits = 3), "oF", "\n") # use cat function
cat("Centigrade: ", round(val, digits = 3), "oC", "\n")
}
```
\
3\. Create a vector of normally distributed data, of length 100, mean 35 and standard deviation of 15. Write a function to calculate the mean, median, and range of the vector, print these values out with appropriate labels. Also get the function to plot a histogram (as a proportion) of the values and add a density curve.
```{r Q3, echo=SOLUTIONS}
# Create a vector of normally distributed data
# length 100, mean 35 and standard deviation of 29
vals <- rnorm(100, 35, 15) # create some norm dist data mean 35, sd = 15
summary.fun <- function(dat){
mymean <- round(mean(dat), digits = 4) # calc mean
mymedian <- round(median(dat), digits = 4) # calc median
mymin <- round(min(dat), digits = 4) # calc min
mymax <- round(max(dat), digits = 4) # calc max
print(paste("mean:", mymean, sep = " "), quote = FALSE) # print mean
print(paste("median:", mymedian, sep = " "), quote = FALSE) # print median
print(paste("range:", "from:", mymin, "to", mymax, sep = " "), quote = FALSE)
dens <- density(dat) # estimate density curve
hist(dat, main = "",type = "l",freq = FALSE) # plot histogram
lines(dens, lty = 1, col = "red") # plot density curve
}
# use the function
summary.fun(vals)
```
\
4\. Write a function to calculate the median value of a vector of numbers (yes I know there's a ```median()``` function already but this is fun!). Be careful with vectors of an even sample size, as you will have to take the average of the two central numbers (hint: use modulo %%2 to determine whether the vector is an odd or an even size). Test your function on vectors with both odd and even sample sizes.
```{r Q4, echo=SOLUTIONS}
# calculate a median
ourmedian <- function(x){
n <- length(x)
if (n %% 2 == 1) # odd numbers
sort(x)[(n + 1)/2] # find the middle number by adding 1 to length and div 2
else { # even numbers
middletwo <- sort(x)[(n/2) + 0:1] #find the two middle numbers
mean(middletwo)
}
}
# use the function
mydat <- sample(1:50, size = 10, replace = TRUE )
# our function
ourmedian(mydat)
# R median function
median(mydat)
```
\
5\. You are a population ecologist for the day and wish to investigate the properties of the [Ricker model](https://en.wikipedia.org/wiki/Ricker_model). The Ricker model is defined as:
\
$$ N_{t+1} = N_{t} exp\left[r\left(1- \frac{N_{t}}{K} \right) \right] $$
\
5\. (cont) Where *N~t~* is the population size at time *t*, *r* is the population growth rate and *K* is the carrying capacity. Write a function to simulate this model so you can conveniently determine the effect of changing *r* and the initial population size N0. *K* is often set to 100 by default, but you want the option of being able to change this with your function. So, you will need a function with the following arguments; nzero which sets the initial population size, *r* which will determine the population growth rate, time which sets how long the simulation will run for and *K* which we will initially set to 100 by default.
```{r Q5, echo=SOLUTIONS, tidy = TRUE}
# function to simulate Ricker model
Ricker.model <- function(nzero, r, time, K=1){ # sets initial parameters
N <- numeric(time + 1) # creates a real vector of length time+1 to store values of Nt+1
N[1] <- nzero # sets initial population size in first element of N
for (i in 1:time) { # loops over time
N[i+1] <- N[i]*exp(r*(1 - N[i]/K))
}
Time <- 0:time # creates vector for x axis
plot(Time, N, type = "o", xlim = c(0, time), xlab = "Time", ylab = "Population size (N)", main = paste("r =", r, sep=" ")) # plots output
}
# To run
# play around with the different parameters, especially r
Ricker.model(nzero=0.1,r=1,time=100)
```
\
End of Exercise 6