4

I've seen examples of plot function where either tilde or particular arguments were used. Following produces exactly the same plot (notice that when tilde is used x and y are swapped):

x = 1:10
y = 11:20
plot(x,y)
plot(y~x)

I've read about tilde in R but did not catch it's purpose. Only fact that I was able to grasp is that it is used in correlation/regression for some reason. Can someone clarify the tilde purpose? Is tilde needed when we can just swap arguments? Isn't tilde just syntactic sugar?

CAFEBABE
  • 3,983
  • 1
  • 19
  • 38
Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122
  • Read all about it in `?formula`. It connects a response variable to the independent variables. `?lm` and `?aov` have other use examples. – Bryan Hanson Jan 03 '16 at 00:39
  • It's an infix function that builds a call. This should bring up the function's help page: `?"~"` – IRTFM Jan 03 '16 at 00:39
  • [?plot.formula](https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/plot.formula.html) is also informative. – Jota Jan 03 '16 at 00:41

1 Answers1

1

With two variables it is merely a matter of convenience, that you can use formular notation. It is quite common in statistics functions to use the tilde notation. Quite often I define dependencies for regression, inspect them in plots and run parameter fitting afterwards.

However, in the more than two variables it makes a significant difference:

x = 1:10
y = 11:20
z = 11:20
plot(x~y+z)
plot(x,y,z)
CAFEBABE
  • 3,983
  • 1
  • 19
  • 38