library(arrow)
library(sf)
library(dplyr)
library(tidyr)
library(terra)
library(magrittr)
library(furrr)
library(future)
library(tictoc)
library(ggplot2)
library(fs)
library(conflicted)
library(stringr)
library(readxl)
library(units)
library(qs)
library(here)
conflicts_prefer(dplyr::filter(), dplyr::select(), .quiet = T)
here("src") %>% list.files(pattern = "\\.R$", full.names = TRUE) %>% purrr::walk(source)
<- file.path(input_feed_profile_path, "all_ingredients.csv")
ingred_inputs_file <- file.path(input_feed_profile_path, "all_feeds.xlsx") feed_input_file
Formulating experimental feeds
1 Introduction
The purpose of this markdown is to:
2 Import ingredients
There is no check or correction here to ensure that protein+lipid+ash+carb = 1 in the incoming data. This was checked manually prior to import.
<- ingred_inputs_file %>%
ingreds read.csv() %>%
mutate(ingredient = as.factor(ingredient))
<- levels(ingreds$ingredient) ingred_nms
3 Import feeds
<- feed_input_file %>%
feed_inputs read_excel(sheet = "all_feeds") %>%
pivot_longer(names_to = "feed", values_to = "proportion", cols = !contains(c("ingredient", "category"))) %>%
mutate(feed = as.factor(feed),
ingredient = as.factor(ingredient)) %>%
merge(ingreds, by = "ingredient")
<- levels(feed_inputs$feed) feed_types
Check that ingredient proportions sum to 1.
<- feed_inputs %>%
feed_inputs filter(!is.na(proportion) & proportion != 0) %>%
group_by(feed) %>%
mutate(
sum = sum(proportion),
proportion = proportion/sum
%>%
) ungroup()
The following chunk allows as many ingredients and feeds to be added to the incoming data as needed, it will still be formatted into a form that the targets
pipelines can use.
<- purrr::map(feed_types, function(ft) {
feed_params <- feed_inputs %>%
df filter(feed == ft)
list(
Proteins = df %>%
select(ingredient, proportion, contains("protein"), -contains("feed")) %>%
rename(macro = protein, digest = protein_digestibility),
Carbohydrates = df %>%
select(ingredient, proportion, contains("carb"), -contains("feed")) %>%
rename(macro = carb, digest = carb_digestibility),
Lipids = df %>%
select(ingredient, proportion, contains("lipid"), -contains("feed")) %>%
rename(macro = lipid, digest = lipid_digestibility)
)%>%
}) setNames(feed_types)
qsave(feed_params, file.path(output_species_data_path, "feed_params.qs"))
4 Similarity
For later - how similar are the diets?
<- feed_inputs %>%
feed_simil select(ingredient, feed, proportion) %>%
as.data.frame() %>%
pivot_wider(
names_from = feed,
values_from = proportion,
id_cols = ingredient,
values_fn = as.numeric
%>%
) mutate(
diff_PD_MD = abs(marine_dominant - plant_dominant),
diff_PD_NI = abs(novel_inclusive - plant_dominant)
)
1 - sum(feed_simil$diff_PD_MD, na.rm = T)
1 - sum(feed_simil$diff_PD_NI, na.rm = T)