分析で使用するカスタム 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 次関数におけるモデルスコアリング関数のパラメータを宣言します。ただし、ドロップダウンリストから選択する入力データフレームおよび入力モデル変数名は除きます。