cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
rcast15
Level III

Scripting the same output for all levels of a categorical variable

Hi,

I have a script that performs some calculations, runs some platforms and extracts certain output, and then builds a tab box in a new window. I want to do all of this for each level of some categorical variable that the user specified in my dialog box.

 

Currently I create individual subsets of the data based on my grouping variable, and then loop through each of those datasets:

dtSubset = dt << Subset( By( Eval( groupVar ) ), Linked, Selected Columns Only( 0 ), All Rows( 1 ), Invisible );

For( i = 1, i <= N Items( unique_groups ), i++,
dt_i = dtSubset[i];

...some code to do stuff

...append output as a new tab box in my window );
Essentially all I am doing is coding up my own analysis and wanting a 'By' variable. I am wondering if there is a better or more efficient way to go about this? The main thing I want with the code is to have the output I create be specific for each level, while also still maintaining a connection to the original data table (meaning the plots in my output are interactive still).
 
As you can see, I create the subsets as Invisible and then within my code I close them at the end of each iteration of the loop.
 
Thanks,
Robert
6 REPLIES 6
jthi
Super User

Re: Scripting the same output for all levels of a categorical variable

Is there a reason for not using By variable within the platforms?

-Jarmo
jthi
Super User

Re: Scripting the same output for all levels of a categorical variable

Here are three options to create Tab Box from data table using grouping column. If you have JMP18+ (I think), last one is definitely the most simple one

Names Default To Here(1); 

dt = Open("$SAMPLE_DATA/Big Class.jmp");

// Using subsets
dt_subsets = dt << Subset(
	By(:age),
	Linked,
	Selected Columns Only(0),
	All Rows(1),
	Invisible
);

nw = New Window("Subsets",
	tb = tab box()
);
dists = {};
For Each({dt_subset}, dt_subsets,
	tb << Insert(Tab Page Box(Title(dt_subset << get name),
		dt_subset << Distribution(
			Continuous Distribution(Column(:weight)),
			Nominal Distribution(Column(:age))
		);	
	));
);
tb << Set Selected(1);


// Using Where in platform
nw = New Window("Where",
	tb = tab box()
);
groups = Associative Array(Column(dt, "age")) << get keys;

dists = {};
For Each({group}, groups,
	tb << Insert(Tab Page Box(Title("age=" || Char(group)),
		dist = dt << Distribution(
			Continuous Distribution(Column(:weight)),
			Nominal Distribution(Column(:age)),
			Where(:age == group)
		);
	));
	(Report(dist) << Top Parent)[TextBox(1)] << Visibility("Collapse"); // to hide Where(... textbox
);
tb << Set Selected(1);


// Using By in platform
dist = dt << Distribution(
	Group Options(Layout("Arrange in Tabs")),
	Continuous Distribution(Column(:weight)),
	Nominal Distribution(Column(:age)),
	By(:age)
);
-Jarmo
rcast15
Level III

Re: Scripting the same output for all levels of a categorical variable

Really the main reason is because I found it simpler to loop through the different levels of my variable and build each tab window that way, then append. Since for each level I am performing calculations for tables, building a graph with graph builder, extracting qq plots from the Dist platform, extracting the studentized residual plot from the fit model platform, etc.

txnelson
Super User

Re: Scripting the same output for all levels of a categorical variable

Expanding on Jarmo's By Group example, here is one that creates the Tab Window your output has.

Names Default To Here(1); 

dt = Open("$SAMPLE_DATA/Big Class.jmp");


// Using By in platform
dist = dt << Distribution(Invisible,
	Group Options(Layout("Arrange in Tabs")),
	Continuous Distribution(Column(:weight)),
	Nominal Distribution(Column(:age)),
	By(:age)
);

// Create a new window and add tabs for each age
nw = New Window( "Tabs",
	tabb = Tab Box();
);

For( i = 1, i <= N Items( dist ), i++,
	display = Report( dist[i] );
	theByGroup = display[Outline Box( 1 )] << get title;
	tabb << append( Tab Page Box( Title( theByGroup ), display ) );
);

txnelson_0-1761843436312.png

 

Jim
rcast15
Level III

Re: Scripting the same output for all levels of a categorical variable

Thank you both for your input, the code provided all works well. I should add that I am building a fairly complex add-in that pulls from a lot of different platforms and creates some novel output as well. What I really wanted was to ensure my plots and tables that are normally linked to my data table to remain that way. I also wanted every single tab to look the exact same, so looping seemed like an easy fix.

Do you think the best way to go about this is to just use the By option in all the platforms I am using and then loop thru the levels of my variable calling each component using the various paths and appending those where I want them?

jthi
Super User

Re: Scripting the same output for all levels of a categorical variable

If you append them like Jim did demonstrate, they will end up loosing the interactivity. Subset/Where do both work quite well but there are cases where you are not able to utilize Where that easily such as creation of summary tables (it uses local data filter instead of simple Where). And with subsets you can potentially end up with a lot of data tables you might have to deal with as they might not close as you expect them to (when closing the main table). Sometimes it might also be worth considering to use Local Data Filter as you use one filter for multiple platforms with Data Filter Context Box.

-Jarmo

Recommended Articles