## Format prediction expression

Community Trekker

Joined:

Mar 28, 2015

Hi,

Within Fit Model (Least squares option), it is possible to change the format of numbers in the "parameter estimates". It is also possible to change the format (e.g. the number of decimals) for the predition expression?

Thank you,

Elie

1 ACCEPTED SOLUTION

Accepted Solutions

Super User

Joined:

Jun 22, 2012

Solution

Elie,

Here is a script that illustrates what I eluded to in my last response:  It uses the formula that is generated when the prediction column is generated.  However, the script could also be written to just grab the parameter estimates from the Parameter Estimates table that is displayed, and to modify it and place it back into the display.  This would be the programmers choice.

Names Default To Here( 1 );

dt = Open( "\$SAMPLE_DATA\semiconductor capability.jmp" );

// Run the analysis

fm = dt << Fit Model(

Y( :NPN1 ),

Effects( :PNP1, :PNP2, :NPN2 ),

Personality( "Standard Least Squares" ),

Emphasis( "Minimal Report" ),

Run(

:NPN1 << {Lack of Fit( 0 ), Plot Actual by Predicted( 0 ), Plot Regression( 0 ),

Plot Residual by Predicted( 0 ), Plot Effect Leverage( 0 )}

)

);

// Output the formula to a new column

fm << Prediction Formula;

// Get the formula back as a character string

TheFormula = Char( Column( dt, N Cols( dt ) ) << get formula );

// Strip out the elements and reformat the values

Intercept = Format( Num( Word( 1, TheFormula, " " ) ), "Fixed Dec", 4 );

Beta1 = Format( Num( Word( 3, TheFormula, " " ) ), "Fixed Dec", 4 );

Beta2 = Format( Num( Word( 7, TheFormula, " " ) ), "Fixed Dec", 4 );

Beta3 = Format( Num( Word( 11, TheFormula, " " ) ), "Fixed Dec", 4 );

// Reconstruct the prediction formula

NewFormula = Intercept || " + " || Beta1 || " * :PNP1 + " || Beta2 ||

" * :PNP2 + " || Beta3 || " * :NPN2";

// Write it back to the displayed output

Report( fm )["Parameter Estimates"] << append( Outline Box( "Prediction Formula", Text Box( NewFormula ) ) );

Jim
4 REPLIES

Super User

Joined:

Jun 22, 2012

If you double click on a column within the parameter estimates table, a window will popup that allows you to change the format of the numbers.  You just have to repeat the same operation for each column. This is a feature available in JMP for all displayed tables in the output.  If you want to do this using JSL, then the answer is also yes.  Number Col Box() can have their formats defined.

Concerning the displayed Prediction Expression.  This is actually a picture object, and isn't changeable.  However, the prediction formula can be saved as a formula in a new column, and the formula could then be picked, and added to the display using whatever level of decimal significance you want.

Jim

Community Trekker

Joined:

Mar 28, 2015

Hi Jim,

Thank you for the quick reply. The first part of your answer, I did know. I was, however, specifically refering to the displayed prediction expression. As I suspected it is indeed a picture object.

Could you elaborate a little bit on what you mean with "picking a saved prediction formula and adding it to the display". I do know how to save a formula (and I often use it) but I don't know what you mean with adding it to the display in whatever format you want. Is this within JMP or are you refering to manually reducing the number of decimals somewhere else?

Thank you,

Elie

Super User

Joined:

Jun 22, 2012

Solution

Elie,

Here is a script that illustrates what I eluded to in my last response:  It uses the formula that is generated when the prediction column is generated.  However, the script could also be written to just grab the parameter estimates from the Parameter Estimates table that is displayed, and to modify it and place it back into the display.  This would be the programmers choice.

Names Default To Here( 1 );

dt = Open( "\$SAMPLE_DATA\semiconductor capability.jmp" );

// Run the analysis

fm = dt << Fit Model(

Y( :NPN1 ),

Effects( :PNP1, :PNP2, :NPN2 ),

Personality( "Standard Least Squares" ),

Emphasis( "Minimal Report" ),

Run(

:NPN1 << {Lack of Fit( 0 ), Plot Actual by Predicted( 0 ), Plot Regression( 0 ),

Plot Residual by Predicted( 0 ), Plot Effect Leverage( 0 )}

)

);

// Output the formula to a new column

fm << Prediction Formula;

// Get the formula back as a character string

TheFormula = Char( Column( dt, N Cols( dt ) ) << get formula );

// Strip out the elements and reformat the values

Intercept = Format( Num( Word( 1, TheFormula, " " ) ), "Fixed Dec", 4 );

Beta1 = Format( Num( Word( 3, TheFormula, " " ) ), "Fixed Dec", 4 );

Beta2 = Format( Num( Word( 7, TheFormula, " " ) ), "Fixed Dec", 4 );

Beta3 = Format( Num( Word( 11, TheFormula, " " ) ), "Fixed Dec", 4 );

// Reconstruct the prediction formula

NewFormula = Intercept || " + " || Beta1 || " * :PNP1 + " || Beta2 ||

" * :PNP2 + " || Beta3 || " * :NPN2";

// Write it back to the displayed output

Report( fm )["Parameter Estimates"] << append( Outline Box( "Prediction Formula", Text Box( NewFormula ) ) );

Jim

Super User

Joined:

Jun 23, 2011

Great idea Jim! And generalizing it to handle prediction formulas of any complexity was a good scripting exercise.

There is a function Expr as Picture() that may come in handy. Complex prediction formulas with multiple Match clauses and escaped quotation marks can be hard to read.

Here's my attempt. Seems to work but only tested for a few models.

Names Default To Here(1);

// Fit Model and save formula to the data table

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

fm = Fit Model(Y(:weight), Effects(:sex, :height, :sex * :height), Run);

col = fm << prediction formula;

// Round all numbers in prediction formula to n digits

n = 2; // Set desired number of decimals here

pos = 1;  // Substr start position

f = Char(col << get formula); // Pred. formula as string

// Expr: Find next raw number

raw = Expr(Regex(Substr(f, pos), "\d+\.\d+"));

// Expr: Round number (Substitute only required for locales with decimal comma)

rounded = Expr(Substitute(Char(Num(raw),, n), ",", "."));

// Replace all numbers with rounded numbers

pos = Contains(f, raw);

While(!Is Missing(raw),

Substitute Into(f, raw, rounded);

pos += Length(rounded);

);

Show(f);