require(stats)
## "lm" and "mlm" are predefined; if they were not this would do it:
## Not run: ------------------------------------
# setOldClass(c("mlm", "lm"))
## ---------------------------------------------
## Define a new generic function to compute the residual degrees of freedom
setGeneric("dfResidual",
function(model) stop(gettextf(
"This function only works for fitted model objects, not class %s",
class(model))))
setMethod("dfResidual", "lm", function(model)model$df.residual)
## dfResidual will work on mlm objects as well as lm objects
myData <- data.frame(time = 1:10, y = (1:10)^.5)
myLm <- lm(cbind(y, y^3) ~ time, myData)
## two examples extending S3 class "lm": class "xlm" directly
## and "ylm" indirectly
setClass("xlm", slots = c(eps = "numeric"), contains = "lm")
setClass("ylm", slots = c(header = "character"), contains = "xlm")
ym1 = new("ylm", myLm, header = "Example", eps = 0.)
## for more examples, see ?\link{S3Class}.
## Not run: ------------------------------------
# ## The code in R that defines "ts" as an S4 class
# setClass("ts", contains = "structure", slots = c(tsp = "numeric"),
# prototype(NA, tsp = rep(1,3)))
# # prototype to be a legal S3 time-series
# ## and now registers it as an S3 class
# setOldClass("ts", S4Class = "ts", where = envir)
## ---------------------------------------------
Run the code above in your browser using DataCamp Workspace