---
title: "Max calcualted using first3"
output: html_notebook
---
This is the code for the Linear Mixed Model for the MMR_3 and AS_3 calculated from the first three MO2 measures following loading the fish into the respirometer 
```{r}
#load AutoResp_MMR excel AutoResp_MMR
library(lme4)
library(emmeans)
library(car)
library(outliers)
AutoResp_MMR_update <- read_excel("AutoResp_MMR_update.xlsx")
AutoResp_MMR_update$Treatment <- factor(AutoResp_MMR_update$Treatment, levels =  c("No Chase","Chase","Chase+Air"))

```


```{r}
#MMR of first three MO2 measures following loading
#create model
repeated.first3<-lmer(formula = `MMR_first3`~Order+Treatment+(1|fishID),data = AutoResp_MMR_update, REML = TRUE)
repeated.first3
summary(repeated.first3)

#check for normality
qqPlot(AutoResp_MMR_update$MMR_first3)
plot(fitted(repeated.first3),residuals(repeated.first3), abline(h=0))
hist(residuals(repeated.first3)) 
AutoResp_MMR_update$residus.first3 <- residuals(repeated.first3)
shapiro.test(AutoResp_MMR_update$residus.first3)
leveneTest(MMR_first3 ~ Treatment, data = AutoResp_MMR_update) 
leveneTest(MMR_first3 ~ Order, data = AutoResp_MMR_update) 
grubbs.test(AutoResp_MMR_update$residus.first3) 

#calculate the analysis of variance table for F-tests 
Anova(repeated.first3, test.statistic="F")
#calculate the means of the Treatments 
moy_var.first3 <- lsmeans(repeated.first3, ~Treatment)
moy_var.first3
#pairwise comparisons
pairs(moy_var.first3, adjust="tukey") 

((248-281)/281) *100 
#these values are coming from moy_var.first3 above
---- #so chase is underestimating the MMR of our fish by aprox 11.7%
((258-281)/281)*100 
##so chase + air is underestimating the MMR of our fish by aprox 8.2%
```


```{r}
#AS from Maxfirst3
#create model
repeated.AS3<-lmer(formula = AS.first3~Order+Treatment+(1|fishID),data = AutoResp_MMR_update, REML = TRUE)
repeated.AS3
summary(repeated.AS3)

#check for normality
plot(fitted(repeated.AS3),residuals(repeated.AS3), abline(h=0))
hist(residuals(repeated.AS3))
AutoResp_MMR_update$residus.AS3 <- residuals(repeated.AS3)
grubbs.test(AutoResp_MMR_update$residus.AS3) 
#calculate the analysis of variance table for F-tests
Anova(repeated.AS3, test.statistic="F")
#calculate the means of the Treatments - these means are adjusted 
moy_var.AS3 <- lsmeans(repeated.AS3, ~Treatment) 
moy_var.AS3
#pairwise comparisons
pairs(moy_var.AS3, adjust="tukey")


((163-192)/192) *100 
#so chase is underestimating the MMR of our fish by aprox 15%
((164-192)/192)*100 
#so chase + air is underestimating the MMR of our fish by aprox 15%
  
```

