turn on suggestions

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

Showing results for

- JMP User Community
- :
- Discussions
- :
- Discussions
- :
- Moving average and Weighted moving average on a da...

Topic Options

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

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

Mar 20, 2017 12:59 PM
(2905 views)

Hi,

I am trying to do a moving average and a weighted moving average on the data set that I have posted below.

I want to do a moving average to the 'Mean' column.

Also can I repeat this formula by the 'Sample' group in my data set

1 ACCEPTED SOLUTION

Accepted Solutions

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

Mar 20, 2017 7:14 PM
(5737 views)

Solution

It's just a simple extension of your formula. You just have to keep track of when the :Sample changes, and then start the calculations from scratch

```
If( :sample != Lag( :sample, 1 ) | Row() == 1,
counter = 0;
startrow = Row();
);
counter = counter + 1;
If( counter < 3,
Mean( :Mean1[Index( startrow, Row() )] ),
Mean( :Mean1[Index( Row() - (3 - 1), Row() )] )
);
```

Jim

14 REPLIES

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

Mar 20, 2017 1:22 PM
(2896 views)

Here is a moving average formula

```
Mean( Lag( :Points, 1 ),
Lag( :Points, 2 ),
Lag( :Points, 3 ),
Lag( :Points, 4 ),
Lag( :Points, 5 )
)
```

and a weighted moving average

```
Mean(
Lag( :points, 1 ) * :mean,
Lag( :points, 2 ) * :mean,
Lag( :points, 3 ) * :mean,
Lag( :points, 4 ) * :mean,
Lag( :points, 5 ) * :mean
)
```

Jim

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

Mar 20, 2017 6:12 PM
(2879 views)

Hi txnelson,

Is the below formula you suggested moving average for the 'Points' column?And you are using the Mean() formula?

How can I put this in a for loop to do it over all the rows?

Also I want to do this By Group

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

Mar 20, 2017 6:39 PM
(2874 views)

Using a formula to define the values of a column is an essential part of JMP. It is well documented in the JMP book called "Using JMP". I suggest you read that book. It was installed on your computer when you installed JMP. You get to it under the Help pulldown menu

Help==>Books==>Using JMP

Regardless, here is how to use the formula that I provided you, create a new column in your data table and then right click on the column header and select "Formula"

As you can see in my example, I named the new column "Moving Average"

The next window that will open up, is the Formula Editor

It allows you to point and click and create whatever formulas you want. However, you can also double click in the blue outlined box that has the wording "no formula" and the editor will come up

As seen in the above window, I just typed in the formula, or it can be pasted into the window. Then when you click on the "OK" button, you will see the graphical form of the formula

Then when you click on the "OK" button, or the "Apply" button the formula will be run for the new column

Jim

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

Mar 20, 2017 6:43 PM
(2873 views)

I was able to get the 3 point Moving Average using.I was able to apply the formula as well.

"If( Row() < 3,

Mean( :Mean1[Index( 1, Row() )] ),

Mean( :Mean1[Index( Row() - (3 - 1), Row() )] )

)"

Ho can I do this By Group for the Column "Sample"?

For each value of Sample I want a moving average for the "Mean1" values and I have 239 Sample values.

Thank you for your help

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

Mar 20, 2017 7:14 PM
(5738 views)

It's just a simple extension of your formula. You just have to keep track of when the :Sample changes, and then start the calculations from scratch

```
If( :sample != Lag( :sample, 1 ) | Row() == 1,
counter = 0;
startrow = Row();
);
counter = counter + 1;
If( counter < 3,
Mean( :Mean1[Index( startrow, Row() )] ),
Mean( :Mean1[Index( Row() - (3 - 1), Row() )] )
);
```

Jim

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

Mar 20, 2017 8:24 PM
(2867 views)

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

Mar 21, 2017 1:36 PM
(2842 views)

How can I do a weighted moving average with the above formula with the coefficients 1,4,6,4,1 ?

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

Mar 21, 2017 2:08 PM
(2833 views)

See the note at the bottom of this reply

~~The formula that is using the index() function isn't going to work for a weighted calculation. What I assume you want to do is illustrated by the following formula~~

`Mean( Lag( ::Mean1, 1 ) * 1, Lag( ::Mean1, 1 ) * 4, Lag( ::Mean1, 1 ) * 6, Lag( ::Mean1, 1 ) * 4, Lag( ::Mean1, 1 ) * 1 );`

~~This structure doesn't allow the simple 2 level calculation, so it has to be expanded, but is is basically the same logic~~

`If( :sample != Lag( :sample, 1 ) | Row() == 1, counter = 0; ); counter = counter + 1; If( counter == 1, :Mean1, counter ==2, Mean( Lag( ::Mean1, 1 ) * 1, Lag( ::Mean1, 1 ) * 4 ), counter ==3, Mean( Lag( ::Mean1, 1 ) * 1, Lag( ::Mean1, 1 ) * 4, Lag( ::Mean1, 1 ) * 6 ), counter ==4, Mean( Lag( ::Mean1, 1 ) * 1, Lag( ::Mean1, 1 ) * 4, Lag( ::Mean1, 1 ) * 6, Lag( ::Mean1, 1 ) * 4 ), Mean( Lag( ::Mean1, 1 ) * 1, Lag( ::Mean1, 1 ) * 4, Lag( ::Mean1, 1 ) * 6, Lag( ::Mean1, 1 ) * 4, Lag( ::Mean1, 1 ) * 1 ) );`

And after all of this, I found the Col Moving Average() function. So don't do anything above that I suggested, instead, go to

Help==>Scripting Index==>Col Moving Average

and you will get a single function that will do everything you have asked for

Jim

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

Mar 21, 2017 2:48 PM
(2828 views)

Will this formula divide the weighted mean by the weights sum that is 16?

I am not getting the expected answer using the formula