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$Temp, data)$coefficients[2] # Calculate m by doing a linear regression 
    Tcorrected_matrix[, i] <- data[,col_numbers[i]] - m * (data$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

FIRST - averaging means for males

means <- manual.data %>% 
  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 <- as.data.frame(means)

NEXT - run the temperature correction

# Set Values that you want to temperature correct for
selections <- c(5:9)
# Do the temperature correction to the target temp, aka 16
result <- Tcorr(selections, target_temp = 16, data = means)
# Format results as a dataframe
Tdat <- as.data.frame(result$Tcorrected_matrix)
# Add information such as ID, Location, Temp, and Log Infection
Tdat<-cbind(means[,c(1,2,3,4)], Tdat)

Plots for Checking Work

# Plots to check work - Call Rate Inst
plot(means$Call_Rate_Inst~means$Temp)

plot(Tdat$Call_Rate_Inst~means$Temp)

# Plots to check work - Call Duration
plot(means$Duration~means$Temp)

plot(Tdat$Duration~means$Temp)

# Plots to check work - ICI
plot(means$ICI~means$Temp)

plot(Tdat$ICI ~ means$Temp)

# Plots to check work - Peak Freq
plot(means$Freq~means$Temp)

plot(Tdat$Freq ~ means$Temp)

Table of Results

cvs <- manual.data %>% 
  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.111   0.0544 0.177  0.150 0.0225 
##  2 PC-0… FR             0       NaN          0.179   0.0765 0.343  0.286 0.0120 
##  3 PC-0… CV             0         0          0.146   0.0672 0.226  0.200 0.00874
##  4 PC-0… CV             0         0          0.175   0.0534 0.301  0.248 0.00570
##  5 PC-0… CV             0         0          0.205   0.0627 0.271  0.230 0.0187 
##  6 PC-0… CV             0       NaN          0.172   0.0748 0.284  0.238 0.0120 
##  7 PC-0… FR             0       NaN          0.186   0.0519 0.244  0.220 0.0136 
##  8 PC-0… CV             0       NaN          0.211   0.0260 0.277  0.249 0.0111 
##  9 PC-0… FR             0         0          0.127   0.0623 0.176  0.154 0      
## 10 PC-0… FR             0         0          0.143   0.0374 0.206  0.173 0.0147 
## # ℹ 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.200   0.0968 
## 2 Duration       0.0707  0.0315 
## 3 ICI            0.442   0.379  
## 4 Period         0.393   0.352  
## 5 Freq           0.0114  0.00717
## 6 Freq_5         0.00749 0.00726
## 7 Freq_95        0.00759 0.00611
## 8 BW             0.113   0.0640
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")+
  scale_fill_brewer(palette = "Set2")

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

GRAPHS

Tdat <- Tdat %>% mutate(Prevalence = case_when(Infection == 0 ~ 'Absent',
                                               Infection != 0 ~ 'Present'))
write.csv(Tdat,"peeper_infection_by_ID.csv",row.names=FALSE)
TdatLoad <- Tdat %>% filter(Prevalence=='Present')

ID.means.load <- TdatLoad %>% 
  group_by(ID, Location.x) %>%
  summarize(Temp = mean(Temp),
            Infection = mean(Infection),
            Call_Rate_Inst = mean(Call_Rate_Inst),
            Duration = mean(Duration),
            ICI = mean(ICI),
            Freq = mean(Freq),
            BW = mean(BW))
## `summarise()` has grouped output by 'ID'. You can override using the `.groups`
## argument.
# Plots are in fact 9 Absent and 21 Present for 
ggplot(Tdat,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(Tdat,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(Tdat,aes(x=Prevalence,y=Duration))+
  facet_wrap(~Location.x,strip.position="bottom")+
  geom_boxplot()+
  geom_jitter(position=position_jitter(0.1))+
  theme_classic()

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

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

ggplot(Tdat,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

library(RColorBrewer)

# Call Rate Inst
ggplot(Tdat,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)+
  scale_color_brewer(palette = "Set2")
## `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)+
  scale_color_brewer(palette = "Set2")
## `geom_smooth()` using formula = 'y ~ x'

# Duration

ggplot(Tdat,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)+
  scale_color_brewer(palette = "Set2")
## `geom_smooth()` using formula = 'y ~ x'

# ICI

ggplot(Tdat,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)+
  scale_color_brewer(palette = "Set2")
## `geom_smooth()` using formula = 'y ~ x'

# Freq

ggplot(Tdat,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)+
  scale_color_brewer(palette = "Set2")
## `geom_smooth()` using formula = 'y ~ x'

# BW

ggplot(Tdat,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)+
  scale_color_brewer(palette = "Set2")
## `geom_smooth()` using formula = 'y ~ x'