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

ObsOPR Documentation

R6 Class for plotting outputs of the OFPE simulation.

Description

R6 class for generating maps and figures from observed data in an OFPE database. This class can be initialized with arguments required to run the methods for acquiring or making figures, or can be initialized empty. When an empty 'ObsOP' class is initialized, the methods must have all arguments passed each time, whereas if the user initializes the 'ObsOP' class with arguments specifying the data selections, the methods can be executed with minimal other arguments. This is meant to simplify use and allow the user to have multiple ObsOP classes for different datasets.

It is recommended to initialize one 'ObsOP' class for each dataset desired for analysis by passing in the specifications for data in the instantiation of the class. Multiple fields will be treated as one, and only one year is allowed for gathering.

This class can be used to import data from an OFPE database for data exploration, quality control, and general use. The user can fetch data by providing specific arguments, or by providing a SQL statement for customized fetching from the database. Data can be returned to an object or can be used internally with the methods below.

Methods are available for combining data from datasets by kriging. This can be useful for putting protein and yield in the same data frame or either/both on data from a 'sat' table on grid points. This would also be required to use the method for calculating net-return with yield and protein.

This class also contains methods for mapping and plotting variables from user specified data. The user can map 10m rasters or the points of the specified data, can create scatterplots of two variables, or can create histograms of a variable. These figures are returned from the methods but also can be saved to a user specified locations. This is an R6 class so all parameters needed for generating figures can be supplied upon class initialization. However, to use more generally, the class can be initialized and figure making methods can be run individually. TODO: correlation/CV maps, summary statistics

Public fields

out_path

Optional, path to folder to save outputs. Also serveds as a logical for whether to create the 'Output' folder and save figures, maps, and tables. If this argument is left null, the user will have to provide the path to save outputs for each individual method.

SAVE

Whether to save outputs. This is set based off of the user providing a path to a folder to make outputs.

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.

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.

db

Connection to a database to gather data from. Must be set up and filled in the OFPE specific format.

farmername

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

fieldname

The name of the field(s) to gather data for. Must match the format in the OFPE database.

year

The year to gather data for.

dat_tab

The table that the data is in within the aggregated schema of the database. This corresponds to the type of data gathered (i.e. 'yld', 'pro', or 'sat').

GRID

Select whether to gather gridded or observed data locations. See the 'AggInputs' class for more information on the 'GRID' option. Provide either "grid" or "obs".

dat_used

Option for the length of year to use data in the analysis, simulation, and prescription building steps. See the 'AggInputs' class documentation for more information on the 'dat_used' selection.

orig_file

The original file name of the data to gather.

agg_dat

Aggregated data saved as a result of the fetchAggDat() method.

raw_dat

Raw data saved as a result of the fetchRawDat() method.

Methods

Public methods


Method new()

Use this class to import data from an OFPE database, save figures and explore observed data. This class can be initialized with a arguments required for executing the methods of the class or with only a 'create' parameter, see below. If the user supplies all arguments to the class, they can typically use the methods with one or two arguments. A database connection must be supplied to import data from the database. If the user imports data, it is initialized into the class.

The logical argument, 'create', gives the user a choice to select whether to save figures and maps. Depending on this choice, the output folders 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 'ObsMaps' which contains maps of observed data.

Usage
ObsOP$new(
  out_path = NULL,
  farmername = NULL,
  fieldname = NULL,
  year = NULL,
  dat_tab = NULL,
  GRID = NULL,
  db = NULL,
  dat_used = NULL,
  utm_fieldname = NULL
)
Arguments
out_path

Optional, path to folder to save outputs. Also serveds as a logical for whether to create the 'Output' folder and save figures, maps, and tables. If this argument is left null, the user will have to provide the path to save outputs for each individual method.

farmername

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

fieldname

Optional, the name of the field(s) to gather data for. Must match the format in the OFPE database.

year

Optional, the year to gather data for.

dat_tab

Optional, the table that the data is in within the aggregated schema of the database. This corresponds to the type of data gathered (i.e. 'yld', 'pro', or 'sat').

GRID

Optional, select whether to gather gridded or observed data locations. See the 'AggInputs' class for more information on the 'GRID' option. Provide either "grid" or "obs".

db

Optional, connection to a database to gather data from. Must be set up and filled in the OFPE specific format.

dat_used

Optional, option for the length of year to use data in the analysis, simulation, and prescription building steps. See the 'AggInputs' class documentation for more information on the 'dat_used' selection. Provide either "decision_point" or "full_year".

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.

Returns

Initialized R6 class and a folder created in the path for model output figures if specified.


Method fetchAggDat()

Gather data from an aggregated table in an OFPE database. Provide the farmername, fieldname(s), year(s), and type of data to gather. Requires database connection.

Usage
ObsOP$fetchAggDat(
  db = self$db,
  farmername = self$farmername,
  fieldname = self$fieldname,
  year = self$year,
  dat_tab = self$dat_tab,
  GRID = self$GRID,
  dat_used = self$dat_used,
  store = TRUE
)
Arguments
db

Connection to a database to gather data from. Must be set up and filled in the OFPE specific format.

farmername

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

fieldname

The name of the field(s) to gather data for. Must match the format in the OFPE database.

year

The year(s) to gather data for.

dat_tab

The table that the data is in within the aggregated schema of the database. This corresponds to the type of data gathered (i.e. 'yld', 'pro', or 'sat').

GRID

Select whether to gather gridded or observed data locations. See the 'AggInputs' class for more information on the 'GRID' option. Provide either "grid" or "obs".

dat_used

Optional, option for the length of year to use data in the analysis, simulation, and prescription building steps. See the 'AggInputs' class documentation for more information on the 'dat_used' selection. Provide either "decision_point" or "full_year".

store

Logical, whether to save the data internally in the class. Default is TRUE. Access via rxClass$agg_dat. If FALSE will return dataset.

Returns

Data table with specified data.


Method fetchRawDat()

Gather data from a raw table in an OFPE database. Provide the farmername, the type of data, and the name of the original file for the data ('orig_file'). Requires database connection.

Usage
ObsOP$fetchRawDat(
  db = self$db,
  farmername = self$farmername,
  dat_tab = self$dat_tab,
  orig_file,
  store = TRUE
)
Arguments
db

Connection to a database to gather data from. Must be set up and filled in the OFPE specific format.

farmername

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

dat_tab

The table that the data is in within the raw schema of the database. This corresponds to the type of data gathered (i.e. 'yld', 'aa_n_poly', 'aa_sr', etc.).

orig_file

The original file name of the data to gather.

store

Logical, whether to save the data internally in the class. Default is TRUE. Access via rxClass$raw_dat. If FALSE will return dataset.

Returns

Data table with specified data.


Method krigeDat()

Interpolate data using kriging. Takes two datasets and the specified column name in the source data for the variable to be kriged to the the target data.

Usage
ObsOP$krigeDat(source_dat, target_dat, var)
Arguments
source_dat

The data to interpolate from.

target_dat

The data to krige data to.

var

The variable to interpolate.

Returns

Data table with interpolated data.


Method calcNR()

Method for calculating net-return in a dataset. This method looks for columns specified as 'yld' or 'pro' as well as 'aa_X' specifying the experimental input variable. The user also specifies the economic conditions used to calculate net-return. If there is no protein data in the dataset, the protein premium/dockage specifications are not required.

Usage
ObsOP$calcNR(
  dat,
  yld_col_name = NULL,
  pro_col_name = NULL,
  exp_col_name,
  CEXP,
  FC,
  ssAC,
  Bp,
  B0pd = NULL,
  B1pd = NULL,
  B2pd = NULL
)
Arguments
dat

The data frame or data table.

yld_col_name

Character, name of column containing yield data. Leave NULL to calculate net-return based on just protein. If NULL, pro_col_name cannot also be NULL.

pro_col_name

Character, name of column containing protein data. Leave NULL to calculate net-return based on just yield. If NULL, yld_col_name cannot also be NULL.

exp_col_name

Character, name of the column for the experimental input data. Required.

CEXP

The cost of the experimental input.

FC

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

ssAC

The cost ($/acre) 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.

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.

Returns

Data frame with new 'NR' column.


Method plotObsMaps()

This method is for plotting maps of observed variables. The user must supply the data and variable to map. Other arguments relate to labeling the map.

Usage
ObsOP$plotObsMaps(
  dat,
  var,
  var_col_name,
  var_label,
  var_main_label = NULL,
  fieldname = self$unique_fieldname,
  year = self$year,
  SAVE = self$SAVE,
  farmername = self$farmername,
  out_path = self$out_path,
  db = self$dbCon$db,
  utm_fieldname = self$utm_fieldname,
  utm_zone = NULL
)
Arguments
dat

Data frame with variables to map. Must include an x and y column.

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.

year

Year of the observed data.

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. Optional, will try and calculate without. Defaults to whatever was passed in to initialize ObsOP class. Optional, but require 'utm_zone' if omitted.

utm_fieldname

Name of the field for identifying the UTM zone. Defaults to whatever was passed in to initialize ObsOP class. Optional, but require 'utm_zone' if omitted.

utm_zone

The EPSG code for the UTM zone that contains the data. Optional if a database connection is provided to look it up.

Returns

A 'ggmap' object and maps saved in the specified output folder if selected.


Method plotScatters()

This method is for creating a scatterplot of variables specified by the user. The user specifies the x and y column to plot and a variable or variables to color points by.

Usage
ObsOP$plotScatters(
  dat,
  x_var,
  y_var,
  x_lab = NULL,
  y_lab = NULL,
  color_var = NULL,
  color_lab = NULL,
  main_label,
  out_path = self$out_path,
  save_label = NULL,
  SAVE = self$SAVE
)
Arguments
dat

Data frame with variables to plot. Must include the columns for specified data.

x_var

The column name of the variable to plot on the x axis.

y_var

The column name of the variable to plot on the y axis.

x_lab

The label to be applied to the x axis of the plot.

y_lab

The label to be applied to the y axis of the plot.

color_var

The variable or variables, passed in as a character string, to color the data by. If left NULL no coloring is applied.

color_lab

The label to be applied to the legend for the fill color.

main_label

Title for the figure.

out_path

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

save_label

The label to apply to the filename. The y, x, and color variables will also be added to the filename. Optional, but if SAVE = TRUE and save_lable = NULL 'main_label' will be used.

SAVE

Logical, whether to save figure.

Returns

A scatterplot and saved in the specified output folder if selected.


Method plotBoxplots()

This method is for creating a boxplot of variables specified by the user. The user specifies the x and y column to plot and a variable or variables to color points by.

Usage
ObsOP$plotBoxplots(
  dat,
  x_var,
  y_var,
  x_lab = NULL,
  y_lab = NULL,
  color_var = NULL,
  color_lab = NULL,
  main_label,
  out_path = self$out_path,
  save_label = NULL,
  SAVE = self$SAVE
)
Arguments
dat

Data frame with variables to plot. Must include the columns for specified data.

x_var

The column name of the variable to plot on the x axis.

y_var

The column name of the variable to plot on the y axis.

x_lab

The label to be applied to the x axis of the plot.

y_lab

The label to be applied to the y axis of the plot.

color_var

The variable or variables, passed in as a character string, to color the data by. If left NULL no coloring is applied.

color_lab

The label to be applied to the legend for the fill color.

main_label

Title for the figure.

out_path

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

save_label

The label to apply to the filename. The y, x, and color variables will also be added to the filename. Optional, but if SAVE = TRUE and save_lable = NULL 'main_label' will be used.

SAVE

Logical, whether to save figure.

Returns

A boxplot and saved in the specified output folder if selected.


Method plotViolins()

This method is for creating a violin plot of variables specified by the user. The user specifies the x and y column to plot and a variable or variables to color points by. The violin plot shows the probability density of observations, and also contains a boxplot within the violin to show the IQR.

Usage
ObsOP$plotViolins(
  dat,
  x_var,
  y_var,
  x_lab = NULL,
  y_lab = NULL,
  color_var = NULL,
  color_lab = NULL,
  main_label,
  out_path = self$out_path,
  save_label = NULL,
  SAVE = self$SAVE
)
Arguments
dat

Data frame with variables to plot. Must include the columns for specified data.

x_var

The column name of the variable to plot on the x axis.

y_var

The column name of the variable to plot on the y axis.

x_lab

The label to be applied to the x axis of the plot.

y_lab

The label to be applied to the y axis of the plot.

color_var

The variable or variables, passed in as a character string, to color the data by. If left NULL no coloring is applied.

color_lab

The label to be applied to the legend for the fill color.

main_label

Title for the figure.

out_path

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

save_label

The label to apply to the filename. The y, x, and color variables will also be added to the filename. Optional, but if SAVE = TRUE and save_lable = NULL 'main_label' will be used.

SAVE

Logical, whether to save figure.

Returns

A violin plot and saved in the specified output folder if selected.


Method plotHistogram()

This method is for creating a histogram of a variable specified by the user. The user specifies the column in the data holding the data of interest and provides a lavel.

Usage
ObsOP$plotHistogram(
  dat,
  x_var,
  x_lab = NULL,
  color_var = NULL,
  color_lab = NULL,
  main_label,
  out_path = self$out_path,
  save_label = NULL,
  SAVE = self$SAVE
)
Arguments
dat

Data frame with variables to plot. Must include the columns for specified data.

x_var

The column name of the variable to plot on the x axis.

x_lab

The label to be applied to the x axis of the plot.

color_var

The variable or variables, passed in as a character string, to color the data by. If left NULL no coloring is applied.

color_lab

The label to be applied to the legend for the fill color.

main_label

Title for the figure.

out_path

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

save_label

The label to apply to the filename. The y, x, and color variables will also be added to the filename. Optional, but if SAVE = TRUE and save_lable = NULL 'main_label' will be used.

SAVE

Logical, whether to save figure.

Returns

A scatterplot and saved in 'Outputs/Maps' folder if selected.


Method clone()

The objects of this class are cloneable with this method.

Usage
ObsOP$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.


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