cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
The Discovery Summit 2025 Call for Content is open! Submit an abstract today to present at our premier analytics conference.
Choose Language Hide Translation Bar
View Original Published Thread

JMP 18.1.2 Extract RSquare Values From Fit Y by X Analysis

WoHNY
Level III

Running Analyze > Fit Y by X. I have the code below which opens a data table but there are no RSquare values in the table. Any ideas on what I'm missing?

// Open Data Table
dt = Data Table( "Raw Data" );

// Run Fit Y by X Analysis with a By Variable
fitObj = dt << Fit Y by X( Y( :"Mean(VALUEABS)" ), X( :Mean ), By( :PARM_LABEL ) );

// Get the analysis report
rpt = fitObj << Get Report;

// Find all RSquare nodes in the report
rsquareNodes = rpt << Find All Matching Elements( "RSquare" );
Show( "Number of RSquare nodes found: " || Char( N Items( rsquareNodes ) ) );

// Initialize a list to store the by group and RSquare pairs
rsquareData = {};

// Loop over each RSquare node found
For( i = 1, i <= N Items( rsquareNodes ), i++,
	{
// Extract the RSquare text and convert to numeric value
	rsqText = rsquareNodes[i] << Get Text ; rsqVal = Num( rsqText ) ; 

// Try to retrieve the by-group label.
	parentObj = rsquareNodes[i] << Get Parent ; byLabel = "" ;
	While( byLabel == "" & parentObj != {},
		byLabel = parentObj << Get Text;
		If( byLabel == "",
			parentObj = parentObj << Get Parent
		);
	) ; 

// Append the pair (by group label, RSquare value) to list.
	Append( rsquareData, {byLabel, rsqVal} ) ; }
);

Show( rsquareData );

// Create a new data table to store the RSquare values by by-group.
rsqDT = New Table( "RSquare Values",
	Add Columns(
		Column( "By Group", Character, "Nominal" ),
		Column( "RSquare", Numeric, "Continuous" )
	)
);

// Populate the new data table with the extracted values
For( i = 1, i <= N Items( rsquareData ), i++,
	{New Row( rsqDT ) ; rsqDT:("By Group")[Row()] = rsquareData[i][1] ; rsqDT:RSquare[
	Row()] = rsquareData[i][2] ; }
);

// Display the new data table
Show( rsqDT );

(Above JSL moved from plain text display to JMP JSL display format) txnelson

2 ACCEPTED SOLUTIONS

Accepted Solutions
txnelson
Super User


Re: JMP 18.1.2 Extract RSquare Values From Fit Y by X Analysis

There were multiple issues with your syntax.  Below is an example of how I would approach the issue

// Open Data Table
dt = Data Table( "Raw Data" );

// Run Fit Y by X Analysis with a By Variable
fitObj = dt << Fit Y by X( Y( :"Mean(VALUEABS)" ), X( :Mean ), By( :PARM_LABEL ) );
dt=current data table();
fitObj = dt << Bivariate(
	Y( :height ),
	X( :weight ),
	Fit Line( {Line Color( {212, 73, 88} )} ),
	By( :sex )
);

nrpts = nitems(fitObj);

report(fitobj[1])[OutlineBox(1)] << get title

// Find all RSquare nodes in the report
Show( "Number of RSquare nodes found: " || Char( nrpts ) );

rsquareData = {};

// Initialize a list to store the by group and RSquare pairs

For Each( {obj}, fitObj,
	r2=report(obj);
	rsq = (r2["Linear Fit","Summary of Fit",numbercolbox(1)]<<get)[1];
	byLabel = r2[OutlineBox(1)] << get title;
	insert into(rsquareData, rsq);
	insert into(rsquareData, byLabel);
);

show(rsquareData);
Jim

View solution in original post

WoHNY
Level III


Re: JMP 18.1.2 Extract RSquare Values From Fit Y by X Analysis

Thank you for the reply. I am looking for the RSquare value. I had run the Fit Y By X analysis and took the code from the log. Then I added a fit line but that action was not logged. It has been a while and I had forgotten about the right click and Make Into a Data Table feature. So thank you for the refresher on that.

View solution in original post

6 REPLIES 6
jthi
Super User


Re: JMP 18.1.2 Extract RSquare Values From Fit Y by X Analysis

Which RSquare are you looking for? Generally you would do this manually in JMP and then take the script JMP creates for you (or use workflow and get script from that). So in this case:

1. Open your table

2. Run the analysis

jthi_0-1741325216714.png

3. Make the changes to report / analysis as needed (I fit line)

jthi_1-1741325246131.png

4. Make into data table from the table

jthi_2-1741325256691.png

5. Get script

// Open Data Table: Big Class.jmp
// → Data Table("Big Class")
Open("$SAMPLE_DATA/Big Class.jmp");

// Combine similar TableBoxes into a Data Table
// → Data Table("Untitled")
Local({obj},
	obj = Data Table("Big Class") << Bivariate(
		Y(:height),
		X(:weight),
		Fit Line({Line Color({230, 159, 0})}),
		By(:age)
	);
	Report(obj[1])["Linear Fit", "Summary of Fit", Table Box(1)] <<
	Make Combined Data Table;
	obj[1] << Close Window;
);

Then you can modify that to make it a bit better and end up with something like

Names Default To Here(1);

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

biv = dt << Bivariate(
	Y(:height),
	X(:weight),
	Fit Line({Line Color({230, 159, 0})}),
	By(:age)
);

dt_result = Report(biv[1])["Linear Fit", "Summary of Fit", Table Box(1)] << Make Combined Data Table;
dt_result << Select Where(:Column 1 != "RSquare") << delete rows << clear select;
-Jarmo
WoHNY
Level III


Re: JMP 18.1.2 Extract RSquare Values From Fit Y by X Analysis

Thank you for the reply. I am looking for the RSquare value. I had run the Fit Y By X analysis and took the code from the log. Then I added a fit line but that action was not logged. It has been a while and I had forgotten about the right click and Make Into a Data Table feature. So thank you for the refresher on that.

txnelson
Super User


Re: JMP 18.1.2 Extract RSquare Values From Fit Y by X Analysis

There were multiple issues with your syntax.  Below is an example of how I would approach the issue

// Open Data Table
dt = Data Table( "Raw Data" );

// Run Fit Y by X Analysis with a By Variable
fitObj = dt << Fit Y by X( Y( :"Mean(VALUEABS)" ), X( :Mean ), By( :PARM_LABEL ) );
dt=current data table();
fitObj = dt << Bivariate(
	Y( :height ),
	X( :weight ),
	Fit Line( {Line Color( {212, 73, 88} )} ),
	By( :sex )
);

nrpts = nitems(fitObj);

report(fitobj[1])[OutlineBox(1)] << get title

// Find all RSquare nodes in the report
Show( "Number of RSquare nodes found: " || Char( nrpts ) );

rsquareData = {};

// Initialize a list to store the by group and RSquare pairs

For Each( {obj}, fitObj,
	r2=report(obj);
	rsq = (r2["Linear Fit","Summary of Fit",numbercolbox(1)]<<get)[1];
	byLabel = r2[OutlineBox(1)] << get title;
	insert into(rsquareData, rsq);
	insert into(rsquareData, byLabel);
);

show(rsquareData);
Jim
jthi
Super User


Re: JMP 18.1.2 Extract RSquare Values From Fit Y by X Analysis

Initial code definitely looks like AI generated which would explain the many mistakes in the syntax.

-Jarmo
WoHNY
Level III


Re: JMP 18.1.2 Extract RSquare Values From Fit Y by X Analysis

You are right on the money! As I previously stated having forgot about the right click Make Into Data Table option I resorted to ChatGPT.

WoHNY
Level III


Re: JMP 18.1.2 Extract RSquare Values From Fit Y by X Analysis

Jim, thank you for this solution. I do appreciate it.