2017-03-31 82 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