[R] Imputing missing values in time series

Leeds, Mark (IED) Mark.Leeds at morganstanley.com
Fri Jun 22 21:16:09 CEST 2007


I have a function that does this type of thing but it works off a pure
vector so it wouldn have to be modified.
If you make your object a zoo object, the that object has many functions
associated with it and na.locf would
Do what you need, I think.


-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Erik Iverson
Sent: Friday, June 22, 2007 3:02 PM
To: Horace Tso
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Imputing missing values in time series

I think my example should work for you, but I couldn't think of a way to
do this without an interative while loop.

test <- c(1,2,3,NA,4,NA,NA,5,NA,6,7,NA)

while(any(is.na(test)))
test[is.na(test)] <- test[which(is.na(test))-1]

  test
  [1] 1 2 3 3 4 4 4 5 5 6 7 7

Horace Tso wrote:
> Folks,
> 
> This must be a rather common problem with real life time series data 
> but I don't see anything in the archive about how to deal with it. I 
> have a time series of natural gas prices by flow date. Since gas is 
> not traded on weekends and holidays, I have a lot of missing values,
> 
> FDate	Price
> 11/1/2006	6.28
> 11/2/2006	6.58
> 11/3/2006	6.586
> 11/4/2006	6.716
> 11/5/2006	NA
> 11/6/2006	NA
> 11/7/2006	6.262
> 11/8/2006	6.27
> 11/9/2006	6.696
> 11/10/2006	6.729
> 11/11/2006	6.487
> 11/12/2006	NA
> 11/13/2006	NA
> 11/14/2006	6.725
> 11/15/2006	6.844
> 11/16/2006	6.907
>  
> What I would like to do is to fill the NAs with the price from the 
> previous date * gas used during holidays is purchased from the week 
> before. Though real simple, I wonder if there is a function to perform

> this task. Some of the imputation functions I'm aware of (eg. impute, 
> transcan in Hmisc) seem to deal with completely different problems.
> 
> 2.5.0/Windows XP
> 
> Thanks in advance.
> 
> HT
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.

______________________________________________
R-help at stat.math.ethz.ch 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.
--------------------------------------------------------

This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}



More information about the R-help mailing list