SimClass: R6 Class for storing inputs and executing the OFPE simulation

SimClassR Documentation

R6 Class for storing inputs and executing the OFPE simulation

Description

R6 class for for storing information needed for the simulation performed post-analysis and pre-prescription building. This class stores inputs such as the model used for analysis and the subsequent simulation, the number of iterations for the simulation, and the weather scenario to simulate. This class also is used for storing inputs related to the model and simulation outputters.

Inputs can be supplied directly to this class during instantiation, however this is NOT recommended except for advanced users. It is recommended that the user supplies the database connection and uses the interactive selection methods to select user inputs.

This class performs the Monte Carlo simulation to compare management outcomes. The inputs also determine whether to activate and execute methods of the 'LikeYear' class to identify the year from the past that is most likely to reflect the upcoming year.

Public fields

dbCon

Database connection object connected to an OFPE formatted database, see DBCon class.

sPr

Provide the number of iterations to run the simulation. Without user inputs the default will be 100.

opt

The user also has the option to select the optimization method. There are two current options, 'Maximum' or 'Derivative'. Both methods select the optimum as-applied rate at each location in the field based on calculating the net-return at each point under a sequence of as-applied rates. Selecting 'Maximum' selects the optimum rate as the as-applied rate with the highest calculated net-return. The 'Derivative' approach calculates the first derivatives (slope) at each as-applied rate at each point and selects the optimum as the as-applied rate where an increase of one unit of the as-applied input results does not result in an increase in net-return that exceeds the cost of one unit of the as-applied input. For example, at one point in the field, if the net-return is \$100 at 20 lbs N/acre and \$101 at 21 lbs N/acre where N costs \$1.50 per lbs, the 'Maximum' approach would consider 21 lbs N/acre the optimum at that point because \$100 < \$101, whereas the 'Derivative' approach would select 20 lbs N/acre the optimum because the $1 increase between 20 and 21 lbs N/acre is less than the \$1.50 cost for the 1 unit increase in N.

Additionally, the user can select 'Ecological' where rates are optimized based on maximization of profit while considering the cost of nitrogen use inefficiency. Only recommended for N fertilizer trials. At every point and for every N rate the NUE is estimated via modeling performed in Hegedus & Ewing, 2022. Based on estimated NUE, the cost of inefficiency is calculated and subtracted from net- return. After net-return is amended, the derivative approach is used to find the rate where net-return is maximized and adding more N does not increase net-return more than the cost of adding that N.

sim_years

Select the year(s) for which to simulate net-return outcomes for. Multiple years can be selected and will be simulated in sequence or one year can be selected. The user can only select from years that they have aggregated with the 'Satellite' option in the aggregation step of the OFPE data cycle. However, when using the interactive selection method of this class, the user also has the option of performing an analysis of all of the years the user has aggregated in the database and identifying the wettest, driest, average, and the predicted year from the past that will most resemble an upcoming year. See the 'LikeYear' class for more information on this option.

fs

Input the uniform as-applied rate the farmer would have applied to the field if an experiment was not conducted (i.e. 70 lbs N or SR/acre). fs = Farmer Selected. 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.

AArateCutoff

The maximum as-applied rate to simulate management outcomues to (i.e. 200 lbs N or seed per acre).

AAmin

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

EXPvec

Vector of the experimental rates to simulate over. This must begin with the AAmin and end with the AArateCutoff. Note: this vector must be in the same units as those specified in DatClass. i.e. if DatClass$SI == TRUE than EXPvec should be in kg/ha, else in lbs/ac.

SAVE

Logical, whether to save figures. Autofilled to FALSE if a user selects NA in the 'out_path' or is NULL. Autofilled to TRUE otherwise. This will be triggered to FALSE if the user passes FALSE as the only argument to the 'setupOP' method. The user can also select to save/not save individual figures.

out_path

Provide the path to the folder in which to store and save outputs from the simulation including figures and tables on management outcomes. Type NA to not create any folders. You will not be able to save any outputs. (Note, even if a path is provided, the user can pass FALSE as the sole argument to the 'setupOP' method to prevent the creation of folders. This will automatically prevent any plots to be saved.)

datClass

DatClass R6 object that has been initialized. This object executes the gathering and processing of data for the simulation.

modClass

ModClass R6 object that has been initialized and with fitted models. The fitted models in this object are used to predict crop responses at each location in the simulation data for all experimental rates in the user specified range.

econDat

EconDat R6 object that has been initialized and setup with all economic parameters and data used in the Monte Carlo simulation.

sim_list

A list for each user selected simulation year containing n lists of that year's satellite data for n experimental rates. This can be thought of as an array with the x dimension representing each centroid location of the simulation year 'sat' data, and the y dimension as each covariate value. The z dimension replicates the x and y dimensions for each experimental rate from 'AAmin' to 'AArateCutoff' as given by 'EXPvec'. Predicted yield and/or protein and net-returns are estimated for every data point in the array (list of lists), and are used to estimate net-returns for every data point, which are used to identify the optimum experimental rate at each point to generate a site-specific optimum rate map and for identifying the full field optimum rate.

fieldsize

The fieldsize, in acres, of the selected fields, gathered from the OFPE database.

unique_fieldname

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

unique_fxn

The functional form of the models used for analysis.

dat_path

If SAVE is TRUE, is the path to the 'Outputs/SimData/' folder, else to the getwd().

Bp

Mean base price of the crop grown in the system type.

CEXP

Mean cost of the as-applied experimental input.

FC

The fixed costs of operations besides fertilizer and seed..

nue_class

The R6 class for fitting and using the NUE model developed by Hegedus & Ewing, 2022. Only initialized if the optimization method selected is 'Ecological'.

Methods

Public methods


Method new()

Usage
SimClass$new(
  dbCon,
  sPr = NULL,
  opt = NULL,
  sim_years = NULL,
  fs = NULL,
  EXPvec = NULL,
  SAVE = NULL,
  out_path = NULL
)
Arguments
dbCon

Database connection object connected to an OFPE formatted database, see DBCon class.

sPr

Provide the number of iterations to run the simulation. Without user inputs the default will be 100.

opt

The user also has the option to select the optimization method. There are two current options, 'Maximum' or 'Derivative'. Both methods select the optimum as-applied rate at each location in the field based on calculating the net-return at each point under a sequence of as-applied rates. Selecting 'Maximum' selects the optimum rate as the as-applied rate with the highest calculated net-return. The 'Derivative' approach calculates the first derivatives (slope) at each as-applied rate at each point and selects the optimum as the as-applied rate where an increase of one unit of the as-applied input results does not result in an increase in net-return that exceeds the cost of one unit of the as-applied input. For example, at one point in the field, if the net-return is \$100 at 20 lbs N/acre and \$101 at 21 lbs N/acre where N costs \$1.50 per lbs, the 'Maximum' approach would consider 21 lbs N/acre the optimum at that point because \$100 < \$101, whereas the 'Derivative' approach would select 20 lbs N/acre the optimum because the $1 increase between 20 and 21 lbs N/acre is less than the \$1.50 cost for the 1 unit increase in N.

Additionally, the user can select 'Ecological' where rates are optimized based on maximization of profit while considering the cost of nitrogen use inefficiency. Only recommended for N fertilizer trials. At every point and for every N rate the NUE is estimated via modeling performed in Hegedus & Ewing, 2022. Based on estimated NUE, the cost of inefficiency is calculated and subtracted from net- return. After net-return is amended, the derivative approach is used to find the rate where net-return is maximized and adding more N does not increase net-return more than the cost of adding that N.

sim_years

Select the year(s) for which to simulate net-return outcomes for. Multiple years can be selected and will be simulated in sequence or one year can be selected. The user can only select from years that they have aggregated with the 'Satellite' option in the aggregation step of the OFPE data cycle. However, when using the interactive selection method of this class, the user also has the option of performing an analysis of all of the years the user has aggregated in the database and identifying the wettest, driest, average, and the predicted year from the past that will most resemble an upcoming year. See the 'LikeYear' class for more information on this option.

fs

Input the uniform as-applied rate the farmer would have applied to the field if an experiment was not conducted (i.e. 70 lbs N or SR/acre). fs = Farmer Selected. 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.

EXPvec

Vector of the experimental rates to simulate over. This must begin with the AAmin and end with the AArateCutoff. Note: this vector must be in the same units as those specified in DatClass. i.e. if DatClass$SI == TRUE than EXPvec should be in kg/ha, else in lbs/ac.

SAVE

Logical, whether to save figures. Autofilled to FALSE if a user selects NA in the 'out_path' or is NULL. Autofilled to TRUE otherwise. This will be triggered to FALSE if the user passes FALSE as the only argument to the 'setupOP' method. The user can also select to save/not save individual figures.

out_path

Provide the path to the folder in which to store and save outputs from the simulation including figures and tables on management outcomes. Type NA to not create any folders. You will not be able to save any outputs. (Note, even if a path is provided, the user can pass FALSE as the sole argument to the 'setupOP' method to prevent the creation of folders. This will automatically prevent any plots to be saved.)

Returns

A new 'SimClass' object.


Method selectInputs()

Interactive method for selecting inputs related to the analysis and the subsequent simulations and prescription generation. The description below describes the process of interactively selecting the necessary parameters needed for the automated analysis, simulation, and prescription building.

The user will need to select the functional form of a model to use for analysis. The user can select 'GAM' for a generalized additive model, 'Non-Linear Logistic' for a logistic function, or user generated models. If more model options are created, they will need to be added here. They will also need to provide the number of iterations to run the Monte Carlo simulation, as well as the optimization method to use.

There are two current options, 'Maximum' or 'Derivative'. Both methods select the optimum as-applied rate at each location in the field based on calculating the net-return at each point under a sequence of as-applied rates. Selecting 'Maximum' selects the optimum rate as the as-applied rate with the highest calculated net-return. The 'Derivative' approach calculates the first derivatives (slope) at each as-applied rate at each point and selects the optimum as the as-applied rate where an increase of one unit of the as-applied input results does not result in an increase in net-return that exceeds the cost of one unit of the as-applied input. For example, at one point in the field, if the net-return is \$100 at 20 lbs N/acre and \$101 at 21 lbs N/acre where N costs \$1.50 per lbs, the 'Maximum' approach would consider 21 lbs N/acre the optimum at that point because \$100 < \$101, whereas the 'Derivative' approach would select 20 lbs N/acre the optimum because the $1 increase between 20 and 21 lbs N/acre is less than the \$1.50 cost for the 1 unit increase in N.

Finally, the user has to select the year or sequence of years for which to assess management outcomes in under the various economic scenarios of the Monte Carlo simulation. The user must aggregate 'Satellite' data for each year that they would like to simulate. See the 'AggInputs' class and Vignette 3 for an overview of the aggregations step. Alternatively, the user can select to perform an analysis of all of the years that they have aggregated 'Satellite' data for to identify the wettest, driest, and average years from the historic record the user has generated. It also will identify the year most expected to follow a specified year based off of the trend in the historic data the user has gatherd.

Usage
SimClass$selectInputs(farmername, fieldname)
Arguments
farmername

Provide the name of the farmer who owns the field that is the target of analysis/simulation/prescription generation.

fieldname

Provide the name of the field that is the target of analysis/simulation/prescription generation.

Returns

A 'SimClass' object with completed inputs.


Method setupSim()

Method used to prepare the class for the simulation. This method takes a DatClass, ModClass, and EconDat R6 classes that the user has initialized and/or executed the mehtods for. The DatClass object is used to execute a method with the user selected simulation year(s) to gather this data from the database and process in the same way data was processed for the model fitting. The ModClass' fitted models are used in the simulation to predict crop responses at each location of the simulation data for the user specified range of experimental rates. The EconDat object contains the economic parameters and data used in the Monte Carlo simulation to compare management outcomes for each simulation year under a range of economic conditions.

Usage
SimClass$setupSim(datClass, modClass, econDat)
Arguments
datClass

DatClass R6 object that has been initialized. This object executes the gathering and processing of data for the simulation.

modClass

ModClass R6 object that has been initialized and with fitted models. The fitted models in this object are used to predict crop responses at each location in the simulation data for all experimental rates in the user specified range.

econDat

EconDat R6 object that has been initialized and setup with all economic parameters and data used in the Monte Carlo simulation.

Returns

Processed simulation data in the DatClass.


Method executeSim()

Method for executing the Monte Carlo simulation to compare management scenarios for each year the user selected to simulate in. The method begins by creating a list of lists (essentially an array) for each simulated year's data for the length of the range of experimental rates to simulate. A column for the experimental rate and for the net-returns from each of the management scenarios is added to each data table.

Then, the fitted models are used to predict the crop responses for every location in the simulated data for every experimental rate. This is repeated for every simulation year.

The simulation is executed for each year to create a list of outputs for each user selected simulation year. For the length of the number of iterations specified by the user, an economic scenario is selected from the user provided/default economic conditions to select a cost of the experimental variable and the price received for conventionally or organically grown crops. Net-return for every point under every experimental rate is calculated. These net-returns are used to calculate the total net-return under each management scenario.

The management scenarios reported include; NRmin: if the minimum rate ('AAmin') were applied to the entire field (e.g. 0 lbs N/ac), NRfs: if the rate the farmer would have applied without experimentation was applied (i.e. business as usual), NRact: the 'actual' net-return in the simulated year if the experiment in the most recent of the user selected years for analysis was applied (e.g. if you fit models on observed data from 2016 and 2018, NRact is calculated based on the as-applied values in 2018), NRssopt: the site-specific profit maximizing experimental rate, NRffopt: the full-field profit maximizing experimental rate which is only identifiable with site-specific experimentation but still represents a uniform application across the field, and finally NRopp: the net-return if the crop was grown in the opposite system type of the user selected system type (e.g. if conventionally grown wheat, this is the net-return for organically grown wheat). If the system type is conventional, NRopp is from the application of 'AAmin' (e.g. 0 lbs N/ac) otherwise if the system is organic, NRopp is from the application of the farmer selected rate with the conventional price.

NOTE: Depending on the size/number of the fields, the number of iterations to run the simulation, and the number of selected simulation years, this may take some time (i.e. 15 minutes - hours)...

Usage
SimClass$executeSim()
Arguments
None

All required arguments supplied upon the initialization and setup methods.

Returns

A list of lists ('sim_out') with output from the simulation for each selected simulation year.


Method cleanUp()

If the user has selected to not save anything, remove temporary files that could have been used by the SimOP class.

Usage
SimClass$cleanUp()
Arguments
None

No arguments needed.

Returns

Any temporary files deleted.


Method plotEstsVsExp()

Plot the average net-return, yield, and protein, of all points vs the experimental variable for the average price and experimental cost scenario from the Monte Carlo simulation. Calculates net-return for every point under every experimental rate in the user selected range under the average economic conditions from the simulation.

Figure shows the selected response (net-return or predicted yield or predicted protein) for every point for every experimental rate. The mean response across rates is shown as a colored line.

Usage
SimClass$plotEstsVsExp(
  respvar,
  expvar,
  fieldname,
  fxn,
  sim_year,
  SAVE,
  out_path,
  Bp,
  CEXP
)
Arguments
respvar

Response variable(s) used to optimize experimental inputs based off of. The user can select 'Yield' and/or 'Protein' based on data availability.

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.

SAVE

Logical, whether to save figure.

out_path

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

Bp

The mean base price used to plot net-returns vs as-applied data.

CEXP

The mean cost of the as-applied experimental input.

Returns

Data saved in 'Outputs/Predictions'.


Method plotActNR()

Function for creating a map of the actual net-return observed in the field for a selected year available in the data the user specified for fitting the models (i.e. if you used multiple years to fit a model you can map the net-return for either year).

This function uses the observed data for the specified year and if there is multiple response variables, predicts them to the yield dataset because this is more spatially dense. This uses the model fit in the analysis step that was used in the simulation. After both responses are in the same dataset, net-return is calculated based on the actual as-applied experimental input and mapped across the field.

Usage
SimClass$plotActNR(
  dat,
  year,
  Bp,
  B0pd,
  B1pd,
  B2pd,
  B3pd,
  CEXP,
  FC,
  ssAC,
  respvar,
  expvar,
  modClass,
  fieldname,
  farmername,
  fxn,
  SAVE,
  out_path,
  db,
  utm_fieldname
)
Arguments
dat

Data.table or data.frame to use for calculating net-return. Must include all covariates used in the model fitting processes. These are the locations actual net-return is calculated for.

year

A single year, present in 'dat', for which to calculate and map the actual net-return.

Bp

The base price corresponding to the price for the system type selected by the user (i.e. conventional or organic).

B0pd

The intercept for the protein premium/dockage equation.

B1pd

The coefficient for protein in the protein premium/dockage equation.

B2pd

The coefficient for protein squared for the protein premium/dockage equation.

B3pd

The coefficient for protein cubed for the protein premium/dockage equation.

CEXP

The cost of the experimental input.

FC

Fixed costs ($/area) associated with production, not including the input of interest. This includes things like the cost of labor, fuel, etc.

ssAC

The cost ($/area) of using site-specific technology or variable rate applications. For farmers that have variable rate technology this cost may be zero, otherwise is the cost per acre to hire the equipment/operators with variable rate technology.

respvar

Response variable(s) used to optimize experimental inputs based off of. The user can select 'Yield' and/or 'Protein' based on data availability.

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.

modClass

ModClass R6 object that has been initialized and with fitted models. The fitted models in this object are used to predict crop responses at each location in the simulation data for all experimental rates in the user specified range.

fieldname

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

farmername

The name of the farmer that manages the field.

fxn

The functional form of the models used for analysis.

SAVE

Logical, whether to save figure.

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

Map of the actual net-return from a given year in 'Outputs/Maps'.


Method clone()

The objects of this class are cloneable with this method.

Usage
SimClass$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

See Also

DBCon for the database connection class, DatClass for the class containing simulation data, ModClass for the class containing the fitted models, EconDat for the class containing economic data for the simulation, and SimOP for the class that save data and figures from the simulation results.


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