##################################################################### # R code to the manuscript # "Right on track? Performance of satellite telemetry in terrestrial wildlife research" # by M.P.G. Hofman, M.W. Hayward, N. Balkenhol, et al. # # March 2019 # # For more details, contact mpghofman@gmail.com # # This script runs a beta gamboostLSS model for the Fix Success Rate (FSR), # calculates the empirical point-wise confidence intervals, # and plots the variables with their confidence intervals. ##################################################################### library(dplyr) library(stringr) library(ggplot2) library(mboost) library(gamboostLSS) library(lubridate) ### load overall dataframe and subset for FSR calculation #### overallFrame<- as.data.frame(read.csv("S10 Data.csv")) ### set filter, weighting and predictor variables for the FSR model ## FSRfilters <- c("oneBrand") FSRweights <- "numTag" FSRpredictors <- c("qualCovLab","forDen2","terRug", "wmTTF", "forTyp","wmYop", "quanCov", "speBurHib", "speHeight", "mquanTri") # add "mainRet2" for the Overall Fix Succcess Rate FSRresponse <- "adjFSR" FSRrandomeffect <- c("mainBrand5") ### subset the overall dataframe for FSR analysis and further preparations FSRframe <- overallFrame[,c(FSRfilters, FSRweights, FSRpredictors, FSRresponse,FSRrandomeffect)] FSRframe <- FSRframe[-(which(is.na(FSRframe$adjFSR))),] FSRframe <- FSRframe[(which(!is.na(FSRframe$numTag))),] ### other adjustments FSRframe$logspeHeight <- log(FSRframe$speHeight) # log-transform species heigth FSRframe$mainBrand5 <- as.factor(FSRframe$mainBrand5) FSRframe$speBurHib <- as.factor(FSRframe$speBurHib) FSRframe$forTyp <- as.factor(FSRframe$forTyp) FSRframe$forDen2 <- as.factor(FSRframe$forDen2) FSRframe$qualCovLab <- as.factor(FSRframe$qualCovLab) FSRframe$terRug <- as.factor(FSRframe$terRug) FSRframe$wmYop <- as.numeric(FSRframe$wmYop) FSRframe$mquanTri <- as.numeric(FSRframe$mquanTri) ### FUNCTION: Run a gambooslss model and cut it off at the optimised mstop determined by cross-validation #### full.beta.gamboostLSS <- function(data,formula,weights,control) { ## run model # weights <- FSR.weights; formula <- FSR.mA; control <- FSR.boostControl; data <- FSR.dat mod <- gamboostLSS(formula, weights = weights, control = control, families = BetaLSS(stabilization = "MAD"), data = data) ## find the optimal mstop value ## set up the cross validation runs densegrid <- make.grid(max = c(mu = 100, phi = 100), min = 10, length.out = 10, dense_mu_grid = TRUE) cores <- ifelse(grepl("linux|apple", R.Version()$platform), 2, 1) folds <- cv(model.weights(mod), type = "subsampling", prob = .75) ## run cross validation cvr <- cvrisk(mod, grid = densegrid, folds = folds, mc.cores = cores) ## extract mstop and set the model results to it mstop(mod) <- mstop(cvr) return(mod) } ### model preparation #### ### (manually mean-center continuous variables when using bols() without intercept)!! theSeed <- 3 set.seed(theSeed) ### the model FSR.incl <- c("forTyp", "qualCovLab","forDen2","terRug", "speBurHib", "wmTTF","wmYop", "logspeHeight", "mainBrand5", "quanCov", "mquanTri","numTag","adjFSR") FSR.dat <- na.omit(FSRframe[,FSR.incl]) FSR.weights <- FSR.dat$numTag FSR.mA <- as.formula(adjFSR ~ bols(forTyp) + bols(qualCovLab) + bols(forDen2) + bols(terRug) + bols(speBurHib) + brandom(mainBrand5) + mquanTri + quanCov + logspeHeight + wmTTF + wmYop) FSR.boostControl <- boost_control(mstop = c(100,100), risk = "inbag", trace = T) FSR.mod <- full.beta.gamboostLSS(FSR.dat,FSR.mA,FSR.weights,FSR.boostControl) summary(FSR.mod) emp_risk <- risk(FSR.mod, merge = TRUE) tail(emp_risk, n = 1) ### store final model results: # final.FSR.model.fit <- FSR.mod # final.FSR.model <- FSR.mA ### FUNCTION: randomly subsample a data frame (without replacement)#### ## (source: https://gist.github.com/ramhiser/1421185 ) boot_subsample <- function(x, subsample_size) { x[sample(x = seq_len(nrow(x)), size = subsample_size), ] } ### calculation of confidence bands #### ## create an empty list to store the bootstrap models set.seed(theSeed) boot.out <- list() ## set global model parameters (should be the same as the full test model) ## we keep the mstop values the same to avoid the subsampling needed for the cross validation tboostControl <- boost_control(mstop = mstop(FSR.mod), risk = "inbag", trace = T) ## run the bootraps. fileConnc <- sink(paste0("FSR_Log_",str_replace_all(today(),"\\D", ""), "_", theSeed,".txt")) # start a basic log file print(paste0("The seed was set to ", theSeed));print(FSR.mA);print(summary(FSR.mod)) boot.n <- 999; print(paste0("Number of bootstraps = ",boot.n)) sub_size <- floor(nrow(FSR.dat)*.75) for (i in 1:boot.n) { strt <- Sys.time(); print(paste0("bootstrap ",i," started at ", strt)) ## subsample dataset (75% of the data) without replacement idat <- boot_subsample(FSR.dat,sub_size) ## set local weights iweights <- idat$numTag boot.out[[i]] <- gamboostLSS(FSR.mA, weights = iweights, control = tboostControl, families = BetaLSS(stabilization = "MAD"), data = idat) bend <- Sys.time(); print(paste0("bootstrap ",i," took")); print(bend-strt); print("") } sink(fileConnc) ## Create a list of dataframes for each covariate in the models for both distribution parameters) ## the first column being the fitted values of the original model (FSR.mod) ## the other columns will contain the predicted values using the bootstrap models ## first: get covariate names used in the model cov.names <- names(FSR.dat)[-c(match("adjFSR",names(FSR.dat)),match("numTag",names(FSR.dat)))] ## create lists for mu and phi data frames mu.list <- list() phi.list <- list() ## populate lists with single column data frames (fitted FSR.mod values) for each variable for (i in 1:length(cov.names)) { name=cov.names[i]; muname <- paste0(name,".mu"); phiname <- paste0(name,".phi") mu.list[[i]] <- assign(muname, as.data.frame(fitted(FSR.mod$mu, which = name))) names(mu.list)[i] <- muname phi.list[[i]] <- assign(phiname, as.data.frame(fitted(FSR.mod$phi, which = name))) names(phi.list)[i] <- phiname } ## predict partial effects for the original data using the bootstrap models ## and attach predictions for each bootstrap model to the dataframes for (i in 1:length(boot.out)) { for (j in 1:length(cov.names)) { ijpred <- predict(boot.out[[i]], newdata = FSR.dat, which = cov.names[j]) mu.list[[paste0(cov.names[j],".mu")]] <- cbind(mu.list[[paste0(cov.names[j],".mu")]], ijpred$mu) names(mu.list[[paste0(cov.names[j],".mu")]])[ncol(mu.list[[paste0(cov.names[j],".mu")]])] <- paste0("run",i) phi.list[[paste0(cov.names[j],".phi")]] <- cbind(phi.list[[paste0(cov.names[j],".phi")]], ijpred$phi) names(phi.list[[paste0(cov.names[j],".phi")]])[ncol(phi.list[[paste0(cov.names[j],".phi")]])] <- paste0("run",i) } } ### FUNCTION: Calculate the percentiles per row in each data frame and add to data frames #### addQuants <- function(x) { newCols <- data.frame("lower" = numeric(), "upper" = numeric()) for (j in 1:nrow(x)) { quant <- quantile(x[j,], c(.025, .975)) newCols <- rbind(newCols,quant) } result <- cbind(x,newCols) return(result) } ### apply addQuants function #### mu.list <- lapply(mu.list, function(x) addQuants(x)) # for phi we have to exclude phi.list[[5]] (=speBurHib) from the operation as it does not have any predictions for (i in 1:length(phi.list)) { if (i == 5) {next} else {phi.list[[i]] <- addQuants(phi.list[[i]])} } ### FUNCTION: Multiple plots #### # # source: http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/ # # ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects) # - cols: Number of columns in layout # - layout: A matrix specifying the layout. If present, 'cols' is ignored. # # If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE), # then plot 1 will go in the upper left, 2 will go in the upper right, and # 3 will go all the way across the bottom. # multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) { library(grid) # Make a list from the ... arguments and plotlist plots <- c(list(...), plotlist) numPlots = length(plots) # If layout is NULL, then use 'cols' to determine layout if (is.null(layout)) { # Make the panel # ncol: Number of columns of plots # nrow: Number of rows needed, calculated from # of cols layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), ncol = cols, nrow = ceiling(numPlots/cols), byrow = T) } if (numPlots==1) { print(plots[[1]]) } else { # Set up the page grid.newpage() pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout)))) # Make each plot, in the correct location for (i in 1:numPlots) { # Get the i,j matrix positions of the regions that contain this subplot matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE)) print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, layout.pos.col = matchidx$col)) } } } ### create list of mu.plots #### mu.plots <- list() for (i in 1:length(mu.list)) { x <- mu.list[[i]] xname <- str_replace(names(mu.list[i]),".mu","") if (is.factor(FSR.dat[,xname])) { ## for categoricals pldf.x <- data.frame(var = FSR.dat[,xname], fpartial = x[,1], low = x[,ncol(x)-1], high = x[,ncol(x)]) mu.plots[[i]] <- ggplot(data=pldf.x) + xlab(paste0(xname, ".mu")) + geom_boxplot(aes(x = var, y = fpartial)) + geom_boxplot(aes(x = var, y = low), colour = "grey") + geom_boxplot(aes(x = var, y = high), colour = "grey") } else { ## for continuous pldf.x <- data.frame(var = FSR.dat[,xname], fpartial = x[,1], low = x[,ncol(x)-1], high = x[,ncol(x)]) mu.plots[[i]] <- ggplot(data=pldf.x) + xlab(paste0(xname, ".mu")) + geom_ribbon(aes(x = var, ymin=low, ymax = high), fill = "grey") + geom_line(aes(x = var, y = fpartial))# + #geom_point(aes(x = var, y = fpartial)) #geom_line(aes(x = var, y = low), colour = "grey") + #geom_line(aes(x = var, y = high), colour = "grey") + } } png(filename=paste0("FSR_mu",str_replace_all(today(),"\\D", ""), "_", theSeed,".png"), width = 900, height = 900) multiplot(plotlist = mu.plots, cols = ifelse(length(mu.list)<7, 2,3)) dev.off() ### create list of phi.plots #### phi.plots <- list() for (i in 1:length(phi.list)) { if (i == 5) {next} else { # exclude plotting phi.list[[5]] (speBurHib) as it did not produce any perdictions x <- phi.list[[i]] xname <- str_replace(names(phi.list[i]),".phi","") if (is.factor(FSR.dat[,xname])) { ## for categoricals pldf.x <- data.frame(var = FSR.dat[,xname], fpartial = x[,1], low = x[,ncol(x)-1], high = x[,ncol(x)]) phi.plots[[i]] <- ggplot(data=pldf.x) + xlab(paste0(xname, ".phi")) + geom_boxplot(aes(x = var, y = fpartial)) + geom_boxplot(aes(x = var, y = low), colour = "grey") + geom_boxplot(aes(x = var, y = high), colour = "grey") } else { ## for continuous pldf.x <- data.frame(var = FSR.dat[,xname], fpartial = x[,1], low = x[,ncol(x)-1], high = x[,ncol(x)]) phi.plots[[i]] <- ggplot(data=pldf.x) + xlab(paste0(xname, ".phi")) + geom_ribbon(aes(x = var, ymin=low, ymax = high), fill = "grey") + geom_line(aes(x = var, y = fpartial))# + #geom_point(aes(x = var, y = fpartial)) #geom_line(aes(x = var, y = low), colour = "grey") + #geom_line(aes(x = var, y = high), colour = "grey") + } } } png(filename=paste0("FSR_phi",str_replace_all(today(),"\\D", ""), "_", theSeed,".png"), width = 900, height = 900) multiplot(plotlist = phi.plots, cols = ifelse(length(mu.list)<7, 2,3)) dev.off() ### Code to produce the plots as in PlosONE publication #### xplots <- list() varNamList <- c("Species height (mm)","Year of purchase","Burrowing/Hibernating","Time to fix (sec)", "Forest density","Forest type", "Main brand","Terrain ruggedness") forDenLabs <- c("No forest","","","Medium","","","Dense") forTypLabs <- c("NF","TE","TD","TM", "SE", "SD", "SM") speBurHibLabs <- c("No","Yes") terRugLabs <- c("Rugged", "","", "Flat" ) mainBrand5Labs <- c(LETTERS[1:8]) for (i in c(8,7,5,6,3,1,9,4)) { x <- mu.list[[i]] xname <- str_replace(names(mu.list[i]),".mu","") seqIndex <- as.numeric(match(i,c(8,7,5,6,3,1,9,4))) if (is.factor(FSR.dat[,xname])) { pldf.x <- data.frame(var = FSR.dat[,xname], fpartial = x[,1], low = x[,ncol(x)-.5], high = x[,ncol(x)]) if(i == 3) { xplots[[seqIndex]] <- ggplot(data=pldf.x) + geom_boxplot(aes(x = var, y = fpartial)) + geom_boxplot(aes(x = var, y = low), colour = "grey") + geom_boxplot(aes(x = var, y = high), colour = "grey") + ylim(-1.2,.8) + labs(x=varNamList[seqIndex]) + scale_x_discrete(labels = forDenLabs) + theme(axis.text.x = element_text(colour = "black"), axis.title.x=element_text(face="bold", vjust = -.5), axis.text.y = element_text(colour = "black"), axis.title.y=element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "grey")) } else { if(i == 1) { xplots[[seqIndex]] <-ggplot(data=pldf.x) + geom_boxplot(aes(x = var, y = fpartial)) + geom_boxplot(aes(x = var, y = low), colour = "grey") + geom_boxplot(aes(x = var, y = high), colour = "grey") + ylim(-1.2,.8) + labs(x=varNamList[seqIndex]) + scale_x_discrete(labels = forTypLabs) + theme(axis.text.x = element_text(colour = "black"), axis.title.x=element_text(face="bold", vjust = -.5), axis.text.y = element_text(colour = "black"), axis.title.y=element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "grey")) } else { if(i == 5) { xplots[[seqIndex]] <-ggplot(data=pldf.x) + geom_boxplot(aes(x = var, y = fpartial)) + geom_boxplot(aes(x = var, y = low), colour = "grey") + geom_boxplot(aes(x = var, y = high), colour = "grey") + ylim(-1.2,.8) + labs(x=varNamList[seqIndex]) + scale_x_discrete(labels = speBurHibLabs) + theme(axis.text.x = element_text(colour = "black"), axis.title.x=element_text(face="bold", vjust = -.5), axis.text.y = element_text(colour = "black"), axis.title.y=element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "grey")) } else { if(i == 4) { xplots[[seqIndex]] <-ggplot(data=pldf.x) + geom_boxplot(aes(x = var, y = fpartial)) + geom_boxplot(aes(x = var, y = low), colour = "grey") + geom_boxplot(aes(x = var, y = high), colour = "grey") + ylim(-1.2,.8) + labs(x=varNamList[seqIndex]) + scale_x_discrete(labels = terRugLabs) + theme(axis.text.x = element_text(colour = "black"), axis.title.x=element_text(face="bold", vjust = -.5), axis.text.y = element_text(colour = "black"), axis.title.y=element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "grey")) } else { xplots[[seqIndex]] <- ggplot(data=pldf.x) + geom_boxplot(aes(x = var, y = fpartial)) + geom_boxplot(aes(x = var, y = low), colour = "grey") + geom_boxplot(aes(x = var, y = high), colour = "grey") + ylim(-1.2,.8) + labs(x=varNamList[seqIndex], y="Partial effect") + scale_x_discrete(labels = mainBrand5Labs) + theme(axis.text.x = element_text(colour = "black"), axis.title.x=element_text(face="bold", vjust = -.5), axis.text.y = element_text(colour = "black"), axis.title.y=element_text(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "grey")) }}}} } else { ## for continuous pldf.x <- data.frame(var = FSR.dat[,xname], fpartial = x[,1], low = x[,ncol(x)-.5], high = x[,ncol(x)]) if (i == 8) { xplots[[seqIndex]] <- ggplot(data=pldf.x) + labs(x=varNamList[seqIndex], y="Partial effect") + geom_ribbon(aes(x = exp(var), ymin=low, ymax = high), fill = "grey") + geom_line(aes(x = exp(var), y = fpartial)) + ylim(-1.2,.8) + # coord_trans(xtrans = "exp") + # scale_x_exp() + theme(axis.text.x = element_text(colour = "black"), axis.title.x=element_text(face="bold", vjust = -.5), axis.text.y = element_text(colour = "black"), axis.title.y=element_text(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "grey")) } else { if(i == 7) { xplots[[seqIndex]] <- ggplot(data=pldf.x) + labs(x=varNamList[seqIndex]) + geom_ribbon(aes(x = var, ymin=low, ymax = high), fill = "grey") + geom_line(aes(x = var, y = fpartial)) + ylim(-1.2,.8) + # coord_trans(xtrans = "exp") + # scale_x_exp() + theme(axis.text.x = element_text(colour = "black", hjust = .75), axis.title.x=element_text(face="bold", vjust = -.5), axis.text.y = element_text(colour = "black"), axis.title.y=element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "grey")) } else { xplots[[seqIndex]] <- ggplot(data=pldf.x) + labs(x=varNamList[seqIndex], y="Partial effect") + geom_ribbon(aes(x = var, ymin=low, ymax = high), fill = "grey") + geom_line(aes(x = var, y = fpartial)) + ylim(-1.2,.8) + #geom_text(aes(x=min(var), y=max(high)), label=plotLabs[seqIndex]) + theme(axis.text.x = element_text(colour = "black", hjust = .75), axis.title.x=element_text(face="bold", vjust = -.5), axis.text.y = element_text(colour = "black"), axis.title.y=element_text(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "grey")) } } } } tiff(filename=paste0("FSRmuplots_", str_replace_all(today(),"\\D", ""), "_s", theSeed,".tiff"), width = 2250, height = 2250, compression = "lzw", res = 300) multiplot(plotlist = xplots, cols = ifelse(length(xplots)<7, 2,3)) dev.off() ### phi graphs ### names(phi.list) # mainBrand - phi.list[9] yplots <- list() varNamList <- c("Main brand", "Forest density", "Time to fix (sec)", "Terrain Ruggedness Index") forDenLabs <- c("No forest","","","Medium","","","Dense") forTypLabs <- c("NF","TE","TD","TM", "SE", "SD", "SM") speBurHibLabs <- c("No","Yes") terRugLabs <- c("Rugged", "","", "Flat" ) mainBrand5Labs <- c(LETTERS[1:8]) for (i in c(9,3,6,11)) { y <- phi.list[[i]] yname <- str_replace(names(phi.list[i]),".phi","") seqIndex <- as.numeric(match(i,c(9,3,6,11))) if (is.factor(FSR.dat[,yname])) { pldf.y <- data.frame(var = FSR.dat[,yname], fpartial = y[,1], low = y[,ncol(y)-.5], high = y[,ncol(y)]) if(i == 3) { yplots[[seqIndex]] <- ggplot(data=pldf.y) + geom_boxplot(aes(x = var, y = fpartial)) + geom_boxplot(aes(x = var, y = low), colour = "grey") + geom_boxplot(aes(x = var, y = high), colour = "grey") + ylim(-.5,1.3) + labs(x=varNamList[seqIndex]) + scale_x_discrete(labels = forDenLabs) + theme(axis.text.x = element_text(colour = "black"), axis.title.x=element_text(face="bold", vjust = -.5), axis.text.y = element_text(colour = "black"), axis.title.y=element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "grey")) } else { if(i == 1) { yplots[[seqIndex]] <-ggplot(data=pldf.y) + geom_boxplot(aes(x = var, y = fpartial)) + geom_boxplot(aes(x = var, y = low), colour = "grey") + geom_boxplot(aes(x = var, y = high), colour = "grey") + ylim(-.5,1.3) + labs(x=varNamList[seqIndex], y="Partial effect") + scale_x_discrete(labels = forTypLabs) + theme(axis.text.x = element_text(colour = "black"), axis.title.x=element_text(face="bold", vjust = -.5), axis.text.y = element_text(colour = "black"), axis.title.y=element_text(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "grey")) } else { if(i == 5) { yplots[[seqIndex]] <-ggplot(data=pldf.y) + geom_boxplot(aes(x = var, y = fpartial)) + geom_boxplot(aes(x = var, y = low), colour = "grey") + geom_boxplot(aes(x = var, y = high), colour = "grey") + ylim(-.5,1.3) + labs(x=varNamList[seqIndex], y="Partial effect") + scale_x_discrete(labels = speBurHibLabs) + theme(axis.text.x = element_text(colour = "black"), axis.title.x=element_text(face="bold", vjust = -.5), axis.text.y = element_text(colour = "black"), axis.title.y=element_text(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "grey")) } else { if(i == 4) { yplots[[seqIndex]] <-ggplot(data=pldf.y) + geom_boxplot(aes(x = var, y = fpartial)) + geom_boxplot(aes(x = var, y = low), colour = "grey") + geom_boxplot(aes(x = var, y = high), colour = "grey") + ylim(-.5,1.3) + labs(x=varNamList[seqIndex], y = "Partial effect") + scale_x_discrete(labels = terRugLabs) + theme(axis.text.x = element_text(colour = "black"), axis.title.x=element_text(face="bold", vjust = -.5), axis.text.y = element_text(colour = "black"), axis.title.y=element_text(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "grey")) } else { yplots[[seqIndex]] <- ggplot(data=pldf.y) + geom_boxplot(aes(x = var, y = fpartial)) + geom_boxplot(aes(x = var, y = low), colour = "grey") + geom_boxplot(aes(x = var, y = high), colour = "grey") + ylim(-.5,1.3) + labs(x=varNamList[seqIndex], y="Partial effect") + scale_x_discrete(labels = mainBrand5Labs) + theme(axis.text.x = element_text(colour = "black"), axis.title.x=element_text(face="bold", vjust = -.5), axis.text.y = element_text(colour = "black"), axis.title.y=element_text(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "grey")) }}}} } else { ## for continuous pldf.y <- data.frame(var = FSR.dat[,yname], fpartial = y[,1], low = y[,ncol(y)-.5], high = y[,ncol(y)]) if (i == 6) { yplots[[seqIndex]] <- ggplot(data=pldf.y) + labs(x=varNamList[seqIndex], y="Partial effect") + geom_ribbon(aes(x = var, ymin=low, ymax = high), fill = "grey") + geom_line(aes(x = var, y = fpartial)) + ylim(-.5,1.3) + theme(axis.text.x = element_text(colour = "black", hjust = .75), axis.title.x=element_text(face="bold", vjust = -.5), axis.text.y = element_text(colour = "black"), axis.title.y=element_text(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "grey")) } else { if(i == 11) { yplots[[seqIndex]] <- ggplot(data=pldf.y) + labs(x=varNamList[seqIndex]) + geom_ribbon(aes(x = var, ymin=low, ymax = high), fill = "grey") + geom_line(aes(x = var, y = fpartial)) + ylim(-.5,1.3) + theme(axis.text.x = element_text(colour = "black"), axis.title.x=element_text(face="bold", vjust = -.5), axis.text.y = element_text(colour = "black"), axis.title.y=element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "grey")) } else { yplots[[seqIndex]] <- ggplot(data=pldf.y) + labs(x=varNamList[seqIndex], y="Partial effect") + geom_ribbon(aes(x = var, ymin=low, ymax = high), fill = "grey") + geom_line(aes(x = var, y = fpartial)) + ylim(-.5,1.3) + #geom_text(aes(y=min(var), y=max(high)), label=plotLabs[seqIndex]) + theme(axis.text.x = element_text(colour = "black"), axis.title.x=element_text(face="bold", vjust = -.5), axis.text.y = element_text(colour = "black"), axis.title.y=element_text(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "grey")) } } } } tiff(filename=paste0("FSRphiplots_", str_replace_all(today(),"\\D", ""), "_s", theSeed,".tiff"), width = 2250, height = 2250, compression = "lzw", res = 300) multiplot(plotlist = yplots, cols = ifelse(length(yplots)<7, 2,3)) dev.off()