[R-sig-finance] How to set up an ITS object, just the basics?
Gabor Grothendieck
ggrothendieck at myway.com
Thu Sep 30 21:18:33 CEST 2004
1. First define a test data frame. 2. Then convert it to its
and perform a locf. 3. Do the same thing using the upcoming
version of zoo (not yet on CRAN). 4. If you want to eliminate
dependence on external packages note that LOCF can be defined
in a one line function so finally we define our own mylocf and
run it directly on the data frame.
R> # 1. define test data
R> tt <- seq(Sys.time(), len = 5, by = "month")
R> x1 <- c(1,NA,NA,4:5)
R> x2 <- c(1:4,NA)
R> dfNAV <- data.frame(tt, x1, x2)
R> # 2. using its
R> require(its)
[1] TRUE
R> itsNAV <- as.its(data.matrix(dfNAV))
R> itsNAV <- locf(itsNAV)
R> itsNAV
x1 x2
2004-09-30 1 1
2004-10-30 1 2
2004-11-30 1 3
2004-12-30 4 4
2005-01-30 5 4
R> # 3. uses the upcoming version of zoo (not yet on CRAN)
R> require(zoo)
[1] TRUE
R> dfNAV.zoo <- zoo(dfNAV[,2:3], dfNAV[,1])
R> dfNAV.zoo <- LOCF(dfNAV.zoo)
R> dfNAV.zoo
x1 x2
2004-09-30 15:13:42 1 1
2004-10-30 15:13:42 1 2
2004-11-30 15:13:42 1 3
2004-12-30 15:13:42 4 4
2005-01-30 15:13:42 5 4
R> # 4. define own locf function and apply to data frame
R> mylocf<- function(x) x[c(NA,which(!is.na(x)))[cumsum(!is.na(x))+1]]
R> dfNAV[,2:3] <- apply(dfNAV[,2:3], 2, mylocf)
R> dfNAV
tt x1 x2
1 2004-09-30 15:13:42 1 1
2 2004-10-30 15:13:42 1 2
3 2004-11-30 15:13:42 1 3
4 2004-12-30 15:13:42 4 4
5 2005-01-30 15:13:42 5 4
Date: Thu, 30 Sep 2004 15:22:31 +0100
From: Taher Khan <t.khan at econ.bbk.ac.uk>
To: r-sig-finance <r-sig-finance at stat.math.ethz.ch>
Subject: [R-sig-finance] How to set up an ITS object, just the basics?
Greetings!
Apologize for bringing up a really simple question, unfortunately I
cannot make sense of the help file for ITS.
I have a dataframe dfNAV which consists of a POSIXct date value %d/%m/%Y
and two values, some of which are NA's.
I want to set up an ITS object so that I can use locf().
Can someone please explain the syntax for setting it up? For example:
dfNAV.its <- its(as.matrix(dfNAV), format = '%d-%m-%Y')
Is it not working because I already have a date value, should I convert
it to a string before trying to make it an ITS?
Best regards and thanks in advance,
Taher.
More information about the R-sig-finance
mailing list