[英]How does the curve function in R work? - Example of curve function
以下代碼如何工作? 我在閱讀R ?curve
的幫助行時得到了示例。 但是我還不明白這一點。
for(ll in c("", "x", "y", "xy"))
curve(log(1+x), 1, 100, log = ll,
sub = paste("log= '", ll, "'", sep = ""))
特別是,我習慣於將數值作為for-loop
內的參數for-loop
例如,
for(ll in 1:10)
但是以下命令是什么意思:
for(ll in c("","x","y","xy"))
c("","x","y","xy")
看起來像字符串向量嗎? c("","x","y","xy")
在curve
內部作為log(1+x)
[在這里log(1+x)
是什么? 字符串“ x”? 在c("","x","y","xy")
]中, log=ll
?
顯然,關於R中的curve
函數如何工作,尤其是關於log
參數的堆棧溢出問題,沒有任何答案,因此這可能是一個深入研究它的好機會(我喜歡問題btw):
首先是簡單的部分:
c("","x","y","xy")
是字符串矢量,或更正式地是字符矢量。
for(ll in c("","x","y","xy"))
將開始一個4次迭代的循環,每次ll
為'','x','y','xy'
。 不幸的是,構建此示例的方式僅會看到繪制的最后一個ll = 'xy'
。
讓我們深入了解curve
函數的源代碼以回答其余的問題:
x
在log(1+x)
什么? log(1+x)
是一個函數。 x
表示在下面的curve
函數內部創建的數字向量(來自源代碼):
x <- exp(seq.int(log(from), log(to), length.out = n)) #if the log argument is 'x' or
x <- seq.int(from, to, length.out = n) #if the log argument is not 'x'
#in our case from and to are 1 and 100 respectively
只要n
參數是默認參數, x
向量將包含101個元素。 顯然, x
在log(1+x)
是在完全不同的“x”的log
參數。
與y
一樣,它始終創建為(根據源代碼):
y <- eval(expr, envir = ll, enclos = parent.frame()) #where expr is in this case log(1+x), the others are not important to analyse now.
#i.e. you get a y value for each x value on the x vector which was calculated just previously
log
參數的目的是什么? log
參數決定要記錄的是x
軸還是y
軸。 如果'x'
是對log
變量,則x軸;如果'y'
是對log
變量,則'xy'
軸;如果'xy'
是對log
變量,則兩個軸;如果log
參是''
,則無對數刻度。
這里需要提及的是,在curve
函數中的plot
函數中正在計算x軸或y軸的對數,也就是說, curve
函數只是plot
函數的包裝。
上面已經說過了,這就是為什么如果log
參數為'x'(請參見上文)的原因,那么將計算向量x的對數值的指數,以便它們將返回到plot
函數內部的對數。
PS curve
功能的源代碼可以通過在控制台上鍵入graphics::curve
看到。
我希望這現在有點道理!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.