ore.indexApply
関数をシミュレーションで使用しすることで、Oracle Exadataデータベース・マシンなどの高パフォーマンスのコンピューティング・ハードウェアを利用できます。例6-19は、ランダムな正規分布の複数のサンプルを使用してサマリー統計の分布を比較します。各シミュレーションは、データベースの別個のRエンジンで、データベースで許可された並列度まで並列に実行されます。
例6-19では、サンプル・サイズの変数、乱数値の平均および標準偏差および実行するシミュレーションの数を定義します。この例では、num.simulations
をore.indexApply
関数の最初の引数として指定します。ore.indexApply
関数は、num.simulations
をindex
引数としてユーザー定義の関数に渡します。この入力関数はその後、各入力関数の呼出しで異なる乱数値のセットが生成されるように、索引に基づいて乱数シードを設定します。
次に、入力関数は、rnorm
関数を使用してsample.size
ランダムな標準値を生成します。乱数のベクターでsummary
関数を呼び出し、返される結果としてdata.frame
を準備します。ore.indexApply
関数には、シミュレーションの結合された結果を構成するore.frame
を返すように、FUN.VALUE
引数を指定します。res
変数は、ore.indexApply
関数によって返されるore.frame
を取得します。
サンプルの分布を取得するために、この例では、ore.pull
関数を使用した結果であるdata.frame
でboxplot
関数を呼び出し、res
から選択した列をクライアントに渡します。
例6-19 シミュレーションでのore.indexApply関数の使用方法
res <- NULL sample.size = 1000 mean.val = 100 std.dev.val = 10 num.simulations = 1000 res <- ore.indexApply(num.simulations, function(index, sample.size = 1000, mean = 0, std.dev = 1) { set.seed(index) x <- rnorm(sample.size, mean, std.dev) ss <- summary(x) attr.names <- attr(ss, "names") stats <- data.frame(matrix(ss, 1, length(ss))) names(stats) <- attr.names stats$index <- index stats }, FUN.VALUE=data.frame(Min. = numeric(0), "1st Qu." = numeric(0), Median = numeric(0), Mean = numeric(0), "3rd Qu." = numeric(0), Max. = numeric(0), Index = numeric(0)), parallel = TRUE, sample.size = sample.size, mean = mean.val, std.dev = std.dev.val) options("ore.warn.order" = FALSE) head(res, 3) tail(res, 3) boxplot(ore.pull(res[, 1:6]), main=sprintf("Boxplot of %d rnorm samples size %d, mean=%d, sd=%d", num.simulations, sample.size, mean.val, std.dev.val))例6-19のリスト
R> res <- ore.indexApply(num.simulations, + function(index, sample.size = 1000, mean = 0, std.dev = 1) { + set.seed(index) + x <- rnorm(sample.size, mean, std.dev) + ss <- summary(x) + attr.names <- attr(ss, "names") + stats <- data.frame(matrix(ss, 1, length(ss))) + names(stats) <- attr.names + stats$index <- index + stats + }, + FUN.VALUE=data.frame(Min. = numeric(0), + "1st Qu." = numeric(0), + Median = numeric(0), + Mean = numeric(0), + "3rd Qu." = numeric(0), + Max. = numeric(0), + Index = numeric(0)), + parallel = TRUE, + sample.size = sample.size, + mean = mean.val, std.dev = std.dev.val) R> options("ore.warn.order" = FALSE) R> head(res, 3) Min. X1st.Qu. Median Mean X3rd.Qu. Max. Index 1 67.56 93.11 99.42 99.30 105.8 128.0 847 2 67.73 94.19 99.86 100.10 106.3 130.7 258 3 65.58 93.15 99.78 99.82 106.2 134.3 264 R> tail(res, 3) Min. X1st.Qu. Median Mean X3rd.Qu. Max. Index 1 65.02 93.44 100.2 100.20 106.9 134.0 5 2 71.60 93.34 99.6 99.66 106.4 131.7 4 3 69.44 93.15 100.3 100.10 106.8 135.2 3 R> boxplot(ore.pull(res[, 1:6]), + main=sprintf("Boxplot of %d rnorm samples size %d, mean=%d, sd=%d", + num.simulations, sample.size, mean.val, std.dev.val))