显示 lm 语句中使用的前几个 obs
show first few obs used in lm statement
假设我有一个公式(例如,myformula <- y ~ x1 + x2
)和一个数据集 d。出于诊断目的,我想看看最终在我的线性回归中使用的观察结果是什么 (r <- lm(myformula, data=d)
)。这基本上需要根据 d 构建一个包含 y、x1 和 x2 的数据框,并删除所有具有任何缺失数据的观察值 (complete.obs)。或者,也许,操纵 r 对象的内容?
感谢您的建议。
/iaw
在尝试自己动手之前,请先查看模型对象(使用 str(r)
了解其中的内容)或在文档中查找相关位。来自 ?lm
:
model, x, y, qr
logicals. If TRUE
the corresponding components of the fit (the model frame, the model matrix, the response, the QR decomposition) are returned.
由于默认情况下model
为真,我们所要做的就是在结果中寻找它。文档的 Value 部分描述了返回的对象:
y
if requested, the response used.
x
if requested, the model matrix used.
model
if requested (the default), the model frame used.
现在举个例子:
> mod = lm(mpg ~ disp + I(disp^2), data = mtcars)
> head(mod$model)
mpg disp I(disp^2)
Mazda RX4 21.0 160 25600
Mazda RX4 Wag 21.0 160 25600
Datsun 710 22.8 108 11664
Hornet 4 Drive 21.4 258 66564
Hornet Sportabout 18.7 360 129600
Valiant 18.1 225 50625
请注意,这只是数据,仍然是 data.frame
。如果你想要 模型矩阵 ,一个带有虚拟变量的 matrix
用于截距和任何因子,在 lm
调用中设置 x = TRUE
然后查看在 mod$x
.
假设我有一个公式(例如,myformula <- y ~ x1 + x2
)和一个数据集 d。出于诊断目的,我想看看最终在我的线性回归中使用的观察结果是什么 (r <- lm(myformula, data=d)
)。这基本上需要根据 d 构建一个包含 y、x1 和 x2 的数据框,并删除所有具有任何缺失数据的观察值 (complete.obs)。或者,也许,操纵 r 对象的内容?
感谢您的建议。
/iaw
在尝试自己动手之前,请先查看模型对象(使用 str(r)
了解其中的内容)或在文档中查找相关位。来自 ?lm
:
model, x, y, qr
logicals. IfTRUE
the corresponding components of the fit (the model frame, the model matrix, the response, the QR decomposition) are returned.
由于默认情况下model
为真,我们所要做的就是在结果中寻找它。文档的 Value 部分描述了返回的对象:
y
if requested, the response used.
x
if requested, the model matrix used.
model
if requested (the default), the model frame used.
现在举个例子:
> mod = lm(mpg ~ disp + I(disp^2), data = mtcars)
> head(mod$model)
mpg disp I(disp^2)
Mazda RX4 21.0 160 25600
Mazda RX4 Wag 21.0 160 25600
Datsun 710 22.8 108 11664
Hornet 4 Drive 21.4 258 66564
Hornet Sportabout 18.7 360 129600
Valiant 18.1 225 50625
请注意,这只是数据,仍然是 data.frame
。如果你想要 模型矩阵 ,一个带有虚拟变量的 matrix
用于截距和任何因子,在 lm
调用中设置 x = TRUE
然后查看在 mod$x
.