library(tidyverse)
library(lubridate)
Exercise 5
Libraries
Task 1: Write your own functions
Creating the following three functions:
A function which calculates a persons BMI based on their height and weight
A function which converts degrees Celcius to Farenheight
A function which calculates the (Euclidean) distance between two sets of coordinates
BMI Function
BMI = Weight/Height
<- function(weight, height){
bmi <- weight/height^2
bmi_output
return(bmi_output)
}
Function to convert from Celsius to Farnheight
Farneheight = Celsius * (9/5) + 32
<- function(celsius){
converter *9/5 + 32
celsius }
Eucliden Distance Function
Euclidean Distance = sqrt((x2-x1)2 + (y2-y1)2)
<- function(x1, y1, x2, y2){
euclidean_distance sqrt((x2-x1)^2 + (y2-y1)^2)
}
Task 2: Prepare Analysis
Looking for “meet” patterns in wild boar data.
Importing datasets filtering for Sabi and Rosa and in the timespan 01.04.2015 - 15.04.2015.
<- read_delim("Datasets/wildschwein_BE_2056.csv", ",") |>
wildboars filter(
== "Sabi" | TierName == "Rosa",
TierName >= "2015-04-01" & DatetimeUTC < "2015-04-15"
DatetimeUTC )
Task 3: Create Join Key
To compare Rosa and Sabi’s locations, we first need to match the two animals temporally.
At first, we need to round the minutes of DatetimeUTC to a multiple of 15 and store the values in a new column:
<- wildboars |>
wildboars mutate(DateTimeRound = round_date(DatetimeUTC, unit = "15 mins"))
Task 4: Measuring distance at concurrent locations
To measure the distance between concurrent locations, we need to follow the following steps.
Split the
wildschwein_filter
object into onedata.frame
per animalJoin2 these datasets by the new
Datetime
column created in the last task. The joined observations are temporally close.In the joined dataset, calculate Euclidean distances between concurrent observations and store the values in a new column
Use a reasonable threshold on
distance
to determine if the animals are also spatially close enough to constitute a meet (we use 100 meters). Store this Boolean information (TRUE
/FALSE
) in a new column
Splitting the dataset
Creating one Dataset for Sabi and one for Rosa
<- wildboars |>
sabi filter( TierName == "Sabi")
<- wildboars |>
rosa filter( TierName == "Rosa")
Joining by new rounded Datetime
<- inner_join(sabi, rosa, by = "DateTimeRound") joined_wildboars
Calculating Euclidean distances
Using function created in task 1:
<- joined_wildboars |>
joined_wildboars mutate(
EuclideanDistance = euclidean_distance(E.x, N.x, E.y, N.y)
)
Threshold on distance
Creating the threshold values
<- 100 threshold
Creating Static column with boolean values
<- joined_wildboars |>
joined_wildboars mutate(
meet = EuclideanDistance < threshold
)
<-joined_wildboars |>
joined_wildboars pivot_longer(cols = c(TierName.x, TierName.y), values_to = "TierName")
<-joined_wildboars |>
joined_wildboars pivot_longer(cols = c(E.x, E.y), names_to = "old_coord", values_to = "E")
<-joined_wildboars |>
joined_wildboars pivot_longer(cols = c(N.x, N.y), names_to = "old_coord_2", values_to = "N") |>
select(TierName, E, N, meet)
Task 5: Visualize data
<- joined_wildboars |>
meet_points filter(meet == TRUE)
ggplot() +
geom_point(data = rosa, aes(x = E, y = N), color = "red", alpha = 0.3) +
geom_point(data = sabi, aes(x = E, y = N), color = "cyan", alpha = 0.3) +
geom_point(data = meet_points, aes(x = E, y = N, fill = TierName), shape = 21, color = "black", size = 2.5) +
guides(fill = guide_legend(override.aes = list(size = 4))) +
xlim(min(joined_wildboars$E), max(joined_wildboars$E)) +
ylim(min(joined_wildboars$N), max(joined_wildboars$N)) +
theme_minimal()