---
title: "PACF Analysis"
author: "Evgeni Matveev"
date: '2020-10-30'
output:
  html_document:
    code_folding: hide
    theme: lumen
    toc: true
    toc_depth: 2
    number_sections: true
    toc_float:
      
      collapsed: false
      smooth_scroll: false
  pdf_document:
    toc: true
    toc_depth: 2
    number_sections: true
params:
  BothYearsAll: "BothYearsAll-v3.csv"
---
#Package Load
```{r Package Upload,message=FALSE}
#Delete the '#' if packages not yet installed on computer

#install.packages("ggplot2")
#install.packages("scales")
#install.packages("ggpubr")
#install.packages("tidyverse")
#install.packages("patchwork")
#install.packages("Hmisc")
#install.packages("changepoint")
#install.packages("kableExtra")
#install.packages("knitr")
#install.packages("formattable")
#install.packages("reshape2")
#install.packages("tinytex")
#tinytex::install_tinytex() #if this installation does not work, read section below in RStudio 
  

#UPLOAD LIBRARIES

#Graphing packages
library(ggplot2)
library(scales)
library(ggpubr)
library(patchwork)

#data tidying
library(dplyr)
library(tidyverse)
library(reshape2)

#data analysis
library(Hmisc)
library(changepoint)

#Rmarkdown assembly and formatting
library(rmarkdown)
library(knitr)
library(formattable)
library(kableExtra)
library(tinytex) #if this installation does not work, read section below in RStudio 
```

#Data Load and Format: 
```{R Load Data, include=TRUE}
#Upload Data from BothYearsAll-v3.csv
AllDataFull<-read.csv(params$BothYearsAll)

#Format timestamps to POSIXct
AllDataFull$Time<-as.POSIXct(AllDataFull$Time, format="%m/%d/%Y %H:%M:%S",tz="GMT")

#Reorder instruments by year
AllDataFull$Instrument <- factor(AllDataFull$Instrument, levels=c("T89_2013",
                                                                  "T1415_2013",
                                                                  "T1213_2013",
                                                                  "T89_2014",
                                                                  "T1011_2014",
                                                                  "T1011B_2014"))

#Subset Only Periods where probe was in osculum (QF=S)
AllData<-AllDataFull%>%subset(QF=="S")

#Ambient rounded to 1 cm/s intervals for binning
AllData$AmBin<-round(AllData$Am,0)

#Adding rolling median (180= 30-min median)
rollby<-180

AllData<-AllData%>%
  group_by(Instrument)%>%
  mutate(Ex_roll=rollapply(Ex,rollby,mean,align='center',fill=NA),
         Am_roll=rollapply(Am,rollby,mean,align='center',fill=NA),
         Oxy_roll=rollapply(Oxy,rollby,mean,align='center',fill=NA))
```

#Partial Autocorrelation Function 

##Ambient
```{r Ambient PACF}
# 1. AMBIENT

#Select entire section below to make pacf graphs for each instrument  
Instruments<-c("T89_2013","T1415_2013","T1213_2013","T89_2014","T1011_2014","T1011B_2014")
for (i in Instruments) {
  Inst<-AllData%>%
    subset(Instrument==i)
  
  pacf(Inst$Am,lag=100,main="",plot=T)
  title(paste(i," Ambient"),line=-2)
}

#At a lag of about 20 the pacf decreases below 0.05 in for all instruments for Ambient

```

##Excurrent
```{r Excurrent PACF}
# 2. EXCURRENT


#Select entire section below to make pacf graphs for each instrument  
Instruments<-c("T89_2013","T1415_2013","T1213_2013","T89_2014","T1011_2014","T1011B_2014")
for (i in Instruments) {
  Inst<-subset(AllData,Instrument==i)
  
  pacf(Inst$Ex,lag=100,main="",plot=T)
  title(paste(i," Excurrent"),line=-2)
}

#At a lag of about 20 the pacf decreases below 0.05 in for all instruments for Excurrent

```


##Oxygen Removal PACF
```{r Oxygen Removal PACF}
#. OXYGEN REMOVAL 

#Select entire section below to make pacf graphs for each instrument  
Instruments<-c("T89_2013","T1415_2013","T1213_2013","T89_2014","T1011_2014","T1011B_2014")
for (i in Instruments) {
  Inst<-subset(AllData,Instrument==i)
  
  pacf(Inst$Oxy,lag=100,main="",plot=T)
  title(paste(i," Oxy"),line=-2)
}

#At a lag of less than 20 the pacf decreases below 0.05 in for all instruments for Oxygen

# a subset of every 20 points was chosen for independent samples. 

```
