SimOP: R6 Class for plotting outputs of the OFPE simulation.

SimOPR Documentation

R6 Class for plotting outputs of the OFPE simulation.

Description

R6 class for plotting outputs of the OFPE simulation such as figures comparing management scenarios, probability tables, and maps.

This class should be initialized and executed after the user has initialized and executed all other classes and has ran the SimClass$executeSim() method. These figures rely on outputs from the simulation, so will cause errors if used prior to execution of the simulation.

See the SimClass$executeSim() method documentation for more detail on the management scenarios reported. NR is net-return. The abbreviations used are; NRmin: uniform minimum rate ('AAmin'), NRfs: uniform farmer selected rate, NRact: if experiment applied in simulated year, NRssopt: site-specific optimum rates, NRffopt: uniform full- field optimum rate, NRopp: if the crop received the opposite system crop price (e.g. 'conventional' vs. 'organic' or reverse, see SimClass$executeSim() method documentation for specifics on the difference in the calculation of this net-return between systems).

Error checking inputs to these functions is scant because the user should only use these after executing the simulation method in the 'SimClass' R6 class to generate the necessary data for the output generation methods.

Public fields

simClass

'SimClass' R6 object that has been initialized and the OFPE simulation has been executed. This class holds the information necessary to produce the output figures, maps, and tables from the Monte Carlo simulation.

input_list

Optional, list of inputs required for generating the output figures. Recreation of relevant info from a 'SimClass' object. Only needed if a 'SimClass' object is not passed into class.

'input_list' is a named list with the following fields;

dat_path: The path to where the data from the simulation was exported. Must contain 'Bp.var', 'NRffmax', and 'NRopt' tables exported from the 'SimClass'. This class requires file names to be in the same format as exported from the 'SimClass' to import.

unique_fieldname: Unique name for the field(s) used in the simulation. This is used for saving and labeling of outputs. e.g. "sec35middle" or "sec1east & sec1west". This must match the 'unique_fieldname' used to save the simulation outputs.

unique_fxn: The function name generated by the 'SimClass' object that was used for saving the simulation outputs. e.g. "yldGAM-proGAM".

sim_years: Years that the simulation was run. The output tables from the simulation must be labeled with these years as exported from the 'SimClass' object.

opt: The optimization method used in the simulation. Used to identify and label data. Must be in the same format as from the 'SimClass', e.g. "deriv".

fieldsize: Size of the field in acres.

fs: The uniform rate of the experimental input that the farmer would have applied in the absence of variable rate application.

expvar: The OFPE experimental input code for the experimental variable, i.e. "aa_n" for as-applied nitrogen fertilizer or "aa_sr" for as-applied seed rate.

farmername: The name of the farmer, corresponding to the format in the database, e.g. lowercase.

respvar: Character vector of OFPE response variable codes that were used in the model fitting and simulation processes, e.g. "yld" or "pro" or 'c("yld", "pro")'.

db: Connection to an OFPE formatted database.

utm_fieldname: The name of the field (corresponding to the name in the OFPE database) that should be used for identifying the UTM zone. In the case of multiple fields (i.e. "sec1east & sec1west") choose one because their UTM code will be the same.

out_path: Path to the folder to create an 'Outputs' folder and save figures and tables.

out_path

Output path to create folder for outputs. Taken from either a 'SimClass' object or an optional 'input_list' field upon initialization.

SAVE

Whether to save simulation outputs If NULL uses the user selected choice. If not NULL and is logical, argument replaces previously set SAVE options for the entire class.

SI

Logical, whether to plot in SI units. Defaults to FALSE where imperial units (lbs, ac, bu) are used. If TRUE kg and ha are used.

dat_path

The path to where the data from the simulation was exported. Must contain 'Bp.var', 'NRffmax', and 'NRopt' tables exported from the 'SimClass'. This class requires file names to be in the same format as exported from the 'SimClass' to import.

unique_fieldname

Unique name for the field(s) used in the simulation. This is used for saving and labeling of outputs. e.g. "sec35middle" or "sec1east & sec1west". This must match the 'unique_fieldname' used to save the simulation outputs.

unique_fxn

The function name generated by the 'SimClass' object that was used for saving the simulation outputs. e.g. "yldGAM-proGAM".

sim_years

Years that the simulation was run. The output tables from the simulation must be labeled with these years as exported from the 'SimClass' object.

opt

The optimization method used in the simulation. Used to identify and label data. Must be in the same format as from the 'SimClass', e.g. "deriv".

fieldsize

Size of the field. Note: this must be in the same units as those specified in DatClass. i.e. if DatClass$SI == TRUE than fieldsize should be in hectares, else in acres.

fs

The uniform rate of the experimental input that the farmer would have applied in the absence of variable rate application. Note: this rate must be in the same units as those specified in DatClass. i.e. if DatClass$SI == TRUE than fs should be in kg/ha, else in lbs/ac.

expvar

The OFPE experimental input code for the experimental variable, i.e. "aa_n" for as-applied nitrogen fertilizer or "aa_sr" for as-applied seed rate.

farmername

The name of the farmer, corresponding to the format in the database, e.g. lowercase.

respvar

Character vector of OFPE response variable codes that were used in the model fitting and simulation processes, e.g. "yld" or "pro" or 'c("yld", "pro")'.

db

Connection to an OFPE formatted database.

utm_fieldname

The name of the field (corresponding to the name in the OFPE database) that should be used for identifying the UTM zone. In the case of multiple fields (i.e. "sec1east & sec1west") choose one because their UTM code will be the same.

AAmin

The minimum as-applied rate to simulate management outcomues from (i.e. 0 lbs N per acre or 25 lbs seed per acre).

Methods

Public methods


Method new()

Use this class to save figures and tables from simulations performed using the 'SimClass' R6 object. This class can be initialized with an executed 'SimClass' object, or a list of inputs. See 'input_list' in this class' description. This class can also be initialized with neither and methods for generating individual figures and tables can be executed by passing in arguments. When used in this manner this may be more susceptible to errors as the user is required to specify arguments in the same format as found in the 'SimClass' object.

Also pass in a logical to the 'create' argument to select whether to save figures. Depending on this choice, the output folder will be generated. This is done by a private method that sets up the output location for the figures that the model produces. This will not overwrite any previously generated diagnostic or validation plots from the ModClass. Functions for plots and maps all have save options which must be accompanied by a folder path. The folder created is named 'Outputs'. This folder contains a folder called 'Maps' which contains maps of estimated responses, net-return, and site-specific optimized rates. Another folder called 'EXP' is created that holds figures related to the amounts and distributions for as-applied rates of the experimental variable. Within this folder, subfolders called 'ffoptEXP', 'ssoptEXP', and 'EXPapplied' are created. The acronyms SSOPT and FFOPT correspond to site-specific optimized and full-field optimized rates. Another main folder named 'NR' is created to hold net-return figures comparing management outcomes. Subfolders within 'NR' are 'NRboxplots', 'NRbarplots' and 'NRprobabilities'. The two former contain figures, and the latter contains a table with the probability that the SSOPT strategy yields a higher net-return than each of the other management strategies.

Usage
SimOP$new(simClass = NULL, input_list = NULL, create = TRUE, SI = FALSE)
Arguments
simClass

Optional, 'SimClass' R6 object that has been initialized and the OFPE simulation has been executed. This class holds the information necessary to produce the output figures, maps, and tables from the Monte Carlo simulation.

input_list

Optional, list of inputs required for generating the output figures. Recreation of relevant info from a 'SimClass' object. Only needed if a 'SimClass' object is not passed into class.

create

create Logical, whether to create the 'Output' folder and save figures, maps, and tables. Default is TRUE, pass FALSE to skip output folder step and prevent any output generation.

SI

Logical, whether to plot in SI units. Defaults to FALSE where imperial units (lbs, ac, bu) are used. If TRUE kg and ha are used.

Returns

A folder created in the path for model output figures.


Method savePlots()

Method for saving simulation output plots (see below). This method is used to call the public methods for each figure, map, and table generated. This applies over the user selected simulation years. The user can pass a logical argument to this function which will disable all plotting and not save anything to your computer It will override any previous setting you have set for 'SAVE'. If calling plots individually, a SAVE option for each can be selected.

This method will only work if the 'SimOP' class has been initialized with a 'SimClass' or 'input_list' object. If the 'SimOP' class is initialized with neither, than individual plotting functions must be executed with user supplied inputs. This class uses internal fields in the 'SimOP' class that are created on instantiation with a 'SimClass' or 'input_list' object. class from these objects.

The output methods and output created by this method are; 'plotNRbox', 'plotNRbar', 'mgmtNRprobTable', 'plotFFOPThist', 'plotTotExpAppl', and 'plotSSOPThist'. Maps are created with the 'plotSimMaps' methods. The maps saved are net-return maps for the SSOPT, FFOPT, FS, and Act management strategies, estimated yield and protein maps for the SSOPT and FS management strategies, and the SSOPT experimental rate map.

Usage
SimOP$savePlots(SAVE = NULL)
Arguments
SAVE

Whether to save simulation outputs If NULL uses the user selected choice. If not NULL and is logical, argument replaces previously set SAVE options for the entire class.

Returns

Simulation outputs in the 'Outputs' folder.


Method plotNRbox()

Plot the variation in net returns over different price years with a boxplot.

Usage
SimOP$plotNRbox(
  Bp.var,
  fieldname = self$unique_fieldname,
  fxn = self$unique_fxn,
  sim_year,
  opt = self$opt,
  SAVE = self$SAVE,
  out_path = self$out_path
)
Arguments
Bp.var

Data.table containing the average net-returns from the field for each management type for every iteration of the simulation.

fieldname

Unique field name corresponding to all fields used in the simulation.

fxn

The functional form of the models used for analysis.

sim_year

Year that the simulation was performed for. Indicates the weather conditions used.

opt

The optimization method used in the simulation.

SAVE

Logical, whether to save figure.

out_path

The path to the folder in which to store and save outputs from the simulation.

Returns

A 'ggplot' object and data saved in 'Outputs/NR/NRboxplots' if selected.


Method plotDiffNRbox()

Plot the variation in the difference in net returns over different price years.

Usage
SimOP$plotDiffNRbox(
  Bp.var,
  fieldname = self$unique_fieldname,
  fxn = self$unique_fxn,
  sim_year,
  opt = self$opt,
  SAVE = self$SAVE,
  out_path = self$out_path
)
Arguments
Bp.var

Data.table containing the average net-returns from the field for each management type for every iteration of the simulation.

fieldname

Unique field name corresponding to all fields used in the simulation.

fxn

The functional form of the models used for analysis.

sim_year

Year that the simulation was performed for. Indicates the weather conditions used.

opt

The optimization method used in the simulation.

SAVE

Logical, whether to save figure.

out_path

The path to the folder in which to store and save outputs from the simulation.

Returns

A 'ggplot' object and data saved in 'Outputs/NR/NRboxplots' if selected.


Method mgmtNRprobTable()

Calculate the probability that a given management method is more profitable than the other management strategies. For example, compare the probability that the site-specific optimization strategy yielded a higher net-retrun than the other management strategies. This probability is generated from across all iterations of the Monte Carlo simulation, so reflects the variation in economic conditions.

Usage
SimOP$mgmtNRprobTable(
  mgmt,
  Bp.var,
  rt,
  fieldname = self$unique_fieldname,
  fxn = self$unique_fxn,
  sim_year,
  opt = self$opt,
  SAVE = self$SAVE,
  out_path = self$out_path
)
Arguments
mgmt

Select the management strategy to compare, select from; 'NR.ssopt', 'NR.min', 'NR.fs', 'NR.ffopt', 'NR.act', 'NR.opp'. See 'SimClass' documentation or this class' description for acronym definitions.

Bp.var

Data.table containing the average net-returns from the field for each management type for every iteration of the simulation.

rt

Number of times to sample the data with replacement to generate the probability that the net-return of the selected strategy was higher than each other strategy.

fieldname

Unique field name corresponding to all fields used in the simulation.

fxn

The functional form of the models used for analysis.

sim_year

Year that the simulation was performed for. Indicates the weather conditions used.

opt

The optimization method used in the simulation.

SAVE

Logical, whether to save figure.

out_path

The path to the folder in which to store and save outputs from the simulation.

Returns

A data.frame and data saved in 'Outputs/NR/NRProbabilities' if selected.


Method plotNRbar()

Plot the mean net return for each management strategy as a bar plot and a quarter standard deviation around each mean.

Usage
SimOP$plotNRbar(
  Bp.var,
  fieldname = self$unique_fieldname,
  fxn = self$unique_fxn,
  sim_year,
  opt = self$opt,
  SAVE = self$SAVE,
  out_path = self$out_path
)
Arguments
Bp.var

Data.table containing the average net-returns from the field for each management type for every iteration of the simulation.

fieldname

Unique field name corresponding to all fields used in the simulation.

fxn

The functional form of the models used for analysis.

sim_year

Year that the simulation was performed for. Indicates the weather conditions used.

opt

The optimization method used in the simulation.

SAVE

Logical, whether to save figure.

out_path

The path to the folder in which to store and save outputs from the simulation.

Returns

A 'ggplot' object and data saved in 'Outputs/NR/NRbarplots' if selected.


Method plotTotExpAppl()

Create a bar plot of the total experimental input applied with each strategy. The amount of the site-specific input applied is averaged across the simulation for each point and summed to get a total amount of experimental input applied with the site- specific method. The total amount of experimental input applied with all other strategies is calculated the same way.

Usage
SimOP$plotTotExpAppl(
  TF4,
  expvar = self$expvar,
  fieldname = self$unique_fieldname,
  fxn = self$unique_fxn,
  sim_year,
  opt = self$opt,
  SAVE = self$SAVE,
  out_path = self$out_path
)
Arguments
TF4

Data.frame with columns for the amount of experimental rates applied for each strategy.

expvar

Experimental variable optimized, select/input 'As-Applied Nitrogen' or 'As-Applied Seed Rate'. This is the type of input that was experimentally varied across the field as part of the on-farm experimentation.

fieldname

Unique field name corresponding to all fields used in the simulation.

fxn

The functional form of the models used for analysis.

sim_year

Year that the simulation was performed for. Indicates the weather conditions used.

opt

The optimization method used in the simulation.

SAVE

Logical, whether to save figure.

out_path

The path to the folder in which to store and save outputs from the simulation.

Returns

A 'ggplot' object and data saved in 'Outputs/EXP/EXPapplied' if selected.


Method plotSSOPThist()

Generate a histogram of site specific experimental inptut rates within a field. Uses the average site-specific rate for each point across the simulation, indicating the mean optimum rate at each point across all economic conditions.

Usage
SimOP$plotSSOPThist(
  NRopt,
  TF4,
  expvar = self$expvar,
  fieldname = self$unique_fieldname,
  fxn = self$unique_fxn,
  sim_year,
  opt = self$opt,
  SAVE = self$SAVE,
  out_path = self$out_path
)
Arguments
NRopt

Data frame with the net-returns and experimental optimums for every point for every simulation iteration.

TF4

Data.frame with columns for the amount of experimental rates applied for each strategy.

expvar

Experimental variable optimized, select/input 'As-Applied Nitrogen' or 'As-Applied Seed Rate'. This is the type of input that was experimentally varied across the field as part of the on-farm experimentation.

fieldname

Unique field name corresponding to all fields used in the simulation.

fxn

The functional form of the models used for analysis.

sim_year

Year that the simulation was performed for. Indicates the weather conditions used.

opt

The optimization method used in the simulation.

SAVE

Logical, whether to save figure.

out_path

The path to the folder in which to store and save outputs from the simulation.

Returns

A 'ggplot' object and data saved in 'Outputs/EXP/ssoptEXP' if selected.


Method plotFFOPThist()

Generate a histogram of full-field experimental input rates identified in the simulation. Shows the distribution of full-field optimum rates across all economic scenarios in the simulation.

Usage
SimOP$plotFFOPThist(
  Bp.var,
  TF4,
  expvar = self$expvar,
  fieldname = self$unique_fieldname,
  fxn = self$unique_fxn,
  sim_year,
  opt = self$opt,
  SAVE = self$SAVE,
  out_path = self$out_path
)
Arguments
Bp.var

Data.table containing the average net-returns from the field for each management type for every iteration of the simulation.

TF4

Data.frame with columns for the amount of experimental rates applied for each strategy.

expvar

Experimental variable optimized, select/input 'As-Applied Nitrogen' or 'As-Applied Seed Rate'. This is the type of input that was experimentally varied across the field as part of the on-farm experimentation.

fieldname

Unique field name corresponding to all fields used in the simulation.

fxn

The functional form of the models used for analysis.

sim_year

Year that the simulation was performed for. Indicates the weather conditions used.

opt

The optimization method used in the simulation.

SAVE

Logical, whether to save figure.

out_path

The path to the folder in which to store and save outputs from the simulation.

Returns

A 'ggplot' object and data saved in 'Outputs/EXP/ffoptEXP' if selected.


Method plotSimMaps()

This method is for plotting maps of simulation outcomes. These include the net-returns from the management strategies, predicted responses, and optimized rates. Is a wrapper to OFPE::plotMaps().

Usage
SimOP$plotSimMaps(
  dat,
  var,
  var_col_name,
  var_label,
  var_main_label,
  fieldname = self$unique_fieldname,
  sim_year,
  fxn = self$unique_fxn,
  opt = self$opt,
  SAVE = self$SAVE,
  farmername = self$farmername,
  out_path = self$out_path,
  db = self$db,
  utm_fieldname = self$utm_fieldname
)
Arguments
dat

Data frame with the net-returns and experimental optimums for every point for every simulation iteration.

var

The label of the variable to map. Used in figure name.

var_col_name

The name of the column of the variable in the supplied data ('dat').

var_label

The label to be applied to the legend of the map corresponding to the variable mapped.

var_main_label

The main label to apply to the map.

fieldname

Unique field name corresponding to all fields used in the simulation.

sim_year

Year that the simulation was performed for. Indicates the weather conditions used.

fxn

The functional form of the models used for analysis.

opt

The optimization method used in the simulation. For labeling.

SAVE

Logical, whether to save figure.

farmername

The name of the farmer that manages the field.

out_path

The path to the folder in which to store and save outputs from the simulation.

db

Connection to the OFPE database to identify UTM zone.

utm_fieldname

Name of the field for identifying the UTM zone.

Returns

A 'ggmap' object and maps saved in 'Outputs/Maps' folder if selected.


Method loadBpVar()

Gets the Bp.var output table from the 'SimClass' simulation. The Bp.var table contains means across each field for each iteration of the simulation. This records base price received, the cost of the input, the mean net-return per acre for each management strategy, and the full-field optimum rate. Arguments are used to fill in the file name in the same format as how the file was saved from the simulation.

Usage
SimOP$loadBpVar(
  dat_path = self$dat_path,
  unique_fieldname = self$unique_fieldname,
  unique_fxn = self$unique_fxn,
  sim_year,
  opt = self$opt
)
Arguments
dat_path

The path to the folder where the data is saved.

unique_fieldname

Unique field name corresponding to the fieldname in the file name.

unique_fxn

The functional form of the models used for analysis found in the file name.

sim_year

Year that the simulation was performed for. Indicates the weather conditions used. Found in the file name.

opt

The optimization method used in the simulation. Found in file name.

Returns

Data frame with 10 columns and rows equal to the number of iterations in the simulation.


Method loadNRffmax()

Gets the NRffmax table that is an output from the 'SimClass' simulation with rows for each iteration and the full-field optimum experimental input rate and the total net-return from the full-field. Arguments are used to fill in the file name in the same format as how the file was saved from the simulation.

Usage
SimOP$loadNRffmax(
  dat_path = self$dat_path,
  unique_fieldname = self$unique_fieldname,
  unique_fxn = self$unique_fxn,
  sim_year,
  opt = self$opt
)
Arguments
dat_path

The path to the folder where the data is saved.

unique_fieldname

Unique field name corresponding to the fieldname in the file name.

unique_fxn

The functional form of the models used for analysis found in the file name.

sim_year

Year that the simulation was performed for. Indicates the weather conditions used. Found in the file name.

opt

The optimization method used in the simulation. Found in file name.

Returns

Data frame with 3 columns and rows equal to the number of iterations in the simulation.


Method makeTF4()

Create the table TF4, which is used for the output functions 'plotFFOPThist', 'plotTotExpAppl', and 'plotSSOPThist'. This contains mean values for each of the strategies to create bar charts of experimental inputs used.

Usage
SimOP$makeTF4(NRopt, NRffmax, fieldsize = self$fieldsize, fs = self$fs)
Arguments
NRopt

'SimClass' output table that contains every point with the net-return and experimenal rates for each management strategy, and the estimated crop responses for each location for the simulation year that are the means across all simulations.

NRffmax

'SimClass' output table that contains the optimum full-field experimental rate, the total net-return for each iteration of the simulation.

fieldsize

Size of the field in acres.

fs

The farmer selected experimental rate used in the simulation.

Returns

Data frame with SSOPT and FFOPT experimental input results for each management strategy.


Method loadNRopt()

Gets the NRopt table that is an output from the 'SimClass' simulation with rows for each in the field. Columns contain the means across all iterations of the simulation. Columns in this file are the price received, cost of nitrogen, SSOPT and FFOPT experimental rates and the estimated net-return, yield, and protein for each management strategy at each location in the field. Arguments are used to fill in the file name in the same format as how the file was saved from the simulation.

Usage
SimOP$loadNRopt(
  dat_path = self$dat_path,
  unique_fieldname = self$unique_fieldname,
  unique_fxn = self$unique_fxn,
  sim_year,
  opt = self$opt
)
Arguments
dat_path

The path to the folder where the data is saved.

unique_fieldname

Unique field name corresponding to the fieldname in the file name.

unique_fxn

The functional form of the models used for analysis found in the file name.

sim_year

Year that the simulation was performed for. Indicates the weather conditions used. Found in the file name.

opt

The optimization method used in the simulation. Found in file name.

Returns

Data frame with 22 columns and rows equal to the number of observations in the dataset.


Method clone()

The objects of this class are cloneable with this method.

Usage
SimOP$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

See Also

SimClass for the class that executes the Monte Carlo simulation and provides the data for saving and plotting in this class.


paulhegedus/OFPE documentation built on Nov. 23, 2022, 5:09 a.m.