简体   繁体   English

这个R代码是什么意思?

[英]What does this R code mean?

f <- function(x,q){ ## one step of the Newton iteration
  x-(pnorm(x)-q)/dnorm(x)
  }

x <- 0; #starting value
xj <- x # I don't know what is happening from this point onward! 
for (i in 1:10){
  x <- f(x,0.99);
  xj <- c(xj,x)
  }
print(xj)

Basically, here I am trying to compute the 0.99 quantile of the Normal Distribution using Newtons Algorithm, and apparently this is the solution. 基本上,这里我尝试使用牛顿算法计算正态分布的0.99分位数,显然这是解决方案。 However, I don't follow what is happening from the step I have pointed out above. 但是,我不遵循上面指出的步骤所发生的情况。 Could someone explain this to me in simple terms? 有人可以简单地向我解释一下吗? What is happening in the for loop exactly? for循环中到底发生了什么? Mainly, what is going on in the xj <- c(xj,x) step? 主要是,xj <-c(xj,x)步骤中发生了什么? I'm completely new to programming and I would really appreciate the help! 我是编程的新手,我将非常感谢您的帮助!

Thanks! 谢谢!

You are running the function you defined as f 10 times, each time updating the value plugged into it (x) with the result of the previous run. 您正在运行定义为f的函数10次,每次使用上一次运行的结果更新插入到(x)的值(x) The first time the function runs, the value of x is 0 , so the equation, 0-(pnorm(0)-0.99)/dnorm(0) gets computed as 1.228248 . 该函数第一次运行时, x值为0 ,因此方程0-(pnorm(0)-0.99)/dnorm(0)的计算结果为1.228248 This result then gets appended to the numeric vector, xj . 然后,该结果将附加到数值向量xj The function f then runs a second time with this first result plugged in as x -- 1.228248-(pnorm(1.228248)-0.99)/dnorm(1.228248)=1.759464 . 然后,函数f第二次运行,并将第一个结果插入为x - 1.228248-(pnorm(1.228248)-0.99)/dnorm(1.228248)=1.759464 This result then gets appended to the end of vector xj . 然后将该结果附加到向量xj的末尾。

This continues for 10 iterations when the loop ends. 循环结束时,将继续进行10次迭代。 Finally, xj is printed to the console: 最后,将xj打印到控制台:

[1] 0.000000 1.228248 1.759464 2.104157 2.280355 2.324003 2.326341 2.326348
 [9] 2.326348 2.326348 2.326348  

As you can see, the algorithm converges after the 7th iteration, so increasing the number of times the loop runs will cause the program to continually append the vector xj with the same final value, 2.326348 . 如您所见,该算法在第7次迭代后收敛,因此增加循环运行的次数将使程序连续将向量xj附加相同的最终值2.326348 If you want to observe each iteration individually, you can simply remove the for statement from the sixth line and the curly brackets surrounding it and run the last four lines repeatedly. 如果要单独观察每个迭代,则只需从第六行及其周围的大括号中除去for语句,然后重复运行最后四行。

This just seems like bad programming. 这似乎是不好的编程。 Basically the first step of the for loop takes the result of f(x,0.99) with x=0 and store it as the new value of x. 基本上,for循环的第一步采用x=0f(x,0.99)结果并将其存储为x的新值。 This new value is then stored as a new element of xj with the first element being 0. As the for loop goes through the iterations, a new x gets calculated from f(x,0.99) and is stored as new element of xj. 然后,将这个新值存储为xj的新元素,第一个元素为0。随着for循环进行迭代,将从f(x,0.99)计算出一个新x并将其存储为xj的新元素。

I say that this is bad programming practice, because when you do something like xj <- c(xj,x) , the vector "xj" runs out of room for a new element "x", so R creates a new vector with length(xj)+1 elements and copies the whole xj into the new vector. 我说这是不好的编程习惯,因为当您执行xj <- c(xj,x) ,向量“ xj”的空间不足以容纳新元素“ x”,因此R创建了一个新的具有长度的向量(xj)+1个元素,并将整个 xj复制到新向量中。 R does this every time it goes through xj <- c(xj,x) . R每次经过xj <- c(xj,x) While this is fine if the algorithm converges in small number of steps (and is generally the case with Newton's method), but it becomes slow for other algorithms where it takes a large number of steps to converge. 虽然如果算法以较小的步长收敛(这通常是牛顿方法的情况),这很好,但是对于需要大量步长收敛的其他算法,它变得很慢。

An better, but not perfect alternative to xj <- c(xj,x) would be to first declare xj as a vector of length n, say xj = rep(NA, n) (where n is the approximate number of steps the algorithm converges or larger), then only return the xj elements that are not NAs. 对于xj <- c(xj,x)来说xj <- c(xj,x)更好但不是完美的替代方法是首先将xj声明为长度为n的向量,例如xj = rep(NA, n) (其中n是算法的近似步数)收敛或更大),则仅返回不是NA的xj元素。 This way, even if you pick a large n, it will still be much faster than xj <- c(xj,x) . 这样,即使您选择了一个大的n,它仍然比xj <- c(xj,x)快得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM