1

所以我对 lm() 命令有点困惑。我试过了lm(x~y, mydata)lm(y~x, mydata)我得到了不同的输出。那么这只是将哪个变量用作 x 以及将哪个变量用作 y 吗?很抱歉问这样一个菜鸟问题,但我不确定,我找不到任何解释该命令参数的东西!

4

1 回答 1

8

答案可以在该功能的帮助页面上找到。在该Details部分中,我们有:

A typical model has the form response ~ terms where response is the (numeric) response vector and terms is a series of terms which specifies a linear predictor for response.

还有更多详细信息(也从lm帮助页面链接到formula。在 的详细信息部分formula,我们有:

The ~ operator is basic in the formation of such models. An expression of the form y ~ model is interpreted as a specification that the response y is modelled by a linear predictor specified symbolically by model.

总而言之,您以符号术语定义模型,其中 LHS 是您的响应变量,而 RHS 是您的预测变量。您会得到不同的答案,因为在一个模型中,y是您的响应变量,而另一个是x

如果您不知道,您可以使用?命令行访问几乎所有功能的帮助页面,即?lm?formula.

于 2012-09-13T19:43:19.563 回答