# Capsule Based Regression/Prediction

## Recommended Posts

The following steps will create a prediction model for every capsule in a condition.

Step 1.  pick a condition with capsules that isolate the desired area of regression.  Any condition with non-overlapping capsules will work as long as there are enough sample points within its duration. For this example, an increasing temperature condition will be used.  However, periodic conditions and value search conditions will work as well.

Step 2. Create a time counter for each capsule in the condition.  This can be done with the new timesince() function in the formula tool. The timesince() function will have samples spaced depending on the selected period so it is important to select a period that has enough points to build a model with.  See below for details on the timesince() formula setup.

Step 3. In this step a condition with capsule properties that hold the regression constants will be made.  This will be done in the formula tool with one formula.  The concept behind the formula below is to split the condition from step one into individual capsules and use each of the capsules as the training window for a regression model. Once the regression model is done for one capsule the coefficients of the model are assigned as properties to the capsule used for the training window.

The formula syntax for a linear model-based condition can be seen below.  An example of a polynomial regression model can be found in the post below.

```\$Condtition.removeLongerThan(24h).transform(\$cap->
{
\$model=\$SignalToModel.validValues().regressionModelOLS( group(\$cap),false,\$Time)
\$cap.setProperty('Slope',\$model.get('coefficient1'))
.setProperty('Intercept',\$model.get('intercept'))})```
` `

Below is a screenshot of how the formula looks in Seeq.

Note: The regression constants can be added to the capsule pane by clicking on the black stats button and selecting add column. Below is a screen shot of the results.

Step 4. Once a condition with the regression coefficients has been created the information will need to be extracted to a signal form. The following formula syntax will extract the information. This will need to be repeated for every constant from your regression model. e.g.(So for a linear model this must be done for both the slope and for the intercept.)

The formula syntax for extracting the regression coefficients can be seen below.

```\$signal=\$Condition.transformToSamples(
\$cap -> sample(\$cap.getmiddle(),
\$cap.getProperty('Intercept').toNumber()), 1min)

\$signal.aggregate(average(),\$Condition,durationKey())```

Below is a screenshot of the formula in Seeq.

Below is a screenshot of the display window of how the signals should look.

Step 5. Use the formula tool to plot the equation. See screenshot below for details.

Final Result

Edited by Teddy
• 3
##### Share on other sites

Here is the syntax for a polynomial regression based condition.

```\$condition.removeLongerThan(15h).transform(\$cap->{

\$model=\$signaltomodel.regressionModelOLS(group(\$cap), false, \$Time, \$Time^2)
\$cap.setProperty('Coeff1',\$model.get('coefficient1'))
.setProperty('Coeff2',\$model.get('coefficient2'))
.setProperty('Intercept',\$model.get('intercept'))})```

Below is a screenshot of it setup in the formula tool.

The result

• 2
##### Share on other sites

• 2 years later...
• Seeq Team

In Step 4 of the Original Post, the formula used for creating a signal with values from the Condition properties, 'Intercept' and 'Slope', was...

```\$signal=\$Condition.transformToSamples(
\$cap -> sample(\$cap.getmiddle(),
\$cap.getProperty('Intercept').toNumber()), 1min)

\$signal.aggregate(average(),\$Condition,durationKey())```

This complex formula has been simplified and can now be written as...

`\$Condition.toSignal('Intercept',durationKey())`

If the condition is unbounded you may have to modify slightly...

`\$Condition.removeLongerThan(40h).toSignal('Intercept',durationKey())`