2017-03-31 66 views
0

我正在學習R並編寫了我的第一個for循環。對於1000次迭代,我創建了4個矢量,每個矢量的尺寸分別爲10,100,1000和10000個元素。這些所謂的theta1值不被識別。我試着在當時運行一個theta1,它工作但運行所有4給出錯誤'找不到對象'。我不明白這裏有什麼問題,任何人都可以解釋嗎?For循環R返回錯誤'找不到對象'

k=1000 

'Method1' 

for(i in 1:k){ 
    N10=runif(10) 
    N100=runif(100) 
    N1000=runif(1000) 
    N10000=runif(10000) 
    theta1_10[i]=(1/10)*4*sum(sqrt(1-N10^2)) 
    theta1_100[i]=(1/100)*4*sum(sqrt(1-N100^2)) 
    theta1_1000[i]=(1/1000)*4*sum(sqrt(1-N1000^2)) 
    theta1_10000[i]=(1/10000)*4*sum(sqrt(1-N10000^2)) 
} 

'Result Method 1' 
m_theta1_10 = mean(theta1_10) 
sd_theta1_10 = sd(theta1_10) 
m_theta1_100 = mean(theta1_100) 
sd_theta1_100 = sd(theta1_100) 
m_theta1_1000 = mean(theta1_1000) 
sd_theta1_1000 = sd(theta1_1000) 
m_theta1_10000 = mean(theta1_10000) 
sd_theta1_10000 = sd(theta1_10000) 
+0

什麼是''法1'?你在'vba'中沒有編碼。如果你想用評論,請使用'#'!這不是你的問題,但。 – Masoud

+2

您需要在for循環之前將'theta1_10'初始化爲'theta1_100000'。例如'theta1_10 = vector(,k)' – TooYoung

+1

[如何創建一個空的R向量來添加新項目](http://stackoverflow.com/questions/3413879/how-to-create-an-empty -r-vector-to-add-new-items) – vincentmajor

回答

1

您需要在給它賦值之前初始化一個向量。 對於這種情況,它是:

# Initialize 
k=1000 
theta1_10 = vector(,k) 
theta1_100 = vector(,k) 
theta1_1000 = vector(,k) 
theta1_10000 = vector(,k) 

# Method1 
for(i in 1:k){ 
    N10=runif(10) 
    N100=runif(100) 
    N1000=runif(1000) 
    N10000=runif(10000) 
    theta1_10[k] = (1/10)*4*sum(sqrt(1-N10^2)) 
    theta1_100[k] = (1/100)*4*sum(sqrt(1-N100^2)) 
    theta1_1000[k] = (1/1000)*4*sum(sqrt(1-N1000^2)) 
    theta1_10000[k] = (1/10000)*4*sum(sqrt(1-N10000^2)) 
} 

# Result Method 1 
result = data.frame(mean = c(mean(theta1_10),mean(theta1_100),mean(theta1_1000),mean(theta1_10000)), 
        sd = c(sd(theta1_10),sd(theta1_100),sd(theta1_1000),sd(theta1_10000))) 
rownames(result) <- c("theta1_10","theta1_100","theta1_1000","theta1_10000") 
result 

       mean   sd 
theta1_10 3.145259 0.287263626 
theta1_100 3.142640 0.089207786 
theta1_1000 3.140476 0.027901399 
theta1_10000 3.141695 0.009046627 
+0

謝謝,分配一個向量來完成交易。爲了更好地理解,theta1_10作爲theta1_10 = c(theta1_10,(1/10)* 4 * sum(sqrt(1-N10^2)))中的參數附加到向量theta1_10? – Orongo

+0

是的,'c(theta1_10,....)'表示將以下內容附加到'theta1_10'。正如Uwe Block指出的,當k很大時,這不是一個好主意。 – TooYoung

1

我在寫這個答案,因爲這兩個問題和接受的答案中的R證明不好的編程風格:他們是在不斷增長的一個向量循環。 (請參閱圈2Patrick Burns' The R Inferno。)

從簡單的基準測試中可以看出效果。的任務是創建矢量x其中將包含整數1至k

k <- 10000L 
microbenchmark::microbenchmark(
    grow = { 
    x <- integer(0) 
    for (i in seq.int(k)) x <- c(x, i) 
    x 
    }, 
    subscript = { 
    x <- integer(k) 
    for (i in seq.int(k)) x[i] <- i 
    x 
    }, 
    colon_operator = { 
    x <- 1L:k 
    x 
    }, 
    times = 10L 
) 
#Unit: microseconds 
#   expr  min  lq  mean median  uq  max neval 
#   grow 93491.676 96127.568 104219.0140 97123.627 99459.343 165545.063 10 
#  subscript 9067.607 9215.996 9483.0962 9551.288 9771.795 9938.307 10 
# colon_operator  5.664  7.552  7.9675  8.307  8.685  9.063 10 

這是明顯的是,即使對於長度10000的小矢量附加元件是一個幅度比預先分配所需的長度更慢。這裏包含了冒號操作符的時間,以演示內置矢量化函數的好處。

因此,有問題的兩個代碼和answer需要重新編寫使用下標以提高效率。

# initialize the random number generator for reproducible results 
set.seed(1234L) 
# allocate memory for the vectors beforehand 
theta1_10 = numeric(k) 
theta1_100 = numeric(k) 
theta1_1000 = numeric(k) 
theta1_10000 = numeric(k) 

# Method1 
for(i in seq.int(k)){ 
    N10=runif(10) 
    N100=runif(100) 
    N1000=runif(1000) 
    N10000=runif(10000) 
    # update by subscripting 
    theta1_10[i] = (1/10)*4*sum(sqrt(1-N10^2)) 
    theta1_100[i] = (1/100)*4*sum(sqrt(1-N100^2)) 
    theta1_1000[i] = (1/1000)*4*sum(sqrt(1-N1000^2)) 
    theta1_10000[i] = (1/10000)*4*sum(sqrt(1-N10000^2)) 
} 

然而,整個代碼可以重新寫成一個更簡潔的方式:

library(data.table) 
set.seed(1234) 
k <- 1000L 
N <- 10^(1:4) 
rbindlist(
    lapply(N, function(i) { 
    theta1 <- replicate(k, 4/i * sum(sqrt(1 - runif(i)^2))) 
    data.table(N = i, mean = mean(theta1), sd = sd(theta1)) 
    })) 
#  N  mean   sd 
#1: 10 3.144974 0.27238683 
#2: 100 3.140716 0.09040696 
#3: 1000 3.141791 0.02654225 
#4: 10000 3.141585 0.00886737