9.3.4 ore.tableApply関数の使用
ore.tableApply関数は、入力データとしてore.frameを指定してRスクリプトをコールします。
               
ore.tableApply関数は、ore.frameを最初の引数としてユーザー定義の入力関数に渡します。ore.tableApply関数は、ore.frameオブジェクトまたはシリアライズRオブジェクトをore.objectオブジェクトとして返します。
                  
ore.tableApply関数の構文は次のとおりです。
                  
ore.tableApply(X, FUN, ..., FUN.VALUE = NULL, FUN.NAME = NULL, FUN.OWNER = NULL)
例9-8 ore.tableApply関数の使用方法
この例では、ore.tableApply関数を使用して、irisデータセットに基づいて線形回帰モデルを構築します。linear regression関数はe1071パッケージにあり、クライアントとデータベース・サーバー・マシンの両方のRエンジンにインストールする必要があります。ore.tableApply関数の最初の引数として、ore.push(iris)の呼出しでは一時データベース表およびその表のプロキシであるore.frameが作成されます。2番目の引数は入力関数で、これには引数datがあります。ore.tableApply関数は、ore.frame表プロキシをdat引数として入力関数に渡します。この入力関数は、ore.tableApply関数がore.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  
出力は、次のようなものです。
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  
表9-7 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")] } | 
この例のリスト
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
                  親トピック: 埋込みRの実行用のRインタフェース