library(gdata)
library(lme4)
activedir <- file.choose()  # all files should be in the same directory, just choose one
setwd(dirname(activedir))  # set the working directory so that everything is relative

#Process fertility data
fullfert<-read.xls("CITP_fertility.xls")
fertdata <- fullfert[ which(fullfert$Strain!='QX1211'), ] # strain QX1211 displays the mortal germline phenotype
fertreml<-lmer(Fertility~(1|Lab/Trial)+(1|Species/Strain)+(1|Lab:Strain)+(1|Lab:Species),data=fertdata)
fertprofile<-profile(fertreml)  # get confidence regions on estimates
summary(fertreml)
confint(fertprofile)
fertgml<-glmer(Fertility~(1|Lab/Trial)+(1|Species/Strain)+(1|Lab:Strain)+(1|Lab:Species),data=fertdata,family=poisson(link=sqrt))
fertprofile2<-profile(fertgml) 
summary(fertgml)
confint(fertprofile2)

#Process developmental rate (alpha-time) data
alphadata<-read.xls("CITP_alphatime.xls")
alphareml<-lmer(AlphaTime~(1|Lab/Trial)+(1|Species/Strain)+(1|Lab:Strain)+(1|Lab:Species),data=alphadata)
alphaprofile<-profile(alphareml)  # get confidence regions on estimates
summary(alphareml)
confint(alphaprofile)
devreml<-lmer(DevRate~(1|Lab/Trial)+(1|Species/Strain)+(1|Lab:Strain)+(1|Lab:Species),data=alphadata)
devprofile<-profile(devreml) 
summary(devreml)
confint(devprofile)

#Process lifespan data
baseline<-read.xls("CITP_lifespan_nocompounds.xls")
baselinereml<-lmer(DeathNoCen~(1|Lab/Experimenter/Trial/Plate)+(1|Species/Strain)+(1|Lab:Strain)+(1|Lab:Species),data=baseline)
baselineprofile<-profile(baselinereml)  # get confidence regions on estimates
summary(baselinereml)
confint(baselineprofile)
lbaselinereml<-lmer(LogDeath~(1|Lab/Experimenter/Trial/Plate)+(1|Species/Strain)+(1|Lab:Strain)+(1|Lab:Species),data=baseline)
lbaselineprofile<-profile(lbaselinereml)  # get confidence regions on estimates
summary(lbaselinereml)
confint(lbaselineprofile)
baselinegml<-glmer(DeathNoCen~(1|Lab/Experimenter/Trial/Plate)+(1|Species/Strain)+(1|Lab:Strain)+(1|Lab:Species),data=baseline,family=poisson(link=sqrt))
baselinegprofile<-profile(baselinegml)  # get confidence regions on estimates
summary(baselinegml)
confint(baselinegprofile)

#Process subset of strains used in compound study
basesub <- droplevels(baseline[ which(baseline$Strain %in% c( "N2","MY16","JU775","AF16","HK104","JU1348")),])
lbasesubreml<-lmer(LogDeath~(1|Lab/Experimenter/Trial/Plate)+(1|Species/Strain)+(1|Lab:Strain)+(1|Lab:Species),data=basesub)
lbasesubprofile<-profile(lbasesubreml)  # get confidence regions on estimates
summary(lbasesubreml)
confint(lbasesubprofile)
