0.setup(package, directory)

library(readxl)
library(ggplot2)
library(rstatix)
library(tidyverse)
library(lawstat) ##levene test
library(ggpubr)
library(lme4)
library(lmerTest)


df<-read_xlsx("Homepen.xlsx")
df <- df %>% 
  mutate(df, 
         unique_pen=as.factor(paste(Replicate,Pen)),
         pen_pos=as.numeric(substr(df$Pen,3,3)),
         AB2=BN2+INC2,
         AB5=BN5+INC5) %>% 
  convert_as_factor(Replicate,Pen,Stress,unique_pen,Litter)


df2 <- read_xlsx("Homepen2.xlsx")
df2 <- df2 %>%  
  mutate(df2,
         AB=BN+INC,
         ID=as.factor(paste(Replicate,Pen,Pig)),
         unique_pen=as.factor(paste(Replicate,Pen))) %>%    
  convert_as_factor(Replicate,Pen,Litter,Pig,Stress,Age)

SV<-subset(df, Stress=="SV")
SR<-subset(df, Stress=="SR")

1. SV vs. SR all factors

# 1.1 Agonistic Behaviors(Day2)
# 1.1.1 FT
#boxplot
p <- ggplot(df, aes(x=Stress, y=FT2)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/Homogeneous of Variance/block o
levene.test(df$FT2,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$FT2
## Test Statistic = 0.60036, p-value = 0.4421
summary(aov(FT2~Stress+paste(unique_pen,Litter), data=df)) ## block combined
##                           Df   Sum Sq Mean Sq F value  Pr(>F)   
## Stress                     1    31853   31853   0.703 0.42341   
## paste(unique_pen, Litter) 41 10408197  253858   5.604 0.00469 **
## Residuals                  9   407663   45296                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
lmm<-lmer(FT2~Stress+(1|unique_pen)+(1|Litter),data=df)
summary(lmm)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: FT2 ~ Stress + (1 | unique_pen) + (1 | Litter)
##    Data: df
## 
## REML criterion at convergence: 753.4
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -1.82760 -0.36596 -0.06748  0.24232  2.35293 
## 
## Random effects:
##  Groups     Name        Variance Std.Dev.
##  Litter     (Intercept)  59370   243.7   
##  unique_pen (Intercept) 103277   321.4   
##  Residual                73994   272.0   
## Number of obs: 52, groups:  Litter, 26; unique_pen, 21
## 
## Fixed effects:
##             Estimate Std. Error     df t value Pr(>|t|)   
## (Intercept)   313.47     105.56  28.64   2.969  0.00598 **
## StressSV       50.43      89.92  17.83   0.561  0.58188   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##          (Intr)
## StressSV -0.433
# 1.1.2 AB(INC+BN)
#boxplot
p <- ggplot(df, aes(x=Stress, y=AB2)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/Homogeneous of Variance/lmm check
levene.test(df$AB2,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$AB2
## Test Statistic = 0.66537, p-value = 0.4185
summary(aov(AB2~Stress+paste(unique_pen,Litter), data=df))
##                           Df Sum Sq Mean Sq F value Pr(>F)
## Stress                     1    185  184.69   2.006  0.190
## paste(unique_pen, Litter) 41   7066  172.34   1.871  0.159
## Residuals                  9    829   92.09
lmm<-lmer(AB2~Stress+(1|unique_pen)+(1|Litter),data=df) #lmm check
summary(lmm)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: AB2 ~ Stress + (1 | unique_pen) + (1 | Litter)
##    Data: df
## 
## REML criterion at convergence: 385.8
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -1.6869 -0.4490 -0.0859  0.3565  3.2969 
## 
## Random effects:
##  Groups     Name        Variance Std.Dev.
##  Litter     (Intercept) 107.92   10.388  
##  unique_pen (Intercept)   0.00    0.000  
##  Residual                49.97    7.069  
## Number of obs: 52, groups:  Litter, 26; unique_pen, 21
## 
## Fixed effects:
##             Estimate Std. Error     df t value Pr(>|t|)    
## (Intercept)   10.346      2.464 34.079   4.198 0.000182 ***
## StressSV       3.769      1.961 25.000   1.922 0.066012 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##          (Intr)
## StressSV -0.398
## optimizer (nloptwrap) convergence code: 0 (OK)
## boundary (singular) fit: see help('isSingular')
# 1.2 Non-Agonistic Behaviors(Day2)
# 1.2.1 NC
#boxplot
p <- ggplot(df, aes(x=Stress, y=NC2)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/equal-variance/block o & stress o *****
levene.test(df$NC2,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$NC2
## Test Statistic = 0.74617, p-value = 0.3918
summary(aov(NC2~Stress+paste(unique_pen,Litter), data=df)) #block o & stress o *****
##                           Df Sum Sq Mean Sq F value Pr(>F)  
## Stress                     1   9694    9694   8.001 0.0198 *
## paste(unique_pen, Litter) 41 178396    4351   3.591 0.0230 *
## Residuals                  9  10905    1212                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
lmm<-lmer(NC2~Stress+(1|unique_pen)+(1|Litter),data=df)
summary(lmm)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: NC2 ~ Stress + (1 | unique_pen) + (1 | Litter)
##    Data: df
## 
## REML criterion at convergence: 547.6
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -1.37766 -0.51128 -0.04896  0.25844  3.15519 
## 
## Random effects:
##  Groups     Name        Variance Std.Dev.
##  Litter     (Intercept)  238     15.43   
##  unique_pen (Intercept) 2100     45.82   
##  Residual               1559     39.48   
## Number of obs: 52, groups:  Litter, 26; unique_pen, 21
## 
## Fixed effects:
##             Estimate Std. Error    df t value Pr(>|t|)    
## (Intercept)    87.18      13.73 29.45   6.348 5.74e-07 ***
## StressSV       16.32      12.76 21.33   1.278    0.215    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##          (Intr)
## StressSV -0.471
# 1.3 Daily Behaviors(Day2)
# 1.3.1 SLP -zero inflation
#boxplot
p <- ggplot(df, aes(x=Stress, y=SLP2)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/unequal-variance/too many zeros - anova block, factor o
levene.test(df$SLP2,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$SLP2
## Test Statistic = 5.3523, p-value = 0.02485
summary(aov(SLP2~Stress+paste(unique_pen,Litter), data=df)) ##unequal
##                           Df Sum Sq Mean Sq F value   Pr(>F)    
## Stress                     1    246   245.6   15.16 0.003656 ** 
## paste(unique_pen, Litter) 41   8711   212.5   13.12 0.000159 ***
## Residuals                  9    146    16.2                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
oneway.test(SLP2~Stress, data=df)
## 
##  One-way analysis of means (not assuming equal variances)
## 
## data:  SLP2 and Stress
## F = 1.3862, num df = 1.000, denom df = 25.094, p-value = 0.2501
# 1.3.2 SP 
#boxplot
p <- ggplot(df, aes(x=Stress, y=SP2)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/equal-variance/zero-inflated
levene.test(df$SP2,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$SP2
## Test Statistic = 1.5379, p-value = 0.2207
summary(aov(SP2~Stress+paste(unique_pen,Litter), data=df)) ## Stress, block o
##                           Df Sum Sq Mean Sq F value   Pr(>F)    
## Stress                     1   7416    7416   9.804   0.0121 *  
## paste(unique_pen, Litter) 41 622167   15175  20.061 2.63e-05 ***
## Residuals                  9   6808     756                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 1.3.3 FE 
#boxplot
p <- ggplot(df, aes(x=Stress, y=FE2)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/equal-variance/
levene.test(df$FE2,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$FE2
## Test Statistic = 2.9629, p-value = 0.09138
summary(aov(FE2~Stress+paste(unique_pen,Litter), data=df)) ## x
##                           Df   Sum Sq Mean Sq F value Pr(>F)  
## Stress                     1   119904  119904   0.204  0.662  
## paste(unique_pen, Litter) 41 64353598 1569600   2.674  0.059 .
## Residuals                  9  5281914  586879                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 1.3.4 LD 
#boxplot
p <- ggplot(df, aes(x=Stress, y=LD2)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/equal-variance/ 
levene.test(df$LD2,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$LD2
## Test Statistic = 0.0099597, p-value = 0.9209
summary(aov(LD2~Stress+paste(unique_pen,Litter), data=df)) # block o, Stress o
##                           Df    Sum Sq Mean Sq F value  Pr(>F)   
## Stress                     1   5235577 5235577   8.337 0.01796 * 
## paste(unique_pen, Litter) 41 156732131 3822735   6.087 0.00344 **
## Residuals                  9   5651786  627976                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 1.3.5 EX 
#boxplot
p <- ggplot(df, aes(x=Stress, y=EX2)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/equal-variance/
levene.test(df$EX2,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$EX2
## Test Statistic = 2.4928, p-value = 0.1207
summary(aov(EX2~Stress+paste(unique_pen,Litter), data=df)) #x
##                           Df   Sum Sq Mean Sq F value Pr(>F)
## Stress                     1  1151176 1151176   0.826  0.387
## paste(unique_pen, Litter) 41 62053007 1513488   1.086  0.483
## Residuals                  9 12538618 1393180
# 1.3.6 DR 
#boxplot
p <- ggplot(df, aes(x=Stress, y=DR2)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/equal-variance/ block,Stress x
levene.test(df$DR2,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$DR2
## Test Statistic = 0.32203, p-value = 0.5729
summary(aov(DR2~Stress+paste(unique_pen,Litter), data=df)) #x
##                           Df Sum Sq Mean Sq F value Pr(>F)
## Stress                     1   32.3   32.33   0.256  0.625
## paste(unique_pen, Litter) 41 2668.5   65.09   0.515  0.928
## Residuals                  9 1137.8  126.42
##################################################

# 1.4 Agonistic Behaviors(Day5)
# 1.4.1 FT
#boxplot
p <- ggplot(df, aes(x=Stress, y=FT5)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/equal-variance/
levene.test(df$FT5,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$FT5
## Test Statistic = 0.15955, p-value = 0.6913
summary(aov(FT5~Stress+paste(unique_pen,Litter), data=df)) ## stress,block o
##                           Df Sum Sq Mean Sq F value Pr(>F)  
## Stress                     1   3233    3233   7.262 0.0246 *
## paste(unique_pen, Litter) 41  77660    1894   4.255 0.0128 *
## Residuals                  9   4006     445                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 1.4.2 AB
#boxplot
p <- ggplot(df, aes(x=Stress, y=AB5)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/equal-variance/
levene.test(df$FT5,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$FT5
## Test Statistic = 0.15955, p-value = 0.6913
summary(aov(AB5~Stress+paste(unique_pen,Litter), data=df)) ## x
##                           Df Sum Sq Mean Sq F value Pr(>F)  
## Stress                     1    277  276.92   3.087 0.1128  
## paste(unique_pen, Litter) 41   9171  223.67   2.493 0.0727 .
## Residuals                  9    807   89.72                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 1.5 Non-Agonistic Behaviors(Day5)
# 1.5.1 NC
#boxplot
p <- ggplot(df, aes(x=Stress, y=NC5)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/equal-variance/
levene.test(df$NC5,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$NC5
## Test Statistic = 2.2443, p-value = 0.1404
summary(aov(NC5~Stress+paste(unique_pen,Litter), data=df)) #x
##                           Df Sum Sq Mean Sq F value Pr(>F)  
## Stress                     1    438   438.5   0.542 0.4803  
## paste(unique_pen, Litter) 41  73996  1804.8   2.231 0.0999 .
## Residuals                  9   7280   808.9                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 1.6 Daily Behaviors(Day2)
# 1.6.1 SLP -zero inflation
#boxplot
p <- ggplot(df, aes(x=Stress, y=SLP5)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/unequal-variance/too many zeros
levene.test(df$SLP5,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$SLP5
## Test Statistic = 10.203, p-value = 0.002428
summary(aov(SLP5~Stress+paste(unique_pen,Litter), data=df)) ##unequal
##                           Df Sum Sq Mean Sq F value Pr(>F)  
## Stress                     1   96.9   96.94   4.340 0.0669 .
## paste(unique_pen, Litter) 41 1513.0   36.90   1.652 0.2149  
## Residuals                  9  201.1   22.34                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
oneway.test(SLP5~Stress, data=df) # x
## 
##  One-way analysis of means (not assuming equal variances)
## 
## data:  SLP5 and Stress
## F = 2.8279, num df = 1.000, denom df = 28.682, p-value = 0.1035
# 1.6.2 SP - zero inflated
#boxplot
p <- ggplot(df, aes(x=Stress, y=SP5)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/equal-variance
levene.test(df$SP5,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$SP5
## Test Statistic = 2.1816, p-value = 0.1459
summary(aov(SP5~Stress+paste(unique_pen,Litter), data=df)) #Stress, block 
##                           Df Sum Sq Mean Sq F value   Pr(>F)    
## Stress                     1    746   746.3   20.48  0.00144 ** 
## paste(unique_pen, Litter) 41  54744  1335.2   36.63 1.94e-06 ***
## Residuals                  9    328    36.4                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 1.6.3 FE 
#boxplot
p <- ggplot(df, aes(x=Stress, y=FE5)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/equal-variance/
levene.test(df$FE5,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$FE5
## Test Statistic = 3.7686, p-value = 0.05787
summary(aov(FE5~Stress+paste(unique_pen,Litter), data=df)) #x
##                           Df   Sum Sq Mean Sq F value Pr(>F)
## Stress                     1   497646  497646   0.327  0.582
## paste(unique_pen, Litter) 41 45760767 1116116   0.733  0.764
## Residuals                  9 13707893 1523099
# 1.6.4 LD 
#boxplot
p <- ggplot(df, aes(x=Stress, y=LD5)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/equal-variance/block o
levene.test(df$LD5,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$LD5
## Test Statistic = 0.62986, p-value = 0.4312
summary(aov(LD5~Stress+paste(unique_pen,Litter), data=df)) # x
##                           Df    Sum Sq Mean Sq F value Pr(>F)  
## Stress                     1    772261  772261   0.328 0.5811  
## paste(unique_pen, Litter) 41 219313264 5349104   2.269 0.0953 .
## Residuals                  9  21217917 2357546                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# 1.6.5 EX 
#boxplot
p <- ggplot(df, aes(x=Stress, y=EX5)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/equal-variance/block o
levene.test(df$EX5,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$EX5
## Test Statistic = 0.83665, p-value = 0.3647
summary(aov(EX5~Stress+paste(unique_pen,Litter), data=df)) #x
##                           Df   Sum Sq Mean Sq F value Pr(>F)
## Stress                     1    18545   18545   0.044  0.838
## paste(unique_pen, Litter) 41 23769241  579738   1.387  0.313
## Residuals                  9  3761363  417929
# 1.6.6 DR 
#boxplot
p <- ggplot(df, aes(x=Stress, y=DR5)) + 
  geom_boxplot(outlier.colour="red", outlier.shape=8,
               outlier.size=2)
p

#blocked anova test/equal-variance/
levene.test(df$DR5,df$Stress, location="mean")
## 
##  Classical Levene's test based on the absolute deviations from the mean
##  ( none not applied because the location is not set to median )
## 
## data:  df$DR5
## Test Statistic = 0.039832, p-value = 0.8426
summary(aov(DR5~Stress+paste(unique_pen,Litter), data=df)) #x
##                           Df Sum Sq Mean Sq F value Pr(>F)  
## Stress                     1    0.1    0.08   0.003 0.9591  
## paste(unique_pen, Litter) 41 2655.8   64.78   2.337 0.0877 .
## Residuals                  9  249.5   27.72                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

2. SV vs. SR all factors with ages

# 2.1 
# 2.1.1 FT *** interaction x, stress x, block o, age o
summary(aov(FT~Stress*Age+paste(unique_pen,Litter)+Error(ID/Age),data=df2)) 
## 
## Error: ID
##                           Df  Sum Sq Mean Sq F value  Pr(>F)   
## Stress                     1   27690   27690   1.092 0.32338   
## paste(unique_pen, Litter) 41 5450083  132929   5.240 0.00603 **
## Residuals                  9  228312   25368                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: ID:Age
##            Df  Sum Sq Mean Sq F value   Pr(>F)    
## Age         1 2024145 2024145  19.392 5.61e-05 ***
## Stress:Age  1    7395    7395   0.071    0.791    
## Residuals  50 5219130  104383                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
interaction.plot(df2$Age,df2$Stress,df2$FT,
                 fun=mean,type="b",pch=c(2,4),col=c(3,4),xlab="Time",ylab="Mean of FT", trace.label = "Stress", legend = F) 
legend("topright",legend=c("SR","SV"),pch=c(2,4),col=c(3,4), bg="gray90")

ggplot(df2, aes(x=Stress, y=FT, color=as.factor(Age)))+
  geom_boxplot()+
  ggtitle("FT distribution by Stress and Age")+
  theme(plot.title=element_text(face="bold", hjust=0.5))+
  scale_color_discrete(name="Age")

# 2.1.2 INC *** interaction x, stress x, block o, age x
summary(aov(AB~Stress*Age+paste(unique_pen,Litter)+Error(ID/Age),data=df2)) 
## 
## Error: ID
##                           Df Sum Sq Mean Sq F value Pr(>F)  
## Stress                     1      5    4.65   0.062 0.8085  
## paste(unique_pen, Litter) 41  11013  268.61   3.595 0.0229 *
## Residuals                  9    673   74.72                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: ID:Age
##            Df Sum Sq Mean Sq F value Pr(>F)  
## Age         1     42    41.9   0.338 0.5633  
## Stress:Age  1    457   457.0   3.693 0.0604 .
## Residuals  50   6187   123.7                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
interaction.plot(df2$Age,df2$Stress,df2$INC,
                 fun=mean,type="b",pch=c(2,4),col=c(3,4),xlab="Time",ylab="Mean of INC", trace.label = "Stress", legend = F) 
legend("topleft",legend=c("SR","SV"),pch=c(2,4),col=c(3,4), bg="gray90")

ggplot(df2, aes(x=Stress, y=INC, color=as.factor(Age)))+
  geom_boxplot()+
  ggtitle("INC distribution by Stress and Age")+
  theme(plot.title=element_text(face="bold", hjust=0.5))+
  scale_color_discrete(name="Age")

# 2.2
# 2.2.1 NC *interaction x, stress o, block o, age o
summary(aov(NC~Stress*Age+paste(unique_pen,Litter)+Error(ID/Age),data=df2)) 
## 
## Error: ID
##                           Df Sum Sq Mean Sq F value  Pr(>F)   
## Stress                     1   7128    7128  12.193 0.00681 **
## paste(unique_pen, Litter) 41 168461    4109   7.028 0.00198 **
## Residuals                  9   5261     585                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: ID:Age
##            Df Sum Sq Mean Sq F value Pr(>F)  
## Age         1  12916   12916   6.668 0.0128 *
## Stress:Age  1   3005    3005   1.551 0.2188  
## Residuals  50  96855    1937                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
interaction.plot(df2$Age,df2$Stress,df2$NC,
                 fun=mean,type="b",pch=c(2,4),col=c(3,4),xlab="Time",ylab="Mean of NC", trace.label = "Stress", legend = F) 
legend("topright",legend=c("SR","SV"),pch=c(2,4),col=c(3,4), bg="gray90")

ggplot(df2, aes(x=Stress, y=NC, color=as.factor(Age)))+
  geom_boxplot()+
  ggtitle("NC distribution by Stress and Age")+
  theme(plot.title=element_text(face="bold", hjust=0.5))+
  scale_color_discrete(name="Age")

#2.3
#2.3.1 SLP *interaction x, stress x, block o, age x - zero-inflated
summary(aov(SLP~Stress*Age+paste(unique_pen,Litter)+Error(ID/Age),data=df2)) 
## 
## Error: ID
##                           Df Sum Sq Mean Sq F value  Pr(>F)   
## Stress                     1     17   16.96   0.786 0.39828   
## paste(unique_pen, Litter) 41   5037  122.86   5.696 0.00442 **
## Residuals                  9    194   21.57                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: ID:Age
##            Df Sum Sq Mean Sq F value Pr(>F)  
## Age         1      5     4.7   0.044  0.835  
## Stress:Age  1    326   325.5   3.048  0.087 .
## Residuals  50   5340   106.8                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
interaction.plot(df2$Age,df2$Stress,df2$SLP,
                 fun=mean,type="b",pch=c(2,4),col=c(3,4),xlab="Time",ylab="Mean of SLP", trace.label = "Stress", legend = F) 
legend("topright",legend=c("SR","SV"),pch=c(2,4),col=c(3,4), bg="gray90")

ggplot(df2, aes(x=Stress, y=SLP, color=as.factor(Age)))+
  geom_boxplot()+
  ggtitle("SLP distribution by Stress and Age")+
  theme(plot.title=element_text(face="bold", hjust=0.5))+
  scale_color_discrete(name="Age")

#2.3.2 SP *interaction x, stress o, block o, age x - zero-inflated
summary(aov(SP~Stress*Age+paste(unique_pen,Litter)+Error(ID/Age),data=df2)) 
## 
## Error: ID
##                           Df Sum Sq Mean Sq F value   Pr(>F)    
## Stress                     1   6434    6434   11.44   0.0081 ** 
## paste(unique_pen, Litter) 41 428068   10441   18.56 3.67e-05 ***
## Residuals                  9   5062     562                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: ID:Age
##            Df Sum Sq Mean Sq F value Pr(>F)  
## Age         1  17889   17889   3.565 0.0648 .
## Stress:Age  1   1729    1729   0.344 0.5599  
## Residuals  50 250917    5018                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
interaction.plot(df2$Age,df2$Stress,df2$SP,
                 fun=mean,type="b",pch=c(2,4),col=c(3,4),xlab="Time",ylab="Mean of SP", trace.label = "Stress", legend = F) 
legend("topright",legend=c("SR","SV"),pch=c(2,4),col=c(3,4), bg="gray90")

ggplot(df2, aes(x=Stress, y=SP, color=as.factor(Age)))+
  geom_boxplot()+
  ggtitle("SP distribution by Stress and Age")+
  theme(plot.title=element_text(face="bold", hjust=0.5))+
  scale_color_discrete(name="Age")

#2.3.3 FE *interaction x, stress x, block x, age o
summary(aov(FE~Stress*Age+paste(unique_pen,Litter)+Error(ID/Age),data=df2)) 
## 
## Error: ID
##                           Df   Sum Sq Mean Sq F value Pr(>F)
## Stress                     1   553049  553049   0.374  0.556
## paste(unique_pen, Litter) 41 58410600 1424649   0.963  0.573
## Residuals                  9 13315160 1479462               
## 
## Error: ID:Age
##            Df   Sum Sq  Mean Sq F value  Pr(>F)   
## Age         1 10261521 10261521   8.942 0.00432 **
## Stress:Age  1    64501    64501   0.056 0.81356   
## Residuals  50 57378412  1147568                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
interaction.plot(df2$Age,df2$Stress,df2$FE,
                 fun=mean,type="b",pch=c(2,4),col=c(3,4),xlab="Time",ylab="Mean of FE", trace.label = "Stress", legend = F) 
legend("topleft",legend=c("SR","SV"),pch=c(2,4),col=c(3,4), bg="gray90")

ggplot(df2, aes(x=Stress, y=FE, color=as.factor(Age)))+
  geom_boxplot()+
  ggtitle("FE distribution by Stress and Age")+
  theme(plot.title=element_text(face="bold", hjust=0.5))+
  scale_color_discrete(name="Age")

#2.3.4 LD *interaction x, stress x, block o, age o
summary(aov(LD~Stress*Age+paste(unique_pen,Litter)+Error(ID/Age),data=df2)) 
## 
## Error: ID
##                           Df    Sum Sq Mean Sq F value Pr(>F)  
## Stress                     1   5014698 5014698   3.441 0.0966 .
## paste(unique_pen, Litter) 41 231991238 5658323   3.882 0.0176 *
## Residuals                  9  13116668 1457408                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: ID:Age
##            Df    Sum Sq  Mean Sq F value   Pr(>F)    
## Age         1  51756891 51756891  16.399 0.000178 ***
## Stress:Age  1    993140   993140   0.315 0.577335    
## Residuals  50 157807192  3156144                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
interaction.plot(df2$Age,df2$Stress,df2$LD,
                 fun=mean,type="b",pch=c(2,4),col=c(3,4),xlab="Time",ylab="Mean of LD", trace.label = "Stress", legend = F) 
legend("topleft",legend=c("SR","SV"),pch=c(2,4),col=c(3,4), bg="gray90")

ggplot(df2, aes(x=Stress, y=LD, color=as.factor(Age)))+
  geom_boxplot()+
  ggtitle("LD distribution by Stress and Age")+
  theme(plot.title=element_text(face="bold", hjust=0.5))+
  scale_color_discrete(name="Age")

#2.3.5 EX *interaction x, stress x, block x, age o
summary(aov(EX~Stress*Age+paste(unique_pen,Litter)+Error(ID/Age),data=df2)) 
## 
## Error: ID
##                           Df   Sum Sq Mean Sq F value Pr(>F)
## Stress                     1   730971  730971   0.866  0.376
## paste(unique_pen, Litter) 41 60268276 1469958   1.741  0.190
## Residuals                  9  7598516  844280               
## 
## Error: ID:Age
##            Df   Sum Sq Mean Sq F value   Pr(>F)    
## Age         1  9955378 9955378   14.53 0.000379 ***
## Stress:Age  1   438750  438750    0.64 0.427349    
## Residuals  50 34255437  685109                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
interaction.plot(df2$Age,df2$Stress,df2$EX,
                 fun=mean,type="b",pch=c(2,4),col=c(3,4),xlab="Time",ylab="Mean of EX", trace.label = "Stress", legend = F) 
legend("topright",legend=c("SR","SV"),pch=c(2,4),col=c(3,4), bg="gray90")

ggplot(df2, aes(x=Stress, y=EX, color=as.factor(Age)))+
  geom_boxplot()+
  ggtitle("EX distribution by Stress and Age")+
  theme(plot.title=element_text(face="bold", hjust=0.5))+
  scale_color_discrete(name="Age")

#2.3.6 DR *interaction x, stress x, block x, age o
summary(aov(DR~Stress*Age+paste(unique_pen,Litter)+Error(ID/Age),data=df2)) 
## 
## Error: ID
##                           Df Sum Sq Mean Sq F value Pr(>F)
## Stress                     1     18   17.78   0.250  0.629
## paste(unique_pen, Litter) 41   3539   86.33   1.212  0.403
## Residuals                  9    641   71.23               
## 
## Error: ID:Age
##            Df Sum Sq Mean Sq F value   Pr(>F)    
## Age         1 1211.8  1211.8  23.938 1.08e-05 ***
## Stress:Age  1   14.6    14.6   0.289    0.593    
## Residuals  50 2531.1    50.6                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
interaction.plot(df2$Age,df2$Stress,df2$DR,
                 fun=mean,type="b",pch=c(2,4),col=c(3,4),xlab="Time",ylab="Mean of DR", trace.label = "Stress", legend = F) 
legend("topright",legend=c("SR","SV"),pch=c(2,4),col=c(3,4), bg="gray90")

ggplot(df2, aes(x=Stress, y=DR, color=as.factor(Age)))+
  geom_boxplot()+
  ggtitle("DR distribution by Stress and Age")+
  theme(plot.title=element_text(face="bold", hjust=0.5))+
  scale_color_discrete(name="Age")