Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 对分位数回归的系数应用正/负约束_R_Constraints_Quantile_Quantreg - Fatal编程技术网

R 对分位数回归的系数应用正/负约束

R 对分位数回归的系数应用正/负约束,r,constraints,quantile,quantreg,R,Constraints,Quantile,Quantreg,在包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

在包
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
  }
}
quant添加

j=2
(1:23中的k){
如果(II[k]){
if(k0){
等式$coeff[j]=0}
j=j+1}
}
}
打印(eq$coeff)
就在做出预测之前,解决了这个问题

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)