cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
miguello
Level VI

Need help writing optimal code for extracting R^2 and Slope from Bivariate Fit

All, 

 

I'd like to ask the community to nudge me in the right direction.

I need to extract R^2 and Slope form the report and display it on the plot itself like so:

2022-06-01 12_13_08-Window.png

 

I'm going to do this with parsing Display Trees and then looking into Add Graphics Script to the plot itself.

But, since this task seems to me very standard and routine, I have to ask couple of questions before digging into Display Trees:

1. Is there a more straight forward way of doing that? Like Combined Data Table (which gives slope but does not give R^2) for getting number or like Line Of Fit( X, Y, Legend( 5 ), R²( 1 ), Equation( 1 ) ) in graph builder (customer wants to use Bivariate Fit)

2. Maybe somebody already has a snippet of code ready to use for this task?

2 ACCEPTED SOLUTIONS

Accepted Solutions
txnelson
Super User

Re: Need help writing optimal code for extracting R^2 and Slope from Bivariate Fit

Here is an example that should get you started.

txnelson_0-1654155994175.png

 

Names Default To Here( 1 );

// Open Data Table: big class.jmp
// → Data Table( "big class" )
dt = Open( "$SAMPLE_DATA/big class.jmp" );

biv = Bivariate( Y( :height ), X( :weight ), Fit Line( {Line Color( {212, 73, 88} )} ) );
Report( biv )[framebox( 1 )] << add graphics script(
	Text Color( {212, 73, 88} );
	xMin = Current Report()[axisbox( 2 )] << get min;
	yMin = Current Report()[axisbox( 1 )] << get min;
	xMax = Current Report()[axisbox( 2 )] << get max;
	yMax = Current Report()[axisbox( 1 )] << get max;
	textList = {};
	Insert Into( textList, xMin + (xMax - xMin) * .25 );
	Insert Into( textList, yMax - (yMax - yMin) * .1 );
	R2 = Current Report()["Summary of Fit"][Number Col Box( 1 )][1];
	R2 = "R^2 = " || Format( R2, "Fixed Dec", 5, 3 );
	Text( Right Justified, textList, R2 );
	textList = {};
	Insert Into( textList, xMin + (xMax - xMin) * .32 );
	Insert Into( textList, yMax - (yMax - yMin) * .15 );
	Slope = Current Report()["Parameter Estimates"][Number Col Box( 1 )][2];
	Slope = "Slope = " || format(Slope,"Fixed Dec",9,5);
	Text(Right Justified, textList, Slope);
);
report(biv)["Linear Fit"] << visibility("collapse");
report(biv)[PictureBox(2)] << visibility("collapse");

Documentation for the Graphic additions, and the Display Tree manipulationq can be found in the Scripting Guide and the Scripting Index

Jim

View solution in original post

Re: Need help writing optimal code for extracting R^2 and Slope from Bivariate Fit

Names Default To Here(1);

dt=Open("$SAMPLE_DATA/Tablet Production.jmp");

biv=dt<<Bivariate(
	Y( :Dissolution ),
	X( :Mill Time ),
	Fit Line( {Line Color( {212, 73, 88} )} )

);

slope=report(biv)[NumberColBox(13)][2];
r2=report(biv)[NumberColBox(1)][1];

slope=round(slope,3);
r2=round(r2,3);


Eval(Parse(Eval Insert("biv<<SendToReport(
		Dispatch(
			{},
			\!"Bivar Plot\!",
			FrameBox,
			Add Text Annotation(
				Text( \!"Rsq=^r2^
Slope=^slope^\!" ),
				Fixed Size( 0 ),
				Text Box( {2, 16, 100, 70} ),
				Filled( 0 )
			)
		)
	);")));

View solution in original post

7 REPLIES 7
jthi
Super User

Re: Need help writing optimal code for extracting R^2 and Slope from Bivariate Fit

There are at least three different ways to get those values:

  1. Get them from Bivariate Report with your method of choice
  2. Get them by using Make Into Data Table/Make Combined Data Table and get them from the data tables created
  3. Use matrix function

In this case I would either use option 1 or 2 because you will have to create the Bivariate anyway

-Jarmo
Craige_Hales
Super User

Re: Need help writing optimal code for extracting R^2 and Slope from Bivariate Fit

You can get really close with Graph Builder

Equation includes the slope.Equation includes the slope.

Craige
miguello
Level VI

Re: Need help writing optimal code for extracting R^2 and Slope from Bivariate Fit

Yes, I scripted that already (as I mentioned in the initial question it's easy by adding Line Of Fit( X, Y, Legend( 5 ), R²( 1 ), Equation( 1 ) ) into the Graph Builder Code), however, customer doesn't like Graph Builder for some reason and insists on using Bivariate Fit.

txnelson
Super User

Re: Need help writing optimal code for extracting R^2 and Slope from Bivariate Fit

If you just need the values, and not the graph, you can use the Linear Regression() function, and get the values from there

Names Default To Here( 1 );

// Open Data Table: big class.jmp
// → Data Table( "big class" )
dt = Open( "$SAMPLE_DATA/big class.jmp" );
/*Simple Linear Regression: y = intercept + beta * x + error*/
y = :height << get values;
X = :weight << get values;
{Estimates, Std_Error, Diagnostics} = Linear Regression( y, X, <<printToLog ); 
 
RSquare = Diagnostics["RSquare"];
slope = Estimates[2];
rsquare = 0.502917160762804;
slope = 0.135507698709946;
Jim
miguello
Level VI

Re: Need help writing optimal code for extracting R^2 and Slope from Bivariate Fit

I need graph too, and it has to be Bivariate Fit. Previously the customer was taking a screenshot of the plot and portion of the report below to have Slope and R^2 included (basically image in my initial question without red markings). I'm automating the whole thing and I want to get rid of the report and just write those two values on the plot itself (as illustrated on my screenshot with red text on the plot).

txnelson
Super User

Re: Need help writing optimal code for extracting R^2 and Slope from Bivariate Fit

Here is an example that should get you started.

txnelson_0-1654155994175.png

 

Names Default To Here( 1 );

// Open Data Table: big class.jmp
// → Data Table( "big class" )
dt = Open( "$SAMPLE_DATA/big class.jmp" );

biv = Bivariate( Y( :height ), X( :weight ), Fit Line( {Line Color( {212, 73, 88} )} ) );
Report( biv )[framebox( 1 )] << add graphics script(
	Text Color( {212, 73, 88} );
	xMin = Current Report()[axisbox( 2 )] << get min;
	yMin = Current Report()[axisbox( 1 )] << get min;
	xMax = Current Report()[axisbox( 2 )] << get max;
	yMax = Current Report()[axisbox( 1 )] << get max;
	textList = {};
	Insert Into( textList, xMin + (xMax - xMin) * .25 );
	Insert Into( textList, yMax - (yMax - yMin) * .1 );
	R2 = Current Report()["Summary of Fit"][Number Col Box( 1 )][1];
	R2 = "R^2 = " || Format( R2, "Fixed Dec", 5, 3 );
	Text( Right Justified, textList, R2 );
	textList = {};
	Insert Into( textList, xMin + (xMax - xMin) * .32 );
	Insert Into( textList, yMax - (yMax - yMin) * .15 );
	Slope = Current Report()["Parameter Estimates"][Number Col Box( 1 )][2];
	Slope = "Slope = " || format(Slope,"Fixed Dec",9,5);
	Text(Right Justified, textList, Slope);
);
report(biv)["Linear Fit"] << visibility("collapse");
report(biv)[PictureBox(2)] << visibility("collapse");

Documentation for the Graphic additions, and the Display Tree manipulationq can be found in the Scripting Guide and the Scripting Index

Jim

Re: Need help writing optimal code for extracting R^2 and Slope from Bivariate Fit

Names Default To Here(1);

dt=Open("$SAMPLE_DATA/Tablet Production.jmp");

biv=dt<<Bivariate(
	Y( :Dissolution ),
	X( :Mill Time ),
	Fit Line( {Line Color( {212, 73, 88} )} )

);

slope=report(biv)[NumberColBox(13)][2];
r2=report(biv)[NumberColBox(1)][1];

slope=round(slope,3);
r2=round(r2,3);


Eval(Parse(Eval Insert("biv<<SendToReport(
		Dispatch(
			{},
			\!"Bivar Plot\!",
			FrameBox,
			Add Text Annotation(
				Text( \!"Rsq=^r2^
Slope=^slope^\!" ),
				Fixed Size( 0 ),
				Text Box( {2, 16, 100, 70} ),
				Filled( 0 )
			)
		)
	);")));