#Clear R's brain of objects
rm(list=ls())
#Run packages
library(ggplot2)
library(ggfortify)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
#Import dataset, double check file pathway every time
lady <- read.csv("D:/9915/R/R_Master_Datasets/ladybirds_morph_colour.csv")
glimpse(lady)
## Rows: 20
## Columns: 4
## $ Habitat      <chr> "Rural", "Rural", "Rural", "Rural", "Rural", "Rural", "Ru…
## $ Site         <chr> "R1", "R2", "R3", "R4", "R5", "R1", "R2", "R3", "R4", "R5…
## $ morph_colour <chr> "black", "black", "black", "black", "black", "red", "red"…
## $ number       <int> 10, 3, 4, 7, 6, 15, 18, 9, 12, 16, 32, 25, 25, 17, 16, 17…
totals <- lady %>%
  group_by(Habitat, morph_colour) %>%
    summarize(total.number = sum (number))
## `summarise()` has regrouped the output.
## ℹ Summaries were computed grouped by Habitat and morph_colour.
## ℹ Output is grouped by Habitat.
## ℹ Use `summarise(.groups = "drop_last")` to silence this message.
## ℹ Use `summarise(.by = c(Habitat, morph_colour))` for per-operation grouping
##   (`?dplyr::dplyr_by`) instead.
ggplot(totals, aes(x=Habitat, y=total.number,
                   fill = morph_colour)) +
  geom_bar(stat = 'identity', position = 'dodge') +
  scale_fill_manual(values = c(black = "black", red = "red"))

#Turns data from a 4x3 data frame into a matrix
lady.mat <- xtabs(number ~ Habitat + morph_colour,
                  data = lady)
lady.mat
##             morph_colour
## Habitat      black red
##   Industrial   115  85
##   Rural         30  70
#Perform a chi squared test
chisq.test(lady.mat)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  lady.mat
## X-squared = 19.103, df = 1, p-value = 1.239e-05
#assign to a variable and use names() to get components
#Import dataset, double check file pathway every time
ozone <- read.csv("D:/9915/R/R_Master_Datasets/ozone.csv")
glimpse(ozone)
## Rows: 20
## Columns: 3
## $ Ozone           <dbl> 61.7, 64.0, 72.4, 56.8, 52.4, 44.8, 70.4, 67.6, 68.8, …
## $ Garden.location <chr> "West", "West", "West", "West", "West", "West", "West"…
## $ Garden.ID       <chr> "G1", "G2", "G3", "G4", "G5", "G6", "G7", "G8", "G9", …
ggplot(ozone, aes(x = Ozone)) +
  geom_histogram(binwidth = 10) + 
  facet_wrap (~Garden.location, ncol = 1)+
  theme_bw()

t.test(Ozone ~ Garden.location, data = ozone)
## 
##  Welch Two Sample t-test
## 
## data:  Ozone by Garden.location
## t = 4.2363, df = 17.656, p-value = 0.0005159
## alternative hypothesis: true difference in means between group East and group West is not equal to 0
## 95 percent confidence interval:
##   8.094171 24.065829
## sample estimates:
## mean in group East mean in group West 
##              77.34              61.26
#Import dataset, double check file pathway every time
plant_gr <- read.csv("D:/9915/R/R_Master_Datasets/plant.growth.rate.csv")
glimpse(plant_gr)
## Rows: 50
## Columns: 2
## $ soil.moisture.content <dbl> 0.4696876, 0.5413106, 1.6979915, 0.8255799, 0.85…
## $ plant.growth.rate     <dbl> 21.31695, 27.03072, 38.98937, 30.19529, 37.06547…
ggplot(plant_gr,
       aes(x = soil.moisture.content, y = plant.growth.rate)) +
  geom_point() +
  ylab("Plant Growth Rate (mm/week)") +
  theme_bw()

model_pgr <- lm(plant.growth.rate ~ soil.moisture.content,
                data = plant_gr)
#plot(model_pgr)
autoplot(model_pgr, smooth.colour = NA)
## Warning: `fortify(<lm>)` was deprecated in ggplot2 4.0.0.
## ℹ Please use `broom::augment(<lm>)` instead.
## ℹ The deprecated feature was likely used in the ggfortify package.
##   Please report the issue at <https://github.com/sinhrks/ggfortify/issues>.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## ℹ The deprecated feature was likely used in the ggfortify package.
##   Please report the issue at <https://github.com/sinhrks/ggfortify/issues>.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 50 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Removed 50 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Removed 50 rows containing missing values or values outside the scale range
## (`geom_line()`).

#anova() DOES NOT RUN AN ANOVA, returns a classic stats table
anova(model_pgr)
## Analysis of Variance Table
## 
## Response: plant.growth.rate
##                       Df  Sum Sq Mean Sq F value    Pr(>F)    
## soil.moisture.content  1 2521.15 2521.15  156.08 < 2.2e-16 ***
## Residuals             48  775.35   16.15                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(model_pgr)
## 
## Call:
## lm(formula = plant.growth.rate ~ soil.moisture.content, data = plant_gr)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.9089 -3.0747  0.2261  2.6567  8.9406 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             19.348      1.283   15.08   <2e-16 ***
## soil.moisture.content   12.750      1.021   12.49   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.019 on 48 degrees of freedom
## Multiple R-squared:  0.7648, Adjusted R-squared:  0.7599 
## F-statistic: 156.1 on 1 and 48 DF,  p-value: < 2.2e-16
ggplot(plant_gr, aes(x = soil.moisture.content,
                     y = plant.growth.rate)) +
  geom_point() + 
  geom_smooth(method = 'lm') +
  ylab("Plant Growth Rate (mm/week)") +
  theme_bw()
## `geom_smooth()` using formula = 'y ~ x'

#Import dataset, double check file pathway every time
daphnia <- read.csv("D:/9915/R/R_Master_Datasets/Daphniagrowth.csv")
glimpse(daphnia)
## Rows: 40
## Columns: 3
## $ parasite    <chr> "control", "control", "control", "control", "control", "co…
## $ rep         <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, …
## $ growth.rate <dbl> 1.0747092, 1.2659016, 1.3151563, 1.0757519, 1.1967619, 1.3…
ggplot(daphnia, aes (x = parasite, y = growth.rate)) +
  geom_boxplot() +
  geom_point()+
  theme_bw() + 
  coord_flip()

model_grow <- lm(growth.rate ~ parasite, data = daphnia)
autoplot(model_grow, smooth.colour = NA)
## Warning: Removed 40 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Removed 40 rows containing missing values or values outside the scale range
## (`geom_line()`).

anova(model_grow)
## Analysis of Variance Table
## 
## Response: growth.rate
##           Df Sum Sq Mean Sq F value    Pr(>F)    
## parasite   3 3.1379 1.04597  32.325 2.571e-10 ***
## Residuals 36 1.1649 0.03236                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(model_grow)
## 
## Call:
## lm(formula = growth.rate ~ parasite, data = daphnia)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.41930 -0.09696  0.01408  0.12267  0.31790 
## 
## Coefficients:
##                                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                        1.21391    0.05688  21.340  < 2e-16 ***
## parasiteMetschnikowia bicuspidata -0.41275    0.08045  -5.131 1.01e-05 ***
## parasitePansporella perplexa      -0.13755    0.08045  -1.710   0.0959 .  
## parasitePasteuria ramosa          -0.73171    0.08045  -9.096 7.34e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1799 on 36 degrees of freedom
## Multiple R-squared:  0.7293, Adjusted R-squared:  0.7067 
## F-statistic: 32.33 on 3 and 36 DF,  p-value: 2.571e-10
sumDat <- daphnia %>%
  group_by(parasite) %>%
  summarise(meanGR = mean(growth.rate))
sumDat
## # A tibble: 4 × 2
##   parasite                  meanGR
##   <chr>                      <dbl>
## 1 Metschnikowia bicuspidata  0.801
## 2 Pansporella perplexa       1.08 
## 3 Pasteuria ramosa           0.482
## 4 control                    1.21
predict(model_grow)
##         1         2         3         4         5         6         7         8 
## 1.2139088 1.2139088 1.2139088 1.2139088 1.2139088 1.2139088 1.2139088 1.2139088 
##         9        10        11        12        13        14        15        16 
## 1.2139088 1.2139088 0.8011541 0.8011541 0.8011541 0.8011541 0.8011541 0.8011541 
##        17        18        19        20        21        22        23        24 
## 0.8011541 0.8011541 0.8011541 0.8011541 1.0763551 1.0763551 1.0763551 1.0763551 
##        25        26        27        28        29        30        31        32 
## 1.0763551 1.0763551 1.0763551 1.0763551 1.0763551 1.0763551 0.4822030 0.4822030 
##        33        34        35        36        37        38        39        40 
## 0.4822030 0.4822030 0.4822030 0.4822030 0.4822030 0.4822030 0.4822030 0.4822030