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

Computing Slope And Intercept in JSL

I have a data table where the data points (both y's and x's) for slope calculation are in non-contiguous columns and I need to compute slope and intercept data for each row. Is there a way to do this in JMP? Tried to do it in excel but Slope function doesn't work, due to y's and x's being in non-contiguous columns.

To illustrate the problem, in the sample data table attached, I need the slope of (height1, height2, ...) against (weight1, weight2, ...) computed for every row. I intend to build a column with slope data

I would appreciate any ideas or suggestions.

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
ms
Super User (Alumni) ms
Super User (Alumni)

Re: Computing Slope And Intercept in JSL

Matrix formulas could solve it very efficiently. But if we stick with the Fit Y by X platform the columns must be stacked before analysis. For your example table, this code stacks the columns, fits the lines and then transfer all slopes and intercept to a new table (which can be joined with the original data table if desired):

//Stack with multiple series option

dt_stacked = Data Table( "Example-2.jmp" ) << Stack(

  columns( :height, :weight, :height1, :weight1, :height2, :weight2 ),

  Source Label Column( "Label" ),

  Stacked Data Column( "Data" ),

  Number of Series( 2 )

);

//Fit all slopes

biv = dt_stacked << Bivariate( Y( :Data 2 ), X( :Data ), Group by( :Temp1 2 ), Fit line( 1 ) );

//Make into data table

slopetable = Report( biv )[Outline Box( "Parameter Estimates" )][1] << make combined data table;

// Split table to get slope and intercept on the same row

slopetable << Split(

  Split By( :Term ),

  Split( :Estimate ),

  Group( :X, :Y, :Temp1 2 ),

  Remaining Columns( Drop All )

);


View solution in original post

7 REPLIES 7
mattflynn
Level III

Re: Computing Slope And Intercept in JSL

// one way...

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

dt << Graph Builder(

     Show Control Panel(0),

     Variables( x( :weight ), Y( :height ),

     Elements(

          Points( X, Y, Legend( 1 ), Jitter( 1 ),

          Line of Fit(

               X,

               Y,

               Legend(  5 ),

               Confidence of Fit( 1 ),

               Confidence of Prediction( 0 ),

               Degree( "linear" ),

               Equation( 1 ),

               Root Mean Square Error( 0 )

          )

     )

);

Re: Computing Slope And Intercept in JSL

Matt, thanks for the reply, but that's not what I wanted.

Attached is an example data table to illustrate. I need the slope of (height1, height2) against (weight1, weight2) computed for every row. I intend to build a column of slope data.

Thanks,

Ravi

ms
Super User (Alumni) ms
Super User (Alumni)

Re: Computing Slope And Intercept in JSL

If there only are two points involved the slope can be calculated for each row by the formula (:height2 - :height1) / (:weight2 - :weight1).

Re: Computing Slope And Intercept in JSL

Hi MS,

I have more than 2 points involved.

ms
Super User (Alumni) ms
Super User (Alumni)

Re: Computing Slope And Intercept in JSL

Matrix formulas could solve it very efficiently. But if we stick with the Fit Y by X platform the columns must be stacked before analysis. For your example table, this code stacks the columns, fits the lines and then transfer all slopes and intercept to a new table (which can be joined with the original data table if desired):

//Stack with multiple series option

dt_stacked = Data Table( "Example-2.jmp" ) << Stack(

  columns( :height, :weight, :height1, :weight1, :height2, :weight2 ),

  Source Label Column( "Label" ),

  Stacked Data Column( "Data" ),

  Number of Series( 2 )

);

//Fit all slopes

biv = dt_stacked << Bivariate( Y( :Data 2 ), X( :Data ), Group by( :Temp1 2 ), Fit line( 1 ) );

//Make into data table

slopetable = Report( biv )[Outline Box( "Parameter Estimates" )][1] << make combined data table;

// Split table to get slope and intercept on the same row

slopetable << Split(

  Split By( :Term ),

  Split( :Estimate ),

  Group( :X, :Y, :Temp1 2 ),

  Remaining Columns( Drop All )

);


Re: Computing Slope And Intercept in JSL

MS,

Thanks for the help!

I have a follow up question. Is there an easy way to combine RSquare (only) from 'Summary of Fit' along with slope and intercept. I tried below command, but looks like once it goes one layer down the outline, it can't go back up a level to the outline nodes.

slopetable = Report( biv )[Outline Box( "Parameter Estimates" )][1][Outline Box( "Summary of Fit" )][1] << make combined data table;

Thanks!

ms
Super User (Alumni) ms
Super User (Alumni)

Re: Computing Slope And Intercept in JSL

That's right, you're digging deeper inside the Parameter Estimates and won't find the RSquare down there. The Summary of Fit sits on the same level in tree, so use a separate call instead of go down and back up again.

I do not think it is possible to get parameters and summary of fit in the same table with one command. Make two tables and then there are several ways to bring the data together.

Try this:

//Stack with multiple series option

dt_stacked = Data Table( "Example-2.jmp" ) << Stack(

  columns( :height, :weight, :height1, :weight1, :height2, :weight2 ),

  Source Label Column( "Label" ),

  Stacked Data Column( "Data" ),

  Number of Series( 2 )

);

//Fit all slopes

biv = dt_stacked << Bivariate( Y( :Data 2 ), X( :Data ), Group by( :Temp1 2 ), Fit line( 1 ) );

//Make into data tables

slopetable = Report( biv )[Outline Box( "Parameter Estimates" )][1] << make combined data table;

R2table = Report( biv )[Outline Box( "Summary of Fit" )][1] << make combined data table;

// Split table to get slope and intercept on the same row, then add a column with R2-values

results = slopetable << Split(

  Split By( :Term ),

  Split( :Estimate ),

  Group( :X, :Y, :Temp1 2 ),

  Remaining Columns( Drop All )

);

results << New Column( "R2",

  numeric,

  set values(

  Column( R2table, "Column 2" )[R2table << get rows where(

  Column( R2table, "Column 1" )[] == "RSquare"

  )]

  )

);

// Close intermediate windows

Close( dt_stacked, slopetable, R2table, no save );