[R] Different result on using apply.
Dennis Murphy
djmuser at gmail.com
Fri Jul 29 10:29:48 CEST 2011
Hi:
To add to Peter Dalgaard's comments, a look at head(ret) and
head(returns) would also have clued you in:
> head(ret, 1)
GOOG.Open GOOG.High GOOG.Low GOOG.Close GOOG.Volume
2007-01-03 1.377067e-05 3.184271e-05 3.378641e-05 7.049545e-05 3.01405e-09
GOOG.Adjusted
2007-01-03 7.049545e-05
> head(returns, 1)
GOOG.Open GOOG.High GOOG.Low GOOG.Close GOOG.Volume GOOG.Adjusted
2007-01-03 NA NA NA NA NA NA
Dennis
On Thu, Jul 28, 2011 at 11:09 PM, Ashim Kapoor <ashimkapoor at gmail.com> wrote:
> Dear R-helpers,
>
> In the following example I compute ret and returns the SAME way. In ret I
> use compute returns for EACH column and in returns I do it for the whole
> data frame. Could someone please tell me why I see a lagged result,by which
> I mean ret and returns are different by one lag.
>
>
> getSymbols("GOOG",src="yahoo")
> ret<-apply(GOOG,2,function(x) diff(log(x)) / lag(x,1) )
> returns<-diff(log(GOOG))/lag(GOOG,1)
> tail(ret)
> tail(returns)
>
>> tail(ret)
> GOOG.Open GOOG.High GOOG.Low GOOG.Close
> 2011-07-21 3.188905e-05 3.065345e-05 2.882942e-05 3.022824e-05
> 2011-07-22 2.160452e-05 1.532645e-05 2.373743e-05 1.961091e-06
> 2011-07-25 1.241901e-05 5.334479e-06 1.119182e-05 9.213213e-06
> 2011-07-26 -2.279176e-06 -1.672208e-05 -3.306823e-05 -3.997397e-05
> 2011-07-27 -3.178693e-05 -1.294157e-05 -4.791985e-06 1.005828e-05
> 2011-07-28 1.060350e-05 2.464065e-05 2.583624e-05 5.395451e-05
> GOOG.Volume GOOG.Adjusted
> 2011-07-21 4.835664e-09 3.022824e-05
> 2011-07-22 -3.379734e-08 1.961091e-06
> 2011-07-25 -9.265378e-08 9.213213e-06
> 2011-07-26 2.212510e-07 -3.997397e-05
> 2011-07-27 -5.989484e-08 1.005828e-05
> 2011-07-28 7.472583e-09 5.395451e-05
>> tail(returns)
> GOOG.Open GOOG.High GOOG.Low GOOG.Close
> 2011-07-21 -2.262875e-05 1.432963e-05 -3.784855e-06 3.252347e-05
> 2011-07-22 3.188905e-05 3.065345e-05 2.882942e-05 3.022824e-05
> 2011-07-25 2.160452e-05 1.532645e-05 2.373743e-05 1.961091e-06
> 2011-07-26 1.241901e-05 5.334479e-06 1.119182e-05 9.213213e-06
> 2011-07-27 -2.279176e-06 -1.672208e-05 -3.306823e-05 -3.997397e-05
> 2011-07-28 -3.178693e-05 -1.294157e-05 -4.791985e-06 1.005828e-05
> GOOG.Volume GOOG.Adjusted
> 2011-07-21 1.988491e-07 3.252347e-05
> 2011-07-22 4.835664e-09 3.022824e-05
> 2011-07-25 -3.379734e-08 1.961091e-06
> 2011-07-26 -9.265378e-08 9.213213e-06
> 2011-07-27 2.212510e-07 -3.997397e-05
> 2011-07-28 -5.989484e-08 1.005828e-05
>>
>
> Many thanks for your help.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list