10.3.4 Use the ore.tableApply Function
The ore.tableApply
function calls an R script with an ore.frame
as the input data.
The ore.tableApply
function passes the ore.frame
to the user-defined input function as the first argument to that function. The ore.tableApply
function returns an ore.frame
object or a serialized R object as an ore.object
object.
The syntax of the ore.tableApply
function is the following:
ore.tableApply(X, FUN, ..., FUN.VALUE = NULL, FUN.NAME = NULL, FUN.OWNER = NULL)
Example 10-8 Using the ore.tableApply Function
This example uses the ore.tableApply
function to build a
linear regression model on the iris
data set. The linear
regression
function is in the e1071
package, which must be
installed on both the client and database server machine R engines. As the first argument to
the ore.tableApply
function, the ore.push(iris)
invocation
creates a temporary database table and an ore.frame
that is a proxy for the
table. The second argument is the input function, which has as an argument
dat
. The ore.tableApply
function passes the
ore.frame
table proxy to the input function as the dat
argument. The input function creates a model, which the ore.tableApply
function returns as an ore.object
object.
%r
# Create a user-defined function that builds and returns a model using R's lm() function
build.lm <- function(dat){
mod <- lm(Petal.Length~Petal.Width+Sepal.Width+Sepal.Length, dat)
x <- dat[['Petal.Width']]
y <- dat[['Petal.Length']]
return(mod)
}
# Run the user-defined function on the local iris data.frame
res1 <- build.lm(iris)
res1
# Create a temporary R data.frame proxy object IRIS and run the user-defined function using ore.tableApply. The function name is passed to the FUN argument.
IRIS <- ore.push(iris)
res2 <- ore.tableApply(IRIS, FUN=build.lm)
res2
# Save the user-defined function to the R script repository with the same name. Run the function stored in the script repository using ore.tableApply.
# The script name is passed to the FUN.NAME argument. Overwrite any script with the same name if it exits.
ore.scriptCreate("build.lm", build.lm, overwrite=TRUE)
ore.scriptList()
res3 <- ore.tableApply(IRIS, FUN.NAME="build.lm")
res3
The output is similar to the following:
Call:
lm(formula = Petal.Length ~ Petal.Width + Sepal.Width + Sepal.Length,
data = dat)
Coefficients:
(Intercept) Petal.Width Sepal.Width Sepal.Length
-0.2627 1.4468 -0.6460 0.7291
Call:
lm(formula = Petal.Length ~ Petal.Width + Sepal.Width + Sepal.Length,
data = dat)
Coefficients:
(Intercept) Petal.Width Sepal.Width Sepal.Length
-0.2627 1.4468 -0.6460 0.7291
Table 10-7 A data.frame: 6 x 2
NAME | SCRIPT |
---|---|
<chr> | <chr> |
build.lm | function (dat) { mod <- lm(Petal.Length ~ Petal.Width + Sepal.Width + Sepal.Length, dat) x <- dat[["Petal.Width"]] y <- dat[["Petal.Length"]] return(mod) } |
build.lm.1 | function (dat) { regr <- lm(Petal.Length ~ Petal.Width + Sepal.Width + Sepal.Length, dat) x <- dat[["Petal.Width"]] y <- dat[["Petal.Length"]] return(regr) } |
buildLM.group | function (dat) { mod <- lm(Petal.Length ~ Petal.Width, dat) return(mod) } |
buildLM.group.1 | function (dat) { mod <- lm(mpg ~ hp + vs, dat) return(mod) } |
myRandomRedDots | function (divisor = 100) { id <- 1:10 plot(1:100, rnorm(100), pch = 21, bg = "red", cex = 2) data.frame(id = id, val = id/divisor) } |
scoreLM.1 | function (dat, dsname) { ore.load(dsname) dat$Petal.Length_prediction <- predict(mod, newdata = dat) dat[, c("Petal.Length_prediction", "Petal.Length", "Species")] } |
Listing for This Example
R> nbmod <- ore.tableApply(
+ ore.push(iris),
+ function(dat) {
+ library(e1071)
+ dat$Species <- as.factor(dat$Species)
+ naiveBayes(Species ~ ., dat)
+ })
R> class(nbmod)
[1] "ore.object"
attr(,"package")
[1] "OREembed"
R> nbmod
Naive Bayes Classifier for Discrete Predictors
Call:
naiveBayes.default(x = X, y = Y, laplace = laplace)
A-priori probabilities:
Y
setosa versicolor virginica
0.3333333 0.3333333 0.3333333
Conditional probabilities:
Sepal.Length
Y [,1] [,2]
setosa 5.006 0.3524897
versicolor 5.936 0.5161711
virginica 6.588 0.6358796
Sepal.Width
Y [,1] [,2]
setosa 3.428 0.3790644
versicolor 2.770 0.3137983
virginica 2.974 0.3224966
Petal.Length
Y [,1] [,2]
setosa 1.462 0.1736640
versicolor 4.260 0.4699110
virginica 5.552 0.5518947
Petal.Width
Y [,1] [,2]
setosa 0.246 0.1053856
versicolor 1.326 0.1977527
virginica 2.026 0.2746501
Parent topic: R Interface for Embedded R Execution