[R-sig-finance] How can I do this better? (Filling in last tradedprice for NA)

Rich at mango-solutions.com Rich at mango-solutions.com
Mon Sep 13 14:12:34 CEST 2004


Here's a function that does what you want . haven't look at it for a while
so it may need work.  Should be pretty fast, since it just uses
position-based referencing (ie. no cumsums etc).

retain <- function(vec) {
 isAv <- !is.na(vec)
 if (all(isAv) | all(!isAv)) return(vec)
 posAvToDiff <- posAv <- (1:length(vec))[isAv]
 if (max(posAvToDiff) != length(vec))  posAvToDiff <- c(posAvToDiff,
length(vec)+1)
 repLengths <- diff(posAvToDiff)
 if (max(posAvToDiff) == length(vec)) repLengths <- c(repLengths, 1)
 myOut <- rep(vec[posAv], repLengths)
 if (posAv[1] != 1)  myOut <- c(rep(NA, posAv[1]-1), myOut)
 myOut
}
myVec <- sample(c(1:5, NA), 50, T)
myVec
retain(myVec)

Hope this helps,
Rich.

R and S Consulting and Training for the Finance Industry
mangosolutions
Tel   +44 118 902 6617
Fax  +44 118 902 6401

-----Original Message-----
From: r-sig-finance-bounces at stat.math.ethz.ch
[mailto:r-sig-finance-bounces at stat.math.ethz.ch] On Behalf Of Gabor
Grothendieck
Sent: 13 September 2004 12:29
To: ajayshah at mayin.org; r-sig-finance at stat.math.ethz.ch
Subject: RE: [R-sig-finance] How can I do this better? (Filling in last
tradedprice for NA)



This is referred to as LOCF, last observation carried forward.
First create some test data -- we are using ts here but using
its would be analogous:

   v1 <- c(1, NA, NA, 2, 3, NA, 5)
   v2 <- rev(v1)
   vv <- ts.union(ts(v1), ts(v2, start = 2))

Now define LOCF as a function and apply it:

   LOCF <- function(v) {
      L <- !is.na(v)
      v[c(NA,which(L))[cumsum(L)+1]]
   }
   apply(vv, 2, LOCF) 



Date:   	Sun, 12 Sep 2004 21:03:36 +0530
From:   	Ajay Shah <ajayshah at mayin.org>
To:   	r-sig-finance <r-sig-finance at stat.math.ethz.ch>
Subject:   	[R-sig-finance] How can I do this better? (Filling in last
traded price for NA)

I have 3 different daily time-series. Using union() in the "its"
package, I can make a long matrix, where rows are created when even
one of the three time-series is observed:

massive <- union(nifty.its, union(inrusd.its, infosys.its))

Now in this, I want to replace NA values for prices by the
most-recently observed price. I can do this painfully --

for (i in 2:nrow(massive)) {
for (j in 1:3) {
if (is.na(massive[i,j])) {
massive[i,j] = massive[i-1,j]
}
}
}

But this is horribly slow. Is there a more clever way?

-- 
Ajay Shah Consultant
ajayshah at mayin.org Department of Economic Affairs
http://www.mayin.org/ajayshah Ministry of Finance, New Delhi

_______________________________________________
R-sig-finance at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance


	[[alternative HTML version deleted]]



More information about the R-sig-finance mailing list