Script? Applying non-linear model column formula to multiple columnsI have a model formula in column 1 with 2 parameters and I would like to fit this model to 80 different data columns (Cols 2 - 82), where each data column has those 2 parameters custom-fit for it. Is there an easy way to do this?
Would I have to write a script?
Re: Script? Applying non-linear model column formula to multiple columnsending up converging on this as a solution:
Names Default to Here( 1 );
dlg = Column Dialog(
Title( "Nonlinear Regression" ),
yCol = Col List( "Y, Response",
Data Type( Numeric ),
Min Col( 1 )
),
xCol = Col List( "X, Predictor Formula",
Data Type( Numeric ),
Min Col( 1 ),
Max Col( 1 )
)
);
If( dlg["Button"] != 1,
Throw( "User cancelled" );
);
Remove From( dlg );
Eval List( dlg );
dt = Current Data Table();
n fits = N Items( yCol );
formula = xCol[1] << Get Formula;
n parms = N Items( Arg( formula, 1 ) );
parm = J( n fits, 2* n parms, .);
clmnm={};
parmnm = Arg(formula,1);
parclnm = As List(J(1,n parms));
For( i = 1, i<= n parms,i++,
parmnm[i]=Char(parmnm[i]);
parclnm[i]=Left(parmnm[i],Munger(parmnm[i],1,"=")-1);
);
For( f = 1, f <= n fits, f++,
Try(
nl fit = dt << Nonlinear( Y( yCol[f] ), X( xCol[1] ), Newton, Numeric Derivatives Only (1), Finish, Go );
parm[f,[1,2]] = Transpose( Report( nl fit )["Solution"][NumberColBox(5)] << Get As Matrix );
parm[f,[3,4]] = Transpose( Report( nl fit )["Solution"][NumberColBox(6)] << Get As Matrix );
);
clmnm[f]=Column(yCol[f])<);
dt = As Table( parm );
dt<col = Column(1);
For( f = 1, f <= n fits, f++,
col[f]=clmnm[f];
);
For( i = 1, i<= n parms,i++,
col = Column(i+1);
col << Set Name(parclnm[i]||"Estimate");
col = Column(i+1+n parms);
col << Set Name(parclnm[i]||"ApproxStdError");
);
Re: Script? Applying non-linear model column formula to multiple columnsI figured it out!
Thank you!
Re: Script? Applying non-linear model column formula to multiple columnsit errors out at this step
parm[f,0] = Transpose( Report( nl fit )["Parameter Estimates"][NumberColBox(1)] << Get As Matrix );
Re: Script? Applying non-linear model column formula to multiple columnsThank you!
Testing now ...
received following error upon debugging
Cannot subscript Display Box in access or evaluation of 'Subscript' , Report( nl fit )[/*###*/"Parameter Estimates"]
@Mark_Bailey wrote:
This solution turns out to be a bit tricky. It only works if the model you are using is NOT one of those in the Fit Curve library. (If it is, then Nonlinear launches Fit Curve, which expects the model to be specified by name, which this script doesn't provide.) Based on your claim that the actual model is more complicated, then I took the chance that it is not in the Fit Model library and wrote this script:
Names Default to Here( 1 );
dlg = Column Dialog(
Title( "Nonlinear Regression" ),
yCol = Col List( "Y, Response",
Data Type( Numeric ),
Min Col( 1 )
),
xCol = Col List( "X, Predictor Formula",
Data Type( Numeric ),
Min Col( 1 ),
Max Col( 1 )
)
);
If( dlg["Button"] != 1,
Throw( "User cancelled" );
);
Remove From( dlg );
Eval List( dlg );
dt = Current Data Table();
n fits = N Items( yCol );
formula = xCol[1] << Get Formula;
n parms = N Items( Arg( formula, 1 ) );
parm = J( n fits, n parms, . );
For( f = 1, f <= n fits, f++,
nl fit = dt << Nonlinear( Y( yCol[f] ), X( xCol[1] ), Newton, Finish );
parm[f,0] = Transpose( Report( nl fit )["Parameter Estimates"][NumberColBox(1)] << Get As Matrix );
);
As Table( parm );
Open the JMP data table first, then open and run this script. Select all of the response columns (columns 2-82 in your example) and click Y, Response. Click the column with the model formula (column 1 in your example) and click X, Predictor Formula, . Then click OK.
Re: Script? Applying non-linear model column formula to multiple columnsThis solution turns out to be a bit tricky. It only works if the model you are using is NOT one of those in the Fit Curve library. (If it is, then Nonlinear launches Fit Curve, which expects the model to be specified by name, which this script doesn't provide.) Based on your claim that the actual model is more complicated, then I took the chance that it is not in the Fit Model library and wrote this script:
Names Default to Here( 1 );
dlg = Column Dialog(
Title( "Nonlinear Regression" ),
yCol = Col List( "Y, Response",
Data Type( Numeric ),
Min Col( 1 )
),
xCol = Col List( "X, Predictor Formula",
Data Type( Numeric ),
Min Col( 1 ),
Max Col( 1 )
)
);
If( dlg["Button"] != 1,
Throw( "User cancelled" );
);
Remove From( dlg );
Eval List( dlg );
dt = Current Data Table();
n fits = N Items( yCol );
formula = xCol[1] << Get Formula;
n parms = N Items( Arg( formula, 1 ) );
parm = J( n fits, n parms, . );
For( f = 1, f <= n fits, f++,
nl fit = dt << Nonlinear( Y( yCol[f] ), X( xCol[1] ), Newton, Finish );
parm[f,0] = Transpose( Report( nl fit )["Parameter Estimates"][NumberColBox(1)] << Get As Matrix );
);
As Table( parm );
Open the JMP data table first, then open and run this script. Select all of the response columns (columns 2-82 in your example) and click Y, Response. Click the column with the model formula (column 1 in your example) and click X, Predictor Formula, . Then click OK.
Re: Script? Applying non-linear model column formula to multiple columnsYou can do it without a script but it will involve a lot of clicking. Assuming that you already have set up the non-linear model as a column formula, follow these steps:
- Select Tables > Stack.
- Select all the response columns (2-82 in your example) and click Stack.
- Make sure that the option to copy the formulas is selected.
- Enter a name for the Data column, such as Response.
- Enter a name for the Label column, such as Curve.
- Click OK.
- Select Analyze > Specialized Models > Nonlinear.
- Select the predictor column (83 in your example) and click X
- Select the model column (1 in your example) and click Y.
- Select the Curve column and click By.
- Click OK.
- Now click Go for each Curve. (This is the tedious part.)
- Right-click on any Parameter Estimates report and select Make Combined Data Table.
This process should give you a new data table with all of the parameter estimates.
Re: Script? Applying non-linear model column formula to multiple columnsMark,
Thank you.
The responses to all your Verifying questions are yes.
The formula can be anything, but let's say its y = h*exp(-b*x^(0.65)) for simplicity, with h and b as parameters and x>0.
The actual formula is much more complicated, but that extra complication shouldn't impact the solution.
Re: Script? Applying non-linear model column formula to multiple columnsI will ask again: what is the non-linear model ( f(X) ) expressed in the column 1 formula? What are the two parameters?
Verifying: predictor (X) is last column and it is used in formula in column 1?
Verifying: the individual responses (Y) are in columns 2-82?
Verifying: you want to fit Y = f(X) separately to each Y and capture the parameter estimates for each Y?
Re: Script? Applying non-linear model column formula to multiple columnsYes, a predictor variable exists in another column.
We can call it column 83 if you like.
And I suppose my target output would be a table which shows the best fit values of the two parameters per column with associated confidence intervals.
Re: Script? Applying non-linear model column formula to multiple columnsWhat is your non-linear model that is stored as a formula in column 1? This formula is usually a function of the predictor variable. Which column holds the predictor values?
If this case is simple, I can write a script.