### R script for three-part partitioning of fractions and error calculation using matricies # 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, Flux1, and Flux2 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 all possible solutions library(plyr) data <- read.csv("Supplementary_Dataset_3.csv") # Imports file data # Displays top of your data, to check it is correct fractions <- function(dTotal1,dTotal2,dA,dB,dC1,dC2,FluxMean) { R <- matrix(c(dTotal1,dTotal2,1),ncol=1) D <- matrix(c(dA,dA,1,dB,dB,1,dC1,dC2,1),ncol=3) fit <- lm(R ~ 0 + D) coeffs<-data.frame(summary(fit)[4])[,1] fA <- coeffs[1] fB <- coeffs[2] fC <- coeffs[3] FluxA <- coeffs[1]*FluxMean FluxB <- coeffs[2]*FluxMean FluxC <- coeffs[3]*FluxMean m = c(fA,fB,fC,FluxA,FluxB,FluxC) return(m) # return(summary(fit)) } fractionscalculator <- function(x){ results <- data.frame(fA=as.numeric(""),fB=as.numeric(""),fC=as.numeric(""), FluxA=as.numeric(""), FluxB=as.numeric(""),FluxC=as.numeric("")) for (i in 1:dim(x)[1]) { results[i,] <- t(fractions(x[i,1],x[i,2],x[i,3],x[i,4],x[i,5],x[i,6],x[i,7])) } return(results) } # Creating the function that will calculate the three fractions, given the d13C values as inputs # This is done by making a 3x3 matrix with the d13C values dA, dB, dC1 / dA, dB, dC2 / 1,1,1. These are our X values in our linear model # We make a 3x1 matrix with the d13C values dTotal1 / dTotal2 / 1. These are the Y values in our linear model. # When we fit the linear model, it returns the three coefficients that best fit the two matrices, which correspond to our fA, fB, and fC values. alld13C <- expand.grid(data[,1:6]) alld13C <- alld13C[complete.cases(alld13C),] # Creates a dataframe with all possible values of each variable, excluding any NA values. FluxTable <- data.frame(dTotal1=as.numeric(""),dTotal2=as.numeric(""),Flux1=as.numeric(""),Flux2=as.numeric("")) for (i in 1:dim(data)[1]){ FluxTable[i,1] <- data[i,1] FluxTable[i,2] <- data[i,2] FluxTable[i,3] <- data[i,7] FluxTable[i,4] <- data[i,8] } FluxTable<-FluxTable[complete.cases(FluxTable),] FluxTable # Creating lookup table with just the Total d13C and Total flux alld13CFlux1 <- join(alld13C, data.frame(dTotal1=FluxTable[,1],Flux1=FluxTable[,3]), by = "dTotal1") alld13CFlux <- join(alld13CFlux1, data.frame(dTotal2=FluxTable[,2],Flux2=FluxTable[,4]), by = "dTotal2") FluxMean <- (alld13CFlux$Flux1 + alld13CFlux$Flux2)/2 alld13CFlux$FluxMean <- FluxMean alld13CFlux <- alld13CFlux[,c(1:6,9)] alld13CFlux[1:20,] # Adding the flux values to the corresponding d13C values allfractions<-fractionscalculator(alld13CFlux) # Creates a table reporting all possible values of each fraction # This can take a long time if you have a large dataset and a regular computer. fAma <- mean(allfractions$fA) fBma <- mean(allfractions$fB) fCma <- mean(allfractions$fC) FluxAma <- mean(allfractions$FluxA) FluxBma <- mean(allfractions$FluxB) FluxCma <- mean(allfractions$FluxC) DataSummaryAll<-data.frame(fA=fAma, fB=fBma, fC=fCma, FluxA=FluxAma, FluxB=FluxBma, FluxC=FluxCma) # Calculates mean fractions of each component, A, B, and C and stores them in a data frame DataSummaryAll fAva <- var(allfractions$fA) fBva <- var(allfractions$fB) fCva <- var(allfractions$fC) FluxAva <- var(allfractions$FluxA) FluxBva <- var(allfractions$FluxB) FluxCva <- var(allfractions$FluxC) DataSummaryAll<-rbind(DataSummaryAll,data.frame(fA=fAva, fB=fBva, fC=fCva, FluxA=FluxAva, FluxB=FluxBva, FluxC=FluxCva)) # Calculates the variances for each fraction, fA, fB, and fC and adds them to the summary data frame DataSummaryAll[3,]<-sqrt(DataSummaryAll[2,]) # Adds standard deviation to our Data Summary Table n=dim(FluxTable)[1] # Counts total experimental replicates DataSummaryAll[4,] <- DataSummaryAll[3,]/sqrt(n) # Adds standard error to the table alpha = 0.05 # Sets our alpha value of interest #DataSummaryAll[5,] <- DataSummaryAll[1,]-1.96*DataSummaryAll[4,] #DataSummaryAll[6,] <- DataSummaryAll[1,]+1.96*DataSummaryAll[4,] DataSummaryAll[5,] <- DataSummaryAll[1,]-qt(1-alpha/2,df=n-1)*DataSummaryAll[4,] DataSummaryAll[6,] <- DataSummaryAll[1,]+qt(1-alpha/2,df=n-1)*DataSummaryAll[4,] # Adds the lower and upper quantiles to the plot - note the function assumes two tails row.names(DataSummaryAll)<-c("Mean","Variance","StDev","SE","lQ","hQ") # Sets the final row names for the table write.table(DataSummaryAll,"ResultsSummaryMatrix.csv",sep=",",row.names=TRUE,col.names=NA) # Saves the summary data in a file in your working directory