简体   繁体   English

lm()$ assign:这是什么?

[英]lm()$assign: what is it?

What is the assign attribute of a linear model fit? 线性模型拟合的assign属性是什么? 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() . 据我了解, assign是S的结转,并且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. 我需要提取glm的等效信息,但是我不了解该实现对lm并且似乎也找不到源代码。 The help file for lm.fit says, unhelpfully: lm.fit的帮助文件lm.fit说:

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 非空拟合将具有与线性拟合相关的成分assigneffectsqr (除非没有要求),供提取函数(例如summaryeffects

You find this in help("model.matrix") , which creates these values: 您可以在help("model.matrix")找到它,它创建以下值:

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. 有一个属性“ assign”,一个整数向量,矩阵中每个列都有一个条目,在公式中产生了引起该列的项。 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. 值0对应于截距(如果有的话),正值对应于条件,按与对象相对应的条件结构的term.labels属性给出的顺序。

So, it maps the design matrix to the formula. 因此,它将设计矩阵映射到公式。

The numbers from $assign represent the corresponding predictor variable. $assign的数字表示相应的预测变量。 If your predictor is categorical with 3 levels, you will see the corresponding number (3-1) times in your $assign call. 如果您的预测变量是3级分类,则$assign调用中将看到相应的次数(3-1)次。 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. 您会看到定量预测变量只有一个值(在上面的示例中为hwy ),因为它们在设计公式中由一个术语表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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