Using runmean from caTools the first one below does
it in under 1 second but will not handle NAs. The
second one takes under 15 seconds and handles
them by replacing them with linear approximations.
Note that k must be odd.
# 1
library(caTools)
set.seed(1)
system.time({
y -
Thank you very much. It rocks. And actually I
discovered that what really slow down the program is
return$vol.cap[[i]]=mean(VOL[(i-12):(i-1)],na.rm=TRUE)/return$cap[[i]]
If I took out this and my original code takes about 10
minutes and halt at the place where all NA shows up.
It seems R is
Sorry to bother you guys again. This is great. But
this is for 61 number and the second case will change
60 to 61. run* only accept odd number window. How
to get around it with 60? Any suggestion? Thanks.
--- Gabor Grothendieck [EMAIL PROTECTED]
wrote:
Using runmean from caTools the first
Try
runmean2 - function(x, k) # k must be even
(coredata(runmean(x, k-1)) * (k-1) +
coredata(lag(x, -k/2, na.pad = TRUE)))/k
Also, in your code use matrices or vectors instead of data frames
to avoid any overhead in using data frames.
On 5/2/06,
It does not work though. How is the lag work? How
does the lag work? I read the help and do not quite
understand. Here is a test
y
[1] 1 2 3 4 5 6 7 8 9 10
coredata(lag(y,-1))
[1] 1 2 3 4 5 6 7 8 9 10
attr(,tsp)
[1] 2 11 1
--- Gabor Grothendieck [EMAIL PROTECTED]
I was assuming that this would be added to my example
where the data is a zoo object so lag.zoo is being used.
Try this:
library(zoo)
z - zoo(11:15)
z
1 2 3 4 5
11 12 13 14 15
lag(z,-1,na.pad=TRUE)
1 2 3 4 5
NA 11 12 13 14
?lag.zoo
On 5/2/06, Guojun Zhu [EMAIL PROTECTED] wrote:
For my perticular purpose (run regression on trailing
60 number b on 60 a), I have writen a short function
for it because no available function (runmean,
rollmean) can take care of NA as I wanted. I
basically want the regression ignore the NA row. I
have read a little bit source code of runmean
I basically has a long data.frame a. but I only need
three columns x,y. Let us say the index of row is t.
I need to produce new column s_t as the linear
regression coefficient of (x_(t-60),...x_(t-1)) on
(y_(t-60),...,y_(t-1)). The data is about 140,000
rows. I wrote a simple code on this which