在R中多次运行循环,并将结果保存在数据框中

时间:2019-11-14 19:29:06

标签: r dataframe for-loop repeat

我在与R和论坛打交道方面还比较陌生,所以请原谅我一两个错误。我要执行的操作如下:我想为每个14个变量生成6000个观察值。

这是我到目前为止所做的。我已经用适当的长度初始化了每个变量的向量:

#Market1
price_1                           <- vector(mode = "numeric", length = 6002)
demandChartist_1                  <- vector(mode = "numeric", length = 6000)
demandFundamentalist_1            <- vector(mode = "numeric", length = 6001)
percentFT_Wf_1                    <- vector(mode = "numeric", length = 6001)
percentCT_Wc_1                    <- vector(mode = "numeric", length = 6001)
fitnessTradingFundamentalist_Af_1 <- vector(mode = "numeric", length = 6001)
fitnessTradingChartist_Ac_1       <- vector(mode = "numeric", length = 6001)

#Market2
price_2                           <- vector(mode = "numeric", length = 6002)
demandChartist_2                  <- vector(mode = "numeric", length = 6000)
demandFundamentalist_2            <- vector(mode = "numeric", length = 6001)
percentFT_Wf_2                    <- vector(mode = "numeric", length = 6001)
percentCT_Wc_2                    <- vector(mode = "numeric", length = 6001)
fitnessTradingFundamentalist_Af_2 <- vector(mode = "numeric", length = 6001)
fitnessTradingChartist_Ac_2       <- vector(mode = "numeric", length = 6001)

percentNoTrading                  <- vector(mode = "numeric", length = 6001)
T <- 1:6000

下一步是设置起始值。

# set the first 4 values for price equal to 0, 1 whatever otherwise we can't compute the previous periods
price_1[1:4]  <- 0 
price_2[1:4]  <- 0 
fitnessTradingChartist_Ac_1[1:3] <- 0
fitnessTradingFundamentalist_Af_1[1:3] <- 0
fitnessTradingChartist_Ac_2[1:3] <- 0
fitnessTradingFundamentalist_Af_2[1:3] <- 0
a       <- 1
b       <- 0.05
c       <- 0.05
d       <- 0.975
e       <- 300
F1      <- 0
F2      <- 0

我设法建立了For循环,以便所有向量都填充有随机生成的值。 (请参见代码)由于这些是随机生成的值,因此,如果我可以多次执行循环并为每个变量创建一个数据框,则该模型的准确性将大大提高,其中第一次运行的6000个观测值存储在第1列中第二次运行的6000个观测值存储在第2列等中,我最终可以计算出每个周期的平均值。

for (i in 4:6002) {
  # [i-2] weil fitness tradingchartist der jetzigen Periode Demand der Periode t-2 also 2 erfordert.
  demandChartist_1[i-2]  =  
    b * (price_1[i-2] - price_1[i-3]) + rnorm(1, mean=0, sd=0.05)
  # [i-2] weil fitness tradingFundamentalist der jetzigen Periode Demand der Periode t-2 also 2 erfordert.

  demandChartist_2[i-2]  =  
    b * (price_2[i-2] - price_2[i-3]) + rnorm(1, mean=0, sd=0.05)

  demandFundamentalist_1[i-2] = 
    c * (F1 - price_1[i-2]) + rnorm(1, mean=0, sd=0.01)

  demandFundamentalist_2[i-2] = 
    c * (F2 - price_2[i-2]) + rnorm(1, mean=0, sd=0.01)

  fitnessTradingChartist_Ac_1[i] = 
    (exp(price_1[i]) - exp(price_1[i-1])) * demandChartist_1[i-2] + 
    d * fitnessTradingChartist_Ac_1[i-1]

  fitnessTradingChartist_Ac_2[i] = 
    (exp(price_2[i]) - exp(price_2[i-1])) * demandChartist_2[i-2] + 
    d * fitnessTradingChartist_Ac_2[i-1]

  fitnessTradingFundamentalist_Af_1[i] = 
    (exp(price_1[i]) - exp(price_1[i-1])) * demandFundamentalist_1[i-2] + 
    d * fitnessTradingChartist_Ac_1[i-1]  

  fitnessTradingFundamentalist_Af_2[i] = 
    (exp(price_2[i]) - exp(price_2[i-1])) * demandFundamentalist_2[i-2] + 
    d * fitnessTradingChartist_Ac_2[i-1]  

  percentCT_Wc_1[i] = 
    exp(e * fitnessTradingChartist_Ac_1[i]) / 
    (exp(e * fitnessTradingChartist_Ac_1[i]) + 
       exp(e * fitnessTradingFundamentalist_Af_1[i]) + 
       exp(e * fitnessTradingChartist_Ac_2[i]) + 
       exp(e * fitnessTradingFundamentalist_Af_2[i]) +
       exp(0)
    )

  percentCT_Wc_2[i] = 
    exp(e * fitnessTradingChartist_Ac_2[i]) / 
    (exp(e * fitnessTradingChartist_Ac_1[i]) + 
       exp(e * fitnessTradingFundamentalist_Af_1[i]) +
       exp(e * fitnessTradingChartist_Ac_2[i]) + 
       exp(e * fitnessTradingFundamentalist_Af_2[i]) + 
       exp(0)
    )

  percentFT_Wf_1[i] = 
    exp(e * fitnessTradingFundamentalist_Af_1[i]) / 
    (exp(e * fitnessTradingChartist_Ac_1[i]) + 
       exp(e * fitnessTradingFundamentalist_Af_1[i]) +
       exp(e * fitnessTradingChartist_Ac_2[i]) + 
       exp(e * fitnessTradingFundamentalist_Af_2[i]) + 
       exp(0)
    )

  percentFT_Wf_2[i] = 
    exp(e * fitnessTradingFundamentalist_Af_2[i]) / 
    (exp(e * fitnessTradingChartist_Ac_1[i]) + 
       exp(e * fitnessTradingFundamentalist_Af_1[i]) +
       exp(e * fitnessTradingChartist_Ac_2[i]) + 
       exp(e * fitnessTradingFundamentalist_Af_2[i]) + 
       exp(0)
    )

  percentNoTrading[i] = 
    1- percentCT_Wc_1[i] - percentFT_Wf_1[i] - percentCT_Wc_2[i] - percentFT_Wf_2[i]

  price_1[i] = 
    price_1[i-1] + 
    a * ((percentCT_Wc_1[i-1] * demandChartist_1[i-1] + 
        percentFT_Wf_1[i-1] * demandFundamentalist_1[i-1]
          )
        ) + 
    rnorm(1, mean=0, sd=0.01)

  price_2[i] = 
    price_2[i-1] + 
    a * ((percentCT_Wc_2[i-1] * demandChartist_2[i-1] + 
        percentFT_Wf_2[i-1] * demandFundamentalist_2[i-1]
          )
        ) + 
    rnorm(1, mean=0, sd=0.01)
}

有人知道如何做到这一点吗?我将不胜感激! 干杯

1 个答案:

答案 0 :(得分:0)

您的代码并非绝对不容易跟踪。

在这里,我要尝试改变的两点是要获得具有50列和6000个观测值的14个数据框。

# definition of dataframe
#Market1
price_1                           <- matrix(ncol = 50, nrow =6002, 0L)
demandChartist_1                  <- matrix(ncol = 50, nrow =6002, 0L)
demandFundamentalist_1            <- matrix(ncol = 50, nrow =6002, 0L)
percentFT_Wf_1                    <- matrix(ncol = 50, nrow =6002, 0L)
percentCT_Wc_1                    <- matrix(ncol = 50, nrow =6002, 0L)
fitnessTradingFundamentalist_Af_1 <- matrix(ncol = 50, nrow =6002, 0L)
fitnessTradingChartist_Ac_1       <- matrix(ncol = 50, nrow =6002, 0L)

#Market2
price_2                           <- matrix(ncol = 50, nrow =6002, 0L)
demandChartist_2                  <- matrix(ncol = 50, nrow =6002, 0L)
demandFundamentalist_2            <- matrix(ncol = 50, nrow =6002, 0L)
percentFT_Wf_2                    <- matrix(ncol = 50, nrow =6002, 0L)
percentCT_Wc_2                    <- matrix(ncol = 50, nrow =6002, 0L)
fitnessTradingFundamentalist_Af_2 <- matrix(ncol = 50, nrow =6002, 0L)
fitnessTradingChartist_Ac_2       <- matrix(ncol = 50, nrow =6002, 0L)

percentNoTrading                  <- matrix(ncol = 50, nrow =6002, 0L)

然后,您定义第一个值:

#set the first 4 values for price equal to 0, 1 whatever otherwise we cant compute the previous periods
price_1[1:4,]  <- 0 
price_2[1:4,]  <- 0 
fitnessTradingChartist_Ac_1[1:3,] <- 0
fitnessTradingFundamentalist_Af_1[1:3,] <- 0
fitnessTradingChartist_Ac_2[1:3,] <- 0
fitnessTradingFundamentalist_Af_2[1:3,] <- 0
a       <- 1
b       <- 0.05
c       <- 0.05
d       <- 0.975
e       <- 300
F1      <- 0
F2      <- 0

最后,您的大部分代码包括第二个列循环

for(j in 1:50)
{
  for (i in 4:6002) {
    # [i-2] weil fitness tradingchartist der jetzigen Periode Demand der Periode t-2 also 2 erfordert.
    demandChartist_1[i-2,j]  =  
      b * (price_1[i-2,j] - price_1[i-3,j]) + rnorm(1, mean=0, sd=0.05)
    # [i-2] weil fitness tradingFundamentalist der jetzigen Periode Demand der Periode t-2 also 2 erfordert.

    demandChartist_2[i-2,j]  =      b * (price_2[i-2,j] - price_2[i-3,j]) + rnorm(1, mean=0, sd=0.05)
    print("1 OK")
    demandFundamentalist_1[i-2,j] =     c * (F1 - price_1[i-2,j]) + rnorm(1, mean=0, sd=0.01)

    demandFundamentalist_2[i-2,j] =     c * (F2 - price_2[i-2,j]) + rnorm(1, mean=0, sd=0.01)

    fitnessTradingChartist_Ac_1[i,j] =     (exp(price_1[i,j]) - exp(price_1[i-1,j])) * demandChartist_1[i-2,j] + 
      d * fitnessTradingChartist_Ac_1[i-1,j]

    fitnessTradingChartist_Ac_2[i,j] =     (exp(price_2[i,j]) - exp(price_2[i-1,j])) * demandChartist_2[i-2,j] + 
      d * fitnessTradingChartist_Ac_2[i-1,j]

    fitnessTradingFundamentalist_Af_1[i,j] =     (exp(price_1[i,j]) - exp(price_1[i-1,j])) * demandFundamentalist_1[i-2,j] + 
      d * fitnessTradingChartist_Ac_1[i-1,j]  

    fitnessTradingFundamentalist_Af_2[i,j] =     (exp(price_2[i,j]) - exp(price_2[i-1,j])) * demandFundamentalist_2[i-2,j] + 
      d * fitnessTradingChartist_Ac_2[i-1,j]  

    percentCT_Wc_1[i,j] =     exp(e * fitnessTradingChartist_Ac_1[i,j]) / 
      (exp(e * fitnessTradingChartist_Ac_1[i,j]) + 
         exp(e * fitnessTradingFundamentalist_Af_1[i,j]) + 
         exp(e * fitnessTradingChartist_Ac_2[i,j]) + 
         exp(e * fitnessTradingFundamentalist_Af_2[i,j]) +
         exp(0)
      )

    percentCT_Wc_2[i,j] = 
      exp(e * fitnessTradingChartist_Ac_2[i,j]) / 
      (exp(e * fitnessTradingChartist_Ac_1[i,j]) + 
         exp(e * fitnessTradingFundamentalist_Af_1[i,j]) +
         exp(e * fitnessTradingChartist_Ac_2[i,j]) + 
         exp(e * fitnessTradingFundamentalist_Af_2[i,j]) + 
         exp(0)
      )

    percentFT_Wf_1[i,j] = 
      exp(e * fitnessTradingFundamentalist_Af_1[i,j]) / 
      (exp(e * fitnessTradingChartist_Ac_1[i,j]) + 
         exp(e * fitnessTradingFundamentalist_Af_1[i,j]) +
         exp(e * fitnessTradingChartist_Ac_2[i,j]) + 
         exp(e * fitnessTradingFundamentalist_Af_2[i,j]) + 
         exp(0)
      )

    percentFT_Wf_2[i,j] = 
      exp(e * fitnessTradingFundamentalist_Af_2[i,j]) / 
      (exp(e * fitnessTradingChartist_Ac_1[i,j]) + 
         exp(e * fitnessTradingFundamentalist_Af_1[i,j]) +
         exp(e * fitnessTradingChartist_Ac_2[i,j]) + 
         exp(e * fitnessTradingFundamentalist_Af_2[i,j]) + 
         exp(0)
      )

    percentNoTrading[i,j] = 
      1- percentCT_Wc_1[i,j] - percentFT_Wf_1[i,j] - percentCT_Wc_2[i,j] - percentFT_Wf_2[i,j]

    price_1[i,j] = 
      price_1[i-1,j] + 
      a * ((percentCT_Wc_1[i-1,j] * demandChartist_1[i-1,j] + 
              percentFT_Wf_1[i-1,j] * demandFundamentalist_1[i-1,j]
      )
      ) + 
      rnorm(1, mean=0, sd=0.01)

    price_2[i,j] = 
      price_2[i-1,j] + 
      a * ((percentCT_Wc_2[i-1,j] * demandChartist_2[i-1,j] + 
              percentFT_Wf_2[i-1,j] * demandFundamentalist_2[i-1,j]
      )
      ) + 
      rnorm(1, mean=0, sd=0.01)
  }
}

希望这是您想要的。也许有更简单的方法,但是代码的结构首先需要一些优化。