library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0      ✔ purrr   0.3.4 
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.2.1      ✔ stringr 1.4.1 
✔ readr   2.1.2      ✔ forcats 0.5.2 
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(ggpubr)
library(rstatix)

Attaching package: 'rstatix'

The following object is masked from 'package:stats':

    filter
library(readxl)
library(readxl)
ENDOPHYTES <- read_excel("D:/MARV BS MATH/4th year, 2nd sem/JOLINA/ENDOPHYTES.xlsx", 
    sheet = "SAureus(Endo)")
ENDOPHYTES
# A tibble: 56 × 5
   Hour              Concentration SAureusPositiveControl   SAureus SAureusGro…¹
   <chr>             <chr>                          <dbl>     <dbl>        <dbl>
 1 Pre-incubation    2- 128 mg/ml                  0.0377 -0.00300        0.037 
 2 3-hrs Incubation  3- 64mg/ml                    0.0373  0.012          0.0707
 3 6-hrs Incubation  4- 32mg/ml                    0.0373  0.0553         0.114 
 4 9-hrs Incubation  5-16mg/ml                     0.0365  0.076          0.131 
 5 12-hrs Incubation 6-8mg/ml                      0.0368  0.096          0.145 
 6 15-hrs Incubation 7-4mg/ml                      0.036   0.114          0.164 
 7 18-hrs Incubation 8-2mg/ml                      0.0345  0.124          0.180 
 8 Pre-incubation    2- 128 mg/ml                  0.0377 -0.000667       0.037 
 9 3-hrs Incubation  3- 64mg/ml                    0.0373  0.022          0.0707
10 6-hrs Incubation  4- 32mg/ml                    0.0373  0.0593         0.114 
# … with 46 more rows, and abbreviated variable name ¹​SAureusGrowthControl
head(ENDOPHYTES,7)
# A tibble: 7 × 5
  Hour              Concentration SAureusPositiveControl  SAureus SAureusGrowt…¹
  <chr>             <chr>                          <dbl>    <dbl>          <dbl>
1 Pre-incubation    2- 128 mg/ml                  0.0377 -0.00300         0.037 
2 3-hrs Incubation  3- 64mg/ml                    0.0373  0.012           0.0707
3 6-hrs Incubation  4- 32mg/ml                    0.0373  0.0553          0.114 
4 9-hrs Incubation  5-16mg/ml                     0.0365  0.076           0.131 
5 12-hrs Incubation 6-8mg/ml                      0.0368  0.096           0.145 
6 15-hrs Incubation 7-4mg/ml                      0.036   0.114           0.164 
7 18-hrs Incubation 8-2mg/ml                      0.0345  0.124           0.180 
# … with abbreviated variable name ¹​SAureusGrowthControl
str(ENDOPHYTES)
## tibble [56 × 5] (S3: tbl_df/tbl/data.frame)
##  $ Hour                  : chr [1:56] "Pre-incubation" "3-hrs Incubation" "6-hrs Incubation" "9-hrs Incubation" ...
##  $ Concentration         : chr [1:56] "2- 128 mg/ml" "3- 64mg/ml" "4- 32mg/ml" "5-16mg/ml" ...
##  $ SAureusPositiveControl: num [1:56] 0.0377 0.0373 0.0373 0.0365 0.0368 ...
##  $ SAureus               : num [1:56] -0.003 0.012 0.0553 0.076 0.096 ...
##  $ SAureusGrowthControl  : num [1:56] 0.037 0.0707 0.1137 0.1313 0.1447 ...
summary(ENDOPHYTES)
     Hour           Concentration      SAureusPositiveControl
 Length:56          Length:56          Min.   :0.03450       
 Class :character   Class :character   1st Qu.:0.03600       
 Mode  :character   Mode  :character   Median :0.03683       
                                       Mean   :0.03660       
                                       3rd Qu.:0.03733       
                                       Max.   :0.03767       
    SAureus         SAureusGrowthControl
 Min.   :-0.00300   Min.   :0.03700     
 1st Qu.: 0.02025   1st Qu.:0.07067     
 Median : 0.06317   Median :0.13133     
 Mean   : 0.06045   Mean   :0.12029     
 3rd Qu.: 0.09283   3rd Qu.:0.16433     
 Max.   : 0.13000   Max.   :0.18033     
TT<-ENDOPHYTES%>%
  gather(key = "Control", value = SAureus,'SAureusGrowthControl', 'SAureusGrowthControl') %>%
  convert_as_factor(Hour, Concentration)
TT
# A tibble: 56 × 5
   Hour              Concentration SAureusPositiveControl Control        SAureus
   <fct>             <fct>                          <dbl> <chr>            <dbl>
 1 Pre-incubation    2- 128 mg/ml                  0.0377 SAureusGrowth…  0.037 
 2 3-hrs Incubation  3- 64mg/ml                    0.0373 SAureusGrowth…  0.0707
 3 6-hrs Incubation  4- 32mg/ml                    0.0373 SAureusGrowth…  0.114 
 4 9-hrs Incubation  5-16mg/ml                     0.0365 SAureusGrowth…  0.131 
 5 12-hrs Incubation 6-8mg/ml                      0.0368 SAureusGrowth…  0.145 
 6 15-hrs Incubation 7-4mg/ml                      0.036  SAureusGrowth…  0.164 
 7 18-hrs Incubation 8-2mg/ml                      0.0345 SAureusGrowth…  0.180 
 8 Pre-incubation    2- 128 mg/ml                  0.0377 SAureusGrowth…  0.037 
 9 3-hrs Incubation  3- 64mg/ml                    0.0373 SAureusGrowth…  0.0707
10 6-hrs Incubation  4- 32mg/ml                    0.0373 SAureusGrowth…  0.114 
# … with 46 more rows
plot.design(SAureus  ~ ., data = TT)

Summary Statistics

SAureus by Hour

TT %>%
  group_by(Hour) %>%
   get_summary_stats(SAureus, type = "mean_sd")
# A tibble: 7 × 5
  Hour              variable     n  mean    sd
  <fct>             <fct>    <dbl> <dbl> <dbl>
1 12-hrs Incubation SAureus      8 0.145     0
2 15-hrs Incubation SAureus      8 0.164     0
3 18-hrs Incubation SAureus      8 0.18      0
4 3-hrs Incubation  SAureus      8 0.071     0
5 6-hrs Incubation  SAureus      8 0.114     0
6 9-hrs Incubation  SAureus      8 0.131     0
7 Pre-incubation    SAureus      8 0.037     0

SAureus by Concentration

TT %>%
  group_by(Concentration) %>%
   get_summary_stats(SAureus, type = "mean_sd")
# A tibble: 7 × 5
  Concentration variable     n  mean    sd
  <fct>         <fct>    <dbl> <dbl> <dbl>
1 2- 128 mg/ml  SAureus      8 0.037     0
2 3- 64mg/ml    SAureus      8 0.071     0
3 4- 32mg/ml    SAureus      8 0.114     0
4 5-16mg/ml     SAureus      8 0.131     0
5 6-8mg/ml      SAureus      8 0.145     0
6 7-4mg/ml      SAureus      8 0.164     0
7 8-2mg/ml      SAureus      8 0.18      0

SAureus by SAureusGrowthControl

TT %>%
  group_by(ENDOPHYTES$SAureusGrowthControl) %>%
   get_summary_stats(SAureus, type = "mean_sd")
# A tibble: 7 × 5
  `ENDOPHYTES$SAureusGrowthControl` variable     n  mean    sd
                              <dbl> <fct>    <dbl> <dbl> <dbl>
1                            0.037  SAureus      8 0.037     0
2                            0.0707 SAureus      8 0.071     0
3                            0.114  SAureus      8 0.114     0
4                            0.131  SAureus      8 0.131     0
5                            0.145  SAureus      8 0.145     0
6                            0.164  SAureus      8 0.164     0
7                            0.180  SAureus      8 0.18      0

SAureus by SAureusPositiveControl

TT %>%
  group_by(ENDOPHYTES$SAureusPositiveControl) %>%
   get_summary_stats(SAureus, type = "mean_sd")
# A tibble: 6 × 5
  `ENDOPHYTES$SAureusPositiveControl` variable     n  mean    sd
                                <dbl> <fct>    <dbl> <dbl> <dbl>
1                              0.0345 SAureus      8 0.18  0    
2                              0.036  SAureus      8 0.164 0    
3                              0.0365 SAureus      8 0.131 0    
4                              0.0368 SAureus      8 0.145 0    
5                              0.0373 SAureus     16 0.092 0.022
6                              0.0377 SAureus      8 0.037 0    

SAureus by Control

TT %>%
  group_by(ENDOPHYTES$Control) %>%
   get_summary_stats(SAureus, type = "mean_sd")
Warning: Unknown or uninitialised column: `Control`.
# A tibble: 1 × 4
  variable     n  mean    sd
  <fct>    <dbl> <dbl> <dbl>
1 SAureus     56  0.12 0.048

SAureus by Concentration and Hour

TT %>%
  group_by(ENDOPHYTES$Concentration, ENDOPHYTES$Hour) %>%
   get_summary_stats(SAureus, type = "mean_sd")
# A tibble: 7 × 6
  `ENDOPHYTES$Concentration` `ENDOPHYTES$Hour` variable     n  mean    sd
  <chr>                      <chr>             <fct>    <dbl> <dbl> <dbl>
1 2- 128 mg/ml               Pre-incubation    SAureus      8 0.037     0
2 3- 64mg/ml                 3-hrs Incubation  SAureus      8 0.071     0
3 4- 32mg/ml                 6-hrs Incubation  SAureus      8 0.114     0
4 5-16mg/ml                  9-hrs Incubation  SAureus      8 0.131     0
5 6-8mg/ml                   12-hrs Incubation SAureus      8 0.145     0
6 7-4mg/ml                   15-hrs Incubation SAureus      8 0.164     0
7 8-2mg/ml                   18-hrs Incubation SAureus      8 0.18      0

SAureus by Concentration and Control

TT %>%
  group_by(ENDOPHYTES$Concentration, Control) %>%
   get_summary_stats(SAureus, type = "mean_sd")
# A tibble: 7 × 6
  Control              `ENDOPHYTES$Concentration` variable     n  mean    sd
  <chr>                <chr>                      <fct>    <dbl> <dbl> <dbl>
1 SAureusGrowthControl 2- 128 mg/ml               SAureus      8 0.037     0
2 SAureusGrowthControl 3- 64mg/ml                 SAureus      8 0.071     0
3 SAureusGrowthControl 4- 32mg/ml                 SAureus      8 0.114     0
4 SAureusGrowthControl 5-16mg/ml                  SAureus      8 0.131     0
5 SAureusGrowthControl 6-8mg/ml                   SAureus      8 0.145     0
6 SAureusGrowthControl 7-4mg/ml                   SAureus      8 0.164     0
7 SAureusGrowthControl 8-2mg/ml                   SAureus      8 0.18      0

SAureus by Hour and Control

TT %>%
  group_by(ENDOPHYTES$Hour, Control) %>%
   get_summary_stats(SAureus, type = "mean_sd")
# A tibble: 7 × 6
  Control              `ENDOPHYTES$Hour` variable     n  mean    sd
  <chr>                <chr>             <fct>    <dbl> <dbl> <dbl>
1 SAureusGrowthControl 12-hrs Incubation SAureus      8 0.145     0
2 SAureusGrowthControl 15-hrs Incubation SAureus      8 0.164     0
3 SAureusGrowthControl 18-hrs Incubation SAureus      8 0.18      0
4 SAureusGrowthControl 3-hrs Incubation  SAureus      8 0.071     0
5 SAureusGrowthControl 6-hrs Incubation  SAureus      8 0.114     0
6 SAureusGrowthControl 9-hrs Incubation  SAureus      8 0.131     0
7 SAureusGrowthControl Pre-incubation    SAureus      8 0.037     0

SAureus by Concentration. Hour and Control

TT %>%
  group_by(Concentration, Hour, Control) %>%
   get_summary_stats(SAureus, type = "mean_sd")
# A tibble: 7 × 7
  Hour              Concentration Control              varia…¹     n  mean    sd
  <fct>             <fct>         <chr>                <fct>   <dbl> <dbl> <dbl>
1 Pre-incubation    2- 128 mg/ml  SAureusGrowthControl SAureus     8 0.037     0
2 3-hrs Incubation  3- 64mg/ml    SAureusGrowthControl SAureus     8 0.071     0
3 6-hrs Incubation  4- 32mg/ml    SAureusGrowthControl SAureus     8 0.114     0
4 9-hrs Incubation  5-16mg/ml     SAureusGrowthControl SAureus     8 0.131     0
5 12-hrs Incubation 6-8mg/ml      SAureusGrowthControl SAureus     8 0.145     0
6 15-hrs Incubation 7-4mg/ml      SAureusGrowthControl SAureus     8 0.164     0
7 18-hrs Incubation 8-2mg/ml      SAureusGrowthControl SAureus     8 0.18      0
# … with abbreviated variable name ¹​variable

Plots of two-way interaction effects of factors on SMarcescens response variable

op <- par(mfrow = c(3, 1))
with(TT, {
  interaction.plot(Hour, Concentration, SAureus)
  interaction.plot(Hour, Control, SAureus)
  interaction.plot(Concentration, Control, SAureus)
  }
)

Visualization

library(ggplot2)
ggplot(TT) +
  aes(x = Hour, y = "SAureus", color=Hour) +
  geom_jitter() +
  theme(legend.position = "none")

library(ggplot2)
ggplot(TT) +
  aes(x = Concentration, y = "SAureus", color=Concentration) +
  geom_jitter() +
  theme(legend.position = "none")

library(ggplot2)
ggplot(TT) +
  aes(x = Control, y = "SAureus", color=Control) +
  geom_jitter() +
  theme(legend.position = "none")

boxplot <- ggboxplot(TT,x ="Hour", y = "SAureus", color = "Hour",palette = "jco")
boxplot

boxplot <- ggboxplot(TT,x ="Concentration", y = "SAureus", color = "Concentration",palette = "jco")
boxplot

Inference and models of 2-way Anova

model<-aov(SAureus ~ Concentration + Hour, data = ENDOPHYTES)
summary(model)
##               Df  Sum Sq  Mean Sq F value Pr(>F)    
## Concentration  6 0.08078 0.013464   107.7 <2e-16 ***
## Residuals     49 0.00612 0.000125                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
model<-aov(SAureus ~ Hour + Concentration, data = ENDOPHYTES)
summary(model)
##             Df  Sum Sq  Mean Sq F value Pr(>F)    
## Hour         6 0.08078 0.013464   107.7 <2e-16 ***
## Residuals   49 0.00612 0.000125                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Interpret the result of 2-way ANOVA tests

Since the p values are <2e-16 and <2e-16 for Concentration and Hour respectively, which are both greater than the significance level 0.05, both are statistically significant.

Do a post-hoc test

tukey.two.way<-TukeyHSD(model)

tukey.two.way
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = SAureus ~ Hour + Concentration, data = ENDOPHYTES)

$Hour
                                           diff          lwr           upr
15-hrs Incubation-12-hrs Incubation  0.01141667 -0.005767222  0.0286005558
18-hrs Incubation-12-hrs Incubation  0.02441667  0.007232778  0.0416005558
3-hrs Incubation-12-hrs Incubation  -0.06754167 -0.084725556 -0.0503577775
6-hrs Incubation-12-hrs Incubation  -0.03925000 -0.056433889 -0.0220661109
9-hrs Incubation-12-hrs Incubation  -0.02029167 -0.037475556 -0.0031077775
Pre-incubation-12-hrs Incubation    -0.08558333 -0.102767222 -0.0683994442
18-hrs Incubation-15-hrs Incubation  0.01300000 -0.004183889  0.0301838891
3-hrs Incubation-15-hrs Incubation  -0.07895833 -0.096142222 -0.0617744442
6-hrs Incubation-15-hrs Incubation  -0.05066667 -0.067850556 -0.0334827775
9-hrs Incubation-15-hrs Incubation  -0.03170833 -0.048892222 -0.0145244442
Pre-incubation-15-hrs Incubation    -0.09700000 -0.114183889 -0.0798161109
3-hrs Incubation-18-hrs Incubation  -0.09195833 -0.109142222 -0.0747744442
6-hrs Incubation-18-hrs Incubation  -0.06366667 -0.080850556 -0.0464827775
9-hrs Incubation-18-hrs Incubation  -0.04470833 -0.061892222 -0.0275244442
Pre-incubation-18-hrs Incubation    -0.11000000 -0.127183889 -0.0928161109
6-hrs Incubation-3-hrs Incubation    0.02829167  0.011107778  0.0454755558
9-hrs Incubation-3-hrs Incubation    0.04725000  0.030066111  0.0644338891
Pre-incubation-3-hrs Incubation     -0.01804167 -0.035225556 -0.0008577775
9-hrs Incubation-6-hrs Incubation    0.01895833  0.001774444  0.0361422225
Pre-incubation-6-hrs Incubation     -0.04633333 -0.063517222 -0.0291494442
Pre-incubation-9-hrs Incubation     -0.06529167 -0.082475556 -0.0481077775
                                        p adj
15-hrs Incubation-12-hrs Incubation 0.4023580
18-hrs Incubation-12-hrs Incubation 0.0011974
3-hrs Incubation-12-hrs Incubation  0.0000000
6-hrs Incubation-12-hrs Incubation  0.0000001
9-hrs Incubation-12-hrs Incubation  0.0112498
Pre-incubation-12-hrs Incubation    0.0000000
18-hrs Incubation-15-hrs Incubation 0.2529426
3-hrs Incubation-15-hrs Incubation  0.0000000
6-hrs Incubation-15-hrs Incubation  0.0000000
9-hrs Incubation-15-hrs Incubation  0.0000149
Pre-incubation-15-hrs Incubation    0.0000000
3-hrs Incubation-18-hrs Incubation  0.0000000
6-hrs Incubation-18-hrs Incubation  0.0000000
9-hrs Incubation-18-hrs Incubation  0.0000000
Pre-incubation-18-hrs Incubation    0.0000000
6-hrs Incubation-3-hrs Incubation   0.0001219
9-hrs Incubation-3-hrs Incubation   0.0000000
Pre-incubation-3-hrs Incubation     0.0338198
9-hrs Incubation-6-hrs Incubation   0.0218673
Pre-incubation-6-hrs Incubation     0.0000000
Pre-incubation-9-hrs Incubation     0.0000000