cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
%3CLINGO-SUB%20id%3D%22lingo-sub-275257%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3E%E7%A7%BB%E5%8B%95%E6%9C%80%E5%B0%8F%E5%80%BC%EF%BC%88%E7%A7%BB%E5%8B%95%E8%A6%96%E7%AA%97%EF%BC%89%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-275257%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3E%3CP%3E%E6%9C%89%E8%AA%B0%E7%9F%A5%E9%81%93%E5%A6%82%E4%BD%95%E8%A8%88%E7%AE%97%E7%A7%BB%E5%8B%95%E6%9C%80%E5%B0%8F%E5%80%BC%EF%BC%88%E6%88%96%E8%80%85%E5%B0%B1%E6%AD%A4%E8%80%8C%E8%A8%80%EF%BC%8C%E5%A6%82%E4%BD%95%3CSPAN%3E%E8%BF%AD%E4%BB%A3%3C%2FSPAN%3E%E9%81%B8%E6%93%87%20X%20%E6%95%B8%E9%87%8F%E7%9A%84%E6%BB%AF%E5%BE%8C%E8%A1%8C%E4%B8%A6%E5%B0%8D%E5%AE%83%E5%80%91%E5%9F%B7%E8%A1%8C%E4%BB%BB%E4%BD%95%E6%93%8D%E4%BD%9C%EF%BC%8C%E4%BE%8B%E5%A6%82%E8%A8%88%E7%AE%97%E6%9C%80%E5%B0%8F%E5%80%BC%E3%80%81%E6%9C%80%E5%A4%A7%E5%80%BC%E3%80%81%E7%AF%84%E5%9C%8D%E7%AD%89%E3%80%82%E5%9C%A8%E9%80%99%E5%80%8B%E5%85%B7%E9%AB%94%E7%A4%BA%E4%BE%8B%E4%B8%AD%EF%BC%8C%E6%88%91%E6%83%B3%E5%9B%9E%E9%A1%A7%E4%B8%80%E4%B8%8B%E8%AA%AA%E5%87%BA%E6%9C%80%E5%BE%8C100%20%E8%A1%8C%E4%B8%A6%E5%8F%96%E5%BE%97%E6%9C%80%E5%B0%8F%E5%80%BC%EF%BC%8C%E7%84%B6%E5%BE%8C%E7%A7%BB%E8%87%B3%E4%B8%8B%E4%B8%80%E8%A1%8C%E4%B8%A6%E5%86%8D%E6%AC%A1%E5%9F%B7%E8%A1%8C%E7%9B%B8%E5%90%8C%E6%93%8D%E4%BD%9C%EF%BC%88%E4%B8%80%E9%81%8D%E5%8F%88%E4%B8%80%E9%81%8D%EF%BC%89%E3%80%82%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-275257%22%20slang%3D%22en-US%22%20mode%3D%22UPDATE%22%3E%3CLINGO-LABEL%3E%E5%AF%A6%E9%A9%97%E8%A8%AD%E8%A8%88%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-275272%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9A%E6%9C%80%E5%B0%8F%E7%A7%BB%E5%8B%95%EF%BC%88%E7%A7%BB%E5%8B%95%E7%AA%97%E5%8F%A3%EF%BC%89%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-275272%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%E4%B8%8B%E9%9D%A2%E7%9A%84%E5%AE%8F%E4%B8%A6%E4%B8%8D%E6%98%AF%E7%89%B9%E5%88%A5%E5%84%AA%E9%9B%85%EF%BC%8C%E4%BD%86%E6%98%AF%E5%8F%AF%E4%BB%A5%E5%AE%8C%E6%88%90%E5%B7%A5%E4%BD%9C%E3%80%82%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3EFor%20(%20i%3D%20100%2C%20i%20%26lt%3B%3D%20N%20Rows%20(%20dt%20)%2C%20i%2B%2B%2C%0A%0A%20dt%20%26lt%3B%26lt%3B%20Select%20Rows%20(Index%20(i-99%2C%20i))%3B%0A%20%0A%20dt%20%26lt%3B%26lt%3B%20Subset(%0A%20Selected%20Rows(%201%20)%2C%0A%20Selected%20Columns%20(%200%20)%2C%0A%20Output%20Table%20(%22out%22)%0A)%3B%0A%0ACurrent%20Data%20Table%20(%20Data%20Table%20(%22out%22))%3B%0A%0Ax%20%3D%20Col%20Minimum%20(%20%3AYourDataColumn%20)%3B%0A%0AClose%20(%20Data%20Table%20(%20%22out%22)%2C%20no%20save)%3B%0A%0AColumn%20(%20%20%22YourMovingWindow%22%20)%5Bi%5D%20%3D%20x%3B%0A%0A)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%E5%AD%90%E8%A8%AD%E7%BD%AE%E6%89%80%E9%81%B8%E6%95%B8%E6%93%9A%EF%BC%88%E6%88%91%E5%B7%B2%E7%B6%93%E5%AE%8C%E6%88%90%E4%BA%86%EF%BC%89%E5%BE%88%E6%85%A2%E3%80%82%E4%B9%9F%E8%A8%B1%E5%8F%A6%E4%B8%80%E5%80%8B%E7%94%A8%E6%88%B6%E5%B0%8D%E5%A6%82%E4%BD%95%E8%A8%AA%E5%95%8F%E6%89%80%E9%81%B8%E6%95%B8%E6%93%9A%E4%B8%A6%E6%8F%90%E5%8F%96%E6%89%80%E9%9C%80%E7%94%A2%E5%93%81%E6%9C%89%E6%9B%B4%E5%A5%BD%E7%9A%84%E4%B8%BB%E6%84%8F%E3%80%82%3C%2FP%3E%3CP%3E%E6%88%91%E9%82%84%E6%83%B3%E7%9F%A5%E9%81%93%EF%BC%8C%E6%B2%92%E6%9C%89%E5%AE%8F%EF%BC%8C%E5%88%97%E5%85%AC%E5%BC%8F%E5%B0%87%E5%A6%82%E4%BD%95%E5%AF%A6%E7%8F%BE%E7%B5%90%E6%9E%9C%E3%80%82%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%E4%B9%BE%E6%9D%AF%EF%BC%8C%3C%2FP%3E%3CP%3E%E7%A7%91%E8%8F%B2%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-275287%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9A%E6%9C%80%E5%B0%8F%E7%A7%BB%E5%8B%95%EF%BC%88%E7%A7%BB%E5%8B%95%E7%AA%97%E5%8F%A3%EF%BC%89%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-275287%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%E9%80%99%E6%A8%A3%E7%9A%84%E4%BA%8B%E6%83%85%E8%A1%8C%E5%97%8E%EF%BC%9F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20default%20to%20here(%201%20)%3B%0Adt%20%3D%20open(%22%24SAMPLE_DATA%5CBig%20Class.jmp%22)%3B%0A%0Adt%20%26lt%3B%26lt%3B%20New%20Column(%22Min%20height%205%20back%22%2C%20%0A%20Formula(%0A%20%20Min(%20%2F%2F%20since%20we%20want%20a%20minumum%20%0A%20%20%20%3Aheight%5B%2F%2F%20the%20column%20we%20want%20the%20minimum%20of%0A%20%20%20%20%2F%2F%20a%20slice%20of%20the%20current%20row%20minus%205%20(minimum%201%20so%20it's%20still%20on%20the%20datatable)%0A%20%20%20%20%2F%2F%20and%20current%20row%0A%20%20%20%20Min(%20Row()%20-%205%20)%3A%3ARow()%20%0A%20%20%20%5D%20%0A%20%20)%0A%20)%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
RVhydrA
Level III

Moving minimum (moving window)

Does anyone know how to calculate a moving minimum (or for that matter how to iteratively select X number of lag rows and do anything to them such as calculate a min, max, range, etc. In this specific example i want to look back over say the last 100 rows and get the minimum value, then move to the next row down and do the same again (over and over). 

1 ACCEPTED SOLUTION

Accepted Solutions
vince_faller
Super User (Alumni)

Re: Moving minimum (moving window)

Would something like this work?

 

Names default to here( 1 );
dt = open("$SAMPLE_DATA\Big Class.jmp");

dt << New Column("Min height 5 back", 
	Formula(
		Min( // since we want a minumum 
			:height[// the column we want the minimum of
				// a slice of the current row minus 5 (minimum 1 so it's still on the datatable)
				// and current row
				Min( Row() - 5 )::Row() 
			] 
		)
	)
);
Vince Faller - Predictum

View solution in original post

2 REPLIES 2
Monomorphist
Level III

Re: Moving minimum (moving window)

The macro below isn't particularly elegant, but will do the job. 

 

For ( i= 100, i <= N Rows ( dt ), i++,

 dt << Select Rows (Index (i-99, i));
 
 dt << Subset(
	Selected Rows( 1 ),
	Selected Columns ( 0 ),
	Output Table ("out")
);

Current Data Table ( Data Table ("out"));

x = Col Minimum ( :YourDataColumn );

Close ( Data Table ( "out"), no save);

Column (  "YourMovingWindow" )[i] = x;

)

Sub-setting the selected data, the way I've done it, is slow. Perhaps another user has a better idea how to access the selected data and extract the desired product.

I also wonder how a column formula could look like to achieve the results without a macro.

 

Cheers,

Kofi

vince_faller
Super User (Alumni)

Re: Moving minimum (moving window)

Would something like this work?

 

Names default to here( 1 );
dt = open("$SAMPLE_DATA\Big Class.jmp");

dt << New Column("Min height 5 back", 
	Formula(
		Min( // since we want a minumum 
			:height[// the column we want the minimum of
				// a slice of the current row minus 5 (minimum 1 so it's still on the datatable)
				// and current row
				Min( Row() - 5 )::Row() 
			] 
		)
	)
);
Vince Faller - Predictum