Skip to content

Commit 1c8cdb9

Browse files
committed
Merge branch 'master' into pches-statesandconstraints
2 parents 2a4d4bb + be3d382 commit 1c8cdb9

24 files changed

+6505
-6532
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Suggests:
2929
rmarkdown
3030
BugReports: https://github.com/JGCRI/gcamland/issues
3131
Collate:
32+
'adaptive_expectation.R'
3233
'ag_production_technology.R'
3334
'bayesian.R'
3435
'constants.R'
@@ -37,7 +38,6 @@ Collate:
3738
'generate_price_data.R'
3839
'generate_scenario_info.R'
3940
'helpers.R'
40-
'lagged_expectation.R'
4141
'land_allocator.R'
4242
'land_leaf.R'
4343
'land_node.R'

NAMESPACE

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export(SRB.SCENARIO.INFO)
1616
export(ScenarioInfo)
1717
export(add_parameter_data)
1818
export(as.ScenarioInfo)
19-
export(calc_lagged_expectation)
19+
export(calc_adaptive_expectation)
2020
export(export_results)
2121
export(get_PCHES_results)
2222
export(get_hindcast_AgProdChange)

R/lagged_expectation.R renamed to R/adaptive_expectation.R

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
# lagged_expectation.R
1+
# adaptive_expectation.R
22

3-
#' LaggedExpectation_calcExpectedYield
3+
#' AdaptiveExpectation_calcExpectedYield
44
#'
55
#' @details Calculate the expected yield for a LandLeaf using
6-
#' a lagged approach -- use linear combination of previous expectation and
6+
#' an adaptive approach -- use linear combination of previous expectation and
77
#' new information.
88
#' @param aLandLeaf LandLeaf to calculate expected yield for
99
#' @param aPeriod Current model period
1010
#' @param aScenarioInfo Scenario-related information, including names, logits, expectations
1111
#' @author KVC November 2017
12-
LaggedExpectation_calcExpectedYield <- function(aLandLeaf, aPeriod, aScenarioInfo) {
12+
AdaptiveExpectation_calcExpectedYield <- function(aLandLeaf, aPeriod, aScenarioInfo) {
1313
# Silence package checks
1414
sector <- year <- yield <- lm <- predict <- GCAM_commodity <- NULL
1515

@@ -30,7 +30,7 @@ LaggedExpectation_calcExpectedYield <- function(aLandLeaf, aPeriod, aScenarioInf
3030
previousExpectation <- aLandLeaf$mExpectedYield[[aPeriod - 1]]
3131

3232
# Get new information
33-
if( aScenarioInfo$mExpectationType == "LaggedCurr" ) {
33+
if( aScenarioInfo$mExpectationType == "HybridPerfectAdaptive" ) {
3434
newInformation <- aLandLeaf$mYield[[aPeriod]]
3535
} else {
3636
newInformation <- aLandLeaf$mYield[[aPeriod - 1]]
@@ -59,14 +59,14 @@ LaggedExpectation_calcExpectedYield <- function(aLandLeaf, aPeriod, aScenarioInf
5959
}
6060
yield_table$yield <- yield_table$base_yield * yield_table$yield_ratio
6161

62-
# Now, we call calc_lagged_expectation() to calculate the expectations
63-
if( aScenarioInfo$mExpectationType == "LaggedCurr" ) {
62+
# Now, we call calc_adaptive_expectation() to calculate the expectations
63+
if( aScenarioInfo$mExpectationType == "HybridPerfectAdaptive" ) {
6464
currYear <- get_per_to_yr(aPeriod, aScenarioInfo$mScenarioType)
65-
expectedYield <- calc_lagged_expectation(currYear, shareOld, yield_table, 'yield')
65+
expectedYield <- calc_adaptive_expectation(currYear, shareOld, yield_table, 'yield')
6666
} else {
6767
timestep <- get_per_to_yr(aPeriod+1, aScenarioInfo$mScenarioType) - get_per_to_yr(aPeriod, aScenarioInfo$mScenarioType)
6868
prevYear <- get_per_to_yr(aPeriod, aScenarioInfo$mScenarioType) - timestep
69-
expectedYield <- calc_lagged_expectation(prevYear, shareOld, yield_table, 'yield')
69+
expectedYield <- calc_adaptive_expectation(prevYear, shareOld, yield_table, 'yield')
7070
}
7171
}
7272

@@ -76,16 +76,16 @@ LaggedExpectation_calcExpectedYield <- function(aLandLeaf, aPeriod, aScenarioInf
7676
return(expectedYield)
7777
}
7878

79-
#' LaggedExpectation_calcExpectedPrice
79+
#' AdaptiveExpectation_calcExpectedPrice
8080
#'
8181
#' @details Calculate the expected price for a LandLeaf using
82-
#' a lagged approach -- use linear combination of previous expectation and
82+
#' an adaptive approach -- use linear combination of previous expectation and
8383
#' new information.
8484
#' @param aLandLeaf LandLeaf to calculate expected price for
8585
#' @param aPeriod Current model period
8686
#' @param aScenarioInfo Scenario-related information, including names, logits, expectations
8787
#' @author KVC November 2017
88-
LaggedExpectation_calcExpectedPrice <- function(aLandLeaf, aPeriod, aScenarioInfo){
88+
AdaptiveExpectation_calcExpectedPrice <- function(aLandLeaf, aPeriod, aScenarioInfo){
8989
# Silence package checks
9090
sector <- lm <- predict <- year <- price <- NULL
9191

@@ -106,17 +106,17 @@ LaggedExpectation_calcExpectedPrice <- function(aLandLeaf, aPeriod, aScenarioInf
106106

107107
if(aLandLeaf$mProductName[1] %in% unique(price_table$sector)) {
108108
# Calculate expected price
109-
if( aScenarioInfo$mExpectationType == "LaggedCurr" ) {
109+
if( aScenarioInfo$mExpectationType == "HybridPerfectAdaptive" ) {
110110
currYear <- get_per_to_yr(aPeriod, aScenarioInfo$mScenarioType)
111-
expectedPrice <- calc_lagged_expectation(currYear, shareOld, price_table, 'price')
111+
expectedPrice <- calc_adaptive_expectation(currYear, shareOld, price_table, 'price')
112112
} else {
113113
if( aPeriod > 1 ) {
114114
prevYear <- get_per_to_yr(aPeriod-1, aScenarioInfo$mScenarioType)
115115
} else {
116116
timestep <- get_per_to_yr(aPeriod+1, aScenarioInfo$mScenarioType) - get_per_to_yr(aPeriod, aScenarioInfo$mScenarioType)
117117
prevYear <- get_per_to_yr(aPeriod, aScenarioInfo$mScenarioType) - timestep
118118
}
119-
expectedPrice <- calc_lagged_expectation(prevYear, shareOld, price_table, 'price')
119+
expectedPrice <- calc_adaptive_expectation(prevYear, shareOld, price_table, 'price')
120120
}
121121

122122
} else {
@@ -155,7 +155,7 @@ LaggedExpectation_calcExpectedPrice <- function(aLandLeaf, aPeriod, aScenarioInf
155155
#' @param colname Name of the column that has the data for which we are
156156
#' computing the expectation (e.g. \code{'price'})
157157
#' @export
158-
calc_lagged_expectation <- function(t, alpha, datatbl, colname)
158+
calc_adaptive_expectation <- function(t, alpha, datatbl, colname)
159159
{
160160
year <- datatbl[['year']]
161161
x <- datatbl[[colname]]

R/ag_production_technology.R

+5-5
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ AgProductionTechnology_calcProfitRate <- function(aLandLeaf, aPeriod, aScenarioI
9999
} else if(aScenarioInfo$mExpectationType == "Linear") {
100100
expectedPrice <- LinearExpectation_calcExpectedPrice(aLandLeaf, aPeriod, aScenarioInfo)
101101
expectedYield <- LinearExpectation_calcExpectedYield(aLandLeaf, aPeriod, aScenarioInfo)
102-
} else if(aScenarioInfo$mExpectationType == "Lagged" | aScenarioInfo$mExpectationType == "LaggedCurr") {
103-
expectedPrice <- LaggedExpectation_calcExpectedPrice(aLandLeaf, aPeriod, aScenarioInfo)
104-
expectedYield <- LaggedExpectation_calcExpectedYield(aLandLeaf, aPeriod, aScenarioInfo)
105-
} else if(aScenarioInfo$mExpectationType == "Mixed") {
106-
expectedPrice <- LaggedExpectation_calcExpectedPrice(aLandLeaf, aPeriod, aScenarioInfo)
102+
} else if(aScenarioInfo$mExpectationType == "Adaptive" | aScenarioInfo$mExpectationType == "HybridPerfectAdaptive") {
103+
expectedPrice <- AdaptiveExpectation_calcExpectedPrice(aLandLeaf, aPeriod, aScenarioInfo)
104+
expectedYield <- AdaptiveExpectation_calcExpectedYield(aLandLeaf, aPeriod, aScenarioInfo)
105+
} else if(aScenarioInfo$mExpectationType == "HybridLinearAdaptive") {
106+
expectedPrice <- AdaptiveExpectation_calcExpectedPrice(aLandLeaf, aPeriod, aScenarioInfo)
107107
expectedYield <- LinearExpectation_calcExpectedYield(aLandLeaf, aPeriod, aScenarioInfo)
108108
}
109109

R/constants.R

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ TIME.PARAMS <- list(
99
FUTURE.YEARS=seq(1976, 2015, 1)),
1010
"Hindcast1990" = list(FINAL_CALIBRATION_PERIOD=1, HISTORY.YEARS=1990,
1111
FUTURE.YEARS=seq(1991, 2015, 1)),
12-
"Hindcast5yr" = list(FINAL_CALIBRATION_PERIOD=1, HISTORY.YEARS=1975,
13-
FUTURE.YEARS=seq(1980, 2015, 5)),
12+
"Hindcast2005" = list(FINAL_CALIBRATION_PERIOD=1, HISTORY.YEARS=2005,
13+
FUTURE.YEARS=seq(2006, 2015, 1)),
14+
"Hindcast5yr" = list(FINAL_CALIBRATION_PERIOD=1, HISTORY.YEARS=1990,
15+
FUTURE.YEARS=seq(1995, 2015, 5)),
1416
"Reference" = list(FINAL_CALIBRATION_PERIOD=4, HISTORY.YEARS=c(1975, 1990,
1517
2005, 2010),
1618
FUTURE.YEARS=seq(2015, 2100, 5)),

R/generate_scenario_info.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ DEFAULT.SCENARIO.TYPE <- "Reference"
4444
#' @return New ScenarioInfo object
4545
#' @export
4646
#' @author KVC November 2017
47-
ScenarioInfo <- function(# Currently only "Perfect", "Linear", "Lagged", and "LaggedCurr" ExpectationType are supported
47+
ScenarioInfo <- function(# Currently only "Perfect", "Linear", "Adaptive", "HybridLinearAdaptive", and "HybridPerfectAdaptive" ExpectationType are supported
4848
aExpectationType = NULL,
4949
aLaggedShareOld1 = NA,
5050
aLaggedShareOld2 = NA,
@@ -288,7 +288,7 @@ update_scen_info <- function(aName = NULL, aScenarioType = DEFAULT.SCENARIO.TYPE
288288
new_scen_info$mLinearYears3 <- aLinearYears3
289289
}
290290

291-
# Set share of old expectations in lagged expectation if specified
291+
# Set share of old expectations in adaptive expectation if specified
292292
if(is.numeric(aLaggedShareOld)) {
293293
# Set all groups to this value if it specified. These can be individually overwritten later.
294294
new_scen_info$mLaggedShareOld1 <- aLaggedShareOld

R/helpers.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ getStartYear <- function(aScenType) {
7373
#' @details Get the scenario name to use in ensemble runs
7474
#' @param aScenName Scenario base name
7575
#' @param aExpectation Expectation type
76-
#' @param aYears Years for Lagged or Linear expectations (NULL for Perfect)
76+
#' @param aYears Years for Adaptive or Linear expectations (NULL for Perfect)
7777
#' @param aAgFor Logit exponent for AgroForestLand
7878
#' @param aAgForNonPast Logit exponent for AgroForestLand_NonPasture
7979
#' @param aCrop Logit exponent for Cropland
@@ -84,9 +84,9 @@ getScenName <- function(aScenName, aExpectation, aYears, aAgFor, aAgForNonPast,
8484
# Add expectation information
8585
if(aExpectation == "Linear") {
8686
scenNameAdj <- paste(aScenName, "_", aExpectation, aYears, sep="")
87-
} else if (aExpectation == "Lagged") {
87+
} else if (aExpectation == "Adaptive") {
8888
scenNameAdj <- paste(aScenName, "_", aExpectation, aYears, sep="")
89-
} else if (aExpectation == "Mixed") {
89+
} else if (aExpectation == "HybridLinearAdaptive") {
9090
scenNameAdj <- paste(aScenName, "_", aExpectation, aYears, sep="")
9191
} else {
9292
scenNameAdj <- paste(aScenName, "_", aExpectation, sep="")

R/main.R

+14-14
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#' Parameter combinations are selected by generating a quasi-random
77
#' sequence and mapping it to a specified range for each parameter.
88
#' Then, each parameter set is run through the offline land model in
9-
#' each of the Perfect, Lagged, and Linear variants. (I.e., if N
10-
#' parameter sets are selected, then 3N scenarios are run.)
9+
#' each of the Perfect, Adaptive, HybridPerfectAdaptive, HybridLinearAdaptive, and Linear variants.
10+
#' (I.e., if N parameter sets are selected, then 5N scenarios are run.)
1111
#'
1212
#' This function is strictly for running the ensemble of models. Analysis
1313
#' must be completed after the fact.
@@ -58,7 +58,7 @@ run_ensemble <- function(N = 500, aOutputDir = "./outputs", skip = 0,
5858
message("****************************************************")
5959

6060
# Determine the number of parameters. If aDifferentiateParamByCrop = TRUE, then we have 3 parameters each for
61-
# lagged share and linear years. If FALSE, then only one paramter for each. In both cases, there are 3 logit exponents
61+
# lagged share and linear years. If FALSE, then only one parameter for each. In both cases, there are 3 logit exponents
6262
if( aDifferentiateParamByCrop ) {
6363
NPARAM <- 9
6464
} else {
@@ -187,8 +187,8 @@ run_ensemble <- function(N = 500, aOutputDir = "./outputs", skip = 0,
187187
#' Parameter combinations are selected by generating a quasi-random
188188
#' sequence and mapping it to a specified range for each parameter.
189189
#' Then, each parameter set is run through the offline land model in
190-
#' each of the Perfect, Lagged, and Linear variants. (I.e., if N
191-
#' parameter sets are selected, then 3N scenarios are run.)
190+
#' each of the Perfect, Adaptive, HybridLinearAdaptive, HybridPerfectAdaptive, and Linear variants.
191+
#' (I.e., if N parameter sets are selected, then 5N scenarios are run.)
192192
#'
193193
#' If the scenario type is "Hindcast", then after each model has been run, the
194194
#' Bayesian analysis will be run so that its results can be stored with the rest
@@ -364,7 +364,7 @@ run_ensemble_bayes <- function(N = 500, aOutputDir = "./outputs", skip = 0,
364364

365365
#' Generate the ensemble members for a single set of parameters
366366
#'
367-
#' This generates one each of the Perfect, Lagged, and Linear scenario types
367+
#' This generates one each of the Perfect, Adaptive, HybridLinearAdaptive, HybridPerfectAdaptive, and Linear scenario types
368368
#' using the input parameters. The return value is a list of the three
369369
#' \code{ScenarioInfo} objects for the scenarios generated.
370370
#'
@@ -411,12 +411,12 @@ gen_ensemble_member <- function(agFor, agForNonPast, crop, share1, share2, share
411411
aOutputDir = aOutputDir)
412412

413413

414-
## Lagged scenario - without including current prices (i.e., y[i] = a*y[i-1] + (1-a)*x[i-1])
414+
## Adaptive scenario - without including current prices (i.e., y[i] = a*y[i-1] + (1-a)*x[i-1])
415415
share <- paste(share1, share2, share3, sep="-")
416-
scenName <- getScenName(aScenType, "Lagged", share, agFor, agForNonPast, crop)
416+
scenName <- getScenName(aScenType, "Adaptive", share, agFor, agForNonPast, crop)
417417

418418
lagscen <- ScenarioInfo(aScenarioType = aScenType,
419-
aExpectationType = "Lagged",
419+
aExpectationType = "Adaptive",
420420
aLinearYears1 = NA,
421421
aLinearYears2 = NA,
422422
aLinearYears3 = NA,
@@ -433,12 +433,12 @@ gen_ensemble_member <- function(agFor, agForNonPast, crop, share1, share2, share
433433
aSerialNum = serialnum+0.2,
434434
aOutputDir = aOutputDir)
435435

436-
## Lagged scenario - with including current prices (i.e., y[i] = a*y[i-1] + (1-a)*x[i])
436+
## HybridPerfectAdaptive scenario - with including current prices (i.e., y[i] = a*y[i-1] + (1-a)*x[i])
437437
share <- paste(share1, share2, share3, sep="-")
438-
scenName <- getScenName(aScenType, "LaggedCurr", share, agFor, agForNonPast, crop)
438+
scenName <- getScenName(aScenType, "HybridPerfectAdaptive", share, agFor, agForNonPast, crop)
439439

440440
lagcurrscen <- ScenarioInfo(aScenarioType = aScenType,
441-
aExpectationType = "LaggedCurr",
441+
aExpectationType = "HybridPerfectAdaptive",
442442
aLinearYears1 = NA,
443443
aLinearYears2 = NA,
444444
aLinearYears3 = NA,
@@ -479,9 +479,9 @@ gen_ensemble_member <- function(agFor, agForNonPast, crop, share1, share2, share
479479
## mixed scenario, using linear for yield and adaptive for prices
480480
linyears <- paste(linyears1, linyears2, linyears3, sep="-")
481481
share <- paste(share1, share2, share3, sep="-")
482-
scenName <- getScenName(aScenType, "Mixed", paste(linyears, share, sep="_"), agFor, agForNonPast, crop)
482+
scenName <- getScenName(aScenType, "HybridLinearAdaptive", paste(linyears, share, sep="_"), agFor, agForNonPast, crop)
483483
mixedscen <- ScenarioInfo(aScenarioType = aScenType,
484-
aExpectationType = "Mixed",
484+
aExpectationType = "HybridLinearAdaptive",
485485
aLinearYears1 = linyears1,
486486
aLinearYears2 = linyears2,
487487
aLinearYears3 = linyears3,

R/process_hindcast_data.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ get_historic_yields <- function(){
7171
#' @export
7272
get_hindcast_AgProdChange <- function(aScenType){
7373
# Silence package checks
74-
year <- prev_year <- GCAM_commodity <- yield.x <- yield.y <- region <- AgProdChange <- yield <- NULL
74+
year <- prev_year <- GCAM_commodity <- yield <- yield.x <- yield.y <- region <- AgProdChange <- NULL
7575

7676
# Compute AgProdChange
7777
if(aScenType != "Hindcast5yr") {

data-raw/stats-testdata.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ gen_stats_testdata <- function()
7979
limits <- c(0, 6)
8080
scentype <- 'Hindcast'
8181
exptype1 <- 'Perfect'
82-
exptype2 <- 'Lagged'
82+
exptype2 <- 'Adaptive'
8383

8484
rn <- randtoolbox::sobol(Nsample, NPARAM)
8585
p <- limits[1] + rn*(limits[2]-limits[1])

0 commit comments

Comments
 (0)