---
title: "Linear models"
output: html_notebook
---
```{r}
library(tidyverse)
library(car)
library(DHARMa)
```
data
```{r}
pheno_data <- read_csv("./data/final_phenotype_data.csv",
col_names = TRUE,
col_types = list(pathogen = col_factor(levels = c("CTRL", "C1", "C14", "C20")),
mat_temp = col_factor(levels = c("20", "25")),
acc_temp = col_factor(levels = c("20", "25")),
temp_treat = col_factor(levels = c("20-20", "20-25", "25-20", "25-25")),
exposed = col_factor(NULL))) %>%
unite("path_acc_temp", c(pathogen, acc_temp), sep = "_", remove = FALSE) %>%
mutate(path_acc_temp = as.factor(path_acc_temp)) %>%
filter(indv_id != "C1-20-25-19") #animal died and didnt have any offspring - removing for simplicity
pheno_data
```
Phenotype variables:
size_at_mat
size_at_20
growth_rate
age_at_maturity
age_of_first_clutch
size_first_clutch
total_offspring
total_clutches
mean_clutch_size
little_r
lifespan
########################
#Linear models with maternal temp as additive effect and focal temp and pathgoen treatment as interacting fixed effects
########################
#Size at maturity
```{r}
size_at_mat_lm2 <- lm(size_at_mat ~ mat_temp + acc_temp * pathogen, contrasts = list(mat_temp = contr.Sum, acc_temp = contr.Sum, pathogen = contr.Sum), data = pheno_data)
```
```{r}
Anova(size_at_mat_lm2, type = 3, white.adjust = TRUE)
```
#size at day 20
```{r}
size_at_20_lm2 <- lm(size_at_20 ~ mat_temp + acc_temp * pathogen, contrasts = list(mat_temp = contr.Sum, acc_temp = contr.Sum, pathogen = contr.Sum), data = pheno_data)
```
```{r}
Anova(size_at_20_lm2, type = 3, white.adjust = TRUE)
```
#Growth rate
```{r}
growth_rate_lm2 <- lm(growth_rate ~ mat_temp + acc_temp * pathogen, contrasts = list(mat_temp = contr.Sum, acc_temp = contr.Sum, pathogen = contr.Sum), data = pheno_data)
```
```{r}
Anova(growth_rate_lm2, type = 3, white.adjust = TRUE)
```
#Age at maturity - glm
```{r}
age_at_maturity_glm2 <- glm(age_at_maturity ~ mat_temp + acc_temp * pathogen, family = "poisson", contrasts = list(mat_temp = contr.Sum, acc_temp = contr.Sum, pathogen = contr.Sum), data = pheno_data)
```
```{r}
Anova(age_at_maturity_glm2, type = 3, white.adjust = TRUE)
```
#Age of first clutch - glm
```{r}
age_of_first_clutch_glm2 <- glm(age_of_first_clutch ~ mat_temp + acc_temp * pathogen, family = "poisson", contrasts = list(mat_temp = contr.Sum, acc_temp = contr.Sum, pathogen = contr.Sum), data = pheno_data)
```
```{r}
Anova(age_of_first_clutch_glm2, type = 3, white.adjust = TRUE)
```
#Size of first clutch
```{r}
size_first_clutch_lm2 <- lm(size_first_clutch ~ mat_temp + acc_temp * pathogen, contrasts = list(mat_temp = contr.Sum, acc_temp = contr.Sum, pathogen = contr.Sum), data = pheno_data)
```
```{r}
Anova(size_first_clutch_lm2, type = 3, white.adjust = TRUE)
```
#Lifetime fecundity - log transformed. Still long tails, but symetric
```{r}
total_offspring_lm2 <- lm(log(total_offspring) ~ mat_temp + acc_temp * pathogen, contrasts = list(mat_temp = contr.Sum, acc_temp = contr.Sum, pathogen = contr.Sum), data = pheno_data)
```
```{r}
Anova(total_offspring_lm2, type = 3, white.adjust = TRUE)
```
#Total clutches - should probably use poisson glm - maybe discard
```{r}
total_clutches_lm2 <- lm(log(total_clutches) ~ mat_temp + acc_temp * pathogen, contrasts = list(mat_temp = contr.Sum, acc_temp = contr.Sum, pathogen = contr.Sum), data = pheno_data)
```
```{r}
Anova(total_clutches_lm2, type = 3, white.adjust = TRUE)
```
#Mean clutch size
```{r}
mean_clutch_size_lm2 <- lm(mean_clutch_size ~ mat_temp + acc_temp * pathogen, contrasts = list(mat_temp = contr.Sum, acc_temp = contr.Sum, pathogen = contr.Sum), data = pheno_data)
```
```{r}
Anova(mean_clutch_size_lm2, type = 3, white.adjust = TRUE)
```
#Intrinsic rate of increase - left skew... used a square... tried reflecting and log transforming, and also removing large outliers, qualitatively all give the same result
```{r}
little_r_lm2 <- lm(little_r^2 ~ mat_temp + acc_temp * pathogen, contrasts = list(mat_temp = contr.Sum, acc_temp = contr.Sum, pathogen = contr.Sum), data = pheno_data)
```
```{r}
Anova(little_r_lm2, type = 3, white.adjust = TRUE)
```
#Lifespan
```{r}
lifespan_lm2 <- lm(lifespan ~ mat_temp + acc_temp * pathogen, contrasts = list(mat_temp = contr.Sum, acc_temp = contr.Sum, pathogen = contr.Sum), data = pheno_data)
```
```{r}
Anova(lifespan_lm2, type = 3, white.adjust = TRUE)
```