# BMI (Equation 21.1)
<- function(weight_kg, height_m) {
calc_bmi <- weight_kg / (height_m^2)
bmi return(bmi)
}
# Fahrenheit (Equation 21.2)
<- function(celsius) {
cels_to_fahr <- (celsius * 9/5) + 32
fahrenheit return(fahrenheit)
}
# Function to calculate Euclidean distance between two coordinate points (Equation 21.3)
<- function(x1, y1, x2, y2) {
euc_dist <- sqrt((x2 - x1)^2 + (y2 - y1)^2)
distance return(distance)
}
Ex5
Task 1: Write your own functions
A function which calculates a persons BMI based on their height and weight (Equation 21.1)
A function which converts degrees Celcius to Farenheight (Equation 21.2)
A function which calculates the (Euclidean) distance between two sets of coordinates (see Equation 21.3)
Task 2: Prepare Analysis
Use the dataset wildschwein_BE_2056.csv (on moodle). Import the csv as a data.frame and filter it with the following criteria:
- individuals Rosa and Sabi for the timespan 01.04.2015 - 15.04.2015
library("dplyr")
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library("tidyr")
library("readr")
<- read_delim("../data/wildschwein_BE_2056.csv", delim=",") wildschwein_BE
Rows: 51246 Columns: 6
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): TierID, TierName
dbl (3): CollarID, E, N
dttm (1): DatetimeUTC
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
<- wildschwein_BE |>
wildschw_filter filter(TierName %in% c("Sabi", "Rosa") &
>= as.POSIXct("2015-04-01 00:00:00", tz="UTC") &
DatetimeUTC <= as.POSIXct("2015-04-15 23:59:59", tz="UTC")) DatetimeUTC
Task 3: Create Join Key
To compare Rosa and Sabi’s locations, we first need to match the two animals temporally. For that we can use a join, but need identical time stamps to serve as a join key. We therefore need to slightly adjust our time stamps to a common, concurrent interval. Thererfore, round the minutes of DatetimeUTC to a multiple of 15 (00, 15, 30,45) and store the values in a new column1. You can use the lubridate function round_date() for this. See the examples here to see how this goes.
library("lubridate")
Attaching package: 'lubridate'
The following objects are masked from 'package:base':
date, intersect, setdiff, union
<- wildschw_filter %>%
wildschw_rounded mutate(RoundedDatetime = round_date(DatetimeUTC, "15 minutes"))
Task 4: Measuring distance at concurrent locations
Split the wildschwein_filter object into one data.frame per animal
Join 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
# Split the data
<- wildschw_rounded %>% filter(TierName == "Rosa")
wildschw_rosa <- wildschw_rounded %>% filter(TierName == "Sabi")
wildschw_sabi
# Join the two datasets by RoundedDatetime
<- inner_join(wildschw_rosa, wildschw_sabi, by = "RoundedDatetime", suffix = c("_Rosa", "_Sabi"))
wildschw_join
# Calculate Euclidean distances
<- wildschw_join %>%
euc_dist mutate(
distance = sqrt((E_Rosa - E_Sabi)^2 + (N_Rosa - N_Sabi)^2),
meet = distance <= 100 # TRUE if the distance is less than or equal to 100 meters
)
Task 5: Visualize data
library(ggplot2)
# Filter the joined dataset for only the meets (distance <= 100 meters)
<- euc_dist %>% filter(meet == TRUE)
meets_data
# Create the plot
ggplot() +
# Plot Rosa
geom_point(data = wildschw_rosa, aes(x = E, y = N), color = "blue", alpha = 0.5) +
# Plot Sabi
geom_point(data = wildschw_sabi, aes(x = E, y = N), color = "red", alpha = 0.5) +
# Highlight the meets
geom_point(data = meets_data, aes(x = E_Rosa, y = N_Rosa), color = "black", shape = 1, size = 2) +
geom_point(data = meets_data, aes(x = E_Sabi, y = N_Sabi), color = "black", shape = 1, size = 2) +
# Set the x and y axis limits
xlim(min(c(wildschw_rosa$E, wildschw_sabi$E)) - 100, max(c(wildschw_rosa$E, wildschw_sabi$E)) + 100) +
ylim(min(c(wildschw_rosa$N, wildschw_sabi$N)) - 100, max(c(wildschw_rosa$N, wildschw_sabi$N)) + 100) +
# Add labels and title
labs(x = "East (E)", y = "North (N)", title = "Meets Between Rosa and Sabi") +
theme_minimal()