Peeper Analysis!

Importing the Datasets

# Import spectrogram data into R from Raven Selection Tables. 
data <- imp_raven(all.data = TRUE, 
                  only.spectro.view = TRUE,
                  path = '~/Raven Pro 1.6/Selections')

# Import metadata containing body temperature and collection data
meta.data <- read_csv("~/Desktop/UTK/Tanner Lab/Analysis/Call Analysis/Peeper Analysis/Spring Peeper Data - Collection Data.csv")
## Rows: 73 Columns: 18
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (7): Male ID, Date, Location, Catcher, Recorder, Processer, Prevalence
## dbl (11): Body Temp, Weight (Bag + Frog), Weight (Bag), SVL, Tibia, Frog Wei...
## 
## ℹ 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.
# Take the file (in the table as PC-000-24_MMDDYYYY_XX.wav) and separate into it's parts
data <- data %>% separate(`Begin File`, 
                          c("ID", "Date", "Location.wav"), 
                          sep = "_",
                          remove = FALSE
                          ) 
# New simpler column for location 
data <- data %>% mutate(Location = case_when(`Location.wav`=='FR.wav' ~ 'FR',
                                              `Location.wav`=='CV.wav' ~ 'CV'))

# Discard columns not needed for future
data <- data %>% select(-c(`View`,`Channel`,`Occupancy`,`Low Freq (Hz)`,`High Freq (Hz)`,`Location.wav`, `Frame Duration`, `Reason`))

# Calculate ICI for values based on strict row calculation.
data <- data %>% mutate(ICI = lead(`Begin Time (s)`)-`End Time (s)`)
data <- data %>% filter (ICI >= 0)

# Calculate Call_Period (Duration + ICI) and Instantaneous Rate (1/Call_Period)*60
data <- data %>% mutate(Call_Period = `Delta Time (s)`+ICI)
data <- data %>% mutate(Call_Rate_Inst = (1/Call_Period)*60)

# Output the resulting files into a CSV, gets written in a working directory. Only have to do this once if nothing before this is being done
# write.csv(data,"data.csv",row.names=FALSE)

# In the CSV file, go through each Raven file and decide which 30 consecutive calls to use. Ideally, in a calling bout, start from the 6th bout and exclude the last call. Want consecutive call numbers. Save this as a new file (e.g. edited.data and import).
import.data <- read_csv("~/Desktop/UTK/Tanner Lab/Analysis/Call Analysis/Peeper Analysis/edited.data.csv")
## Rows: 1796 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (5): Begin File, ID, Date, selec.file, Location
## dbl (11): Selection, Begin Time (s), End Time (s), Delta Time (s), Peak Freq...
## 
## ℹ 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.
# When importing data, going to go with the manually edited data. 
manual.data <- left_join(import.data, meta.data, by = join_by(ID == `Male ID`))
manual.data <- manual.data %>% select(-Selection)
manual.data <- as.data.frame(manual.data)

Temperature Correction Function

Tcorr <- function(col_numbers, target_temp, data){
  num_cols <- length(col_numbers)
  Tcorrected_matrix <- matrix(NA, nrow = nrow(data), ncol = num_cols)
  
  for(i in seq_along(col_numbers)){
    m <- lm(data[,col_numbers[i]] ~ data$`Body Temp`, data)$coefficients[2] # Calculate m by doing a linear regression 
    Tcorrected_matrix[, i] <- data[,col_numbers[i]] - m * (data$`Body Temp` - target_temp) # Save a vector so I can use it outside the function 
  }
  
  avg <- apply(Tcorrected_matrix, 1, mean)
  stdev <- apply(Tcorrected_matrix, 1, sd)
  colnames(Tcorrected_matrix) <- colnames(data[, col_numbers])
  
  result <- list(avg = avg, stdev = stdev, Tcorrected_matrix = Tcorrected_matrix)

  return(result)
}

Doing the Temperature Correction

# Set Values that you want to temperature correct for
selections <- c(3:7,13:15)

# Do the temperature correction to the target temp, aka 16
result <- Tcorr(selections, target_temp = 16, data = manual.data)
# Format results as a dataframe
Tdat <- as.data.frame(result$Tcorrected_matrix)
# Add information such as ID, Location, and Prevalence
Tdat<-cbind(manual.data[,c(9,10,12,18,31,32)], Tdat)

Plots for Checking Work

# Plots to check work - Call Rate Inst
plot(manual.data$Call_Rate_Inst~manual.data$`Body Temp`)

plot(Tdat$Call_Rate_Inst~manual.data$`Body Temp`)

# Plots to check work - Call Duration
plot(manual.data$`Delta Time (s)`~manual.data$`Body Temp`)

plot(Tdat$`Delta Time (s)`~manual.data$`Body Temp`)

# Plots to check work - ICI
plot(manual.data$ICI~manual.data$`Body Temp`)

plot(Tdat$ICI ~ manual.data$`Body Temp`)

# Plots to check work - Peak Freq
plot(manual.data$`Peak Freq (Hz)`~manual.data$`Body Temp`)

plot(Tdat$`Peak Freq (Hz)` ~ manual.data$`Body Temp`)

Table of Results

means <- Tdat %>% 
  group_by(ID, Location.x) %>%
  summarize(Temp = mean(`Body Temp`),
            Infection = mean(`Log Infection`),
            Call_Rate_Inst = mean(Call_Rate_Inst),
            Duration = mean(`Delta Time (s)`),
            ICI = mean(ICI),
            Freq = mean(`Peak Freq (Hz)`),
            BW = mean(`BW 90% (Hz)`))
## `summarise()` has grouped output by 'ID'. You can override using the `.groups`
## argument.
means
## # A tibble: 60 × 9
## # Groups:   ID [60]
##    ID       Location.x  Temp Infection Call_Rate_Inst Duration   ICI  Freq    BW
##    <chr>    <chr>      <dbl>     <dbl>          <dbl>    <dbl> <dbl> <dbl> <dbl>
##  1 PC-002-… FR          12.8     0               98.6   0.115  0.419 2778.  265.
##  2 PC-003-… FR          12.4     0               92.7   0.116  0.498 2968.  351.
##  3 PC-004-… CV          13.1     1.01            92.8   0.0731 0.530 3052.  171.
##  4 PC-005-… CV          13.3     1.25           112.    0.0956 0.365 2802.  257.
##  5 PC-006-… CV          11.8     2.11            92.9   0.110  0.497 2760.  249.
##  6 PC-007-… CV          11.9     0              111.    0.0861 0.338 2735.  204.
##  7 PC-009-… FR           7.1     0               89.3   0.0841 0.756 3055.  312.
##  8 PC-011-… CV          10.3     0               92.4   0.0708 0.557 3033.  233.
##  9 PC-012-… FR           8.3     1.99            92.3   0.115  0.539 2964.  250.
## 10 PC-013-… FR           9.3     0.788           93.0   0.126  0.485 3081.  335.
## # ℹ 50 more rows
sds <- Tdat %>% 
  group_by(ID, Location.x) %>%
  summarize(Temp = sd(`Body Temp`),
            Infection = sd(`Log Infection`),
            Call_Rate_Inst = sd(Call_Rate_Inst),
            Duration = sd(`Delta Time (s)`),
            ICI = sd(ICI),
            Freq = sd(`Peak Freq (Hz)`),
            BW = sd(`BW 90% (Hz)`))
## `summarise()` has grouped output by 'ID'. You can override using the `.groups`
## argument.
sds
## # A tibble: 60 × 9
## # Groups:   ID [60]
##    ID       Location.x  Temp Infection Call_Rate_Inst Duration   ICI  Freq    BW
##    <chr>    <chr>      <dbl>     <dbl>          <dbl>    <dbl> <dbl> <dbl> <dbl>
##  1 PC-002-… FR             0         0           9.13  0.00717 0.108  61.3  29.8
##  2 PC-003-… FR             0         0          13.3   0.0103  0.244  35.0  37.4
##  3 PC-004-… CV             0         0          11.4   0.00592 0.158  26.3  15.7
##  4 PC-005-… CV             0         0          17.3   0.00585 0.158  15.7  15.7
##  5 PC-006-… CV             0         0          14.7   0.00825 0.202  50.2  15.7
##  6 PC-007-… CV             0         0          15.6   0.00802 0.165  32.0  43.4
##  7 PC-009-… FR             0         0           8.20  0.00674 0.313  39.7  37.1
##  8 PC-011-… CV             0         0          13.4   0.00261 0.248  32.6  35.0
##  9 PC-012-… FR             0         0           6.78  0.00965 0.175   0    15.7
## 10 PC-013-… FR             0         0           8.43  0.00599 0.182  43.7   0  
## # ℹ 50 more rows
cvs <- Tdat %>% 
  group_by(ID, Location.x) %>%
  summarize(Temp = cv(`Body Temp`),
            Infection = cv(`Log Infection`),
            Call_Rate_Inst = cv(Call_Rate_Inst),
            Duration = cv(`Delta Time (s)`),
            ICI = cv(ICI),
            Period = cv(Call_Period),
            Freq = cv(`Peak Freq (Hz)`),
            Freq_5 = cv(`Freq 5% (Hz)`),
            Freq_95 = cv(`Freq 95% (Hz)`),
            BW = cv(`BW 90% (Hz)`))
## `summarise()` has grouped output by 'ID'. You can override using the `.groups`
## argument.
cvs
## # A tibble: 60 × 12
## # Groups:   ID [60]
##    ID    Location.x  Temp Infection Call_Rate_Inst Duration   ICI Period    Freq
##    <chr> <chr>      <dbl>     <dbl>          <dbl>    <dbl> <dbl>  <dbl>   <dbl>
##  1 PC-0… FR             0       NaN         0.0925   0.0622 0.257  0.207 0.0220 
##  2 PC-0… FR             0       NaN         0.144    0.0888 0.490  0.394 0.0118 
##  3 PC-0… CV             0         0         0.123    0.0809 0.299  0.262 0.00861
##  4 PC-0… CV             0         0         0.154    0.0612 0.433  0.341 0.00561
##  5 PC-0… CV             0         0         0.158    0.0751 0.406  0.332 0.0182 
##  6 PC-0… CV             0       NaN         0.140    0.0932 0.487  0.386 0.0117 
##  7 PC-0… FR             0       NaN         0.0918   0.0802 0.413  0.370 0.0130 
##  8 PC-0… CV             0       NaN         0.145    0.0368 0.445  0.395 0.0108 
##  9 PC-0… FR             0         0         0.0735   0.0837 0.325  0.270 0      
## 10 PC-0… FR             0         0         0.0907   0.0476 0.375  0.295 0.0142 
## # ℹ 50 more rows
## # ℹ 3 more variables: Freq_5 <dbl>, Freq_95 <dbl>, BW <dbl>
write.csv(cvs,"peeper_infection_cvs.csv",row.names=FALSE)

CV Table

# Calculated and formatted in Excel
cv.table <- read_xlsx("~/Desktop/UTK/Tanner Lab/Analysis/Call Analysis/Peeper Analysis/Peeper_CVs_Means_SDs.xlsx")
cv.table
## # A tibble: 8 × 3
##   Variable         Means     SDs
##   <chr>            <dbl>   <dbl>
## 1 Call_Rate_Inst 0.185   0.0964 
## 2 Duration       0.0747  0.0379 
## 3 ICI            0.472   0.385  
## 4 Period         0.419   0.359  
## 5 Freq           0.0113  0.00717
## 6 Freq_5         0.00744 0.00720
## 7 Freq_95        0.00756 0.00610
## 8 BW             0.113   0.0642
ggplot(cv.table, aes(x=reorder(Variable, Means), y=Means,fill=Variable)) +
  geom_bar(stat="identity")+
  theme_classic()+
  theme(axis.text.x = element_text(angle = 50, vjust = 0.5, hjust=0.5),
        legend.position = "none")+
  geom_errorbar( aes(ymin = Means-SDs, ymax = Means+SDs), 
                 data = cv.table, width = 0.2, alpha = 0.3)+
  geom_text(aes(label=round(Means,4)), vjust=-0.3, size=3.5, alpha=1)+
  labs(x = "Variable",
       y = "CV",
       title = "Variables ordered by CV")

knitr::include_graphics("~/Desktop/UTK/Tanner Lab/Analysis/Call Analysis/Peeper Analysis/Gerhardt.png")

GRAPHS

TdatLoad <- Tdat %>% filter(Prevalence=='Present')

ID.means <- Tdat %>% 
  group_by(ID, Location.x,Prevalence) %>%
  summarize(Temp = mean(`Body Temp`),
            Infection = mean(`Log Infection`),
            Call_Rate_Inst = mean(Call_Rate_Inst),
            Duration = mean(`Delta Time (s)`),
            ICI = mean(ICI),
            Freq = mean(`Peak Freq (Hz)`),
            BW = mean(`BW 90% (Hz)`))
## `summarise()` has grouped output by 'ID', 'Location.x'. You can override using
## the `.groups` argument.
write.csv(ID.means,"peeper_infection_by_ID.csv",row.names=FALSE)

ID.means.load <- TdatLoad %>% 
  group_by(ID, Location.x) %>%
  summarize(Temp = mean(`Body Temp`),
            Infection = mean(`Log Infection`),
            Call_Rate_Inst = mean(Call_Rate_Inst),
            Duration = mean(`Delta Time (s)`),
            ICI = mean(ICI),
            Freq = mean(`Peak Freq (Hz)`),
            BW = mean(`BW 90% (Hz)`))
## `summarise()` has grouped output by 'ID'. You can override using the `.groups`
## argument.
# Plots are in fact 9 Absent and 21 Present for 
ggplot(ID.means,aes(x = Prevalence))+
  geom_histogram(stat="count")+
  facet_wrap(~Location.x,strip.position="bottom")+
  theme_classic()+
  theme(aspect.ratio = 1,
        strip.background = element_blank(),
        strip.placement = "outside")+
  labs(title = "Prevalence across sites")
## Warning in geom_histogram(stat = "count"): Ignoring unknown parameters:
## `binwidth`, `bins`, and `pad`

Prevalence Graphs

ggplot(ID.means,aes(x=Prevalence,y=Call_Rate_Inst))+
  facet_wrap(~Location.x,strip.position="bottom")+
  geom_boxplot()+
  geom_jitter(position=position_jitter(0.1))+
  theme_classic()

ggplot(ID.means,aes(x=Prevalence,y=Duration))+
  facet_wrap(~Location.x,strip.position="bottom")+
  geom_boxplot()+
  geom_jitter(position=position_jitter(0.1))+
  theme_classic()

ggplot(ID.means,aes(x=Prevalence,y=ICI))+
  facet_wrap(~Location.x,strip.position="bottom")+
  geom_boxplot()+
  geom_jitter(position=position_jitter(0.1))+
  theme_classic()

ggplot(ID.means,aes(x=Prevalence,y=BW))+
  facet_wrap(~Location.x,strip.position="bottom")+
  geom_boxplot()+
  geom_jitter(position=position_jitter(0.1))+
  theme_classic()

ggplot(ID.means,aes(x=Prevalence,y=Freq))+
  facet_wrap(~Location.x,strip.position="bottom")+
  geom_boxplot()+
  geom_jitter(position=position_jitter(0.1))+
  theme_classic()

Fungal Load Graphs

# Call Rate Inst

ggplot(ID.means,aes(x =Infection,y = Call_Rate_Inst,group=Location.x))+
  theme_classic()+
  geom_point(aes(color=Location.x))+
  geom_smooth(aes(color=Location.x),
              method = 'lm',
              se = FALSE)
## `geom_smooth()` using formula = 'y ~ x'

ggplot(ID.means.load,aes(x =Infection,y = Call_Rate_Inst,group=Location.x))+
  theme_classic()+
  geom_point(aes(color=Location.x))+
  geom_smooth(aes(color=Location.x),
              method = 'lm',
              se = FALSE)
## `geom_smooth()` using formula = 'y ~ x'

# Duration

ggplot(ID.means,aes(x =Infection,y = Duration,group=Location.x))+
  theme_classic()+
  geom_point(aes(color=Location.x))+
  geom_smooth(aes(color=Location.x),
              method = 'lm',
              se = FALSE)
## `geom_smooth()` using formula = 'y ~ x'

# ICI

ggplot(ID.means,aes(x =Infection,y = ICI,group=Location.x))+
  theme_classic()+
  geom_point(aes(color=Location.x))+
  geom_smooth(aes(color=Location.x),
              method = 'lm',
              se = FALSE)
## `geom_smooth()` using formula = 'y ~ x'

# Freq

ggplot(ID.means,aes(x =Infection,y = Freq,group=Location.x))+
  theme_classic()+
  geom_point(aes(color=Location.x))+
  geom_smooth(aes(color=Location.x),
              method = 'lm',
              se = FALSE)
## `geom_smooth()` using formula = 'y ~ x'

# BW

ggplot(ID.means,aes(x =Infection,y = BW,group=Location.x))+
  theme_classic()+
  geom_point(aes(color=Location.x))+
  geom_smooth(aes(color=Location.x),
              method = 'lm',
              se = FALSE)
## `geom_smooth()` using formula = 'y ~ x'