[tech-spec] Re: Problems with DateTime Classes-

  • From: Dirk Eddelbuettel <edd@xxxxxxxxxx>
  • To: tech-spec@xxxxxxxxxxxxx
  • Date: Mon, 4 Apr 2005 22:15:52 -0500

(Sorry for the delay, but I was out of the country and of email for a bit.)

On 30 March 2005 at 12:45, BBands wrote:
| Hi Vince,
| 
| The following works for me:
| 
| library(its)
| x <- priceIts(instrument="INTC", start="2005-01-01", quote="Close")
| names(x) <- "Close"
| x <- data.frame(core(x))
| str(x)
| 
| `data.frame':   59 obs. of  1 variable:
|  $ Close: num  23.1 22.6 22.4 22.5 22.8 ...
| 
| 'core()' is one of those 'hidden' functions I got from Dirk.

Yes, core() is useful when you need to get the "inner" matrix type from its
object, but here it seems wrong -- if you want data from Yahoo! but not as an
its object, why not use the get.hist.quote() function from the tseries
package that returns a data frame?

As for Vince's questions:

[...]
| library(its)
| library(fSeries)
| #
| intc.2005<- priceIts(instrument=c("INTC"), start="2005-01-01", quote = 
| "Close")
| str(intc.2005)
| #
| # which produces-
| #
| str(intc.2005)
| Formal class 'its' [package "its"] with 118 slots
|  num [1:59, 1:2] 23.1 22.6 22.4 22.5 22.8 ...
|   ..- attr(*, "dimnames")=List of 2
|   .. ..$ : NULL
|   .. ..$ : chr [1:2] ".Data" "dates"
| 
| # At this point, the "dates" appear to be in "character" format and not 
| in POSIX to the best of my knowledge.  So it needs to be manipulated 
| further.  However, when i try to coerce it to as.POSIXlt etc, I get an 
| error message...
| 
|  > intc.2005<- as.POSIXlt(date)
| Error in as.POSIXlt(date) : Don't know how to convert `date' to class 
| "POSIXlt"

But was is date here? You need to extract the dates from the its -- just like
core() gets the data as a matrix, dates() gets the date vector:

> class(dates(intc.2005))
[1] "POSIXt"  "POSIXct"

which can then be modified, used, ... just like other DateTimeClass objects:

> format(dates(intc.2005), "%d %b %Y")[1:4]
[1] "03 Jan 2005" "04 Jan 2005" "05 Jan 2005" "06 Jan 2005"

| # Additionally, if I try another fSeries Function (getReturns) which  
| manipulates the raw time series (intc.2005) into a returns vector...
| 
|  > ret.intc.2005<-getReturns(intc.2005)
| Error in its(x, dates = attributes(x)$dates) :
|         dates should be in POSIX format

But nobody said you could feed its objects to the RMetrics functions.
What you wanted was probably 

> ret.intc.2005<-getReturns(core(intc.2005))

but then again -- the its class can do that too:

> ret.intc.2005 <- 100*diff(log(intc.2005))
> print(summary(union(intc.2005, ret.intc.2005)))
         INTC Close   INTC Close
Min.     21.9900000 -2.650511938
1st Qu.  22.6725000 -0.977785568
Median   23.3000000 -0.088534756
Mean     23.3735484 -0.004269128
3rd Qu.  24.0125000  0.854706058
Max.     25.1100000  2.811177357
NA's      0.0000000  1.000000000
non-NA's 62.0000000 61.000000000
s.d.      0.7971467  1.351357435
> 

and allows you to keep it as its object.

Hope this helps, Dirk

-- 
Better to have an approximate answer to the right question than a precise 
answer to the wrong question.  --  John Tukey as quoted by John Chambers

Other related posts: