## two examples extending S3 class "lm", class "xlm" directly
## and "ylm" indirectly
setClass("xlm", representation(eps = "numeric"), contains = "lm")
setClass("ylm", representation(header = "character"), contains = "xlm")
## lm.D9 is as computed in the example for stats::lm
y1 <- new("ylm", lm.D9, header = "test", eps = .1)
xx <- new("xlm", lm.D9, eps =.1)
y2 <- new("ylm", xx, header = "test")
stopifnot(inherits(y2, "lm"))
stopifnot(identical(y1, y2))
stopifnot(identical(S3Part(y1, strict = TRUE), lm.D9))
## note the these classes can insert an S3 subclass of "lm" as the S3 part:
myData <- data.frame(time = 1:10, y = (1:10)^.5)
myLm <- lm(cbind(y, y^3) ~ time, myData) # S3 class: c("mlm", "lm")
ym1 <- new("ylm", myLm, header = "Example", eps = 0.)
##similar classes to "xlm" and "ylm", but extending S3 class c("mlm", "lm")
setClass("xmm", representation(eps = "numeric"), contains = "mlm")
setClass("ymm", representation(header="character"), contains = "xmm")
ym2 <- new("ymm", myLm, header = "Example2", eps = .001)
# but for class "ymm", an S3 part of class "lm" is an error:
try(new("ymm", lm.D9, header = "Example2", eps = .001))
setClass("dataFrameD", representation(date = "Date"),
contains = "data.frame")
myDD <- new("dataFrameD", myData, date = Sys.Date())
## S3Part() applied to classes with a data part (.Data slot)
setClass("NumX", contains="numeric", representation(id="character"))
nn <- new("NumX", 1:10, id="test")
stopifnot(identical(1:10, S3Part(nn, strict = TRUE)))
m1 <- cbind(group, weight)
setClass("MatX", contains = "matrix", representation(date = "Date"))
mx1 <- new("MatX", m1, date = Sys.Date())
stopifnot(identical(m1, S3Part(mx1, strict = TRUE)))
Run the code above in your browser using DataLab