Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

regr_r2()

Uses two numerical fields to calculate R-squared, or goodness of fit. Use regr_r2() to understand how well the trend line fits your data.
1regr_r2(field_y, field_x)

field_y is a grouped dependent numeric expression and field_x is a grouped independent numeric expression. regr_r2(field_y, field_x) uses simple linear regression to calculate a trend line, then calculates R-squared. If the returned value is small, then functions like regr_slope() and regr_intercept() are likely to return accurate results.

The input fields (field_y, field_x) must contain at least two pairs of non-null values. This function works with simple grouped values but not with cogroups.

Example - How Well Does the Calculated Trend Line Fit My Data

Suppose that you have a dataset that includes the number of activities (such as meetings) and the won opportunity amount.

Sample scatter plot.

You want to check the calculated trend line for 'goodness of fit' to see how accurate the results from other statistical functions are.
1q = load "regression";
2q = group q by all;
3
4q = foreach q generate trunc(regr_r2('Amount', 'NumActivities'),2) as 'R Squared';

The value of R squared is 0.95.

Diagram showing R squared result.