From a5d769e73db790c218503074d932de19790a934e Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 6 Nov 2023 09:19:50 -0400 Subject: [PATCH 01/12] fst ignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b6a065 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata From ed648ef6c5588082e71ec646e2fd31a49d74d869 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 6 Nov 2023 09:19:53 -0400 Subject: [PATCH 02/12] fst proj --- wild_pigs.Rproj | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 wild_pigs.Rproj diff --git a/wild_pigs.Rproj b/wild_pigs.Rproj new file mode 100644 index 0000000..d063e8b --- /dev/null +++ b/wild_pigs.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: knitr +LaTeX: pdfLaTeX From 6ac1e4eb60437b2ce53feca30eae25011e7b48be Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 6 Nov 2023 09:28:49 -0400 Subject: [PATCH 03/12] fix typos --- README.md | 4 ++-- scripts/activity_plots.R | 12 ++++++------ scripts/hazard_script.R | 14 +++++++------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 2e2f905..1567bc2 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -## Variation in temporal activity may reduce competetive interaction between native and introduced species +## Variation in temporal activity may reduce competitive interaction between native and introduced species - Authors: - Brendan Carswell @@ -11,6 +11,6 @@ - [Eric Vander Wal](https://weel.gitlab.io/) -This repository contains the code accompanying the paper ["Variation in temporal activity may reduce competetive interaction between native and introduced species"]. +This repository contains the code accompanying the paper ["Variation in temporal activity may reduce competitive interaction between native and introduced species"]. Scripts are under `scripts/`. diff --git a/scripts/activity_plots.R b/scripts/activity_plots.R index b00d4e5..9bbcd3b 100644 --- a/scripts/activity_plots.R +++ b/scripts/activity_plots.R @@ -1,8 +1,8 @@ -### This script is for the 24-hour activity patten graphs. +### This script is for the 24-hour activity pattern graphs. #most of this code was adapted from Louvrier et al. 2021 -##lpackages req'd for figures +##packages req'd for figures library(camtrapR) library(car) library(maptools) @@ -48,14 +48,14 @@ Native <- c("white tailed deer", "coyote", "elk", "mule deer") color_list <- c("#E69F00", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "#29AF7FFF", "#56B4E9") ############################################################################### -#need to fix habiat types column here. typo in my OG data +#need to fix habitat types column here. typo in my OG data unique(BoarDATA$Habitat_Ty) BoarDATA$Habitat_Ty[BoarDATA$Habitat_Ty == "Cropland/?"] <- "Cropland" habs <- unique(BoarDATA$Habitat_Ty) #first make a new column of "Native" or boar. -#just renaming becuase im very inconsistent with names apparently +#just renaming because im very inconsistent with names apparently BoarDATA$native <- BoarDATA$Animal BoarDATA$native[BoarDATA$native == "Boar"] <- "wild pig" BoarDATA$Animal[BoarDATA$native == "White Tailed Deer"] <- "white tailed deer" @@ -146,7 +146,7 @@ plot_overlap <- function(speciesA_for_activity, speciesB_for_activity, d_overlap boarnative <- plot_overlap(speciesA_for_activity,speciesB_for_activity, d_overlap_boarnative) boarnative ############################################################################################################################################################## -# now i can crete circular plots for each sp. combination. and make multipannel +# now i can create circular plots for each sp. combination. and make multipanel #Circular plots !! #going to try to make one for boar v.s. moose @@ -438,7 +438,7 @@ B_C <- ggplot(data = ABCDEF, aes(x = x, y = value, colour = species)) + plot(B_C) ############################################################################################################################################################## -#now, taking the 5 circular plots from above and making a multipannel figure +#now, taking the 5 circular plots from above and making a multipanel figure library(cowplot) library(grid) library(ggplotify) diff --git a/scripts/hazard_script.R b/scripts/hazard_script.R index 56aae0d..9dc1179 100644 --- a/scripts/hazard_script.R +++ b/scripts/hazard_script.R @@ -2,7 +2,7 @@ #Code for Carswell et al. wild pigs paper #Script 1: Contains code for proportional hazard models + plotting coef's -#laoding 1st set of req. packages +#loading 1st set of req. packages library(dplyr) library(readr) library(camtrapR) @@ -21,12 +21,12 @@ BoarDATA <- read_csv("Data/Processed/BoarDATA.csv", ######################################################################################################## #this section will identify the species of interest, and filter out the species recorded -## that are unliekly to be ecologically affected (or at least managers wont care by many interactions) +## that are unlikely to be ecologically affected (or at least managers wont care by many interactions) #convert to data table to work with BoarDATA2 <- as.data.table(BoarDATA) -#coverting boar --> invasive species and mammals of interest to native b/c we need to group to have enough +#converting boar --> invasive species and mammals of interest to native b/c we need to group to have enough #observations species_interest <- c("Native", "Invasive", "Human") @@ -53,7 +53,7 @@ BoarDATA3 <- as.data.frame(BoarDATA3) ######################################################################################################## ## Much of this code is adapted from Louvrier et al. 2021 -#I wasnt able to get the function (below) to run wihtout re-naming my variables to the same names they had. +#I wasnt able to get the function (below) to run without re-naming my variables to the same names they had. #so just aligning our data so the function can run correctly. CT_act_MN <- BoarDATA3 @@ -96,7 +96,7 @@ calculate_delta <- function(species) #delta is going to contain the calculated difference of time between species A and B for each row of the detections of the CT #but only if the two species from row i and i+1 are in the specific order - #if the combination of the species names between the row i and i + 1 is equal to the names of the species of interest stuck togetehr then we calcutalte difftime between the two lines, otherwise, no + #if the combination of the species names between the row i and i + 1 is equal to the names of the species of interest stuck together then we calculate difftime between the two lines, otherwise, no delta <- rep(NA, nrow(B_ordered)) for(i in 1: (nrow(B_ordered))) @@ -143,7 +143,7 @@ delata_boar_boar <- calculate_delta(species) #putting this in a dataframe df_time_diff_boar_boar = plyr::ldply(delata_boar_boar, rbind) -##extracting events that were interuppted by humans (censor). First human --> native species +##extracting events that were interupted by humans (censor). First human --> native species species = c(species_interest[3],species_interest[1]) delata_human_native <- calculate_delta(species) #putting this in a dataframe @@ -272,7 +272,7 @@ almostthere$Habitat_Ty[almostthere$Habitat_Ty == "Pasture"] <- "Apasture" #export this as a df ready to analyze. -cox_analyze <- write_csv(almostthere, "cox_analyze.csv" +cox_analyze <- write_csv(almostthere, "cox_analyze.csv") ###################################################################################################################### # Great, now we can actually run these hazard models ###################################################################################################################### From 2a31ed00f9221da6a7cd1d3724353084fca99911 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 6 Nov 2023 09:40:03 -0400 Subject: [PATCH 04/12] new title --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 1567bc2..c9ef7d5 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ +# Variation in spatiotemporal activity may reduce competitive interactions between invasive wild pigs (Sus scrofa) and native mammal species - -## Variation in temporal activity may reduce competitive interaction between native and introduced species - - Authors: - Brendan Carswell - [Sean Boyle](https://sites.google.com/prod/view/seanboylephd) From 259551176ec6550d03c94de5331baf0006e15ae6 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 6 Nov 2023 09:52:39 -0400 Subject: [PATCH 05/12] fst CITATION.cff --- CITATION.cff | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 CITATION.cff diff --git a/CITATION.cff b/CITATION.cff new file mode 100644 index 0000000..84db912 --- /dev/null +++ b/CITATION.cff @@ -0,0 +1,16 @@ +cff-version: 1.1.0 +message: "If you use this software, please cite it as below." +authors: +- family-names: Carwsell + given-names: B.M. +- family-names: Boyle + given-names: S.P. +- family-names: Brook + given-names: R.K. +- family-names: van Beest + given-names: F.M. +- family-names: Vander Wal + given-names: E. +title: Variation in spatiotemporal activity may reduce competitive interactions between invasive wild pigs (Sus scrofa) and native mammal species +version: v0.0.0.9000 +date-released: 2023-11-06 \ No newline at end of file From 97b51e94606b8b23d3a2dd6c307fb4a12c3a7622 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 6 Nov 2023 09:52:53 -0400 Subject: [PATCH 06/12] fst zenodo.json --- .zenodo.json | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .zenodo.json diff --git a/.zenodo.json b/.zenodo.json new file mode 100644 index 0000000..a9721f0 --- /dev/null +++ b/.zenodo.json @@ -0,0 +1,27 @@ +{ + "description": "

This repository contains the code accompanying the paper “Variation in spatiotemporal activity may reduce competitive interactions between invasive wild pigs (Sus scrofa) and native mammal species”.

", + "title": "Code accompanying - Variation in spatiotemporal activity may reduce competitive interactions between invasive wild pigs (Sus scrofa) and native mammal species", + "creators": [ + { + "affiliation": "Memorial University", + "name": "B.M. Carswell" + }, + { + "affiliation": "Memorial University", + "name": "S.P. Boyle" + }, + { + "affiliation": "University of Saskatchewan", + "name": "R.K. Brook" + }, + { + "affiliation": "Aarhus University", + "name": "F.M. van Beest" + }, + { + "orcid": "0000-0002-8534-4317", + "affiliation": "Memorial University", + "name": "E. Vander Wal" + } + ] +} From 40a0ec4b252df09a8bf96f9a2fcf38f764cc13f9 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 6 Nov 2023 09:53:02 -0400 Subject: [PATCH 07/12] update title --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c9ef7d5..03e0c32 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Variation in spatiotemporal activity may reduce competitive interactions between invasive wild pigs (Sus scrofa) and native mammal species +# Variation in spatiotemporal activity may reduce competitive interactions between invasive wild pigs (*Sus scrofa*) and native mammal species - Authors: From 4d019c7d4332c34de0b2aaa807d894cd4a4d9276 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 6 Nov 2023 09:53:30 -0400 Subject: [PATCH 08/12] journal --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 03e0c32..108f250 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ - Floris van Beest - [Eric Vander Wal](https://weel.gitlab.io/) +Canadian Journal of Zoology. https://doi.org/10.1139.CJZ-2022-0145 This repository contains the code accompanying the paper ["Variation in temporal activity may reduce competitive interaction between native and introduced species"]. Scripts are under `scripts/`. From a43f7c3d941c5cb74d79d79f292ba2669202c949 Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 6 Nov 2023 09:54:12 -0400 Subject: [PATCH 09/12] brief --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 108f250..1868f0b 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ - Floris van Beest - [Eric Vander Wal](https://weel.gitlab.io/) -Canadian Journal of Zoology. https://doi.org/10.1139.CJZ-2022-0145 -This repository contains the code accompanying the paper ["Variation in temporal activity may reduce competitive interaction between native and introduced species"]. -Scripts are under `scripts/`. +This repository contains the code accompanying the paper "Variation in spatiotemporal activity may reduce competitive interactions between invasive wild pigs (*Sus scrofa*) and native mammal species" Canadian Journal of Zoology. https://doi.org/10.1139.CJZ-2022-0145 + +Scripts are under `scripts/`. \ No newline at end of file From 53b28dc11a5e3a11feeba8c47b3cdbd2261a7e9c Mon Sep 17 00:00:00 2001 From: "Alec L. Robitaille" Date: Mon, 6 Nov 2023 09:56:36 -0400 Subject: [PATCH 10/12] badge --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 1868f0b..872e749 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Variation in spatiotemporal activity may reduce competitive interactions between invasive wild pigs (*Sus scrofa*) and native mammal species +[![DOI](https://zenodo.org/badge/540079228.svg)](https://zenodo.org/doi/10.5281/zenodo.10075565) + - Authors: - Brendan Carswell - [Sean Boyle](https://sites.google.com/prod/view/seanboylephd) From 281f0136d45996eb06c201a76cfcbe9bdd42107f Mon Sep 17 00:00:00 2001 From: Brendan Carswell <91228884+carswellb@users.noreply.github.com> Date: Mon, 6 Nov 2023 10:42:45 -0330 Subject: [PATCH 11/12] Update activity_plots.R --- scripts/activity_plots.R | 45 ++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/scripts/activity_plots.R b/scripts/activity_plots.R index 9bbcd3b..c4fa23b 100644 --- a/scripts/activity_plots.R +++ b/scripts/activity_plots.R @@ -19,11 +19,11 @@ library(maptools) library(data.table) library(ggplot2) -###### Importing data +# Data import BoarDATA <- read_csv("Data/Processed/BoarDATA.csv", col_types = cols(posixdate = col_datetime(format = "%m/%d/%Y %H:%M"))) -#renaming all names in the data sheet so I have consistent names for figures as in MS +#species event renames unique(BoarDATA$Animal) BoarDATA$Animal[BoarDATA$Animal == "Boar"] <- "wild pig" BoarDATA$Animal[BoarDATA$Animal == "White Tailed Deer"] <- "white tailed deer" @@ -32,30 +32,28 @@ BoarDATA$Animal[BoarDATA$Animal == "Moose"] <- "moose" BoarDATA$Animal[BoarDATA$Animal == "Mule Deer"] <- "mule deer" BoarDATA$Animal[BoarDATA$Animal == "Elk"] <- "elk" -# everything to character -class(BoarDATA$Camera) #character -class(BoarDATA$posixdate) #posix - +#data structuring +class(BoarDATA$Camera) +class(BoarDATA$posixdate) BoarDATA$posixdate <- as.character(BoarDATA$posixdate) ############################################################################## -# create activity overlap plot -# define species of interest +# create activity overlap plots +#define species of interest species_interest <- c("wild pig", "moose", "white tailed deer", "coyote", "elk", "mule deer", "native") Native <- c("white tailed deer", "coyote", "elk", "mule deer") -#consistent colour patterns with other figures in other script +#colour palate color_list <- c("#E69F00", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "#29AF7FFF", "#56B4E9") ############################################################################### -#need to fix habitat types column here. typo in my OG data +#fix habitat name unique(BoarDATA$Habitat_Ty) BoarDATA$Habitat_Ty[BoarDATA$Habitat_Ty == "Cropland/?"] <- "Cropland" habs <- unique(BoarDATA$Habitat_Ty) -#first make a new column of "Native" or boar. -#just renaming because im very inconsistent with names apparently +#first make a new column of "native" or wild pig BoarDATA$native <- BoarDATA$Animal BoarDATA$native[BoarDATA$native == "Boar"] <- "wild pig" BoarDATA$Animal[BoarDATA$native == "White Tailed Deer"] <- "white tailed deer" @@ -69,9 +67,7 @@ BoarDATA$native <- mapvalues(BoarDATA$native, from = "mule deer", "moose"), to = c("native", "native", "native", "native", "native")) - -# 2D comparison of temporal overlap between wild pigs + all native species in one group - +# 2D comparison of temporal overlap between wild pigs + all native species speciesA_for_activity <- "wild pig" speciesB_for_activity <- "native" dataf <- BoarDATA @@ -88,7 +84,7 @@ d_overlap_boarnative <- activityOverlap(recordTable = dataf, #plotDirectory = getwd() ) -dhat <- "0.72" +dhat <- "0.72" #coef of activtiy overlap. See Ridout and Linkie 2009. plot_overlap <- function(speciesA_for_activity, speciesB_for_activity, d_overlap) { @@ -146,9 +142,8 @@ plot_overlap <- function(speciesA_for_activity, speciesB_for_activity, d_overlap boarnative <- plot_overlap(speciesA_for_activity,speciesB_for_activity, d_overlap_boarnative) boarnative ############################################################################################################################################################## -# now i can create circular plots for each sp. combination. and make multipanel -#Circular plots !! -#going to try to make one for boar v.s. moose +#Circular diel plots of wild pig to each native species +#wild pig / moose # create activity overlap plot # define species of interest @@ -205,8 +200,7 @@ B_M <- ggplot(data = ABCDEF, aes(x = x, y = value, colour = species)) + plot(B_M) ############################################################################################################################################################## -# ROUND 2 -#boar v.s. moose +#wild pig / white tail # create activity overlap plot # define species of interest @@ -263,8 +257,7 @@ B_WTD <- ggplot(data = ABCDEF, aes(x = x, y = value, colour = species)) + plot(B_WTD) ############################################################################################################################################################## -# ROUND 3 -#boar v.s. Mule Deer +#wild pig / Mule Deer # create activity overlap plot # define species of interest @@ -321,8 +314,7 @@ B_MD <- ggplot(data = ABCDEF, aes(x = x, y = value, colour = species)) + plot(B_MD) ############################################################################################################################################################## -# ROUND 4 -#boar v.s. Elk +#wild pig / Elk # create activity overlap plot # define species of interest @@ -379,8 +371,7 @@ B_E <- ggplot(data = ABCDEF, aes(x = x, y = value, colour = species)) + plot(B_E) ############################################################################################################################################################## -# ROUND 5 -#boar v.s. Coyote +#wild pig / Coyote # create activity overlap plot # define species of interest From 7bebbbd761cdf3ebe4010a1a0253b4633c5acaf4 Mon Sep 17 00:00:00 2001 From: Brendan Carswell <91228884+carswellb@users.noreply.github.com> Date: Mon, 6 Nov 2023 10:58:09 -0330 Subject: [PATCH 12/12] Update hazard_script.R --- scripts/hazard_script.R | 117 +++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 68 deletions(-) diff --git a/scripts/hazard_script.R b/scripts/hazard_script.R index 9dc1179..9bc34aa 100644 --- a/scripts/hazard_script.R +++ b/scripts/hazard_script.R @@ -1,8 +1,6 @@ -############################################################################### -#Code for Carswell et al. wild pigs paper #Script 1: Contains code for proportional hazard models + plotting coef's -#loading 1st set of req. packages +#packages library(dplyr) library(readr) library(camtrapR) @@ -15,22 +13,20 @@ library(data.table) library(janitor) library(stats) -#loading OG boar data from. +#data import BoarDATA <- read_csv("Data/Processed/BoarDATA.csv", col_types = cols(posixdate = col_datetime(format = "%m/%d/%Y %H:%M"))) ######################################################################################################## #this section will identify the species of interest, and filter out the species recorded -## that are unlikely to be ecologically affected (or at least managers wont care by many interactions) #convert to data table to work with BoarDATA2 <- as.data.table(BoarDATA) -#converting boar --> invasive species and mammals of interest to native b/c we need to group to have enough -#observations +#converting boar --> invasive species and mammals of interest to native species_interest <- c("Native", "Invasive", "Human") -#creating a new column so I have an OG reference to bounce off of +#new species column to work off as reference BoarDATA2$Native <- BoarDATA2$Animal #then renaming the species to native / invasive @@ -44,15 +40,15 @@ BoarDATA2$Native[BoarDATA2$Native == "Mule Deer"] <- "Native" #importnat to include us at cameras for censoring BoarDATA2$Native[BoarDATA2$Native == "Human"] <- "Human" -#sorting DT by species of interest +#sorting by species of interest BoarDATA2[, interest := Native %in% species_interest] -# and creating a new DF of only species of interest +#only species of interest BoarDATA3 <- filter(BoarDATA2, interest == TRUE) BoarDATA3 <- as.data.frame(BoarDATA3) ######################################################################################################## -## Much of this code is adapted from Louvrier et al. 2021 +##code adapted from Louvrier et al. 2021 #I wasnt able to get the function (below) to run without re-naming my variables to the same names they had. #so just aligning our data so the function can run correctly. CT_act_MN <- BoarDATA3 @@ -76,14 +72,13 @@ class(d1_interest$DateTimeOriginal) id_CT <- unique(d1_interest$Station) ######################################################################################################## -## this is the function published in Louvrier et al. 2021 which calculates time -## between desired sequential events for a given camera trap. +#Louvrier et al. 2021 function which calculates time between sequential events for a given camera trap. calculate_delta <- function(species) { #empty list delta_species <- list(0) - ###loop starts here + #loop starts here for(j in id_CT) { @@ -104,9 +99,7 @@ calculate_delta <- function(species) if (paste(B_ordered[c(i:(i+1)),"Species"], collapse ="") == paste(species, collapse="")) { diff <- difftime(B_ordered[i,"DateTimeOriginal"], B_ordered[i+1,"DateTimeOriginal"]) - if (diff < - 10000) #the only reason i left these in is b/c the f(x)n wouldn't run without it - { #and i dont know enough to re-write. I just extended the time to a massive # to make it useless tho. - # you can adjust this time depending on your desired study + if (diff < - 10000) delta[i] <- 0 } if (diff >= -10000) @@ -123,21 +116,20 @@ calculate_delta <- function(species) return(delta_species) } - ################################################################################# -##selecting the combo of invasive boar --> any native species +#selecting the combo of invasive wild pig --> any native species species = c(species_interest[2],species_interest[1]) delata_boar_any <- calculate_delta(species) #putting this in a dataframe df_time_diff_boar_any = plyr::ldply(delata_boar_any, rbind) -##next interactions.... selecting the combo of any native --> any native sp of interest +##next interactions.... combo of any native --> any native sp of interest species = c(species_interest[1],species_interest[1]) delata_any_any <- calculate_delta(species) #putting this in a dataframe df_time_diff_any_any = plyr::ldply(delata_any_any, rbind) -##final interaction of interest... boar --> boar +##final interaction of interest... wild pig --> wild pig species = c(species_interest[2],species_interest[2]) delata_boar_boar <- calculate_delta(species) #putting this in a dataframe @@ -156,46 +148,43 @@ delata_human_boar <- calculate_delta(species) df_time_diff_human_boar = plyr::ldply(delata_human_boar, rbind) ################################################################################# -#The 'df_time_diff' dataframes are large and very jumbled -# below i extract the TTE's in a weird way... but it works +#need to extract time-to-event values from dataframes -#first step is to pivot longer... then last two columns in the DF end up the TTE's + the camera associated with -#boar --> native species combo +#pivot longer, last two columns in the DF = TTE's + the camera associated with +#wild pig --> native species combo boar_any_extract <- tidyr::pivot_longer(df_time_diff_boar_any, c(2:50), values_drop_na = TRUE) -#and then extracting the two columns at the end.. +# extracting the two columns boar_any_extracted <- cbind.data.frame(boar_any_extract$.id, boar_any_extract$value) #create a new column labelling that this is the interaction boar_any_extracted$Type <- rep(c("Boar_native"), times = 7) -#also need to fix the names. ARGH.There must be an easier way to do this but I'm not sure how. boar_any_extracted$ID <- boar_any_extracted$`boar_any_extract$.id` -#repeating for the next interaction (boar --> boar). same steps as above. +#repeat for wild pig to wild pig boar_boar_extract <- tidyr::pivot_longer(df_time_diff_boar_boar, c(2:50), values_drop_na = TRUE) boar_boar_extracted <- cbind.data.frame(boar_boar_extract$.id, boar_boar_extract$value) boar_boar_extracted$Type <- rep(c("Boar_boar"), times = 11) boar_boar_extracted$ID <- boar_boar_extracted$`boar_boar_extract$.id` -#repeating for the next interaction (native --> native). same steps as above. +#repeat for native to native any_any_extract <- tidyr::pivot_longer(df_time_diff_any_any, c(2:50), values_drop_na = TRUE) any_any_extracted <- cbind.data.frame(any_any_extract$.id, any_any_extract$value) any_any_extracted$Type <- rep(c("Native_native"), times = 517) any_any_extracted$ID <- any_any_extracted$`any_any_extract$.id` -#repeating for the next interaction (human --> native). same steps as above. CENSOR +#repeat for human to native species human_native_extract <- tidyr::pivot_longer(df_time_diff_human_native, c(2:50), values_drop_na = TRUE) human_native_extracted <- cbind.data.frame(human_native_extract$.id, human_native_extract$value) human_native_extracted$Type <- rep(c("Human_native"), times = 27) human_native_extracted$ID <- human_native_extracted$`human_native_extract$.id` -#repeating for the final interaction (human --> native). same steps as above. CENSOR +#repeat for human to wild pig human_boar_extract <- tidyr::pivot_longer(df_time_diff_human_boar, c(2:50), values_drop_na = TRUE) human_boar_extracted <- cbind.data.frame(human_boar_extract$.id, human_boar_extract$value) human_boar_extracted$Type <- rep(c("Human_boar"), times = 1) human_boar_extracted$ID <- human_boar_extracted$`human_boar_extract$.id` ################################################################################# -#now i need to merge these three TTE dataframes to I can analyze it -#but first, correcting the columns so all names line up +#clean up and merge 3 dataframes boar_boar_extracted$TTE <- boar_boar_extracted$`boar_boar_extract$value` boar_any_extracted$TTE <- boar_any_extracted$`boar_any_extract$value` any_any_extracted$TTE <- any_any_extracted$`any_any_extract$value` @@ -203,7 +192,7 @@ any_any_extracted$TTE <- any_any_extracted$`any_any_extract$value` human_boar_extracted$TTE <- human_boar_extracted$`human_boar_extract$value` human_native_extracted$TTE <- human_native_extracted$`human_native_extract$value` -#removing the old columns with the bad names. Sorry, this is not efficient but.. worked for me +#clean up unnecessary columns boar_boar_clean <- dplyr::select(boar_boar_extracted, -1,-2) boar_any_clean <- dplyr::select(boar_any_extracted, -1,-2) any_any_clean <- dplyr::select(any_any_extracted, -1,-2) @@ -212,34 +201,32 @@ human_boar_clean <- dplyr::select(human_boar_extracted, -1,-2) human_native_clean <- dplyr::select(human_native_extracted, -1,-2) ################################################################################# -#merging the DF's into one to analyze +#merge to an analysis dataframe cox_1.0 <- rbind(boar_any_clean, boar_boar_clean, any_any_clean, human_boar_clean, human_native_clean) #save as a CSV cox_1.0 <- write_csv(cox_1.0, "cox_1.0.csv") ############################################################################## -# okay now that I have my DF set up... I need to attach the habitat covars + time etc. +# attaching habitat covariates to TTE's -#re-loading the dataset where the co vars are attached for each camera location +#load habitat covar data BoarDATA <- read_csv("Data/Processed/BoarDATA.csv", col_types = cols(posixdate = col_datetime(format = "%m/%d/%Y %H:%M"))) -#re-loading the processed TTE dataframe +#TTE data cox_1.0 <- read_csv("Data/Processed/cox_1.0.csv") - -#sub set the first data set so that i have what i need from big DF +#subset habitat data to covars of intetest sub <- BoarDATA[c("Camera", "posixdate", "Habitat_Ty" , "ElevationC", "DIST_WATER", "DIST_ROAD")] -#rename ID --> camera in the other DF +#rename colnames(cox_1.0)[2] <- "Camera" #creating a column of unique values in cox_1.0 to sort out later after merge. cox_1.0$Unique <- c(1:530) -#changing names in column to match up for merge. b/c of the weird names this is the best -## way i know how to do it. in hindsight.. would have purely numeric names. +#renaming numerical to camera_id names cox_1.0$Camera[cox_1.0$Camera == "1"] <- "Cam01" cox_1.0$Camera[cox_1.0$Camera == "2"] <- "Cam02" cox_1.0$Camera[cox_1.0$Camera == "3"] <- "Cam03" @@ -258,24 +245,23 @@ cox_1.0$Camera[cox_1.0$Camera == "15"] <- "Cam16" cox_1.0$Camera[cox_1.0$Camera == "16"] <- "Cam17" cox_1.0$Camera[cox_1.0$Camera == "17"] <- "Cam18" -#okay cool. now i can finally merge all cox_1.0 + sub set covars. b/c this dont work yet +#merge intermediate <- merge(cox_1.0, sub, by = "Camera", all.x = FALSE, all.y = FALSE, no.dups = TRUE) -#now can i filter out by unique values? +#filter by unique values almostthere <- intermediate %>% group_by(Unique) %>% filter(row_number()==1) -#final step is to rename that weird cropland/? to normal cropland so it can be a category. +#fix habitat names for plotting later almostthere$Habitat_Ty[almostthere$Habitat_Ty == "Cropland/?"] <- "Cropland" almostthere$Habitat_Ty[almostthere$Habitat_Ty == "Pasture"] <- "Apasture" -#export this as a df ready to analyze. - +# save analysis dataframe cox_analyze <- write_csv(almostthere, "cox_analyze.csv") + ###################################################################################################################### -# Great, now we can actually run these hazard models -###################################################################################################################### +# Hazard models #packages req'd for hazard models library(readr) library(data.table) @@ -289,21 +275,18 @@ library(plotrix) #data cox_analyze <- read_csv("Data/Processed/cox_analyze.csv") -#remove the human observations, just wanted to not include them inturrupting +#remove the human inturrupted observations cox_analyze <- subset(cox_analyze$Type, select = -c("Human_native", "Human_boar")) -#looking at structure -str(cox_analyze) - -#all characters as factors +#structuring cox_analyze$Camera <- as.factor(cox_analyze$Camera) cox_analyze$Type <- as.factor(cox_analyze$Type) cox_analyze$Habitat_Ty <- as.factor(cox_analyze$Habitat_Ty) -#need TTE's as positive not negative numbers. Function calculates TTE as a negative +#TTE's as positive numbers cox_analyze$TTE <- abs(cox_analyze$TTE) ##################################################################################################### -#model set for ranking based on the few imp. covars were interested in +#model set for ranking based on the few important covars #just a test model to make sure structure works. test <- coxph(Surv(TTE) ~ 1, data = cox_analyze) @@ -330,9 +313,8 @@ model <- coxme(Surv(TTE) ~ Habitat_Ty + Type + (1|Camera), data = cox_analyze) summary(model) AIC3 <- AICc(model) -##############3 -#creating an AICc table for model ranking +#creating an AICc table for model ranking #putting AICc values into a vector with models attached Model_names <- c("Base", "Type", "Habitat", "Habitat + Type") AICvector <- c(AICbase, AIC1, AIC2, AIC3) @@ -353,13 +335,14 @@ exp(mean(log(cox_analyze$TTE[cox_analyze$Type == "Boar_native"]))) exp(mean(log(cox_analyze$TTE[cox_analyze$Type == "Anative_native"]))) # mean = 11.18876 hrs median = 6.471181 + ################################################################################################ #Below is predicting risk based on the top suppored model. #creating new column of predicted risk from model cox_analyze$risk <- coxme:::predict.coxme(model, type="risk") -#sub setting the predicted risk by the interaction type and then generating summary stats +#subsetting the predicted risk by the interaction type and then generating summary stats b_b <- filter(cox_analyze, Type == "Boar_boar") mean(b_b$risk) median(b_b$risk) @@ -382,12 +365,12 @@ std.error(n_n$risk) library(ggplot2) library(ggpubr) -#create a new DF dropping the censor data so I can make a boxplot of predicted risk. +#new data just to plot boxplot_data <- cox_analyze boxplot_data$Type <- as.character(boxplot_data$Type) -#just getting names consistent with MS +#consistent names boxplot_data$Type[boxplot_data$Type == "Boar_boar"] <- "wild pig:wild pig" boxplot_data$Type[boxplot_data$Type == "Boar_native"] <- "wild pig:native" boxplot_data$Type[boxplot_data$Type == "Anative_native"] <- "native:native species" @@ -428,7 +411,7 @@ ggplot(data = boxplot_data, mapping = aes(x = Type, y = risk, fill=Type)) + ############################################################################### #plotting the coefficient values from top supported hazard model. -#only new req'd package. +#package call library(tibble) #coe'f names, values, and error @@ -446,7 +429,7 @@ model.summary$Confid <- (model.summary$SE * 1.96) bold.hab <- c("Wild pig:wild pig") bold.labels <- ifelse(levels(model.summary$Coefs) %in% bold.hab, yes = "bold", no = "plain") -#the plot of coef values for the appendix. +#the plot of coef values for the suppliment ggplot(model.summary, aes(x=Coefs, y=Values)) + geom_point(col="black", size=3) + xlab("Coefficients from top supported model") + ylab("Standardized coefficient value") + @@ -463,8 +446,8 @@ ggplot(model.summary, aes(x=Coefs, y=Values)) + fontface = "bold") ################################################################################ -#Figure to show that we have confidence in difference between species to species distributions -#new packages required +#Bootstrapping distributions of each event type to show we have confidence in trends exhibited (Fig 4) +#packages required library(png) library(jpeg) library(grid) @@ -490,7 +473,6 @@ median(b_b_sub$TTE) #11 box_sub <- subset(cox_analyze, !Type == "Native_native") #bootstrapping native-to-native species dist'n to n=7 (smallest sample in data), and reiterating 1000x -#highlighting that despite small sample, trends still hold set.seed(08092023) TTE <- replicate(1000, median(sample(n_n_sub$TTE, 7, replace = TRUE))) @@ -518,7 +500,6 @@ b_n_boxplot_img <- ggplot(box_sub) + axis.title = element_blank()) #saving blank version of the boxplot to be imposed with probabilty distribution - ggsave("/Output/Box_insert.png", plot = b_n_boxplot_img, device = "png")