cups - 2 months ago 40

R Question

So I get a bit confused by the lm()-command. I tried it with

`lm(x~y, mydata)`

`lm(y~x, mydata)`

So is that just which variable to use as x and which one to use as y? I'm sorry to ask such a noob question but I am not sure and I coulnd't find anything explaining the parameters of that command!

Answer

The answers can be found on the help page for the function. In the `Details`

section we have:

`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.`

There are more details (also linked to from the `lm`

help page to `formula`

. In the details sections for `formula`

, we have:

`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.`

So to summarize, you define your model in symbolic terms where the LHS is your response variable, and the RHS are your predictor variable(s). You get different answers because in one model, `y`

is your response variable and the other is `x`

.

If you weren't aware, you can access the help page for nearly all functions with `?`

at the command line, i.e. `?lm`

or `?formula`

.