Search code examples
rgrid-searchquantilequantile-regression

Grid Search in R for Nonparametric Quantile Regression


I use a library called "quantreg" in R and try to estimate full nonparametric quantile regression on time series basis. To get statistically significant results I try lots of variables and smoothing parameter values (lambda). But it's exhausting and very time consuming. Therefore, I want to apply grid search, however it is a little bit hard for me. I want to determine best smoothing values, so I should construct a for loop. But I want that loop to try every combination. At the I want to have the lambda values of best model or models (all variables' p values<0.05 condition). For example if I have three variables in my equation I've written something like that:

lambdas1<-rbind(1,2,3)
lambdas2<-rbind(1,2,3)
lambdas3<-rbind(1,2,3)
    
mylist<-list()
    for (i in 1:3) {
      for (j in 1:3) {
        for (n in 1:3) {
          f <-try(rqss(Y~qss(X1,lambda = lambdas1[i])+qss(X2,lambda = lambdas2[j])+qss(X3,lambda = lambdas3[n]), tau=0.05)) 
          sf<-summary(f)
          if( (sf[["qsstab"]]['X1','Pr(>F)']<0.05)&(sf[["qsstab"]]['X2','Pr(>F)']<0.05)&(sf[["qsstab"]]['X3','Pr(>F)']<0.05) ){
            mylist[[i]]<-f$lambdas
          }
        }
      }
    }

How can I rearrange this code? Is there any shortcut?

Any help will be appreciated. Thank you in advance.


Solution

  • You can use baseR expand.grid to create a data.frame of all the possible combinations and then use apply(grid, MARGIN=2, ...) to loop through its rows, also I "optimized" the code you were looking if each p-value I changed it to use all(p.vals < .05)

    lambdas <- expand.grid(1:3,1:3,1:3) 
    check_lambdas <- function(lambdas){
        f <-try(rqss(Y~qss(X1,lambda = lambdas[1])+qss(X2,lambda = lambdas[2])+qss(X3,lambda = lambdas[3]), tau=0.05)) 
        if( all(summary(f)$qsstab[,'Pr(>F)']<0.05) ) f$lambdas else NULL
    }
    apply(lambdas, 2, check_lambdas)