Choose Language Hide Translation Bar
Community Trekker

## Save to Data Table (scripted)

Hi! I have scripted a simple moving average, but I need to work on the data (calculate the slope variation), so I want to save it to a data table. But how do I script that?

Ideas on how to calculate the slope are also welcome! What I'm thinking is to simply take the derivative as a lag (ds/dt=(time-lag(time,x)) / (position-lag(position,x))

3 REPLIES 3
Super User

## Re: Save to Data Table (scripted)

Here is a simple example taken from the Help==>Scripting Index, for the Time Series Object that illustrates how to save the simple moving averages to a data table in a script.

Names Default To Here( 1 );

dt = Open( "\$SAMPLE_DATA/Stock Prices.jmp" );

obj = Time Series(

Y(:Close ),

Simple
5 ) )

);

resultdt = obj <<

Simple Moving Average( Save to Data Table );

I won't venture an opinion on the calculation of the slope.

Jim
Community Trekker

## Re: Save to Data Table (scripted)

In my data, I often need to calculate the slope of a small section of rows that repeats every 200 rows  in files with > 500K rows, and this is the way I usually do it:

1) Add an artificial marker* [e.g.,0 through 200] to the "row series" using a sequence
*I call this variable measurement time

2) Set the length of the X and Y averages for the slope formula
3) Use an IF statement to estimate the slope of the range or rows I want

4) [In my data] I correct all measurements with the slope

This is the script I use [heavily hard coded, but gets the job done for me ^_^ ]

`    Y_Mean = Mean(        :MA_T1[Row()],        :MA_T1[Row() + 1],        :MA_T1[Row() + 2],        :MA_T1[Row() + 3],        :MA_T1[Row() + 4],        :MA_T1[Row() + 5],        :MA_T1[Row() + 6],        :MA_T1[Row() + 7],        :MA_T1[Row() + 8],        :MA_T1[Row() + 9],        :MA_T1[Row() + 10],        :MA_T1[Row() + 11],        :MA_T1[Row() + 12],        :MA_T1[Row() + 13],        :MA_T1[Row() + 14]    );    X_Mean = Mean(        :Measurement time[Row()],        :Measurement time[Row() + 1],        :Measurement time[Row() + 2],        :Measurement time[Row() + 3],        :Measurement time[Row() + 4],        :Measurement time[Row() + 5],        :Measurement time[Row() + 6],        :Measurement time[Row() + 7],        :Measurement time[Row() + 8],        :Measurement time[Row() + 9],        :Measurement time[Row() + 10],        :Measurement time[Row() + 11],        :Measurement time[Row() + 12],        :Measurement time[Row() + 13],        :Measurement time[Row() + 14]    );    If( :Measurement time == 1,        Slope = ((:Measurement time[Row()] - X_Mean) * (:MA_T1[Row()] - Y_Mean) + (:Measurement time[Row() + 1] - X_Mean) * (:MA_T1[Row()         + 1] - Y_Mean) + (:Measurement time[Row() + 2] - X_Mean) * (:MA_T1[Row() + 2] - Y_Mean) + (:Measurement time[Row() + 3] - X_Mean)         * (:MA_T1[Row() + 3] - Y_Mean) + (:Measurement time[Row() + 4] - X_Mean) * (:MA_T1[Row() + 4] - Y_Mean) + (:Measurement time[Row()         + 5] - X_Mean) * (:MA_T1[Row() + 5] - Y_Mean) + (:Measurement time[Row() + 6] - X_Mean) * (:MA_T1[Row() + 6] - Y_Mean) + (        :Measurement time[Row() + 7] - X_Mean) * (:MA_T1[Row() + 7] - Y_Mean) + (:Measurement time[Row() + 8] - X_Mean) * (:MA_T1[Row() + 8        ] - Y_Mean) + (:Measurement time[Row() + 9] - X_Mean) * (:MA_T1[Row() + 9] - Y_Mean) + (:Measurement time[Row() + 10] - X_Mean) * (        :MA_T1[Row() + 10] - Y_Mean) + (:Measurement time[Row() + 11] - X_Mean) * (:MA_T1[Row() + 11] - Y_Mean) + (:Measurement time[Row()         + 12] - X_Mean) * (:MA_T1[Row() + 12] - Y_Mean) + (:Measurement time[Row() + 13] - X_Mean) * (:MA_T1[Row() + 13] - Y_Mean) + (        :Measurement time[Row() + 14] - X_Mean) * (:MA_T1[Row() + 14] - Y_Mean)) / ((:Measurement time[Row()] - X_Mean) ^ 2 + (        :Measurement time[Row() + 1] - X_Mean) ^ 2 + (:Measurement time[Row() + 2] - X_Mean) ^ 2 + (:Measurement time[Row() + 3] - X_Mean)         ^ 2 + (:Measurement time[Row() + 4] - X_Mean ^ 2) + (:Measurement time[Row() + 5] - X_Mean) ^ 2 + (:Measurement time[Row() + 6]        -X_Mean) ^ 2 + (:Measurement time[Row() + 7] - X_Mean) ^ 2 + (:Measurement time[Row() + 8] - X_Mean) ^ 2 + (:Measurement time[Row()         + 9] - X_Mean) ^ 2 + (:Measurement time[Row() + 10] - X_Mean) ^ 2 + (:Measurement time[Row() + 11] - X_Mean) ^ 2 + (        :Measurement time[Row() + 12] - X_Mean) ^ 2 + (:Measurement time[Row() + 13] - X_Mean) ^ 2 + (:Measurement time[Row() + 14]        -X_Mean) ^ 2)    );    If( :Measurement time < 1,        :MA_T1,        :MA_T1 - Slope * :Measurement time    );)`

Hope this helps,

And if someone know how to do do this please let me know:

[i.e., subscript the [user]-given name of a variable without having to use the actual name of the variable]

-JoseGL

Community Trekker

## Re: Save to Data Table (scripted)

Jim, thanks, that did the trick

Meanwhile I calculated manually, but will now use Jim's solution. And JoseGL, I used the following formula that you also should be able to use. You can use Lag(:variable,index) to indicate row index. it is much more flexible this way, because you can change the counter n to any desired value.