cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
CJenvey
Level II

Calculate moving average based on 2 categorical columns and on combined data

I have a dataset with mean Dozens of eggs per OrderId and OrderDate for Trial (Before, After) and TrialWeek (1, 2, 3, 4, 5). I would like to use the Moving Average function to calculate the mean Dozens at the end of every week, using the data from the previous weeks, and then input this number into a new column. So there should be repeated mean dozens for every row that have the same Trial and TrialWeek.

For example:

Trial == Before & TrialWeek == 1 = Mean Dozens for TrialWeek 1

Trial == Before & TrialWeek == 1 & 2 = Mean Dozens TrialWeek 2

Trial == Before & TrialWeek == 1 & 2 & 3 = Mean Dozens TrialWeek 3, and so on.

 

Moving Average wants me to input the number of rows that the average is calculated on, however the number of rows per TrialWeek is not the same, so I can't use a fixed number of rows to calculate this. I know that I will need to write a formula to do this, but I'm stuck as to how to compose the formula. I am using JMP Pro 16. Any help would be greatly appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
CJenvey
Level II

Re: Calculate moving average based on 2 categorical columns and on combined data

Thank you @txnelson !

I've adjusted the code to also calculate the moving average for Trial == After, so that seems to be working. But I noticed that the code wasn't calculating the moving average for TrialWeek == 1 unless I had the table sorted.

x = .;
curWeek = :TrialWeek;
If(
	:Trial == "Before" & :TrialWeek != :TrialWeek[Row() + 1], x = Col Mean( If( :Trial == "Before" & :TrialWeek <= curWeek, :Dozens, . ) ),
	:Trial == "After" & :TrialWeek != :TrialWeek[Row() + 1], x = Col Mean( If( :Trial == "After" & :TrialWeek <= curWeek, :Dozens, . ) )
);
x;

 

View solution in original post

2 REPLIES 2
txnelson
Super User

Re: Calculate moving average based on 2 categorical columns and on combined data

Is this what you are looking for?

trial.PNG

x = .;
curWeek = :TrialWeek;
If( :Trial == "Before" & :TrialWeek != :TrialWeek[Row() + 1],
	x = Col Mean( If( :Trial == "Before" & :TrialWeek <= curWeek, :Dozens, . ) )
);
x;
Jim
CJenvey
Level II

Re: Calculate moving average based on 2 categorical columns and on combined data

Thank you @txnelson !

I've adjusted the code to also calculate the moving average for Trial == After, so that seems to be working. But I noticed that the code wasn't calculating the moving average for TrialWeek == 1 unless I had the table sorted.

x = .;
curWeek = :TrialWeek;
If(
	:Trial == "Before" & :TrialWeek != :TrialWeek[Row() + 1], x = Col Mean( If( :Trial == "Before" & :TrialWeek <= curWeek, :Dozens, . ) ),
	:Trial == "After" & :TrialWeek != :TrialWeek[Row() + 1], x = Col Mean( If( :Trial == "After" & :TrialWeek <= curWeek, :Dozens, . ) )
);
x;