【问题标题】:Applying a positive/negative constraint to the coefficients of a quantile regression对分位数回归的系数应用正/负约束
【发布时间】:2018-04-03 10:48:04
【问题描述】:

在包quantreg 中可以执行惩罚分位数回归。选择被认为具有统计学意义的变量是“容易的”。但是,当我考虑对系数施加限制时:即一些严格为正/负(否则它们将为零),我只是无法弄清楚它是如何完成的!我到目前为止的代码是这样的:

quant<-c(0.4,0.5,0.6)

for (t in 400:600){     #the first 400 rows are the trainset, the remaining the test set. In each iteration
  x=X[1:399,]           #we increase the trainset by 1row and use it to predict for the next.
  y=Y[1:399]
  for (i in 1:quant) {
    eq=rqss(y~x,method="lasso",tau=quant[i],lambda=lambdas) #find the significant variable though a Lasso quantile.
    s=summary(eq)
    findsigPV=s$coef[2:28,4] #select the stat. significant coefficient/variable
    selectedPV=findsigPV<=0.05
    if (sum(selectedPV)==0){
      SelectedPV=rank(findsigPV)==1
    }
    newx=as.matrix(subset(X[1:t,],select=which(selectedPV))) #new matrix with the selected variable
    eq=rq(y~newx[1:(t-1),],tau=quant[i])  #applies the new q. regression with the selected coeff from the lasso
    pr[t-400+1,i]=c(1,newx[t,])%*%eq$coef #saves the forecast
  }
}

我担心这个问题很明显。我曾考虑使用ifelse(eq$coef&lt;0,0,eq$coef),但考虑到一些变量被限制为正或负,这不是理想的解决方案。有什么想法吗?

编辑:我忘了包括,每次迭代选择一个(可能)与前一次迭代不同的变量!

【问题讨论】:

    标签: r constraints quantile quantreg


    【解决方案1】:

    添加

    j=2
        for (k in 1:23){
    
          if (II[k]){ 
            if (k <=12){  #positive constraint to the first 12 variables lets say
              if (eq$coeff[j] <0){
                eq$coeff[j] =0}
              j=j+1}
            if (k > 12){ #negative constraint to the remaining ones
              if (eq$coeff[j] >0){
                eq$coeff[j] =0}
              j=j+1}  
          }
        }
        print(eq$coeff)
    

    在做出预测之前,解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-19
      • 2018-06-19
      • 2013-09-05
      • 1970-01-01
      • 2020-07-25
      • 1970-01-01
      • 2020-03-02
      • 2016-05-29
      相关资源
      最近更新 更多