Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create meta_learner fit and predict - basic #154

Merged
merged 10 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Authors@R:
email = "[email protected]"))
Description: Near Real Time air pollution model results and code produced by the SET group. It is fully tested, versioned, and open source and open access.
Depends: R (>= 3.5.0)
Imports: dplyr, sf, stats, methods
Imports: dplyr, sf, stats, methods, BART
Suggests: covr, withr, knitr, rmarkdown, testthat (>= 3.0.0), terra, stars, sftime
Encoding: UTF-8
VignetteBuilder: knitr, rmarkdown
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ export(check_means_are_valid)
export(check_output_locations_are_valid)
export(generic_base_learner)
export(generic_meta_learner)
export(meta_learner_fit)
export(meta_learner_predict)
export(vec2nc)
75 changes: 75 additions & 0 deletions R/meta_learner.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#' meta_learner_fit
#' Fit a BART (Bayesian Additive Regression Tree) meta learner. It takes
#' predictions of other models such as kriging, GLM, machine learning models as
#' input and fits a BART Model
#' @param base_predictor_list - a list where each
#' @param y dependent variable
#' @param kfolds integer, index of k-folds for cross-validation. This should be
#' produced with regards to spatial and/or temporal considerations
#' @return meta_fit_obj object of meta learner
#' @export
#' @examples NULL
meta_learner_fit <- function(base_predictor_list,
kfolds, y) {

# check lengths of each base predictor
if (sapply(base_predictor_list, length, simplify = TRUE) |>
stats::var() != 0) {
print("WARNING: base predictors need to be the same length")
}

# convert list to data.frame
x.design <- as.data.frame(base_learner_list)

# Unique k-folds (typically 5 or 10)
n.k <- length(unique(kfolds))
# Pre-allocate list of meta objects
meta_fit_obj <- vector(mode = "list", length = n.k)
for (i in 1:n.k) {
# get the training and test sets
x.tr <- x.design[i != n.k,]
x.te <- x.design[i == n.k,]
y.tr <- y[i != n.k]
# Fit the BART model
bart.fit[[i]] <- mc.wbart(x.tr, y.tr, x.test = x.te)
}
return(meta_fit_obj)
}



#' meta_learner_predict - take the list of BART fit objects and prediction
#' location info to create meta_learner predictions
#'
#' @param meta_fit_obj list of BART objects from meta_learner_fit
#' @param pred_loc sf or dataframe grid of prediction locations
#' @return meta_pred_nc NetCDF (nc) file of the final meta learner predictions
#' @export
#'
#' @examples NULL
meta_learner_predict <- function(meta_fit_obj, pred_loc) {

#Use the predict method
meta_pred_vec <- predict(meta_fit_obj, new = pred_loc)
# Call the vec2nc function to create a NetCDF (nc) file from the
# vector predictions
meta_pred_nc <- vec2nc(meta_pred_vec)
return(meta_pred_nc)

}


#' vec2nc takes a vector or matrix of predictions, some spatial information,
#' and create a NetCDF
#' @param pred_vec vector/matrix of predictions
#' @param pred_bbox an sf bounding box defining the domain of the NetCDF
#' @param field_names vector of field names for each layer in the NetCDF
#' @return nc_out a NetCDF file
#' @export
#'
#' @examples NULL
vec2nc <- function(pred_vec, pred_bbox, field_names) {
nc_out <- pred_vec + pred_bbox
names(nc_out) <- field_names
return(nc_out)
}
34 changes: 0 additions & 34 deletions documentation.yml

This file was deleted.

31 changes: 31 additions & 0 deletions man/meta_learner_fit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions man/meta_learner_predict.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions man/vec2nc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading