Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Calculate Moving R2

Oct 7, 2019 9:32 AM
(1304 views)

How can I calculate R2 between two variables (i.e. columns) in a moving window (e.g., 10 pairs of data at a time)?

Sina

5 REPLIES 5

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Calculate Moving R2

See this discussion about performing linear regression in a column formula. This approach could be extended to obtain R square or other results.

Learn it once, use it forever!

Highlighted
##

Thanks @markbailey. What is the function to calculate R2 in JSL? (sorry I am new to JSL scripting and couldn't find it online).

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Calculate Moving R2

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Calculate Moving R2

take a look at this:

http://www.pega-analytics.co.uk/blog/linear-regression-matrix-form/

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Calculate Moving R2

I understand.

Here is an example based on the Big Class data table in the Sample Data folder. It fits a moving linear regression with a span of 5. It returns the R square value in each row. You can run this script to see how it works.

```
Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
// example creates a new column with a moving R square with a span of 5
// for regression of :weight versus :height
dt << New Column( "Moving R Square",
Numeric,
Continuous,
Formula(
If( Row() > 2 & Row() < (N Row()-1),
x = J( 5, 1, 1 ) || :height[Index( Row() - 2, Row() + 2 )]`;
y = :weight[Index( Row() - 2, Row() + 2 )]`;
sst = Sum( (y - Mean( y )) ^ 2 );
b = (Inv( x` * x ) * x` * y);
yhat = x * b;
sse = Sum( (y - yhat) ^ 2 );
(sst-sse) / sst;
,
Empty()
)
)
);
```

You can select the part of this script that is within the parentheses after the Formula argument and past that expression into a new column formula in your data table, then change the column names to match your X and Y data columns.

Learn it once, use it forever!

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Calculate Moving R2

Thank you @markbailey and @ron_horne . This was very helpful!!

Article Labels

There are no labels assigned to this post.