简体   繁体   中英

lm()$assign: what is it?

What is the assign attribute of a linear model fit? It's supposed to somehow provide the position of the response term, but in practice it seems to enumerate all coefficients in the model. It's my understanding that assign is a carryover from S and it's not supported by glm() . I need to extract the equivalent information for glm , but I don't understand what the implementation does for lm and can't seem to find the source code either. The help file for lm.fit says, unhelpfully:

non-null fits will have components assign , effects and (unless not requested) qr relating to the linear fit, for use by extractor functions such as summary and effects

You find this in help("model.matrix") , which creates these values:

There is an attribute "assign", an integer vector with an entry for each column in the matrix giving the term in the formula which gave rise to the column. Value 0 corresponds to the intercept (if any), and positive values to terms in the order given by the term.labels attribute of the terms structure corresponding to object.

So, it maps the design matrix to the formula.

The numbers from $assign represent the corresponding predictor variable. If your predictor is categorical with 3 levels, you will see the corresponding number (3-1) times in your $assign call. Example:

data(mpg, package = "ggplot2")
m = lm(cty ~ hwy + class,data = mpg)
m$assign 
  [1] 0 1 2 2 2 2 2 2
# Note how there is six 2's to represent the indicator variables
# for the various 'class' levels. (class has 7 levels)

You will see the quantitative predictors will only have one value ( hwy in the example above), since they are represented by one term in the design formula.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM