cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
Sburel
Level IV

Script to convert a string from a list into the body of a formula in new column.

Following the process of identifying some interacting columns after using the fit platform and looking for some interaction (factorial...). I want to use the resulting term (such as cA*0.1+cB*0.3) from the interaction as the starting point to automatically create column formulas from those terms. I struggling to find a way to use the content of a string such as"cA*0.1+cB*0.3" where cA and cB represent the names of 2 columns present in a table to build new formulas. 

 

The last portion of the script create the formula but is just representing the content of the string instead of evaluating its content.

 

Any suggestion would be greatly appreciated.

 

Sebastien

names default to here(1);
clear log();

dtinit=Open( "$SAMPLE_DATA/Bands Data.jmp" );

// Make TableBox into a Data Table
// → Data Table( "Untitled 3" )
//terms tables resulting from a generalized regression taking into consideration term interaction.

dtfeat = New Table( "features",
	Add Rows( 14 ),
	New Column( "Term",
		Character,
		"Nominal",
		Set Property( "ID Column", 1 ),
		Set Values(
			{"press speed", "(roller durometer-34.5151)*(anode space ratio-102.914)", "(proof cut-44.9671)*(blade pressure-30.8904)",
			"(anode space ratio-102.914)*(chrome content-99.6164)", "(humidity-78.6849)*(press speed-1851.21)", "roller durometer",
			"(roller durometer-34.5151)*(current density-39.0164)", "(humidity-78.6849)*(wax-2.4037)", "(wax-2.4037)*(chrome content-99.6164)",
			"(viscosity-50.7836)*(press speed-1851.21)", "(wax-2.4037)*(hardener-0.96014)", "(roughness-0.71969)*(current density-39.0164)",
			"(humidity-78.6849)*(ink pct-55.6473)", "(blade pressure-30.8904)*(press speed-1851.21)"}
		)
	),
	Set Label Columns( :Term )
);

//Identifies interacting terms
// New column: interaction
dtfeat << New Column( "interaction", Character, "Nominal", Formula( If( Starts With( :term, "(" ), :term, "" ) ) ) <<
Move Selected Columns( {:interaction}, after( :term ) );



value = dtfeat:interaction << get values();

uniqueinteraction = Associative Array( value ) << get keys;
nvinteract = N Items( uniqueinteraction );
	
//Will create a column named after the interacting terms
//Should apply a formula based on the interacting terms	
//However, not sure how to extract the string from the list 'uniqueinteraction' so that is it not seen as a string but rather as 'formula instruction'	where some portion of the term is recognized as a column namealready present in the original table
For( i = 1, i <= nvinteract, i++,
	dtinit << New Column( uniqueinteraction[i], Numeric, "Continuous", Format( "Best", 12 ), Formula( Eval( uniqueinteraction[i] ) ) 

 

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Script to convert a string from a list into the body of a formula in new column.

In this example all the "formulas" have column name starting after "(" so you could substitute that with "(:" and then use Parse

Names Default To Here(1);

dtinit = Open("$SAMPLE_DATA/Bands Data.jmp");

dtfeat = New Table("features",
	Add Rows(14),
	New Column("Term",
		Character,
		"Nominal",
		Set Property("ID Column", 1),
		Set Values(
			{"press speed", "(roller durometer-34.5151)*(anode space ratio-102.914)", "(proof cut-44.9671)*(blade pressure-30.8904)",
			"(anode space ratio-102.914)*(chrome content-99.6164)", "(humidity-78.6849)*(press speed-1851.21)", "roller durometer",
			"(roller durometer-34.5151)*(current density-39.0164)", "(humidity-78.6849)*(wax-2.4037)",
			"(wax-2.4037)*(chrome content-99.6164)", "(viscosity-50.7836)*(press speed-1851.21)", "(wax-2.4037)*(hardener-0.96014)",
			"(roughness-0.71969)*(current density-39.0164)", "(humidity-78.6849)*(ink pct-55.6473)",
			"(blade pressure-30.8904)*(press speed-1851.21)"}
		)
	),
	Set Label Columns(:Term)
);

dtfeat << New Column("interaction", Character, "Nominal", Formula(If(Starts With(:term, "("), :term, ""))) <<
Move Selected Columns({:interaction}, after(:term));

value = dtfeat:interaction << get values();

uniqueinteraction = Associative Array(value) << get keys;

For Each({uniq}, uniqueinteraction,
	f_str = Substitute(uniq, "(", "(:");
	If(IsMissing(f_str),
		continue();
	);
	Eval(EvalExpr(
		dtinit << New Column(uniq, Numeric, "Continuous", 
			Formula(Expr(Parse(f_str)))
	)));
);

Insert one expression into another using Eval Insert, Eval Expr, Parse, and Substitute 

-Jarmo

View solution in original post

2 REPLIES 2
jthi
Super User

Re: Script to convert a string from a list into the body of a formula in new column.

In this example all the "formulas" have column name starting after "(" so you could substitute that with "(:" and then use Parse

Names Default To Here(1);

dtinit = Open("$SAMPLE_DATA/Bands Data.jmp");

dtfeat = New Table("features",
	Add Rows(14),
	New Column("Term",
		Character,
		"Nominal",
		Set Property("ID Column", 1),
		Set Values(
			{"press speed", "(roller durometer-34.5151)*(anode space ratio-102.914)", "(proof cut-44.9671)*(blade pressure-30.8904)",
			"(anode space ratio-102.914)*(chrome content-99.6164)", "(humidity-78.6849)*(press speed-1851.21)", "roller durometer",
			"(roller durometer-34.5151)*(current density-39.0164)", "(humidity-78.6849)*(wax-2.4037)",
			"(wax-2.4037)*(chrome content-99.6164)", "(viscosity-50.7836)*(press speed-1851.21)", "(wax-2.4037)*(hardener-0.96014)",
			"(roughness-0.71969)*(current density-39.0164)", "(humidity-78.6849)*(ink pct-55.6473)",
			"(blade pressure-30.8904)*(press speed-1851.21)"}
		)
	),
	Set Label Columns(:Term)
);

dtfeat << New Column("interaction", Character, "Nominal", Formula(If(Starts With(:term, "("), :term, ""))) <<
Move Selected Columns({:interaction}, after(:term));

value = dtfeat:interaction << get values();

uniqueinteraction = Associative Array(value) << get keys;

For Each({uniq}, uniqueinteraction,
	f_str = Substitute(uniq, "(", "(:");
	If(IsMissing(f_str),
		continue();
	);
	Eval(EvalExpr(
		dtinit << New Column(uniq, Numeric, "Continuous", 
			Formula(Expr(Parse(f_str)))
	)));
);

Insert one expression into another using Eval Insert, Eval Expr, Parse, and Substitute 

-Jarmo
Sburel
Level IV

Re: Script to convert a string from a list into the body of a formula in new column.

Outstanding. Did the trick. Thanks a lot.