Exercise 5

Libraries

library(tidyverse)
library(lubridate)

Task 1: Write your own functions

Creating the following three functions:

  1. A function which calculates a persons BMI based on their height and weight

  2. A function which converts degrees Celcius to Farenheight

  3. A function which calculates the (Euclidean) distance between two sets of coordinates

BMI Function

BMI = Weight/Height

bmi <- function(weight, height){
  bmi_output <- weight/height^2
  
  return(bmi_output)
}

Function to convert from Celsius to Farnheight

Farneheight = Celsius * (9/5) + 32

converter <- function(celsius){
  celsius*9/5 + 32
}

Eucliden Distance Function

Euclidean Distance = sqrt((x2-x1)2 + (y2-y1)2)

euclidean_distance <- function(x1, y1, x2, y2){
  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.

wildboars <- read_delim("Datasets/wildschwein_BE_2056.csv", ",") |> 
  filter(
    TierName == "Sabi" | TierName == "Rosa",
    DatetimeUTC >= "2015-04-01" & DatetimeUTC < "2015-04-15"
  )

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.

  1. Split the wildschwein_filter object into one data.frame per animal

  2. Join2 these datasets by the new Datetime column created in the last task. The joined observations are temporally close.

  3. In the joined dataset, calculate Euclidean distances between concurrent observations and store the values in a new column

  4. 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

sabi <- wildboars |> 
  filter( TierName == "Sabi")

rosa <- wildboars |> 
  filter( TierName == "Rosa")

Joining by new rounded Datetime

joined_wildboars <- inner_join(sabi, rosa, by = "DateTimeRound")

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

threshold <- 100

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

meet_points <- joined_wildboars |> 
  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()