This website uses Cookies. Click Accept to agree to our website's cookie use as described in our Privacy Policy. Click Preferences to customize your cookie settings.
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:
Start the script with Names Default to Here(1);
Open the Big Class sample data table and give it a reference dt.
Add the local data filter and give it a reference df.
df = gb << local data filter(add filter(columns(:sex)));
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.