[R-SIG-Finance] Fast way of replacing missing data points in xts object

Brian G. Peterson brian at braverock.com
Thu Nov 19 15:19:45 CET 2009


I looks like with the Near and Far series you're doing linear 
interpolation, and with the VIX series you're doing  locf  interpolation.

Even if my glance at your code is incorrect, studying the way in which 
the na interpolation functions are implemented in zoo is likely to be 
your best way of improving your own code.

wob wu wrote:
> I am sorry if this wasn't clear enough. The interpolation isn't the problem. The logic for it is stated below. I am looking for a faster/more elegant way to execute the loop and if statements given the logic in the loop.
>
> --- Brian G. Peterson <brian at braverock.com> schrieb am Do, 19.11.2009:
>
>   
>> Von: Brian G. Peterson <brian at braverock.com>
>> Betreff: Re: [R-SIG-Finance] Fast way of replacing missing data points in xts object
>> An: "wob wu" <wobwu22 at yahoo.de>
>> Datum: Donnerstag, 19. November 2009, 15:00
>> wob wu wrote:
>>     
>>> Hello,
>>> I keep on having the same problem over and over again
>>>       
>> and couldn't find a satisfying solution yet. I have some
>> missing datapoints in my financial time series and am
>> replacing them currently in the following way:
>>     
>>> for (i in 2:length(SP$Far)) {
>>>      if (is.na(SP$Far[i]))
>>>       
>> {SP$Far[i] <- as.numeric(SP$Far[i-1]) +
>> (as.numeric(SP$Near[i])-as.numeric(SP$Near[i-1]))}
>>     
>>>      if (is.na(SP$Near[i]))
>>>       
>> {SP$Near[i] <- as.numeric(SP$Near[i-1]) +
>> (as.numeric(SP$Far[i])-as.numeric(SP$Far[i-1]))}
>>     
>>>      if (is.na(SP$Vix[i]))
>>>       
>> {SP$Vix[i] <- as.numeric(SP$Vix[i-1])}    }
>>     
>>> SP is a xts object.
>>>
>>> This works fine but is slow. Does anyone knows a
>>>       
>> better solution?
>>     
>>>    
>>>       
>> See the list archives and the large variety of na
>> interpolation actions in zoo.  They will work fine on
>> your xts object.
>>
>>    - Brian
>>
>> -- Brian G. Peterson
>> http://braverock.com/brian/
>> Ph: 773-459-4973
>> IM: bgpbraverock
>>
>>
>>
>>     
>
>
>
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>   


-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list