Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a slot in an S4 class for robust linear models?

Tags:

class

r

s4

I would like to create an S4 class that has slots which can hold robust linear models.

Robust linear models are a type of linear model from the MASS package. They carry all of the information a linear model has and a bit more.

library(MASS)
x <- 1:5
y <- 1:5
mylm <- lm(x~y)
myrlm <- rlm(x~y)

Here is my class:

.MyClass <- setClass("MyClass", list(a="lm", id="numeric"))

Even though .MyClass(a=mylm, id=1) produces the expected object, initializing an object with an rlm fails:

> .MyClass(a=myrlm, id=1)
Error in validObject(.Object) : 
  invalid class “MyClass” object: 1: invalid object for slot "a" in class "MyClass": got class "rlm", should be or extend class "lm"
invalid class “MyClass” object: 2: invalid object for slot "a" in class "MyClass": got class "lm", should be or extend class "lm"

I would have thought that since is(myrlm, "lm") returns TRUE there wouldn't be an issue and the object could fit in the slot. Also, since it is telling me I created an invalid object twice, why is the second one saying that lm is not itself? Is it because lm is a virtual class?

I've tried setting a="list" in the representation (since lm and rlm are both lists), but that produces a similar error. Does the slot need a different class type? I've also tried setting a="rlm", but rlm is not defined class.

like image 950
Will Beason Avatar asked Sep 30 '14 19:09

Will Beason


People also ask

How do you create S4 objects How can you check an object is S4 object?

Example 2: Creation of S4 object We can check if an object is an S4 object through the function isS4() . The function setClass() returns a generator function. This generator function (usually having same name as the class) can be used to create new objects. It acts as a constructor.

What is an S4 object R?

The S4 system in R is a system for object oriented programing. Confusingly, R has support for at least 3 different systems for object oriented programming: S3, S4 and S5 (also known as reference classes).


1 Answers

The problem seems to be the fact that rlm objects has two S3 classes. I suggest, as a work-around, to define a constructor and change the class of the slots before creating the object. Something along these lines:

   library(MASS)
   x <- 1:5
   y <- 1:5
   mylm <- lm(x~y)
   myrlm <- rlm(x~y)
   .MyClass <- setClass("MyClass", list(a="lm", id="numeric"))
   MyClass<-function(a,id) {
     if (!is(a,"lm")) stop("error")
     class(a)<-"lm"
     new("MyClass",a=a,id=id)
   }
   MyClass(myrlm,1)
like image 106
nicola Avatar answered Nov 15 '22 01:11

nicola