An introduction to RLPlot scripting

RLPlot supports a "C"-style scripting language with all elements of the scripts being inside the spreadsheet

 content:  Execution modes
Syntax summary
Loops
Flow control
Example 1: Using a single formula for a range of calulations
Example 2: Bootstrapping a mean
Example 3: Bootstrapping a correlation coefficient


Execution modes

The immediate mode is entered when a formula is entered into a spread sheet cell. Formulas start with a "=" as first character of the cell.
The program mode is entered from immediate mode my calling the eval(), exec(), or call() function.


Syntax summary

Separating statements
Each statement is terminated by either a comma, ',', or a semicolon, ';'. Typically each line is terminated by a semicolon. The possibility to separate statements by a comma should be restricted to cases where it is necessary by the overall syntax.

Parenthesis
Round parenthesis, '(' and ')', are used to overrule operator precedence and they also enclose arguments of a function call.
Curly parenthesis, '{' and '}', enclose a block statement. Block statements are used to structure the code and are required for conditional execution blocks: They follow an if, else, for, or while statement.
Square parenthesis, '[' and ']', are used with arrays. They enclose an index for the array, accessing a single element of the array.

Function arguments
Arguments to functions are separated by ',' or ';'. If arguments may be interpreted as a list, possibly a list with variable length, ',' is used. For multiple list or mixed types ';' is used. If a function expects a fixed number of arguments, either ',' or ';' can be used. See the function descriptions for details.

Comments
Cells starting with a double slash, '//', are treated as comment. All cells following on the same line or row are also treated as comment.

International character sets
No attempt has been made to allow country specific characters. They are allowed inside string constants only. For variable names use 'A..Z', 'a..z', '0..9', and '_' only. Names must not contain spaces or punctuation characters.

top

Loops

The number of iteration for all loops is limited by maxiter(). This provides against infininite loops.

FOR loop: syntax1
Using this syntax the 'for'-token is followed by three statments enclosed in round parenthesis. During execution the first of these three statements is executed once. The third statement and an optional block statement after the last parenthesis are executed as long as the seond statement is true.
example:  s = 0;
a = a1:a11;
for(i = 0; i < count(a); i++) {
    s += a[i];
    }
...


FOR loop: syntax2
This notation steps through an array and assigns its values to a variable. The following example is equivalent to the above.
example:  s = 0;
a = a1:a11;
for(v in a) {
    s += v;
    }
...


WHILE loop
The 'while'-token is followed by an expression enclosed in round parenthesis followed by block statement. The block statement is executed as often and as long as the expression following the 'while' token is true. The following example is equivalent to the above examples.
example:  i = s = 0;
a = a1:a11;
while(i < count(a)) {
    s += a[i]; i++;
    }
...


top

Flow control

return
The return statement exits the current script. An optional argument may be given. With no argument the result of the last operation is returned.

break
Break exits the current block statement.

if(exp){code}
if(exp){code1}else{code2}
If initiates conditional execution. The statement is followed by an expression enclosed in round parenthesis followed by an execution block. The block is executed only if the first expression is true. The execution block may be followed by an optional 'else' statement and a second execution block. This second block is executed only if the first expression evaluates to false. The following example is eqivalent to the above examples.
example:  i = 0;
a = a1:a11;
while(i < count(a)) {
    if(i > 0) {
        s += a[i];
        }
    else {
        s = a[0];
        }
    i++;
    }
...

top

Example 1: Using a single fomula for a range of calulations

This example demonstrates the use of eval(), exec(), and call() to apply the same calculation on a range of data. The formula for this example is given in cell b1. Note that this formula is a text value. All three possibilities give the same result. When the formula occupies only one cell of the spread sheet, as in this example, the function eval() is probably the right choice.

   AB CD
 1  log10(a)   
 2     
 312 =a=a3;eval($b$1)=a=a3;call("b1:b1") =a="a="+a3;exec("b1:b1";a)
 4110 =a=a4;eval($b$1)=a=a4;call("b1:b1") =a="a="+a4;exec("b1:b1";a)
 553 =a=a5;eval($b$1)=a=a5;call("b1:b1") =a="a="+a5;exec("b1:b1";a)
 667 =a=a6;eval($b$1)=a=a6;call("b1:b1") =a="a="+a6;exec("b1:b1";a)
 7675 =a=a7;eval($b$1) =a=a7;call("b1:b1") =a="a="+a7;exec("b1:b1";a)

displays as:
   AB CD
 1  log10(a)   
 2     
 312 1.079181.07918 1.07918
 4110 2.041392.04139 2.04139
 553 1.724281.72428 1.72428
 667 1.826071.82607 1.82607
 7675 2.8293 2.8293 2.8293

changing cell 'b1' only, fom 'log10(a)' to 'sqrt(a)', changes the whole display to:
   AB CD
 1  sqrt(a)   
 2     
 312 3.46413.4641 3.4641
 4110 10.488110.4881 10.4881
 553 7.280117.28011 7.28011
 667 8.185358.18535 8.18535
 7675 25.9808 25.9808 25.9808

top

Example 2: Bootstrapping a mean

Bootstrapping is a resampling technique to determine the accuracy or confidence interval of a parameter, being less sensitive to the actual distribution of the data than parametric solutions. In this example the arithmetic mean is bootstrapped an the mean and 95% confidence interval are given. Changing cell 'd4' to 'true' starts execution.
download bootstrap_mean.rlw

Notes:
1) For asymetric distributions the result can be asymetric. Actual results may vary, but this variation may be reduced by increasing the number of iterations.
2) The value in 'd4' is a semaphore. The script is only executed if this value is 'true'. The semaphore is reset to 'false' in cell 'f12'. If this cell is changed to '//d4 = false' or deleted, the script is executed every time when the spreadsheet is recalculated.

   AB CD EF GH
 1        //bootstrap a mean  
 2            
 3        dim res[1000];  
 4 12  exec:false  for(i=0; i<1000; i++) {  
 5 110   =if(d4)exec(f1:h15)  a = resample(d6);
 6 53  data:a3:a12   res[i] = mean(a);
 7 67       } 
 8 675  lower 95%   b = asort(res);  
 9 45  mean   d9 = b[499];  
 10 64  upper 95%   d10 = b[975];  
 11 12      d8 = b[24];   
 12 54      d4 = false;   
 13        return i+" iterations";  


after execution the spreadsheet displays as:

   AB CD EF GH
 1        //bootstrap a mean  
 2            
 3        dim res[1000];  
 4 12  exec:false  for(i=0; i<1000; i++) {  
 5 110   false   a = resample(d6);
 6 53  data:a3:a12   res[i] = mean(a);
 7 67       } 
 8 675  lower 95%37.3333  b = asort(res);  
 9 45  mean117.778  d9 = b[499];  
 10 64  upper 95%266  d10 = b[975];  
 11 12      d8 = b[24];   
 12 54      d4 = false;   
 13        return i+" iterations";  

Note:
The values in 'd8', 'd9', and 'd10' vary slightly upon each execution of the script because the random number generator is free running.

top

Example 3: Bootstrapping a correlation coefficient

This example gives a more elaborate design than in example 2. Execution is started by changing the value of the semaphore in 'd3' from 'false' to 'true'. It is reset to 'false' by the script in 'f29'.
The script first checks the data for valid pairs and creates a primary index. This primary index contains the values 0 .. n-1. The script is now iterating 1000times, calculating 1000 correlations, using a resample of the primary index to create a resample of the valid data pairs. The 95% confidence interval for the correleation coefficient is obviously given by the 1000 sorted coefficients between the 25th and 975th value.
The results of the scripts are returned in cells 'd6:d9'. Actual values may vary between calculations because the random number generator is free running. The correlation between column 'A' and column 'B' is significant on the 95% level because zero is not in between 'lo 95%' and 'hi 95%'.
download bootstrap_corr.rlw

   AB CDE FGH I
 1        //bootstrap a correlation coefficient  
 2             
 3 08.98 exec:false  md = -9999;   
 4 128.14  =if(d3)exec(f1:h30) xd = mkarr(a2:a50; md);  
 5 29.56.67     yd = mkarr(b2:b50; md);  
 6 436.08 n =9  n = count(xd);   
 7 535.9 r =-0.98809  dim x[n]; dim y[n]; dim pidx[n];  
 8 62.55.83 lo 95% =-0.99777  //find valid tuples   
 9 75.54.68 hi 95% =-0.95412  for(i = j = 0; i < n; i++) {  
 10 854.2      if(xd[i] != md && yd[i] != md) {
 11 933.72       x[j] = xd[i]; y[j] = yd[i];
 12          pidx[j] = j; j++;
 13          } 
 14         }  
 15       n = j; //number of valid pairs 
 16        pidx = subarr(pidx; 0; n-1); 
 17        dim wx[n]; dim wy[n]; dim r[1000]; 
 18        for(i= 0; i < 1000; i++) { 
 19         //resample the index 
 20         idx = resample(pidx); 
 21         for(j = 0; j < n; j++) { 
 22          wx[j] = x[idx[j]];
 23          wy[j] = y[idx[j]];
 24          } 
 25         r[i] = correl(wx; wy); 
 26        }   
 27       rs = asort(r); d6 = n; 
 28       d7 = rs[499]; d8 = rs[24]; d9 = rs[975];
 29       d3 = false;    
 30       return n;    
top