【问题标题】:How to save the results of a nested loop in R如何在 R 中保存嵌套循环的结果
【发布时间】:2017-04-16 20:11:48
【问题描述】:

我有一个包含 15 个变量(1 个正在检查,14 个回归量)的数据集,所有变量都是数字的。我所做的是运行一种递归预测技术的算法。该算法在样本内和样本外切割数据。 这里我想弄清楚如何存储at的每个值产生的结果,它们是cv.hqreg函数(hqreg package)的参数。

  • 注意: 对于t and a 的每个值,我们得到 1 个值(代码中的值是predicedQ。对于每个t and a,我们运行cv.hqreg 648 次。然后对于t and a 的下一个值,再次重复 648 次。因此,最终结果将是一个 648 行和 231 列的矩阵/数据集。

对于每个 cv.hqreg,我得到 100 个拟合模型,我通过 LMQ$fit$beta[,which(LMQ$lambda.min==LMQ$lambda)] 命令行从中选择误差最小的模型。

   dataR<-TRAINSET
    fittedvaluesQRidge<-NULL
        for(i in 1:(nrow(TESTSET)-1)){ #adding a new row and repeat
          for(a in seq(0,1,0.1)){ #for each penalty of selection
            for(t in seq(0,1,0.05)){ #for each quantile
      print(i)                         #to see it works/or where stops
      dataR<-rbind(dataR,TESTSET[i,])  #update dataset
      LMQ<-cv.hqreg(as.matrix(dataR[,-15]),dataR$LHS,method = "quantile",tau=t,alpha = a)  #FIT THE Lasso Quantile-MODEL 
      predictdQR<-LMQ$fit$beta[1,which(LMQ$lambda.min==LMQ$lambda)]+LMQ$fit$beta[2,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,1]+LMQ$fit$beta[3,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,2]+LMQ$fit$beta[4,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,3]+LMQ$fit$beta[5,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,4]+LMQ$fit$beta[6,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,5]+LMQ$fit$beta[7,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,6]+LMQ$fit$beta[8,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,7]+LMQ$fit$beta[9,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,8]+LMQ$fit$beta[10,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,9]+LMQ$fit$beta[11,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,10]+LMQ$fit$beta[12,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,11]+LMQ$fit$beta[13,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,12]+LMQ$fit$beta[14,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,13]+LMQ$fit$beta[15,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,14] #find the forecasts
      fittedvaluesQRidge<-c(fittedvaluesQRidge,predictdQR) #then put them in a vector 
        }
      }
    }

我用来获取预测值的命令非常广泛,一次使用每个变量。但是我尝试使用矩阵代数(covariates %*% data 的矩阵,没有结果,但出现错误:二进制运算符的非数字参数。它以一种丑陋的方式工作,但如果有更短的方式,我想要所有帮助。

【问题讨论】:

  • 那么你的问题是什么?您的标题是保存嵌套循环的结果,但您正在保存每个最后一个向量的数据,fittedvaluesQRidge。代码不起作用吗?不希望的结果?
  • 是的,我在向量中获取数据。但它就像'11 * 21 * 658'长。虽然我想让它作为一个矩阵出来。例如:对于 t=0.1 和 a=0,我得到了 468 个结果。然后对于 t=0.2 和 a=0 我得到他们的下一个 648 等等。我真的可以想象如何将结果设置为矩阵。
  • 我以为你对last question很熟悉!我看到您不使用任何应用功能。 Sapply 可以将 predictedQR 向量绑定到一个矩阵中。 at 循环变量在哪里使用?
  • 我真的试过了,但我失败了。那么't'和'a'在'cv.hqreg'函数中使用。
  • 你需要矩阵还是数组?您显示的尺寸超过两个:11 X 21 X 658。

标签: r for-loop storage


【解决方案1】:

考虑sapply()expand.grid(),因为sapply 可以接受多个输入列表或向量,类似于嵌套的for 循环,但返回一个矩阵。使用expand.grid 交叉连接data.frame 对象中的两个列表,您可以捕获at 之间的每个组合:

at_combns <- expand.grid(a=seq(0,1,0.05), t=seq(0,1,0.1))

matpredictdQR <- sapply(seq(nrow(at_combns)), function(j, i){
  # UPDATE dataset
  dataR <- rbind(TRAINSET, TESTSET[1:i,])

  # FIT THE Lasso Quantile-MODEL 
  LMQ <- cv.hqreg(as.matrix(dataR[,-15]),dataR$LHS,method = "quantile",
                  tau=at_combns$t[j], alpha=at_combns$a[j]) 

  predictdQR <-LMQ$fit$beta[1,which(LMQ$lambda.min==LMQ$lambda)]+
               LMQ$fit$beta[2,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,1]+
               LMQ$fit$beta[3,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,2]+
               LMQ$fit$beta[4,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,3]+
               LMQ$fit$beta[5,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,4]+
               LMQ$fit$beta[6,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,5]+
               LMQ$fit$beta[7,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,6]+
               LMQ$fit$beta[8,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,7]+
               LMQ$fit$beta[9,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,8]+
               LMQ$fit$beta[10,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,9]+
               LMQ$fit$beta[11,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,10]+
               LMQ$fit$beta[12,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,11]+
               LMQ$fit$beta[13,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,12]+
               LMQ$fit$beta[14,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,13]+
               LMQ$fit$beta[15,which(LMQ$lambda.min==LMQ$lambda)]*TESTSET[i+1,14] 

  return(predictdQR)

}, seq(nrow(TESTSET)-1))

【讨论】:

  • 我不认为这会更新每个运行矩阵。比如运行 1:361 行,然后运行 ​​2 362 行,运行 3 363 行等等。我使用了print(dim(dataR)),我知道dataR 现在是一个矩阵1x15。使用rbind(dataR,TESTSET[i,] 我认为会起作用。另外我认为你需要在整个代码的末尾添加一个) 来关闭 sapply 函数。
  • 另外,每次代码运行cv.hqreg函数R时都会崩溃。我知道当cv.hqreg 运行打印您所在的交叉验证的函数时,它确实会导致崩溃。
  • 在某些电脑更改代码运行后确定。唯一的问题是它不更新dataR。 rbind(dataR,TESTSET[1:i,] and rbind(dataR,TESTSET[i,]) 都不会更新它。第一个在每次运行中给出dim = 1008x15,后者给出dim = 362x15
  • 函数从不返回 dataR,而是在sapply 中临时更新它以计算我认为这里主要关注的 predictdQR。您没有检索到预测值矩阵的预期结果吗?在单独的 for 循环中更新 dataR。现在 dataR 已将 TRAINSET 设置为 rbind 调用。见编辑。
  • 可悲的是rbind(dataR,TESTSET[1:i,] 每次运行都给出 dataR 是 362 行。考虑到我们rbind,它似乎没有更新矩阵,这很奇怪。 rbind(dataR,TESTSET[i,] 的结果相同,但这里我们有 1008 行。我不明白为什么。
猜你喜欢
  • 2021-06-15
  • 1970-01-01
  • 2021-02-13
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多