Search code examples
rxtsquantmod

Odd results with R and quantmod monthlyReturn function


I'm having odd results with quantmod monthlyReturn function. Here is an example:

require(quantmod)

getSymbols("VOO")

adj <- Ad(VOO["2010-09"])

monthlyReturn(adj)

(as.numeric(tail(adj)[6]) - as.numeric(adj[1])) / as.numeric(adj[1])

The last two commands gives the same answer 0.03559799

However commands as.numeric(tail(adj)[6]) and as.numeric(adj[1]) give me values 92.81556 and 89.62508 respectively and command (92.81556 - 89.62508)/89.62508 gives a value 0.03559807 which is correct but different from above examples.

Can somebody please explain to me what is wrong and why is there a difference?


Solution

  • You're losing precision when you print the numbers with so few digits.

    options(digits=20)
    as.numeric(tail(adj)[6]) 
    # 92.815557999999995786
    as.numeric(adj[1]) 
    # 89.625084999999998558
    
    (as.numeric(tail(adj)[6]) - as.numeric(adj[1])) / as.numeric(adj[1]) 
    #0.035597991343606506798
    (92.815557999999995786 - 89.625084999999998558)/89.625084999999998558 
    #0.035597991343606506798