"SOC.stockdif"<-function(crop.expert.data="expertdata.csv", land.use.beta="cult
.betas.csv", land.use.cov="LandUseCov.csv", mgmtbeta="mgmt
.betas.csv", mgmtcov= "MgmtCov.csv", C.input.beta="input
.betas.csv", C.input.cov="CinputCov.csv", x.variables="x
.variables.csv", SOC.ref=80, SOC.ref.sd=36, cropland.area
=56890,cropland.area.sd=2845, D = 20, nreps = 10, iseed
=1234, ncases = 6, EF.ft=1, EF.med.input = 1,
nmitscenarios=1, return.option=1)
Soil Organic Carbon Stock Change
This R Script estimates the change in soil organic carbon stock in cropland soils between 2005 and 2010. This function uses a Monte Carlo analysis methods from the 2006 IPCC guidelines. This estimation looks at the carbon stock changes among different crop management types such as full and no till including levels of C input.
This is commented code to serve as descriptors for data types used within the function
# Script developed by N Wiley
# Originally Developed: September 27, 2022
# Last Update: October 20, 2022
#
#Script estimates the SOC stock change different in cropland soils between 2005 and 2020, also showing SOC changes w/
#different management types (full till, no till,) and level of input (low, med, high)
####### Arguments
# crop.expert.data Input file in csv format with data given in proportions of land within defined management cases
# iseed Initial seed value for random draws
# nreps Number of Monte Carlo iterations
# ncases Number of Management cases from the expert data
# beta Parameter from lME model used to derive emission factors
# cov.matrix Matrix with covariance from LME model used to derive emission factors
# D Stock change dependence (years) (20 comes from IPCC guidelines VOL 4 ch 2)
# SOC.ref Reference soil carbon stock
# SOC.ref.sd Error associated with soil carbon stock
# cropland.area Land area (hectares)
# cropland.area.sd Error associated with area
# return.option
##
#___________ Begin Script ___________
{ # Set Seed
set.seed(iseed)
# Load Library
#___________ Import Data ____________
<-read.csv(file=crop.expert.data, header=TRUE, sep=",", fill=FALSE)
expert.data
##Import Betas for EF's
<-read.csv(file=land.use.beta, header=F, sep=",")
landuse.beta#landuse.beta<-c(landuse.beta[1,])
<-as.vector(landuse.beta)
LU.beta#landuse.beta<-c(land.use.beta[,1])
<-read.csv(file=mgmtbeta, header=F, sep=",", fill=FALSE )
mgmt.beta#mgmt.beta<-c(mgmtbeta[,1])
<-read.csv(file=C.input.beta, header=F, sep=",", fill=FALSE)
Cinput.beta#Cinput.beta<-c(C.input.beta[,1])
<-read.csv(file=landuse.cov, header=F, sep=",", fill=FALSE)
landuse.cov<-read.csv(file=mgmtcov, header=F, sep=",", fill=FALSE)
mgmt.cov<-read.csv(file=Cinput.cov, header=F, sep=",", fill=FALSE)
Cinput.cov
<-read.csv(file=x.variables, header=TRUE, sep=",", fill=FALSE) x.variables
#______________ Create Checks ________________
# Check that cropland area and uncertainty is greater than 0
<-cropland.area>=0
check.cropland.areaif(!check.cropland.area) {stop("Cropland area is not greater than 0.")}
<-cropland.area.sd>=0
check.cropland.area.sdif(!check.cropland.area.sd) {stop("Cropland area standard deviation is not greater than 0.")}
# Check that SOC.ref and uncertainty is greater than 0
<-SOC.ref>=0
check.SOC.refif(!check.SOC.ref) {stop("SOC reference stock is not greater than 0.")}
<-SOC.ref.sd>=0
check.SOC.ref.sdif(!check.SOC.ref.sd) {stop("SOC reference stock standard deviation is not greater than 0.")}
# Check that proportion amounts equal 1 in each year for each expert
#______________ Probabilistic draws for Monte Carlo Simulation_______________
# Simulate nreps for SOC.ref
<-rnorm(nreps, mean = SOC.ref, sd=SOC.ref.sd)
SOC.ref.sim# SOC.ref.sim<-as.matrix(t(SOC.ref.sim))
# Simulate nreps for cropland.area
<-rnorm(nreps, mean = cropland.area, sd=cropland.area.sd)
area.sim# area.sim<-as.matrix(area.sim)
# Draw samples from expert proportions (expert.data)
#expert.sim<-matrix(0,ncol = nreps, nrow = 12)
<-expert.data[sample(ncol(expert.data), size = nreps, replace = TRUE)] expert.sim
#______________ Estimate Area _____________
<-sweep(expert.sim, MARGIN=2, area.sim, '*')
area.prop.comb
#These parameters come from the previous LME models to create EF betas and covariances
# Determine number of parameters for each EF
<-length(landuse.beta)
numpar.landuse<-length(mgmt.beta)
numpar.mgmt<-length(Cinput.beta)
numpar.Cinput
# Compute Cholesky decomposition for each EF
<-t(chol(landuse.cov))
chol.decomp.landuse<-t(chol(mgmt.cov))
chol.decomp.mgmt<-t(chol(Cinput.cov))
chol.decomp.Cinput
# Generate random draws of 0 for each EF in a normal distribution
<-matrix(rnorm(nreps*numpar.landuse),numpar.landuse,nreps)
random.landuse<-matrix(rnorm(nreps*numpar.mgmt),numpar.mgmt,nreps)
random.mgmt<-matrix(rnorm(nreps*numpar.Cinput),numpar.Cinput,nreps)
random.Cinput
# Simulated betas
<-(chol.decomp.landuse%*%random.landuse)
landuse.EF.beta.sim.intermediatecalc<-matrix(0, nrow=5, ncol=nreps)
landuse.EFbeta.simfor (e in 1:nreps) {
<- ((landuse.EF.beta.sim.intermediatecalc[,e]+landuse.beta[,e]))
landuse.EFbeta.sim[,e]
}<-(chol.decomp.mgmt%*%random.mgmt)+mgmt.beta
mgmt.EF.beta.sim<-(chol.decomp.Cinput%*%random.Cinput)+Cinput.beta
Cinput.EF.beta.sim
# __________ Estimate Emission Factors __________ (EF = intercept + B1X1) --> put x.cult in model (create csv and import) - then check to make sure that EF reps fit within the CI
# Predictor variables from Assignment 2
# only need wet factors, not dry, because soils are in only a wet climate (don't use dry factors!)
<-as.matrix(x.variables[,1])
x.landuse<-na.omit(x.landuse)
x.landuse<-as.vector(x.variables[,2])
x.rt.wet<-na.omit(x.rt.wet)
x.rt.wet<-as.vector(x.variables[,3])
x.nt.wet<-na.omit(x.nt.wet)
x.nt.wet<-as.vector(x.variables[,4])
x.low<-na.omit(x.low)
x.low<-as.vector(x.variables[,5])
x.high<-na.omit(x.high)
x.high
# Land use EF reps
# The sweep function multiples the beta sim with the predictor variables to get the components of the EF
# EF.landuse.sweep<-sweep(landuse.EFbeta.sim, 1, x.landuse, "*")
#EF.landuse<-apply(EF.landuse.sweep, MARGIN = 1, FUN = sum)
# EF.landuse<-as.matrix(t(EF.landuse))
<-matrix(0, nrow=numpar.landuse, ncol=nreps)
EF.landusefor (e in 1:nreps) {
<-(landuse.EFbeta.sim[e,]*x.landuse)
EF.landuse[,e]
}
# Management EF reps
<-sweep(mgmt.EF.beta.sim, 1, x.rt.wet, "*")
EF.rt.wet.sweep<-apply(EF.rt.wet.sweep, MARGIN = 2, FUN = sum)
EF.rt.wet<-as.matrix(t(EF.rt.wet))
EF.rt.wet
<-sweep(mgmt.EF.beta.sim, 1, x.nt.wet, "*")
EF.nt.wet.sweep<-apply(EF.nt.wet.sweep, MARGIN = 2, FUN = sum)
EF.nt.wet<-as.matrix(t(EF.nt.wet))
EF.nt.wet
# CInput EF reps
<-sweep(Cinput.EF.beta.sim, 1, x.low, "*")
EF.low.sweep<-apply(EF.low.sweep, MARGIN = 2, FUN = sum)
EF.low<-as.matrix(t(EF.low))
EF.low
<-sweep(Cinput.EF.beta.sim, 1, x.high, "*")
EF.high.sweep<-apply(EF.high.sweep, MARGIN = 2, FUN = sum)
EF.high<-as.matrix(t(EF.high)) EF.high
#______________ Probabilistic Results _______________
# SOC stock changes by case
# Case 1: low input, full till
<-matrix(0, nrow = 1, ncol= nreps)
SOC.low.ftfor(r in (1:nreps)) {
<-((SOC.ref.sim[,r]*EF.landuse[,r]*EF.ft*EF.low[,r]*area.prop.comb[7,r])-
SOC.low.ft[,r]*EF.landuse[,r]*EF.ft*EF.low[,r]*area.prop.comb[1,r]))/D
(SOC.ref.sim[,r]
}
# Case 2: low input, no till
<-matrix(0, nrow = 1, ncol= nreps)
SOC.low.ntfor(r in (1:nreps)) {
<-((SOC.ref.sim[,r]*EF.landuse[,r]*EF.nt.wet[,r]*EF.low[,r]*area.prop.comb[8,r])-
SOC.low.nt[,r]*EF.landuse[,r]*EF.nt.wet[,r]*EF.low[,r]*area.prop.comb[2,r]))/D
(SOC.ref.sim[,r]
}
# Case 3: medium input, full till
<-matrix(0, nrow = 1, ncol= nreps)
SOC.med.ftfor(r in (1:nreps)) {
<-((SOC.ref.sim[,r]*EF.landuse[,r]*EF.ft*EF.med.input*area.prop.comb[9,r])-
SOC.med.ft[,r]*EF.landuse[,r]*EF.ft*EF.med.input*area.prop.comb[3,r]))/D
(SOC.ref.sim[,r]
}
# Case 4: medium input, no till
<-matrix(0, nrow = 1, ncol= nreps)
SOC.med.ntfor(r in (1:nreps)) {
<-((SOC.ref.sim[,r]*EF.landuse[,r]*EF.nt.wet[,r]*EF.med.input*area.prop.comb[10,r])-
SOC.med.nt[,r]*EF.landuse[,r]*EF.nt.wet[,r]*EF.med.input*area.prop.comb[4,r]))/D
(SOC.ref.sim[,r]
}
# Case 5: high input, full till
<-matrix(0, nrow = 1, ncol= nreps)
SOC.high.ftfor(r in (1:nreps)) {
<-((SOC.ref.sim[,r]*EF.landuse[,r]*EF.ft*EF.high[,r]*area.prop.comb[11,r])-
SOC.high.ft[,r]*EF.landuse[,r]*EF.ft*EF.high[,r]*area.prop.comb[5,r]))/D
(SOC.ref.sim[,r]
}
# Case 6: high input, no till
<-matrix(0, nrow = 1, ncol= nreps)
SOC.high.ntfor(r in (1:nreps)) {
<-((SOC.ref.sim[,r]*EF.landuse[,r]*EF.nt.wet[,r]*EF.high[,r]*area.prop.comb[12,r])-
SOC.high.nt[,r]*EF.landuse[,r]*EF.nt.wet[,r]*EF.high[,r]*area.prop.comb[6,r]))/D
(SOC.ref.sim[,r]
}
# Create results matrix
<-matrix(0, nrow = 1, ncol = nreps)
C.stockdif.totalfor (r in (1:nreps)) {
<-SOC.low.ft[,r]+SOC.low.nt[,r]+SOC.med.ft[,r]+SOC.med.nt[,r]+SOC.high.ft[,r]+SOC.high.nt[,r]
C.stockdif.total[,r]
}
#___________ Estimate means and confidence intervals of C stock change 2005-2020 ________
# Create matrix with col 1 = median, col 2 = 2.5 percentile, and col 3 = 97.5 percentile
<-matrix(0, nrow = 1, ncol = 3)
C.stockdif.results1]<-mean(C.stockdif.total)
C.stockdif.results[,<-quantile(C.stockdif.total, probs = c(0.025,0.975))
q2]<-q[1]
C.stockdif.results[,3]<-q[2]
C.stockdif.results[,
#___________ Return Statements ____________
if(return.option ==1) {
return(list("Island X mean C stock change in mineral cropland soils (C tonnes /yr)"=C.stockdif.results[,1],
"2.5 percentile C stock change"=C.stockdif.results[,2], "97.5 percentile C stock change"=C.stockdif.results[,3]))
}if(return.option ==2) {
return(hist(x=C.stockdif.total, col = "blue3", main = "Simulated SOC change for all replicates from Monte Carlo Simulation",
xlab = "SOC change replicates (C tonnes/yr)", xlim= c(-50000,100000), labels = TRUE))
}
#____________ End Script ________________
}