adrian1121 adrian1121 - 6 months ago 26
R Question

Distinguish between lm object and rlm object

I am trying to distinguish between a LM object and a RLM (robust LM from MASS) because depending on which type of regression is I want to perform one thing or another. So fitList can be either a lm or a rlm model, being all the elements of the list the same type.

fitList as a list of several normal linear models:

fitList[[1]]$call
Output: lm(formula = frmla, data = xList[[i]])


And in this second case, fitList as a list of several robust linear models:

fitList[[1]][1]$call
Output: rlm(formula = frmla, data = xList[[i]], method = "MM")


As you see, the way to obtain the formula used in both models is different. I'd like a method that distinguishes between both and tells me if the list is a list of linear models or a list of robust linear models. After many attempts I haven't manage to get a way to obtain which one it is.

Any suggestion please?

Answer Source

Reproducible example:

library(MASS)
fitlm <- lm(Height ~., trees)
fitrlm <- rlm(Height ~., trees)
class(fitlm)[1]
class(fitrlm)[1]