RALG_BUILD_FUNCTION

Use the RALG_BUILD_FUNCTION setting to specify the name of an existing registered R script for building an Oracle Machine Learning for SQL model using the R language.

You must specify both the RALG_BUILD_FUNCTION and ALGO_EXTENSIBLE_LANG settings in the model settings table. The R script defines an R function that has as the first input argument an R data.frame object for training data. The function returns an Oracle Machine Learning model object. The first data argument is mandatory. The RALG_BUILD_FUNCTION can accept additional model build parameters.

Note:

The valid inputs for input parameters are numeric and string scalar data types.

Example 6-3 Example of RALG_BUILD_FUNCTION

This example shows how to specify the name of the R script MY_LM_BUILD_SCRIPT that is used to build the model.

Begin
insert into model_setting_table values
(dbms_data_mining.ralg_build_function,'MY_LM_BUILD_SCRIPT');
End;
/ 

The R script MY_LM_BUILD_SCRIPT defines an R function that builds the LM model. You must register the script MY_LM_BUILD_SCRIPT in the Oracle Machine Learning for R script repository which uses the existing Oracle Machine Learning for R security restrictions. You can use the Oracle Machine Learning for R sys.rqScriptCreate procedure to register the script. Oracle Machine Learning for R requires the RQADMIN role to register R scripts.

For example:

Begin
sys.rqScriptCreate('MY_LM_BUILD_SCRIPT', 'function(data, formula, model.frame) {lm(formula = formula, data=data, model = as.logical(model.frame)}');
End;
/

For Clustering and Feature Extraction machine learning function model builds, the R attributes dm$nclus and dm$nfeat must be set on the return R model to indicate the number of clusters and features respectively.

The R script MY_KM_BUILD_SCRIPT defines an R function that builds the k-Means model for clustering. The R attribute dm$nclus is set with the number of clusters for the returned clustering model.

'function(dat) {dat.scaled <- scale(dat)
     set.seed(6543); mod <- list()
     fit <- kmeans(dat.scaled, centers = 3L)
     mod[[1L]] <- fit
     mod[[2L]] <- attr(dat.scaled, "scaled:center")
     mod[[3L]] <- attr(dat.scaled, "scaled:scale")
     attr(mod, "dm$nclus") <- nrow(fit$centers)
     mod}'

The R script MY_PCA_BUILD_SCRIPT defines an R function that builds the PCA model. The R attribute dm$nfeat is set with the number of features for the returned feature extraction model.

'function(dat) {
     mod <- prcomp(dat, retx = FALSE)
     attr(mod, "dm$nfeat") <- ncol(mod$rotation)
     mod}'