cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
ClusterFerret68
Level III

How to calculate moving averages for selected rows

Hello everyone!

 

I am trying to write a script that will calculate moving averages using selected rows in a table.  

To set the stage: 

I have a data table (example attached) with multiple executions (Unique ID) of a 3 stage process (A, B, C).  There is a reported value for each stage and these values are summed to give a Total value for each occasion.  I would like to have a column that reports a moving average of the Total.  I've looked through some of the discussions and have gotten some good tips for how to do this (i.e., moving averages for each Unique ID...THIS example helped and the formula is in :Moving Average (Unique ID).

 

The next step I'd like to do is to ONLY include a subset of the runs in this evaluation.  The runs are highlighted in red and the desired result (in this case the moving average of only three occasions) is in :Desired Result.

I'm having trouble with the script to select the subset of runs for evalutaion.

 

Also - I'm relatively new to JSL and would also welcome suggestions on how to get at the end result in a cleaner or more efficient manner.

 

Thanks in advance for your help!

Chris

 

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: How to calculate moving averages for selected rows

ah.. so you want to calculate col moving average of to include rows but only report it on the last row of each unique ID?

jthi_0-1729781875070.png

Note that this will calculate the moving average using ALL values in the groups, not just the last values but it will report it on the last line (remove the if-statement to see what it really does)

If(:To Include == "Yes" & Row() == Col Max(Row(), :Unique ID),
	Col Moving Average(:"Total (Sum of Stages for each Unique ID)"n, 1, :To Include),
	.
)

jthi_1-1729782036088.png

You can also perform the calculation based only the last rows but the calculation will keep getting slower and more difficult to read

If(:To Include == "Yes" & Row() == Col Max(Row(), :Unique ID),
	Col Moving Average(
		If(Row() == Col Max(Row(), :Unique ID),
			:"Total (Sum of Stages for each Unique ID)"n,
			.
		),
		1,
		:To Include
	),
	.
)

These are easiest to understand when you break it into many separate formulas

-Jarmo

View solution in original post

6 REPLIES 6
jthi
Super User

Re: How to calculate moving averages for selected rows

What is the selection of those three rows based on?

-Jarmo
ClusterFerret68
Level III

Re: How to calculate moving averages for selected rows

Jarmo,

 

This process is run in two different settings: one setting is for development use and the other for "official" use (if that makes sense). 

and those three lines represent the latter.  For the moving average I don't want to include the development runs.  However, I DO want to include all data in the tracking/trending sheet.so I have a "single source of truth" representing all settings/operators/etc.  The attached sheet just represents a minimal set of info to help address the Ask for the JMP community.

 

I've been building similar datasets over the years and have just recently started to work with JSL to try to develop more efficient workstreams.  I don't have any coding background, though, so I'm still on a steep learning curve.  This group has been really helpful!

 

Chris

jthi
Super User

Re: How to calculate moving averages for selected rows

I'm just wondering how to pick those correct rows of interest? Is it done based on color or cells in specific columns? Do you select the rows manually and then the moving average is calculated from those selected rows? 

 

For example you could gave some grouping column and then utilize that

If(!Is Missing(:Group),
	Col Moving Average(As Column("Total (Sum of Stages for each Unique ID)"), 1, :Group),
	.
)

jthi_0-1729780153455.png

 

You could also create a subset of just those rows (maybe linked one) to make the calculation even simpler (Edit: this won't work with linked subset even though it looks like it does in preview)

Col Moving Average(As Column("Total (Sum of Stages for each Unique ID)"), 1)

jthi_1-1729780294560.png

 

 

Col Moving Average(name, options, <By var, ...>) can be good read as it is one of the most confusing formula functions

-Jarmo
ClusterFerret68
Level III

Re: How to calculate moving averages for selected rows

Ahh...now I understand your question.

I was thinking to select rows where :To Include == Yes.  Is there a better way?

 

I'll read that documentation...I have been confused by the function, for sure!

 

Chris

jthi
Super User

Re: How to calculate moving averages for selected rows

ah.. so you want to calculate col moving average of to include rows but only report it on the last row of each unique ID?

jthi_0-1729781875070.png

Note that this will calculate the moving average using ALL values in the groups, not just the last values but it will report it on the last line (remove the if-statement to see what it really does)

If(:To Include == "Yes" & Row() == Col Max(Row(), :Unique ID),
	Col Moving Average(:"Total (Sum of Stages for each Unique ID)"n, 1, :To Include),
	.
)

jthi_1-1729782036088.png

You can also perform the calculation based only the last rows but the calculation will keep getting slower and more difficult to read

If(:To Include == "Yes" & Row() == Col Max(Row(), :Unique ID),
	Col Moving Average(
		If(Row() == Col Max(Row(), :Unique ID),
			:"Total (Sum of Stages for each Unique ID)"n,
			.
		),
		1,
		:To Include
	),
	.
)

These are easiest to understand when you break it into many separate formulas

-Jarmo
ClusterFerret68
Level III

Re: How to calculate moving averages for selected rows

Thanks so much!  I'll start playing with this.

 

C