cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
Add a Summary Statistic Graph Reference Line that Responds to Local Data Filter Changes
 
 

Create a run chart with a reference line for the mean of the data. Add a local data filter and have the mean change as categories are selected. Have the mean honor excluded rows.


Ingredients:
  • Graph Builder
  • Row State Handlers

Sample Data Table: Big Class

Difficulty: Hard
Steps:
  1. Start the script with Names Default to Here(1);
  2. Open the Big Class sample data table and give it a reference dt.
    dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
  3. Exclude some rows at random.
    dt << select randomly(sampling rate(0.2)) << exclude << clear select;
  4. Create the graph using Graph Builder and give it a reference gb.
    gb = Graph Builder(
          Size( 621, 472 ),
          Variables( Y( :height ) ),
          Elements( Line( Y, Legend( 3 ), Row order( 1 ) ) ),
    );
  5. Add the local data filter and give it a reference df.
    df = gb << local data filter(add filter(columns(:sex)));
  6. The make filter change handler (f) message to the data filter object will evaluate the user function argument f every time the data filter is changed. Create the function to remove the reference line if it exists, find the mean of the response for the rows selected by the local data filter and that are not excluded, then add a reference line to the Y axis at the mean of the response for those rows. Then send the message to the data filter.
    //user function to be called when filter is changed
    f = function({a}, //a is not used
    //remove the y axis reference line, if it exists
    	try(report(gb)[axisbox(2)] << remove ref line(meanHeight));
    //get the where clause from the data filter
    //it will look like Select Where(...)
    	txt = df << get where clause;
    //get the argument, if it exists
    	arg = if(length(txt), arg(parse(txt), 1), txt);
    //get row numbers of filtered rows
    	aaDf = if(length(txt), 
    		associative array(dt << get rows where(arg)), 
    		index(1, nrow(dt))
    	);
    //find row numbers of non-excluded rows
    	vecEx = dt << select excluded 
    		<< invert row selection 
    		<< get selected rows 
    		<< clear select;
    	aaEx = associative array(vecEx);
    //get row numbers of both
    	aaEx << intersect(aaDf);
    	lstAll = aaEx << get keys;
    //find the mean of filtered/non-excluded response
    //if nothing is filtered, find the mean of non-excluded only
    	meanHeight = if(length(txt), 
    		mean(column(dt, "height")[lstAll]), 
    		mean(column(dt, "height")[aaEx << get keys])
    	);
    //add reference line	
    	report(gb)[axisbox(2)] << add ref line(meanHeight, "Solid", 19, 2);
    );
    
    rs = df << make filter change handler(f);

Hints For Success

  • Create the Graph Builder and Data Filter objects interactively, then copy their scripts using Save Script > to Script Window or use the Enhanced Log.
  • Use the Scripting Index to understand the messages objects like Data Filter can respond to. Run the examples to see how they work. For example, what is retuned in the argument a to the user function f?
  • Start small. This example was built without considering excluded rows. It was only after the script was created that it was tried on a table with excluded rows. The special case of a table with excluded rows was added after. The other special case of nothing selected in the filter was also added after.