[R] Grid search: avoid "for" loops thanks to apply&co?

From: Matthieu Stigler <stigler3_at_etu.unige.ch>
Date: Mon, 28 Jan 2008 16:29:31 +0100


Hello

I'm trying to implement a grid search for a threshold autoregressive model, it is a model in which the regression coefficients are different according to the regimes (under the lower threshold, between lower and upper, over the upper threshold).

Estimation of the threshold is made with Conditional least squares: once the threshold is given, the usual parameters are computed with usual OLS, the thresholds values are those which minimise the Sum of squares. In order to find the threshold values one has to compute the sum of squares of all models, which takes n x n/2 operations for a model with two regimes (for my case: 300x150=45'000).

Transcription of the Matlab code into R is inefficient because of the loops (very slow), a schema of the actual code gives:

for(in in 1:length(thres1))

    firstThresh<-tresh1[i]

        for(j in 1:length(thres1))
                if(j>i)
              secondThresh<-tresh1[j]
                   regime1<-ifelse(x<thresh1)*x
                   regime2<-ifelse(x>thresh2)*x               
...(some matrix algebra in order to obtain the Sum of Squares of cbind(x, regime1, regime2)

I'm trying to implement it into R with apply&co instead, but don't succeed. Many packages use grid search but all with "for" loops and small number of values. To use apply&co, I saw these solutions:

-write the "matrix building and estimation" function estim(X,thresh1,
thresh2) and then with mapply
mapply(estim, tresh1, thresh2)

The problem is that I have to compute all combinations of thresh 1 and thresh 2 (with the condition thresh 2>thresh1) and not only the combinations arg1[i] with arg2[i]

-create a big array with all possible matrices cbind(y, regime1,
regime2) and then use apply with only an estimation function. But I would need to create an array of 300 x 300 matrices...

What do you think? Do you see other solutions? Is is possible to evaluate other combinations within the mapply function?

Thanks for your help!



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 28 Jan 2008 - 15:33:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 28 Jan 2008 - 16:30:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive