I need to run linear regressions in R on the time series of a universe of stocks, which can be accomplished very simply using lm(), and while I have a solution that uses cbind to construct the data frame of prices from Zorro asset-by-asset, this is painfully slow, and I can't get the way I would think it should be done to work.

I've tried two ways of doing this, each with its own problems.


1) I set up vars Prices[NUM_ASSETS], but while this works easily for a single asset i using Rset("y",Prices[i],DAYS), I'm not sure of what Rset syntax to use to feed the entire array into an R object. Everything I've tried yields nonsense, that's to say, the R object gives values like 9.22632259801047e-238 when I check its contents.


2) I set up var Prices[NUM_ASSETS][DAYS] and feed the price series into this array using a for loop, with the intention of using Rset("y",Prices,DAYS,NUM_ASSETS). NUM_ASSETS = 505, and while this works for a very small number for DAYS, in all other scenarios the script invariably crashes with the for loop for larger numbers, either terminating with Error 111: Crash in function: run() at bar 0 or just crashing Zorro.

The script

Quote:
#include <default.c>
#include <r.h>

#define NUM_ASSETS 505
#define DAYS 90

int asset_num;


function run()
{
StartDate = 20180101;
BarPeriod = 60*24;
LookBack = DAYS;
Commission = Spread = Slippage = 0;

int i;
vars Price;
//vars Prices[NUM_ASSETS]; //for 1)
var Prices[NUM_ASSETS][DAYS]; // for 2)


assetList("History\AssetsSP500.csv");

asset_num = 0;

while(loop(Assets))
{
asset(Loop1);

if(priceClose(0)!= 0 && priceClose(DAYS) != 0 && asset_num < NUM_ASSETS)
{

//Prices[asset_num] = series(priceClose(),DAYS); //for 1)
Price = series(priceClose(),DAYS); // for 2)

for(i=0;i<DAYS;i++) //for 2)
Prices[asset_num][i]= Price[i];
}
asset_num++;
}

// if(!(is(LOOKBACK))) // R code
// {
// Rstart("",2);
// Rset("y",Prices,DAYS,NUM_ASSETS);
// Rx("y",3);
// }
}