[R] Confused about behavior of an S4 object containing a ts object

Lyman, Mark Mark.Lyman at atk.com
Thu Jan 22 17:37:33 CET 2009


I posted the question below about a month ago but received no response.
I still have not been able to figure out what is happening.

I also noticed another oddity. When the data part of the object is a
multivariate time series, it doesn't show up in the structure, but it
can be treated as a multivariate time series. Is this a bug in str?

> setClass("tsExtended", representation = representation(description 
+ = "character"), contains = "ts")
[1] "tsExtended"
> tmp <- new("tsExtended", matrix(1:20, ncol=2), description = "My Time
Series")
> tsp(tmp) <- c(1, 5.5, 2)
> tmp
Object of class "tsExtended"
Time Series:
Start = c(1, 1) 
End = c(5, 2) 
Frequency = 2 
    Series 1 Series 2
1.0        1       11
1.5        2       12
2.0        3       13
2.5        4       14
3.0        5       15
3.5        6       16
4.0        7       17
4.5        8       18
5.0        9       19
5.5       10       20
Slot "description":
[1] "My Time Series"

> str(tmp)
Formal class 'tsExtended' [package ".GlobalEnv"] with 4 slots
  ..@ .Data      : int [1:20] 1 2 3 4 5 6 7 8 9 10 ...
  ..@ description: chr "My Time Series"
  ..@ tsp        : num [1:3] 1 5.5 2
  ..@ .S3Class   : chr "ts"
> tmp[,1]
Time Series:
Start = c(1, 1) 
End = c(5, 2) 
Frequency = 2 
 [1]  1  2  3  4  5  6  7  8  9 10
> plot(tmp[,2])

Mark Lyman


-----Original Message-----
From: Lyman, Mark 
Sent: Thursday, December 18, 2008 1:02 PM
To: 'r-help at r-project.org'
Subject: Confused about behavior of an S4 object containing a ts object

I am trying to define an S4 class that contains a ts class object, a
simple 
example is shown in the code below. However, when I try to create a new
object 
of this class the tsp part is ignored, see below. Am I doing something
wrong, 
or is this just a peril of mixing S3 and S4 objects?

> setClass("tsExtended", representation = representation(description 
= "character"), contains = "ts")
[1] "tsExtended"
> new("tsExtended", ts(1:10, frequency = 2), description = "My Time
Series")
Object of class "tsExtended"
Time Series:
Start = 1
End = 10
Frequency = 1
 [1]  1  2  3  4  5  6  7  8  9 10
Slot "description":
[1] "My Time Series"

> # This however seems to work
> tmp <- new("tsExtended", 1:10, description = "My Time Series")
> tsp(tmp) <- c(1, 5.5, 2)
> tmp
Object of class "tsExtended"
Time Series:
Start = c(1, 1)
End = c(5, 2)
Frequency = 2
 [1]  1  2  3  4  5  6  7  8  9 10
Slot "description":
[1] "My Time Series"

Mark Lyman, Statistician
Engineering Systems & Integration, ATK




More information about the R-help mailing list