### R script for three-part partitioning of fractions and error calculation using Taylor series error propagation # Make sure you are in the correct working directory # To use this script, prepare a .csv file with the following headings, from left to right: dTotal1, dTotal2, dA, dB, dC1, dC2 (the d13C values), Ftotal1, and Ftotal2 (the fluxes or total stocks) as described in the accompanying paper, and all the values for each parameter listed in each column. The example .csv file associated with this script is called "Supplementary_Dataset_3.csv" ############# Calculating fractions library(plyr) library(boot) # Activates the required R packages data <- read.csv("Supplementary_Dataset_3.csv") # Imports file head(data) # Displays top of your data, to check it is correct means <- colMeans(data, na.rm = TRUE, dims = 1) # Calculates mean value for each variable dTotal1m <- as.vector(means[1]) dTotal2m <- as.vector(means[2]) dAm <- as.vector(means[3]) dBm <- as.vector(means[4]) dC1m <- as.vector(means[5]) dC2m <- as.vector(means[6]) # Extracts means and stores them as variables variance <- function(x) var(x,na.rm=TRUE) variances <- colwise(variance)(data) # Calculates variance for each variable. dTotal1v <- as.vector(variances[,1]) dTotal2v <- as.vector(variances[,2]) dAv <- as.vector(variances[,3]) dBv <- as.vector(variances[,4]) dC1v <- as.vector(variances[,5]) dC2v <- as.vector(variances[,6]) # Stores each variance as a variable fCm <- (dTotal1m-dTotal2m)/(dC1m-dC2m) fAm1 <- ((dTotal1m -dBm + fCm*(dBm - dC1m)) / (dAm-dBm)) fAm2 <- ((dTotal2m -dBm + fCm*(dBm - dC2m)) / (dAm-dBm)) fAm <- (fAm1+fAm2)/2 fBm <- 1 - fCm - fAm # Calculates mean fractions of each component, A, B, and C. Note that both approaches for fAm (fAm1 or fAm2) give you the same value. fCv <- (dTotal1v+dTotal2v)/(dC1m-dC2m)^2+(dTotal1m-dTotal2m)^2*(dC1v+dC2v)/(dC1m-dC2m)^4 fAv1 <- (dTotal1v)/(dAm-dBm)^2+(fCm*(dAm-dC1m)+dTotal1m-dAm)^2/(dAm-dBm)^4*dBv+(dBm-dC1m)^2/(dAm-dBm)^2*fCv+fCm^2/(dAm-dBm)^2*dC1v+(dTotal1m-dBm+fCm*dBm-fCm*dC1m)^2/(dAm-dBm)^4*dAv fAv2 <- (dTotal2v)/(dAm-dBm)^2+(fCm*(dAm-dC2m)+dTotal2m-dAm)^2/(dAm-dBm)^4*dBv+(dBm-dC2m)^2/(dAm-dBm)^2*fCv+fCm^2/(dAm-dBm)^2*dC2v+(dTotal2m-dBm+fCm*dBm-fCm*dC2m)^2/(dAm-dBm)^4*dAv fAv<-(fAv1+fAv2)/2 fBv1 <- fCv + fAv1 fBv2 <- fCv + fAv2 fBv<-(fBv1+fBv2)/2 # Calculates the variances for each fraction, fA, fB, and fC. Note we can use both fAv1 and fAv2 to calculate the variance for fA. We use their mean value. FTotal<-c(data[(1:length(na.omit(data[,7]))),7],data[(1:length(na.omit(data[,8]))),8]) FTotalm<-mean(FTotal) FTotalv<-variance(FTotal) # Takes the mean and variance of the two total fluxes Fm <- FTotalm*(c(fAm,fBm,fCm)) # Calculates the mean flux for each component, A, B, and C. Fv <- FTotalv*c(fAm,fBm,fCm)^2+FTotalm^2*c(fAv,fBv,fCv) # Calculates the total fluxes and their associated variances for each component, A, B, and C. DataSummary<-data.frame(fA=fAm, fB=fBm, fC=fCm, FA=Fm[1],FB=Fm[2],FC=Fm[3],row.names=c("Mean")) DataSummary<-rbind(DataSummary,data.frame(fA=fAv, fB=fBv, fC=fCv, FA=Fv[1],FB=Fv[2],FC=Fv[3],row.names=c("Variance"))) # Adds data to the summary data frame. DataSummary[3,]<-sqrt(DataSummary[2,]) # Saves square root of variance (standard deviation) nsamples <- sum(!is.na(data[,1])) # This is used to calculate the standard error in the next step, and is only appropriate if you had equal numbers of samples for each of your Total treatments. CI <- qnorm(0.95) # Z score for the confidence interval of interest - here, 95%. You can change this to your liking DataSummary[4,]<-DataSummary[3,]/sqrt(nsamples) DataSummary[5,]<-DataSummary[1,]+DataSummary[4,]*CI DataSummary[6,]<-DataSummary[1,]-DataSummary[4,]*CI row.names(DataSummary)<-c("Mean","Variance","StDev","StErr","HighCI","LowCI") # Adds standard error, and the upper and lower 95% (or whatever Z value you have chosen) confidence intervals to our Data Summary Table write.table(DataSummary,"ResultsSummaryTaylor.csv",sep=",",row.names=TRUE,col.names=NA) # Saves the summary data in a file in your working directory