2015-12-03 308 views
-1

我是R新手,我希望是一個簡單的問題。我正在運行幾個嵌套循環,似乎無法獲得我期待的輸出。嵌套「for」循環在R

我想跟蹤內部循環的平衡。每次迭代代表1年。在15年的結束時,我想爲InvTotal爲4次模擬中的每一次寫最後的餘額。

我的最終產出應該是3個向量(每個投資1個),長度爲4,顯示在4次模擬結束時每個投資的最終累計值。

我的代碼如下。你可以提供任何援助將是一個巨大的幫助。

謝謝!

Investment1_Balance <- 10000 
Investment2_Balance <- 10000 
Investment3_Balance <- 10000 

Inv1Returns <- c(0, 1000, -500, 500) 
Inv2Returns <- c(0, -9000, 30000, 10000) 
Inv3Returns <- c(0, 4000, -1000, -2000) 

Inv1Outcome = NULL 
Inv2Outcome = NULL 
Inv3Outcome = NULL 

Inv1Total = NULL 
Inv2Total = NULL 
Inv3Total = NULL 

random = NULL 

for (j in 1:4) 


{ 

for (i in 1:15) 


{ 
    random[i] = runif(1, 0, 1) 

    Inv1Outcome[i] = if (random[i] <= .25){Investment1_Balance + Inv1Returns[1]} 
    else if (random[i] > .25 & random[i] <= .50){Investment1_Balance + Inv1Returns[2]} 
    else if (random[i] > .50 & random[i] <= .75){Investment1_Balance + Inv1Returns[3]} 
    else {Investment1_Balance + Inv1Returns[4]} 

    Inv2Outcome[i] = if (random[i] <= .20){Investment2_Balance + Inv2Returns[1]} 
    else if (random[i] > .20 & random[i] <= .30){Investment2_Balance + Inv2Returns[2]} 
    else if (random[i] > .30 & random[i] <= .70){Investment2_Balance + Inv2Returns[3]} 
    else ({Investment2_Balance + Inv2Returns[4]}) 

    Inv3Outcome[i] = if (random[i] <= .50){Investment3_Balance + Inv3Returns[1]} 
    else if (random[i] > .50 & random[i] <= .70){Investment3_Balance + Inv3Returns[2]} 
    else if (random[i] > .70 & random[i] <= .90){Investment3_Balance + Inv3Returns[3]} 
    else ({Investment3_Balance + Inv3Returns[4]}) 

    Investment1_Balance[i] =+ Inv1Outcome[i] 
    Investment2_Balance[i] =+ Inv2Outcome[i] 
    Investment3_Balance[i] =+ Inv3Outcome[i] 

} 

Inv1Total[j] = Investment1_Balance[15] 

Inv2Total[j] = Investment2_Balance[15] 

Inv3Total[j] = Investment3_Balance[15] 

} 

Inv1Total 
Inv2Total 
Inv3Total 

回答

1

歡迎使用stackoverflow!有幾個點同時試圖弄清楚爲什麼按照預期的代碼不工作,你應該考慮:

  1. 「從小事做起」:你的情況在這裏,我將開始與一個投資,甚至可能只有一次模擬。這將驗證計算的邏輯是否正確,而不會分散注意力與其他數字。

  2. 「眼見爲實」:嘗試打印中間結果,以便跟蹤計算錯誤。

應用以前,第一調試將看起來是這樣的:

Investment1_Balance <- 10000 
Inv1Returns <- c(0, 1000, -500, 500) 
Inv1Outcome = NULL 
Inv1Total = NULL 

random = NULL 

for (j in 1:4) { # Loop over simulations 

    cat("\n\n\nSimulation ", j, ": Investment balance:" , Investment1_Balance) 

    for (i in 1:15) { # Loop over years 

    random[i] = runif(1, 0, 1) 

    Inv1Outcome[i] = if (random[i] <= .25){Investment1_Balance + Inv1Returns[1]} 
    else if (random[i] > .25 & random[i] <= .50){Investment1_Balance + Inv1Returns[2]} 
    else if (random[i] > .50 & random[i] <= .75){Investment1_Balance +  Inv1Returns[3]} 
    else {Investment1_Balance + Inv1Returns[4]} 

    Investment1_Balance[i] =+ Inv1Outcome[i]  

    cat("\n Year: ", i, "- Outcome:", Inv1Outcome[i], "- Final balance: " , Investment1_Balance[i]) 

    } 

    Inv1Total[j] = Investment1_Balance[15] 
} 

Inv1Total 

你可以看到一對夫婦的印有號碼的問題:

  1. 的結局總是一樣的平衡。這表明加法可能存在問題。

讓我們嘗試調試:

> x = 10 
> x =+ 2 
> x 
[1] 2 

顯然,=+並不如預期的工作。所以我們必須糾正:Investment1_Balance[i+1] = Investment1_Balance[i] + Inv1Outcome[i]

  1. 當爲每個模擬打印初始投資餘額時,我們可以看到第一個模擬是好的(Simulation 1 : Investment balance: 10000)。但對於其他模擬我們得到(Simulation 2 : Investment balance: 10000 9500 10000 10000 10500 11000 9500 11000 10000 10500 10500 10000 10000 11000 11000)

這表明初始化存在問題。每個模擬都會獲取前一個的輸出,並以此開始。這個邏輯不完全正確。簡單的解決方案將在每次模擬開始時重置Investment1_Balance <- 10000。在這種情況下,重置所有的平衡和結果變量甚至是有意義的。

這裏是調試後的新代碼:

Inv1Returns <- c(0, 1000, -500, 500) 

random = NULL 
Inv1Total = NULL 


for (j in 1:4) { # Loop over simulations 

    Investment1_Balance <- 10000 
    Inv1Outcome = NULL 

    cat("\n\n\nSimulation ", j, ": Investment balance:" , Investment1_Balance) 

    for (i in 1:15) { # Loop over years 

    random[i] = runif(1, 0, 1) 

Inv1Outcome[i] = if (random[i] <= .25){Investment1_Balance + Inv1Returns[1]} 
    else if (random[i] > .25 & random[i] <= .50){Investment1_Balance + Inv1Returns[2]} 
    else if (random[i] > .50 & random[i] <= .75){Investment1_Balance + Inv1Returns[3]} 
    else {Investment1_Balance + Inv1Returns[4]} 

    Investment1_Balance[i+1] = Investment1_Balance[i] + Inv1Outcome[i] 

    cat("\n Year: ", i, "- Outcome:", Inv1Outcome[i], "- Final balance: " , Investment1_Balance[i]) 

    } 

    Inv1Total[j] = Investment1_Balance[15] 
} 

Inv1Total 

檢查輸出現在是正確的。如果是這樣,你可以繼續添加Inv2和Inv3。

+0

這是一個巨大的幫助。謝謝,Deena! – rev201