#### Analysis script for Nettle et al. 'Early-life adversity.... ######
# NB. The variable referred to throughout the paper as 'Effort' is referred to in the dataset and R script as 'Work'
# Its levels are Hard and Easy, as discussed in the paper

### Data loading #####
library(dplyr)
library(ggplot2)
library(grid)
library(corrplot)
library(lme4)
library(psych)

# One bird per row data set
d=read.csv("publication.data.csv")
d$NatalNest=as.factor(d$NatalNest)
d$Trt=factor(d$Trt, levels=c("LH", "LE", "PH", "PE"))

# Growth data
g=read.csv("growth.data.publication.csv")

# Telomere data in long format
t=read.csv("telomere.data.long.csv")

### Arrival weight #####
arrival.weights=subset(g, Type=="Arrival")
mean(arrival.weights$Mass)
sd(arrival.weights$Mass)
m1=lmer(Mass~Male + Amount*Work + (1|Family), REML=FALSE, data=arrival.weights)
summary(m1)
drop1(m1, test="Chisq")
drop1(update(m1, .~. - Amount:Work), test="Chisq")

#### Weight gain ####
# Figure 1a
am.weights=subset(g, Type=="AM")
colnames(am.weights)
fig1a = ggplot(summarise(group_by(am.weights, Trt, Age), Weight=mean(Mass, na.rm=TRUE), sd=sd(Mass, na.rm=TRUE), n=length(Mass)), aes(x=Age, y=Weight, group=Trt, colour=Trt)) +  
  geom_line(size = 1) +
  xlab("Age (days)") +
  ylab("Weight (g)") +
  xlim(4, 20)+
  geom_errorbar(aes(ymin=Weight-sd/sqrt(n), 
                    ymax=Weight+sd/sqrt(n)), 
                width=0.5, position=position_dodge(0)) +
  scale_color_manual(values=c("deeppink4","lightpink2", "midnightblue", "lightskyblue"), labels=c("Lean Hard","Lean Easy","Plenty Hard","Plenty Easy")) +
  labs(colour="Experimental group") +
  theme_bw() +
  theme(panel.grid.major = element_blank()) + # switch off major gridlines
  theme(panel.grid.minor = element_blank()) +
  geom_vline(colour="darkgrey", xintercept=15) +
  geom_vline(colour="darkgrey", xintercept=5) +
  annotate("segment", x =5 , xend=15, y=75, yend=75,arrow=arrow(ends="both")) +
  annotate("text", x=10, y=76.5,label="Manipulation") + 
  theme(legend.justification=c(1,0), legend.position=c(1,0))
fig1a

# Model for weight gain during the manipulation 
manip.weights=subset(g, Age<=15 & Type=="AM")
m=lmer(Mass~Age + Male + Amount + Work + Amount:Work + Age:Male + Age:Amount + Age:Work + Age:Amount:Work+ (1|Family/UniqueID), REML=FALSE, data=manip.weights)
summary(m)
drop1(m, test="Chisq")
m1=update(m, .~.-Age:Amount:Work)
summary(m1)
drop1(m1, test="Chisq")
summary(update(m1, .~.-Age:Amount - Age:Work - Age:Male))
drop1(update(m1, .~.-Age:Amount - Age:Work - Age:Male - Amount:Work), test="Chisq")

### Tarsi #####
# Day 15
m=lmer(tarsusD15~Male + Amount + Work + Amount:Work + (1|NatalNest), REML=FALSE, data=d)
summary(m)
drop1(m, test="Chisq")
m1=update(m, .~.-Amount:Work)
drop1(m1, test="Chisq")
summarize(group_by(d, Amount), x=mean(tarsusD15), sd=sd(tarsusD15))

# Day 56
m=lmer(tarsusD56~Male + Amount + Work + Amount:Work + (1|NatalNest), REML=FALSE, data=d)
summary(m)
drop1(m, test="Chisq")
m1=update(m, .~.-Amount:Work)
drop1(m1, test="Chisq")

#D15-D56 change in tarsus
d=mutate(d, tarsuschange=tarsusD56-tarsusD15)
summarize(group_by(d, Amount), mean=mean(tarsuschange), sd=sd(tarsuschange))
m=lmer(tarsuschange~Male + Amount + Work + Amount:Work + (1|NatalNest), REML=FALSE, data=d)
summary(m)
drop1(m, test="Chisq")
m1=update(m, .~.-Amount:Work)
drop1(m1, test="Chisq")

# Tarsus figure (figure 1b)
one=summarize(group_by(d, Trt), mean=mean(tarsusD15), sd=sd(tarsusD15))
two=summarize(group_by(d, Trt), mean=mean(tarsusD56), sd=sd(tarsusD56))
one$Day=c("Day 15", "Day 15", "Day 15", "Day 15")
two$Day=c("Day 56", "Day 56", "Day 56", "Day 56")
one$Period=rep("Nestling", times=4)
two$Period=rep("Juvenile", times=4)
figdata=rbind(one, two)
colnames(figdata)[1]="Group"
figdata$Period = factor(figdata$Period, levels =c("Nestling","Juvenile"))
figure1b=ggplot(figdata, aes(x=Period, y=mean, colour=Group)) +
  geom_point() + 
  geom_errorbar(aes(ymax=mean+sd/sqrt(8), ymin=mean-sd/sqrt(8)), width=0.2) + 
  geom_line(aes(group=Group)) + 
  theme_bw() + 
  scale_colour_manual(values=c("deeppink4", "lightpink2", "midnightblue", "lightskyblue"), name="Experimental\nGroup", 
                      labels=c("Lean Hard", "Lean Easy", "Plenty Hard", "Plenty Easy")) +
  ylab("Tarsus length (mm)") + 
  xlab("Time point") + 
  coord_cartesian(ylim=c(27, 31)) 
figure1b

png("figure1b.png", res=300, units="in", width=5, height=3)
figure1b
dev.off()

#### Weight at  D56 ####
m=lmer(MassD56~tarsusD56 + Male+Amount*Work + (1|NatalNest), REML=FALSE, data=d)
summary(m)
drop1(m, test="Chisq")
m1=update(m, .~. - Amount:Work)
drop1(m1, test="Chisq")
summarize(group_by(d, Trt), mean=mean(MassD56), sd=sd(MassD56))
a=lm(d$MassD56~d$tarsusD56)
summary(a)
d$body.condition.D56=a$residuals
fig1d=ggplot(summarize(group_by(d, Trt), mean=mean(body.condition.D56), sd=sd(body.condition.D56)), aes(x=Trt, y=mean, fill=Trt)) +
  geom_bar(stat="identity", colour="black") + 
  geom_errorbar(aes(ymax=mean+sd/sqrt(8), ymin=mean-sd/sqrt(8)), width=0.2) + 
  theme_bw() +
  scale_fill_manual(values=c("deeppink4", "lightpink2", "midnightblue", "lightskyblue"), labels=c("Lean Easy","Lean Hard","Plenty Easy","Plenty Hard")) +
  guides(fill=FALSE) +
  xlab("Experimental group") +
  ylab("Body condition (day 56)") 
fig1d


#### Fledging ######
m=lmer(Fledgeday~Male+Amount*Work + (1|NatalNest), REML=FALSE, data=d)
summary(m)
drop1(m, test="Chisq")
m1=update(m, .~. - Amount:Work)
drop1(m1, test="Chisq")  
summarize(group_by(d, Trt), f=mean(Fledgeday), sd=sd(Fledgeday))
summarize(group_by(d, Amount), f=mean(Fledgeday), sd=sd(Fledgeday))
fig1c=ggplot(summarize(group_by(d, Trt), mean=mean(Fledgeday), sd=sd(Fledgeday)), aes(x=Trt, y=mean, fill=Trt)) +
  geom_bar(stat="identity", colour="black") + 
  geom_errorbar(aes(ymax=mean+sd/sqrt(8), ymin=mean-sd/sqrt(8)), width=0.2) + 
  theme_bw() +
  scale_fill_manual(values=c("deeppink4", "lightpink2", "midnightblue", "lightskyblue")) +
  guides(fill=FALSE) +
  xlab("Experimental group") +
  ylab("Age at fledging (days)") + 
  coord_cartesian(ylim=c(18, 24))
fig1c

####Telomere attrition####
# Continuity of telomeres
cor.test(d$TS.5, d$TS.56, use="everything")
t.test(d$TS.56, d$TS.5, paired=TRUE)
fig2a=ggplot(d, aes(x=TS.5, y=TS.56)) +
  geom_point() + 
  theme_bw() + 
  coord_cartesian(xlim=c(0.5, 3.0), ylim=c(0.5, 3.0)) +
  xlab("Telomere length day 5 (T/S)") + 
  ylab("Telomere length day 56 (T/S)") +
  geom_abline() +
  geom_smooth(method="lm", se=FALSE, linetype=2)
fig2a  

png("figure2a.png", res=300, units="in", width=3, height=3)
fig2a
dev.off()

# Figure for mean TS over time (fig 2b)
t=subset(t, !is.na(t$Day) &!is.na(t$TS))
tt=summarize(group_by(t, Day), mean=mean(TS, na.rm=TRUE), sd=sd(TS, na.rm=TRUE), n=length(TS))
tt$se=tt$sd/sqrt(tt$n)
fig2b=ggplot(tt, aes(x=Day, y=mean)) +
  geom_point() +
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=2) + 
  theme_bw() + 
  xlab("Age (days)") + 
  ylab("Telomere length (T/S)") + 
  geom_line() + 
  coord_cartesian(ylim=c(1,2))

png("figure2b.png", res=300, units="in", width=3, height=3)
fig2b
dev.off()

# Figure for effects of manipulation (fig2c)
one=summarize(group_by(d, Trt), mean=mean(verhulst5.56, na.rm=TRUE), sd=sd(verhulst5.56, na.rm=TRUE), n=length(verhulst5.56[!is.na(verhulst5.56)]))
two=summarize(group_by(d, Trt), mean=mean(verhulst5.15, na.rm=TRUE), sd=sd(verhulst5.15, na.rm=TRUE), n=length(verhulst5.15[!is.na(verhulst5.15)]))
three=summarize(group_by(d, Trt), mean=mean(verhulst15.56, na.rm=TRUE), sd=sd(verhulst15.56, na.rm=TRUE), n=length(verhulst15.56[!is.na(verhulst15.56)]))
figdata=rbind(one, two, three)
figdata$Period=c(rep("Day 5 to 56", times=4), rep("Day 5 to 15", times=4), rep("Day 15 to 56", times=4))
figdata$Period=factor(figdata$Period, levels=c("Day 5 to 56", "Day 5 to 15", "Day 15 to 56"))
figdata$se=figdata$mean/sqrt(figdata$n)

fig2c=ggplot(figdata, aes(x=Trt, y=mean, fill=Trt)) +
  geom_bar(stat="identity", colour="black") + 
  geom_errorbar(aes(ymax=mean+se, ymin=mean-se), width=0.2) + 
  theme_bw() +
  scale_fill_manual(values=c("deeppink4", "lightpink2", "midnightblue", "lightskyblue"), labels=c("Lean Easy","Lean Hard","Plenty Easy","Plenty Hard")) +
  guides(fill=FALSE) +
  xlab("Experimental group") +
  ylab("Standardized telomere length change") + 
  facet_wrap(~Period)
fig2c

# Statistical models for telomeres
# Correlation of Verhulst with absolute loss
cor(d$verhulst5.56, I(d$TS.56-d$TS.5), use="complete.obs")
cor(d$verhulst5.15, I(d$TS.15-d$TS.5), use="complete.obs")
cor(d$verhulst15.56, I(d$TS.56-d$TS.15), use="complete.obs")

# Day 5 to day 56
m1=lmer(verhulst5.56~TS.5 + Male + Amount +Work + Amount*Work + (1|NatalNest), REML=FALSE, data=d)
summary(m1)
drop1(m1, test="Chisq")
drop1(update(m1, .~. - Amount:Work), test="Chisq")
summary(update(m1, .~. - Amount:Work))

# Day 5 to day 15
m1=lmer(verhulst5.15~TS.5 + Male + Amount +Work + Amount*Work + (1|NatalNest), REML=FALSE, data=d)
summary(m1)
drop1(m1, test="Chisq")
drop1(update(m1, .~. - Amount:Work), test="Chisq")
summary(update(m1, .~. - Amount:Work), test="Chisq")

# Day 15 to day 56
m1=lmer(verhulst15.56~TS.15 + Male + Amount +Work + Amount*Work + (1|NatalNest), REML=FALSE, data=d)
summary(m1)
drop1(m1, test="Chisq")
drop1(update(m1, .~. - Amount:Work), test="Chisq")
summary(update(m1, .~. - Amount:Work))

# Does compensatory growth explain variation in telomere attrition after day 15?
# Apparently not
m2=lmer(verhulst15.56~TS.15 + Male + Amount +Work + Amount*Work + I(MassD56-D15.weight) + (1|NatalNest), REML=FALSE, data=d)
summary(m2)
drop1(m2, test="Chisq")

#### OH8dG ######
# Associations among OH8DG values and telomere attrition
cor.test(data=d, ~Oxy.value.5+Oxy.value.15, use="complete.obs")
cor.test(data=d, ~Oxy.value.5+Oxy.value.56, use="complete.obs")
cor.test(data=d, ~Oxy.value.15+Oxy.value.56, use="complete.obs")
cor.test(data=d, ~verhulst15.56+Oxy.value.56, use="complete.obs")
cor.test(data=d, ~verhulst15.56+Oxy.value.15, use="complete.obs")
cor.test(data=d, ~verhulst5.15+Oxy.value.15, use="complete.obs")
cor.test(data=d, ~verhulst5.15+Oxy.value.5, use="complete.obs")


# Treatment effect day 5
m1=lmer(Oxy.value.5~Male + Amount +Work + Amount*Work + (1|NatalNest), REML=FALSE, data=d)
summary(m1)
drop1(m1, test="Chisq")
drop1(update(m1, .~. - Amount:Work), test="Chisq")
summary(update(m1, .~. - Amount:Work))

# Day 15
m1=lmer(Oxy.value.15~Male + Amount +Work + Amount*Work + (1|NatalNest), REML=FALSE, data=d)
summary(m1)
drop1(m1, test="Chisq")
drop1(update(m1, .~. - Amount:Work), test="Chisq")
summary(update(m1, .~. - Amount:Work))

# Day 56
m1=lmer(Oxy.value.56~Male + Amount +Work + Amount*Work + (1|NatalNest), REML=FALSE, data=d)
summary(m1)
drop1(m1, test="Chisq")
drop1(update(m1, .~. - Amount:Work), test="Chisq")
summary(update(m1, .~. - Amount:Work))

figdat3=summarize(group_by(d, Trt), m=mean(Oxy.value.56, na.rm=TRUE), sd=sd(Oxy.value.56, na.rm=TRUE), n=length(Oxy.value.56[!is.na(Oxy.value.56)]))
figdat3$se=figdat3$sd/sqrt(figdat3$n)
fig3=ggplot(figdat3, aes(x=Trt, y=m, fill=Trt)) +
  geom_bar(stat="identity", colour="black") + 
  geom_errorbar(aes(ymax=m+se, ymin=m-se), width=0.2) + 
  theme_bw() +
  scale_fill_manual(values=c("deeppink4", "lightpink2", "midnightblue", "lightskyblue"), labels=c("Lean Easy","Lean Hard","Plenty Easy","Plenty Hard")) +
  guides(fill=FALSE) +
  xlab("Experimental group") +
  ylab("Day 56 oxidative damage (8-OHdG)") 
fig3

png("fig3.png", res=300, width=4, height=4, units="in")
fig3
dev.off()

jpeg("fig3.jpg", res=300, width=4, height=4, units="in")
fig3
dev.off()

#### Inflammation #####
# What's the correlation between IL6 and HSCRP?
cor.test(sqrt(d$HSCRP), log(d$IL6), use="complete.obs")

# Treatment effects: IL6
m1=lmer(log(IL6)~Male + Amount +Work + Amount*Work + (1|NatalNest), REML=FALSE, data=d)
summary(m1)
drop1(m1, test="Chisq")
drop1(update(m1, .~. - Amount:Work), test="Chisq")

#Treatment effects: HSCRP
m1=lmer(sqrt(HSCRP)~Male + Amount +Work + Amount*Work + (1|NatalNest), REML=FALSE, data=d)
summary(m1)
drop1(m1, test="Chisq")
drop1(update(m1, .~. - Amount:Work), test="Chisq")

# Figure for treatment effects
figdat=summarize(group_by(d, Trt), mean=mean(log(IL6), na.rm=TRUE), se=describe(log(IL6))$se)
figdat=rbind(figdat, summarize(group_by(d, Trt), mean=mean(sqrt(HSCRP), na.rm=TRUE), se=describe(sqrt(HSCRP))$se))
figdat$marker=c(rep("IL-6", times=4), rep("HS-CRP", times=4))

fig4=ggplot(figdat, aes(x=Trt, y=mean, fill=Trt)) + 
  geom_bar(stat="identity", colour="black") +
  geom_errorbar(aes(ymax=mean+se, ymin=mean-se), width=0.2) +
  facet_wrap(~marker) +
  theme_bw() + 
  xlab("Experimental group") +
  ylab("Mean plasma biomarker level") +
  scale_fill_manual(values=c("deeppink4", "lightpink2", "midnightblue", "lightskyblue")) +
  guides(fill=FALSE) 
fig4

png("figure4.png", res=300, width=6, height=3, units="in")
fig4
dev.off()

jpeg("figure4.jpg", res=300, width=6, height=3, units="in")
fig4
dev.off()

# Telomere attrition and 8OHDG as predictors of adult inflammation: HSCRP
m1=lmer(sqrt(HSCRP)~Male + verhulst5.56 + sqrt(Oxy.value.56) + (1|NatalNest), REML=FALSE, data=subset(d, !is.na(verhulst5.56)))
summary(m1)
plot(m1)
drop1(m1, test="Chisq")

# Telomere attrition and 8OHDG as predictors of adult inflammation: IL6
m1=lmer(log(IL6)~Male + verhulst5.56 + sqrt(Oxy.value.56) + (1|NatalNest), REML=FALSE, data=subset(d, !is.na(verhulst5.56)))
summary(m1)
drop1(m1, test="Chisq")
plot(m1)

fig5=ggplot(d, aes(x=Oxy.value.56, y=log(IL6))) + 
  geom_smooth(method="lm") + 
  geom_point() + 
  theme_bw() + 
  xlab("Day 56 oxidative damage (8-OHdG)") + 
  ylab("Adult IL-6 (log pg/ml)") 
fig5
png("figure5.png", res=300, width=4, height=4, units="in")
fig5
dev.off()

jpeg("figure5.jpg", res=300, width=4, height=4, units="in")
fig5
dev.off()

# Does 8OHDG mediate the treatment effects?
m2=lmer(log(IL6)~Male + sqrt(Oxy.value.56) + Amount*Work + (1|NatalNest), REML=FALSE, data=d)
summary(m2)
drop1(m2, test="Chisq")
