1

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

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
rimorob
  • 624
  • 1
  • 5
  • 16

2 Answers2

5

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.

Roland
  • 127,288
  • 10
  • 191
  • 288
  • ridiculous that this isn't mentioned _at all_ in `?lm`... had to spend 20 minutes tracking down where `assign` was coming from in the first place. – MichaelChirico Aug 11 '16 at 15:40
2

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.

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
cgage1
  • 579
  • 5
  • 15
  • Where's the code that calculates this field? I couldn't find it so far. – rimorob Mar 25 '16 at 19:04
  • Ah, I see the issue now. That is odd that the `$assign` is not in the source code for `lm` or `glm`. I havn't found why yet, but I will keep looking. – cgage1 Mar 25 '16 at 19:16