alex
alex

Reputation: 345

Use one Class of an Object with Multiple

This is a general question motivated by a specific event.

When an object holds multiple classes, each with different generic actions, how can I specify to use "this" class, rather than "that" class?

The example code here is bundled with geepack.

library(stargazer)
library(geepack)

data(dietox)
dietox$Cu <- as.factor(dietox$Cu)
mf <- formula(Weight~Cu*(Time+I(Time^2)+I(Time^3)))

gee0 <- glm(mf, data = dietox, family = poisson("identity")) # a wrong model
gee1 <- geeglm(mf, data=dietox, id=Pig, family=poisson("identity"),corstr="ar1")

class(gee0)
class(gee1)

summary(gee0)
summary(gee1)

stargazer(gee0, type = "text")
stargazer(gee1, type = "text")

I'd like to work with the "glm" class object, not the "geeglm" class object.

@Richard Scriven: I'd just like to pull the results out into a stargazer(...) report. Thanks for the clarifying question.

Upvotes: 1

Views: 880

Answers (1)

Jthorpe
Jthorpe

Reputation: 10167

The class system that uses the class(foo) attribute is not strongly typed. The class vector is used by R to determine which methods to use when that object is passed to a generic like print. For example, if you were to call print(gee1), R would first search for a function called print.geeglm which, in this case, it would find in the package geepack, and R calls that function with the arguments supplied to print().

If R did not find a function called print.geeglm, it would then search for print.gee, then print.glm, then print.default.

So in short, gee1 does not contain 3 objects with different classes, it is a single object with a class vector that informs R where to look for generic methods.

To make things slightly more confusing R has multiple type systems and the class vetcor is used by the S3 type system. A google search for "R s3 class" will get you lots more info on R's class system.

Upvotes: 2

Related Questions