ModClass: R6 Class for storing the user selected model class and...

ModClassR Documentation

R6 Class for storing the user selected model class and associated inputs

Description

R6 class for storing the user selected model class and associated inputs for instantiating the user selected model. The model selection is used to fit functions of the response variables selected to as-applied experimental and covariate data. The instantiation of this class requires the user to provide the exact name of the model they desire for each response variable. Additionally, in order to maintain flexibility in the analysis, the user can provide a path to a folder location with a sourcable model that they have written and would like to use for the analysis. Otherwise, the user can leave this argument NULL and select a model provided in this package.

The user is required to provide the exact name of the model that they want to use. This exact name corresponds to the file containing the scripts and algorithms used to execute that model. If the user writes their own model class, they must use a R6 class. It must follow the interface design of the 'GAM' and 'NonLinear_Logistic' classes. Also, if a user writes their own model class please contact the developer for testing and inclusion in the OFPE package.

Each model class also requires the user to specify a path to a folder in which to save and store outputs from the model fitting process. These include figures such as diagnostic and validation plots to assess the model fit ability.

While this class stores the inputs for the model the user selects for each response variable, it also will hold the instantiated model used beyond the analysis by providing the fitted model used in the simulation and subsequent prescription generation.

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 response variables, and uses the interactive selection methods to select user inputs.

Public fields

fxn

Provide the functional form of a model to use for analysis. The user must provide the name of the file that contains the scripts for the model. Current models available include 'GAM' and 'NonLinear_Logistic', however this frees users to create their own models and supply the file name for these. This must be a list named by each response variable used in the model using 'yld' for the model to use for yield responses, and 'pro' for the model to use for protein responses.

fxn_path

Provide the path to the folder where a user created model class is stored. If using 'GAM' or 'NonLinear_Logistic' models this will be left NULL as these classes are stored in this package. This must be a list named by each response variable used in the model using 'yld' for the model to use for yield responses, and 'pro' for the model to use for protein responses.

out_path

Provide the path to the folder in which to store and save outputs from the model fitting process, including diagnostic and validation plots. 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).

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.

covars

List of character vector of covariates to use for training the model. Must be named by respvar, in the same order.

mod_list

List containing the initialized R6 class for the specified models. All model classes follow the same interface with standardized field and method names. This class is accessed in the analysis and simulation steps.

SI

Logical, whether to use SI units. If TRUE, yield and experimental data are converted to kg/ha. If FALSE, the default values from the database are used. These are bu/ac for yield and lbs/ac for experimental data (nitrogen or seed).

Methods

Public methods


Method new()

Usage
ModClass$new(fxn = NULL, fxn_path = NULL, SAVE = NULL, out_path = NULL)
Arguments
fxn

Provide the functional form of a model to use for analysis. The user must provide the name of the file that contains the scripts for the model. Current models available include 'GAM' and 'NonLinear_Logistic', however this frees users to create their own models and supply the file name for these.

fxn_path

Provide the path to the folder where a user created model class is stored. If using 'GAM' or 'NonLinear_Logistic' models this will be left NULL as these classes are stored in this package.

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 model fitting process, including diagnostic and validation plots. 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.)

respvar

Response variable(s) to optimize on, input 'Yield' or 'Protein'. Multiple options allowed. This can be passed in from the 'datClass' class where the response variables for optimization where selected. This argument must be passed in on class instantiation and is not available to select from the interactive method because it has been selected in the 'datClass' class.

Returns

A instantiated 'ModClass' object.


Method selectInputs()

Interactive method for selecting inputs related to the models used to fit crop responses to experimental and covariate data. The user must pass in the response variables selected in the 'datClass' class in order to select models the user wishes to use for each response variable. If the user is using a model that they have created and written, they must provide the path to the folder where the script of this model is stored. Finally, the user provides the path to a folder in which to store outputs from the model such as diagnostic and validation plots.

Usage
ModClass$selectInputs(respvar)
Arguments
respvar

Response variable(s) to optimize experimental inputs based off of. These are selected in the 'datClass' class and should be passed in from that class here.

Returns

A completed 'ModClass' object.


Method setupOP()

Method used to setup the output location for the figures that the model produces. These include diagnostic and validation plots. Pass FALSE to 'create' to skip any creation of folders. The folder created is named 'Outputs'. This folder contains a folder called 'Diagnostics' for model diagnostic plots and a folder called 'Validation' for plots of the predicted vs. observed responses, and both predicted and observed responses vs. the experimental variable.

Usage
ModClass$setupOP(create = TRUE)
Arguments
create

Logical, whether to create folders for output. If not, no plots will be saved by default.

Returns

A folder created in the path for model output figures.


Method setupMod()

Method used to setup the model. This initializes the specified model for each response variable. The initialization of each model creates a table of the parameters and associated information related to the specific model.

Usage
ModClass$setupMod(datClass, covars = NULL)
Arguments
datClass

datClass class object. Stores the data and inputs necessary for initializing the model.

covars

List of character vector of covariates to use for training the model. Must be named by respvar, in the same order.

Returns

An instantiated model for each response variable.


Method fitModels()

Method for calling the specific model class' method for executing the model fitting function. This can differ between model types and is thus model specific.

Usage
ModClass$fitModels()
Arguments
None

All parameters supplied upon initialization.

Returns

Fitted models.


Method savePlots()

Method for saving diagnostic and validation plots. The diagnostic plots are methods of the specific model class used, while the validation plots of the predicted vs. observed responses, and both predicted and observed responses vs. the experimental variable are generated as methods of this class. These only save plots if the user has supplied a folder path to save the plots to, and if the user does not select SAVE == FALSE. If the user passes in an argument to SAVE, this replaces any previously selected SAVE option (e.g. if SAVE was set to TRUE in the class initialization and the user passes FALSE as an argument here, the class' selection for SAVE is set to FALSE from TRUE).

Usage
ModClass$savePlots(SAVE = NULL)
Arguments
SAVE

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

Returns

Diagnostic and validation plots in the 'Outputs' folder.


Method clone()

The objects of this class are cloneable with this method.

Usage
ModClass$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

See Also

DatClass for data required for model fitting and validation, SimClass for simulation class that rely on models from ModClass, GAM for the class used for fitting a generalized additive model, NonLinear_Logistic for the class that fits a non-linear logistic model, RF for the class used for fitting a random fores model, and BayesLinear for the class used for fitting a Bayesian model.


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