我有一个与数据集一起使用的代码。我发现它不想使用LN(x)功能。可以找到数据集在这里。
LY <- ln(Apple$Close - Apple$Open)
log(x)中的警告:nans产生
Could you please help me to fix this problem?
I've got a code that works with the Data Set. I found out that it doesn't want to work with the ln(x) function. The data set can be found here.
LY <- ln(Apple$Close - Apple$Open)
Warning in log(x) : NaNs produced
Could you please help me to fix this problem?
发布评论
评论(2)
由于股票可能上涨也可能下跌(不幸的是),
收盘价
可能小于开盘价
,并且收盘价 - 开盘价
可能为负值。取负数的自然对数是没有意义的;这就像除以零,或者更准确地说,就像取负数的平方根。实际上,您可以取具有负实部的复数的对数:
...但是“i乘以pi”对于进一步的数据可能不是一个非常有用的结果分析...
(在 R 中,
log()
取自然对数。虽然SciViews
包提供ln()
作为同义词,但您可以不妨习惯使用log()
- 这是大多数编程语言的约定...)根据您想要执行的操作,关闭/打开比率的对数可以是有用的值 (
log(Close/Open)
):当Close
Close
,时,此值为负。开盘
收盘>时为正打开
)。正如 @jpiversen 指出的,这称为对数回报;正如 @KarelZe 指出的,log(Close/Open)
在数学上相当于log(Close) - log(Open)
(这可能是你的教授想要的...... ???)Since stocks can go down as well as up (unfortunately),
Close
can be less thanOpen
andClose - Open
can be negative. It just doesn't make sense to take the natural log of a negative number; it's like dividing by zero, or more precisely like taking the square root of a negative number.Actually, you can take the logarithm of a complex number with a negative real part:
... but "i times pi" is probably not a very useful result for further data analysis ...
(in R,
log()
takes the natural logarithm. While theSciViews
package providesln()
as a synonym, you might as well just get used to usinglog()
- this is a convention across most programming languages ...)Depending on what you're trying to do, the logarithm of the close/open ratio can be a useful value (
log(Close/Open)
): this is negative whenClose < Open
, positive whenClose > Open
). As @jpiversen points out, this is called the logarithmic return; as @KarelZe points out,log(Close/Open)
is mathematically equivalent tolog(Close) - log(Open)
(which might be what your professor wanted ... ???)您在寻找对数回报吗?在这种情况下,公式为:
由于两个正值的
A / B
始终为正,因此不会创建NaN
。Are you looking for logarithmic return? In that case the formula would be:
Since
A / B
for two positive values is always positive, this will not createNaN
s.