В нижнем регистре в формуле geom_smooth влияет на построение линейной регрессии - PullRequest
2 голосов
/ 19 января 2020

Я строил линию регрессии, используя geom_smooth() псевдоним от ggplot2, как показано ниже:

library(ggplot2)

X <- c(3, 7, 10, 14, 15, 11, 13, 18)
Y <- c(11, 5, 3, 9, 7, 5, 2, 1)

ggplot(data = data.frame(X, Y), aes(x = X, y = Y)) + theme_bw() +
  geom_point(col = 'red')  + geom_smooth(method = 'lm', formula = Y ~ X)

Удивительно, но я получил предупреждение как:

Warning messages:
1: 'newdata' had 80 rows but variables found have 8 rows 
2: In base::data.frame(x = xseq, fit, se = pred$se.fit) :
  row names were found from a short variable and have been discarded

С следующий график:

enter image description here

Я ожидал линейный график (прямую линию) в виде:

enter image description here

, который я нашел по исправляя коды R:

library(ggplot2)

x <- c(3, 7, 10, 14, 15, 11, 13, 18)
y <- c(11, 5, 3, 9, 7, 5, 2, 1)

ggplot(data = data.frame(x, y), aes(x = x, y = y)) + theme_bw() +
  geom_point(col = 'red')  + geom_smooth(method = 'lm', formula = y ~ x)

Почему это произошло? Что происходит, если мы напишем это formula в верхнем регистре?

...