## CD-1 PFOA/GenX gestational exposure and latent health outcomes study ## F1 14 weeks of age glucose tolerance tests ## Calculation of Area Under the Curve (AUC) and data analysis ## BEB 03/03/2019 ## Load packages (needs to be done every time R is opened) library(DescTools) library(tidyr) library(MESS) library(dplyr) library(lme4) library(ggplot2) # Set your working directory (replace this example with your file folder destination) setwd("C:/Users/Public/Documents/My Documents/Harlie") # Read in your files df1<-read.csv("Week14GTTdata.csv") df2<-read.csv("Week14GTTAUC.csv") str(df1) str(df2) ## Merge files to create data frame with all necessary variables xy<-merge(df1,df2, by=c("ID")) ## Check structure of df, make sure numbers read in as numbers, characters as characters str(xy) ## Converting data between wide and long forms: ## http://www.cookbook-r.com/Manipulating_data/Converting_data_between_wide_and_long_format/ ## Convert GTT data from wide form to long form (1 line per animal to multiple lines per animal) long.GTT<-gather(df2, time, glucose, 2:7, factor_key=TRUE) ## Check structure str(long.GTT) head(long.GTT) ## Convert character/factor values in "time" column to numeric values (e.g. 0, 20, 40 etc) long.GTT$time<-as.numeric(as.character(gsub("X", "", paste(long.GTT$time)))) str(long.GTT) ## Omit rows with missing data na.omit.long.GTT<-na.omit(long.GTT) ## Convert animal ID number to factor (non-numeric) na.omit.long.GTT$ID<-as.factor(na.omit.long.GTT$ID) baseline<-subset(na.omit.long.GTT, time==0) baseline$base.time<-baseline$time long.GTT.baseline<-merge(na.omit.long.GTT,baseline, by=c("ID")) long.GTT.baseline$auc.glucose<-long.GTT.baseline$glucose.x-long.GTT.baseline$glucose.y names(long.GTT.baseline) ## Obtain mouse-specific AUC values for glucose test ## Can try various methods to compare trapezoid / step/ spline functions ## Create function to use in pipe for AUC calculation auc.fun.trap<-function(x,y){auc(x,y, from=min(x),to=max(x),type=c("linear"))} ## Calculate AUC by mouse ID (trapezoid method) GTT.AUC.trap<-long.GTT.baseline%>%group_by(ID)%>%summarize(auc.trap=auc.fun.trap(time.x,auc.glucose)) ## Merge in AUC with larger dataframe containing all variables df<-merge(xy,GTT.AUC.trap, by=c("ID")) ## Write .csv file #write.csv(df, "Week14GTTAUC_merged.csv", row.names=F) ## Generate group means and standard deviations summary.stats<-df%>%group_by(Group,Sex,Diet)%>%summarise(mean.AUC=mean(auc.trap), sd.AUC=sd(auc.trap)) ## Write .csv file of summary stats #write.csv(summary.stats, "Week14GTTAUC_means_sds_groupedby_group_sex_diet.csv", row.names=F) ## Plot data to visualize spread by sex/diet/group p1<-ggplot(df, aes(x=Group, y=auc.trap))+ geom_boxplot()+ facet_grid(Sex~Diet) p1 ########################### ########################### ## Week 14 GTT/AUC Statistics library(plyr) library(reshape) library(tidyr) library(dplyr) library(multcomp) library(coefplot2) library(lmerTest) library(coda) library(reshape) library(coefplot2) library(ggplot2) library(data.table) library(lme4) library(lmtest) library(nlme) library(sjPlot) names(df) fem.gtt<-subset(df, Sex=="F") mal.gtt<-subset(df, Sex=="M") f.hfd.gtt<-subset(fem.gtt, Diet=="H") f.lfd.gtt<-subset(fem.gtt, Diet=="L") m.hfd.gtt<-subset(mal.gtt, Diet=="H") m.lfd.gtt<-subset(mal.gtt, Diet=="L") fit.f.hfd<-lmer(auc.trap~Group+Weight+(1|DamID)+(1|Cage)+(1|GTTDay),data=f.hfd.gtt) aov<-anova(fit.f.hfd) show_tests(aov, fractions = TRUE)$Weight ranova(fit.f.hfd) drop1(fit.f.hfd) step_result_fit.f.hfd <- step(fit.f.hfd) final_model_fit.f.hfd<- get_model(step_result_fit.f.hfd) confint(final_model_fit.f.hfd, method="Wald") summary(final_model_fit.f.hfd) m0.f.hfd<-lmer(auc.trap~Group+Weight+(1|GTTDay),data=f.hfd.gtt) confint(m0.f.hfd, method="Wald") summary(m0.f.hfd) confint(m0.f.hfd, method="Wald") m0<-lmer(auc.trap~Group+Weight+(1|DamID),data=f.hfd.gtt) m1<-lmer(auc.trap~Group+(1|DamID),data=f.hfd.gtt) anova(m0,m1) m2<-lm(auc.trap~Group+Weight,data=f.hfd.gtt) summary(m2) anova(m0,m2) summary(m0) plot(m0) anova(m0) m0<-lmer(auc.trap~Group+Weight+(1|DamID),data=f.hfd.gtt) f.hfd.gtt.plot<-plot_model(m0, title="Week 14 Female High Fat Diet GTT AUC\n", show.values=T, value.offset = 0.3, vline.color = "grey", dot.size = 3.3, line.size = 1.5, colors="black")+ theme_538(base_size = 12, base_family = "")+ font_size(title = 16, labels.x = 14, labels.y = 14) f.hfd.gtt.plot plot_model(m0) m0<-lmer(auc.trap~Group+Weight+(1|DamID),data=f.lfd.gtt) summary(m0) m0<-lmer(auc.trap~Group+Weight+(1|DamID),data=f.lfd.gtt) f.lfd.gtt.plot<-plot_model(m0, title="Week 14 Female Low Fat Diet GTT AUC\n", show.values=T, value.offset = 0.3, vline.color = "grey", dot.size = 3.3, line.size = 1.5, colors="black")+ theme_538(base_size = 12, base_family = "")+ font_size(title = 16, labels.x = 14, labels.y = 14) f.lfd.gtt.plot m0<-lmer(auc.trap~Group+Weight+(1|DamID),data=m.hfd.gtt) summary(m0) plot_model(m0) m.hfd.gtt.plot<-plot_model(m0, title="Week 14 Male High Fat Diet GTT AUC\n", show.values=T, value.offset = 0.3, vline.color = "grey", dot.size = 3.3, line.size = 1.5, colors="black")+ theme_538(base_size = 12, base_family = "")+ font_size(title = 16, labels.x = 14, labels.y = 14) m.hfd.gtt.plot m0<-lmer(auc.trap~Group+Weight+(1|DamID),data=m.lfd.gtt) summary(m0) plot_model(m0) m.lfd.gtt.plot<-plot_model(m0, title="Week 14 Male Low Fat Diet GTT AUC\n", show.values=T, value.offset = 0.3, vline.color = "grey", dot.size = 3.3, line.size = 1.5, colors="black")+ theme_538(base_size = 12, base_family = "")+ font_size(title = 16, labels.x = 14, labels.y = 14) m.lfd.gtt.plot summary(glht(aov(Weight~Group,data=fem.gtt), linfct=mcp(Group="Tukey"))) summary(glht(aov(Weight~Group,data=mal.gtt), linfct=mcp(Group="Tukey")))