Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- JMP User Community
- :
- Discussions
- :
- Re: How to pass a variable into a formula?

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Feb 28, 2018 9:39 AM
(7044 views)

I am using the below syntax. I would like to modify one part of the formula which is the estimated MA coefficient.

In this case, the value of the MA Coefficient is 0.144654220611782.

My numerous attempts so far, includign using lists, have fallen flat.

How could I assign the coefficient value (of 0.144654220611782) to a "variable" and then use that "variable" in the formula? Meaning I assign the coefficient value before running the formula syntax, with the value passed into the formula in a suitable manner.

```
dt << New Column( "Yt Forecast1", Numeric, "Continuous", Format( "Best", 12 ),
Formula(
ARIMA Forecast(
:Yt,
50,
ARIMA( 0, 1, 1, No Intercept( 1 ) ),
{MA Coefficients( {0.144654220611782} )},
Row() - 50,
Row() - 50
)))
```

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

What has worked for me is this. I get the value "tempMACoeff " from the list from lstModel = ts << Get Models; (ts is the time series analysis)

The column formula accepts a new value for tempMACoeff and will update...

```
col = dt << New Column( "Yt-Forecast", Numeric);
dt << set Table Variable( "coef", tempMACoeff );
col << Set Formula( Eval(
Eval Expr(
ARIMA Forecast(
:Yt,
50,
ARIMA( 0, 1, 1, No Intercept( 1 ) ),
{MA Coefficients( {Expr( :coef )} )},//
Row() - 50,
Row() - 50
)
)
)); // set the formula
```

7 REPLIES 7

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: How to pass a variable into a formula?

edit: after re-reading, this might not be the answer.

Use a Table Variable, by scripting, or from the red triangle.

```
New Table( "Untitled 46",
Add Rows( 3 ),
New Table Variable( "coef", 99.3 ),
New Column( "Column 1",
Numeric,
"Continuous",
Format( "Best", 12 ),
Formula( :coef ),
Set Selected
)
)
```

Craige

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: How to pass a variable into a formula?

You need something like this. When you change the Table Variable, the column updates.

```
Eval(
Eval Expr(
ARIMA Forecast(
:age,
40,
ARIMA( 0, 1, 1, No Intercept( 1 ) ),
{MA Coefficients( {Expr( :coef )} )},
Row() - 9,
Row() - 9
)
)
)
```

Using the same table variable idea, notice how the * eval( eval expr( ... expr(:coef) ... ) )* works.

Craige

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: How to pass a variable into a formula?

Here is a slightly different way of handling the same solution as Craige provided. The only difference is that this version hard codes the Coef value into the formula, not requiring a Table Variable

```
Names Default To Here( 1 );
dt = Current Data Table();
coef = 99.3;
Eval(
Substitute(
Expr(
New Table( "the table",
Add Rows( 3 ),
New Table Variable( "coef", 99.3 ),
New Column( "Column 1",
Numeric,
"Continuous",
Format( "Best", 12 ),
Formula( __coef__ ),
Set Selected
)
)
),
Expr( __coef__ ), coef
)
);
```

Jim

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: How to pass a variable into a formula?

Another approach is to convert the formula to a string representation and then do standard string substitution, converting it back to an expression when you assign the column formula:

```
strFml = Char(Expr(
ARIMA Forecast(
:Yt,
50,
ARIMA( 0, 1, 1, No Intercept( 1 ) ),
{MA Coefficients( {_coeff_} )},
Row() - 50,
Row() - 50
)
));;
coeffValue = 0.144;
SubstituteInto("_coeff_",coeffValue);
dt << New Column("Y Forecast", numeric, Set Formula( Eval(Parse(strFml)) ));
```

-Dave

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: How to pass a variable into a formula?

Dave, I wasn't able to get this to work out. I resorted to the below which seems to the job I'm hoping to get done.

Thanks, Scott.

Thanks, Scott.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

What has worked for me is this. I get the value "tempMACoeff " from the list from lstModel = ts << Get Models; (ts is the time series analysis)

The column formula accepts a new value for tempMACoeff and will update...

```
col = dt << New Column( "Yt-Forecast", Numeric);
dt << set Table Variable( "coef", tempMACoeff );
col << Set Formula( Eval(
Eval Expr(
ARIMA Forecast(
:Yt,
50,
ARIMA( 0, 1, 1, No Intercept( 1 ) ),
{MA Coefficients( {Expr( :coef )} )},//
Row() - 50,
Row() - 50
)
)
)); // set the formula
```

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: How to pass a variable into a formula?

Column formulas subscribe to other columns and table variables to listen for changes and reevaluate when that happens. Not to other JSL variables though. So, the way it works for you, the formula can see the *coef* variable and knows it should subscribe for changes. I think the other suggestions are replacing the variable with a constant, so the formula no longer knows it should subscribe to something.

Craige