R コンポーネントの作成

分析で使用するカスタム R コンポーネントの作成方法。

R コンポーネントを作成する前に、以下の要件が満たされていることを確認します。
  • R スクリプトが有効な R 関数形式で書き込まれている。
  • R スクリプトが R GUI コンソールで実行される。
  • R スクリプトに少なくとも 1 つの主関数が含まれる。
  • マシンと SAP HANA サーバのどちらかに、R スクリプトの実行に必要なパッケージをインストールしている。
  • インデータベース分析に対して書き込まれた R スクリプトが、データフレームを返す。
以下は、R スクリプトの書き込み時に考慮する必要があるベストプラクティスです。
  • インプロセス分析に対して書き込まれた R スクリプトが、データフレームを返す。
  • 出力の型変換が推奨される。たとえば列に数値が含まれる場合、as.numeric(output) と記述する。
  • R スクリプトで使用されるカテゴリ変数に対しては、as.factor コマンドを使用して変数を指定する。
  1. 予測領域の右側のコンポーネントリストで、ナビゲーションパスの開始 add_component.gif 次のナビゲーションステップ R コンポーネント ナビゲーションパスの終了 を選択します。
    新しいカスタム R コンポーネントの作成ウィザードが表示されます。
  2. 全般ページで、次の情報を入力します。
    1. コンポーネント名テキストボックスに、My component と入力します。
    2. コンポーネントタイプドロップダウンリストでアルゴリズムを選択します。
    3. コンポーネント説明テキストボックスに、R component for Simple Linear Regression と入力します。
  3. 次へを選択します。
    スクリプトページが表示されます。
  4. スクリプトページで、スクリプトのロードを選択してファイルをアップロードします。
    注記 テキストボックスで以下のサンプル R スクリプトを書き込むか、コピーアンドペーストすることができます。
    注記 独自の R スクリプトを理解して書き込めるよう、以下の R 関数形式でコメントを参照します。
    #The following is a sample script for a simple linear regression component.
    #You must write the script in a valid R function format.
    #Note that the function name and variable name in R script can be user-defined, and are supported in R.
    #The following is the argument description for the primary function SLR:
    #InputDataFrame: Dataframe in R that contains the output of the parent component.
    #The following two parameters are received from the user through the property view:
    #IndependentColumn: Column name that you want to use as independent variable for the component.
    #DependentColumn; Column name that you want to use as a dependent variable for the component.
    
    SLR<-function(InputDataFrame, IndependentColumn, DependentColumn)
    {
      finalString<-paste(paste(DependentColumn,"~" ), IndependentColumn); 
    #Formatting the final string to
    #pass to "lm" function
    slr_model<-lm(finalString); # calling the "lm" function and storing the output model in "slr_model"
    #To get the predicted values for the Training data set, call the "predict" function with this model and
    #input dataframe, which is represented by "InputDataFrame".
    result<-predict(slr_model, InputDataFrame); # Storing the predicted values in the "result" variable.
    output<- cbind(InputDataFrame, result); # combining "InputDataFrame" and "result" to get the final table.
    plot(slr_model); #Plotting model visualization.
    #returnvalue: function must always return a list that contains results "out", and model variable
    #"slrmodel", if present.
    #The output variable stores the final result.
    #The model variable is used for model scoring.
    return (list(slrmodel=slr_model, out=output))
    }
    
    #The following is the argument description for the model scoring function "SLRModelScoring":
    #InputDataFrame: Dataframe in R that contains the output of the parent component.
    #IndependentColumn: Column name to be used as independent variables for the component.
    #Model: Model variable that is used for scoring.
    
    SLRModelScoring <- function (InputDataFrame, IndependentColumn, Model)
    {
    #Calling "predict" function to get the predictive value with "Model " and "InputDataFrame".
    predicted<-predict(Model, data.frame(InputDataFrame[,IndependentColumn]), level=0.95);
    # Combining “InputDataFrame” and “predicted” to get the final table.
    output <- cbind(InputDataFrame, predicted); 
    #returnvalue: function should always return a list that contains the result ("model result"),
    #The output variable stores the final result
    return(list(modelresult=output))
    }
    

    以下は、R スクリプトを Expert Analytics で認識される有効な R 関数形式に変換する 2 つの例です。

    R スクリプト R 関数形式 (Expert Analytics によって認識される)
     dataFrame<-read.csv("C:\\CSVs\\Iris.csv")
     attach(dataFrame)
     set.seed(4321)
     kmeans_model<-
     kmeans(data.frame(`SepalLength`,`SepalWidth`,
     `PetalLength`,`PetalWidth`),
     centers=5,iter.max=100,nstart=1,algorithm=
     "Hartigan-Wong")
     kmeans_model$cluster
    kmeansfunction<-function(dataFrame,independent,
     Clustersize,Iterations,algotype,numberofinitialdsets)
     { 
     set.seed(4321)
     kmeans_model<-kmeans(data.frame(dataFrame[,independent]),
     centers=Clustersize,iter.max=Iterations, nstart=numberofinitialdsets,
     algorithm= algotype)
     output<- cbind(dataFrame, kmeans_model$cluster);
     boxplot(output); return (list(out=output));
     }
    dataFrame<-
     read.csv("C:\\Datasets\\cnr\\Iris.csv") 
     attach(dataFrame) library(rpart)
     cnr_model<-rpart
     (Species~PetalLength+PetalWidth+SepalLength+
     SepalWidth, method="class") library(rpart) 
     predict(cnr_model, dataFrame,type = c("class"))
    cnrFunction<-function(dataFrame,IndependentColumns,dep)
     { 
     library(rpart); 
     formattedString<-paste(IndependentColumns, collapse = '+');
     finalString<-paste(paste(dep, "~" ),
     formattedString); cnr_model<-rpart(finalString, method="class");
     output<- predict(cnr_model, dataFrame,type=c("class")); 
     out<- cbind(dataFrame, output);
     return (list(result=out,modelcnr=cnr_model));
     } 
     cnrFunctionmodel<-function(dataFrame,ind,modelcnr,type)
     {
     output<-predict(modelcnr,data.frame(dataFrame[,ind]),type=type);
     out<- cbind(dataFrame, output); return (list(result=out));
    注記

    1 次関数におけるモデルスコアリング関数のパラメータを宣言します。ただし、ドロップダウンリストから選択する入力データフレームおよび入力モデル変数名は除きます。

  5. 1 次関数詳細セクションで、以下の情報を入力します。
    1. 1 次関数名ドロップダウンリストで、SLR を選択します。
    2. 入力データフレームドロップダウンリストで、入力データフレーム を選択します。
    3. 出力データフレームボックスに、out を入力します。
    4. モデルを保存するオプションを選択します。
      モデル変数名フィールドが有効化され、モデルスコアリング関数の詳細が表示されます。
    5. モデル変数名フィールドに、slrmodel を入力します。
    6. サマリの表示およびPMML としてエクスポートするオプションチェックボックスを選択します。
  6. モデルスコアリング関数の詳細セクションで、以下の情報を入力します。
    1. モデルスコアリング関数名から、SLRModelScoring を選択します。
    2. 入力データフレームドロップダウンリストで、MInputDataFrame を選択します。
    3. 出力データフレームフィールドに、modelresult を入力します。
    4. 入力モデル変数名ドロップダウンリストで、モデルを選択します。
  7. 次へを選択します。
    設定ページが表示されます。
  8. 1 次関数設定出力テーブル定義セクションで、以下のサブステップを実行します。
    1. 一切考慮しないを選択します。
    2. データ型ドロップダウンリストで整数を選択します。
    3. 新しい予測列名ボックスに、Predicted column を入力します。
  9. プロパティ表示定義セクションで、以下のサブステップを実行します。
    1. プロパティ表示名非依存列ボックスに、Independent Column を入力します。
    2. コントロールタイプドロップダウンリストから、非依存列に対してコントロールタイプとして列セレクタ (単一) を選択します。
    3. プロパティ表示名非依存列ボックスに、Dependent Column を入力します。
    4. コントロールタイプドロップダウンリストから、依存列に対してコントロールタイプとして列セレクタ (単一) を選択します。
  10. モデルスコアリング設定出力テーブル定義セクションで、前コンポーネントのすべての列を考慮するを選択します。
  11. データ型ドロップダウンリストで整数を選択します。
  12. 新しい予測列名ボックスに、出力列 を入力します。
  13. プロパティ表示定義セクションで、以下のサブステップを実行します。
    1. プロパティ表示名に、Independent column を入力します。
    2. コントロールタイプドロップダウンリストから、非依存列に対してコントロールタイプとして列セレクタ (単一) を選択します。
  14. 完了をクリックします。
実行された分析のタイプに応じて、モデルを他のすべてのコンポーネントと同様に作成できます。