user6832972 - 11 months ago 50

R Question

I'm having odd results with quantmod

`monthlyReturn`

`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])`

`as.numeric(adj[1])`

`92.81556`

`89.62508`

`(92.81556 - 89.62508)/89.62508`

`0.03559807`

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

Answer Source

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
```