Options

"Is is possible to see the output from an R model in Rapidminer?"

earmijoearmijo Member Posts: 270 Unicorn
edited June 2019 in Help
I've been trying (unsucessfully so far) to place an operator "Execute R" inside a X-validation operator and get the performance metric (this I can) and the model output (say the regression coefficients--this I can't).
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<process version="6.5.002">
  <context>
    <input/>
    <output/>
    <macros/>
  </context>
  <operator activated="true" class="process" compatibility="6.5.002" expanded="true" name="Process">
    <process expanded="true">
      <operator activated="true" class="retrieve" compatibility="6.5.002" expanded="true" height="60" name="Retrieve Polynomial" width="90" x="45" y="30">
        <parameter key="repository_entry" value="//Samples/data/Polynomial"/>
        <description align="center" color="blue" colored="true" width="126">Fetch example data</description>
      </operator>
      <operator activated="true" class="x_validation" compatibility="6.5.002" expanded="true" height="112" name="Validation" width="90" x="581" y="120">
        <process expanded="true">
          <operator activated="true" class="r_scripting:execute_r" compatibility="6.5.000" expanded="true" height="76" name="Learn Model" width="90" x="246" y="120">
            <parameter key="script" value="# train a linear model on the training data and return the learned model&#10;&#10;rm_main = function(data)&#10;{&#10;&#9;linearModel &lt;- lm(formula =label ~ . ,  data =data)&#10;    &#9;return(linearModel)&#10;}&#10;"/>
            <description align="center" color="red" colored="true" width="126">Train a linear model in R and return it as an R object</description>
          </operator>
          <connect from_port="training" to_op="Learn Model" to_port="input 1"/>
          <connect from_op="Learn Model" from_port="output 1" to_port="model"/>
          <portSpacing port="source_training" spacing="0"/>
          <portSpacing port="sink_model" spacing="0"/>
          <portSpacing port="sink_through 1" spacing="0"/>
        </process>
        <process expanded="true">
          <operator activated="true" class="r_scripting:execute_r" compatibility="6.5.000" expanded="true" height="94" name="Apply R Model" width="90" x="112" y="75">
            <parameter key="script" value="## load the trained model and apply it on the test data&#10;&#10;rm_main = function(model, data)&#10;{&#10;  &#10;  # apply the model and build a prediction&#10;  result &lt;-predict(model, data)&#10;&#10;  # add the prediction to the example set&#10;  data$prediction &lt;- result&#10;  &#10;  # update the meta data&#10;  metaData$data$prediction &lt;&lt;- list(type=&quot;real&quot;, role=&quot;prediction&quot;)&#10;  &#10;  return(data)&#10;}&#10;"/>
            <description align="center" color="red" colored="true" width="126">Apply the trained model on the test data</description>
          </operator>
          <operator activated="true" class="performance" compatibility="6.5.002" expanded="true" height="76" name="Performance" width="90" x="313" y="75"/>
          <connect from_port="model" to_op="Apply R Model" to_port="input 1"/>
          <connect from_port="test set" to_op="Apply R Model" to_port="input 2"/>
          <connect from_op="Apply R Model" from_port="output 1" to_op="Performance" to_port="labelled data"/>
          <connect from_op="Performance" from_port="performance" to_port="averagable 1"/>
          <portSpacing port="source_model" spacing="0"/>
          <portSpacing port="source_test set" spacing="0"/>
          <portSpacing port="source_through 1" spacing="0"/>
          <portSpacing port="sink_averagable 1" spacing="0"/>
          <portSpacing port="sink_averagable 2" spacing="0"/>
        </process>
      </operator>
      <connect from_op="Retrieve Polynomial" from_port="output" to_op="Validation" to_port="training"/>
      <connect from_op="Validation" from_port="model" to_port="result 2"/>
      <connect from_op="Validation" from_port="averagable 1" to_port="result 1"/>
      <portSpacing port="source_input 1" spacing="0"/>
      <portSpacing port="sink_result 1" spacing="0"/>
      <portSpacing port="sink_result 2" spacing="0"/>
      <portSpacing port="sink_result 3" spacing="0"/>
    </process>
  </operator>
</process>
Tagged:

Answers

  • Options
    David_ADavid_A Administrator, Moderator, Employee, RMResearcher, Member Posts: 297 RM Research
    Hi,

    the R lm model your first Operator is returned as a file object, which can passed to another R Operator simply be used there.
    To use this inside the X-Validation you have to bypass the normal workflow and pass the training Object via the "through" port to the validation site of the operator. But as this a special scenario and X-Validation normally expects an RapidMiner model to be used, you have to connect a dummy object as well.

    See the attached process for a solution.
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <process version="6.5.002">
      <context>
        <input/>
        <output/>
        <macros/>
      </context>
      <operator activated="true" class="process" compatibility="6.5.002" expanded="true" name="Process">
        <process expanded="true">
          <operator activated="true" class="retrieve" compatibility="6.5.002" expanded="true" height="60" name="Retrieve Polynomial" width="90" x="45" y="30">
            <parameter key="repository_entry" value="//Samples/data/Polynomial"/>
            <description align="center" color="blue" colored="true" width="126">Fetch example data</description>
          </operator>
          <operator activated="true" class="x_validation" compatibility="6.5.002" expanded="true" height="112" name="Validation" width="90" x="581" y="120">
            <process expanded="true">
              <operator activated="true" class="r_scripting:execute_r" compatibility="6.5.000" expanded="true" height="76" name="Learn Model" width="90" x="246" y="120">
                <parameter key="script" value="# train a linear model on the training data and return the learned model&#10;&#10;rm_main = function(data)&#10;{&#10;&#9;linearModel &lt;- lm(formula =label ~ . ,  data =data)&#10;    &#9;return(linearModel)&#10;}&#10;"/>
                <description align="center" color="red" colored="true" width="126">Train a linear model in R and return it as an R object</description>
              </operator>
              <connect from_port="training" to_op="Learn Model" to_port="input 1"/>
              <connect from_op="Learn Model" from_port="output 1" to_port="model"/>
              <portSpacing port="source_training" spacing="0"/>
              <portSpacing port="sink_model" spacing="0"/>
              <portSpacing port="sink_through 1" spacing="0"/>
            </process>
            <process expanded="true">
              <operator activated="true" class="r_scripting:execute_r" compatibility="6.5.000" expanded="true" height="94" name="Apply R Model" width="90" x="112" y="75">
                <parameter key="script" value="## load the trained model and apply it on the test data&#10;&#10;rm_main = function(model, data)&#10;{&#10;  &#10;  # apply the model and build a prediction&#10;  result &lt;-predict(model, data)&#10;&#10;  # add the prediction to the example set&#10;  data$prediction &lt;- result&#10;  &#10;  # update the meta data&#10;  metaData$data$prediction &lt;&lt;- list(type=&quot;real&quot;, role=&quot;prediction&quot;)&#10;  &#10;  return(data)&#10;}&#10;"/>
                <description align="center" color="red" colored="true" width="126">Apply the trained model on the test data</description>
              </operator>
              <operator activated="true" class="performance" compatibility="6.5.002" expanded="true" height="76" name="Performance" width="90" x="313" y="75"/>
              <connect from_port="model" to_op="Apply R Model" to_port="input 1"/>
              <connect from_port="test set" to_op="Apply R Model" to_port="input 2"/>
              <connect from_op="Apply R Model" from_port="output 1" to_op="Performance" to_port="labelled data"/>
              <connect from_op="Performance" from_port="performance" to_port="averagable 1"/>
              <portSpacing port="source_model" spacing="0"/>
              <portSpacing port="source_test set" spacing="0"/>
              <portSpacing port="source_through 1" spacing="0"/>
              <portSpacing port="sink_averagable 1" spacing="0"/>
              <portSpacing port="sink_averagable 2" spacing="0"/>
            </process>
          </operator>
          <connect from_op="Retrieve Polynomial" from_port="output" to_op="Validation" to_port="training"/>
          <connect from_op="Validation" from_port="model" to_port="result 2"/>
          <connect from_op="Validation" from_port="averagable 1" to_port="result 1"/>
          <portSpacing port="source_input 1" spacing="0"/>
          <portSpacing port="sink_result 1" spacing="0"/>
          <portSpacing port="sink_result 2" spacing="0"/>
          <portSpacing port="sink_result 3" spacing="0"/>
        </process>
      </operator>
    </process>
    Best,
    David
Sign In or Register to comment.