1

I am wondering if the function lm() from the stats package is the same as the one used in geom_smoot(method = lm) in ggplot2. If so, are the parameters calculated by them the same everytime for the same data.

So would...

lm(response ~ variable, data = dat)

calculate the same parameters as...

ggplot(dat, aes(variable, response)) + 
  geom_smooth(method = lm)
kaos
  • 53
  • 5

2 Answers2

3

Yes, geom_smooth uses stats::lm. The?geom_smooth documentation mentions stats::lm specifically, and in the See Also section has a link to the lm documentation.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thank you, I just found it in the documentation myself. Still I was not sure if the lm() function returns the same results everytime. – kaos Jun 15 '20 at 20:24
  • It does. Linear models are deterministic. You can see the [wikipedia page on linear models](https://en.wikipedia.org/wiki/Linear_model) for an introduction. – Gregor Thomas Jun 15 '20 at 20:27
1

The linear model (coded in lm) is fully deterministic. It returns the same results as long as you call it on the same input. geom_smooth uses stats::lm behind the scene, so the results should be the same. Note that it is not possible to extract model parameters from a ggplot plot (link).

slava-kohut
  • 4,203
  • 1
  • 7
  • 24
  • 1
    Thank you very much. I know that I cannot extract the parameters from ggplot. Thats why I just wanted to do the same model externally and use the parameters of that one. I just wanted to be sure, that the parameters I am going to report are the same parameters that I show in my plot. – kaos Jun 15 '20 at 20:26
  • You can get and print the equation and R^2 on the plot using stat_poly_eq from ggpmisc if that helps? – mlcyo Jun 15 '20 at 22:15