R:绘制 geom_line() 的 lm() 预测值和几何平滑不重合

R: plotting geom_line() of lm() prediction values and geometric smooth do not coincide

我有以下数据

df <- data.frame(x= c(0,1,10,100,1000,0,1, 10,100,1000,0,1,10,100,1000), 
                 y=c(7,15,135,1132,6459,-3,11,127,1120,6249,-5,13,126,1208,6208))

使用数据建立线性模型后,我使用该模型根据已知的 x 值预测 y 值。将预测的 y 值存储在数据框“pred.fits”

fit <- lm(data = df, y ~ x)

pred.fits <- expand.grid(x=seq(1, 2000, length=2001))

pm <- predict(fit, newdata=pred.fits, interval="confidence")

pred.fits$py <- pm[,1]

我绘制数据并同时使用 geom_smooth() 和 geom_line(),它们似乎很巧合。

ggplot(df, aes(x=x, y=y)) + 
       geom_point() + 
       geom_smooth(method = lm, formula = y ~ x, se = FALSE, size=1.5) +
       geom_line(data=pred.fits, aes(x=x, y=py), size=.2)

然而,当我绘制相同的数据时,将坐标轴设置为对数刻度,这两个回归差异很大。

ggplot(df, aes(x=x, y=y)) + 
       geom_point() + 
       geom_smooth(method = lm, formula = y ~ x, se = FALSE, size=1.5) +
       geom_line(data=pred.fits, aes(x=x, y=py), size=.2) + 
       scale_x_log10() + 
       scale_y_log10()

我是不是漏掉了什么?

更新

在@Duck 给我指出了正确的方向后,我就能够做到正确。问题是,我希望数据不被转换,但轴转换为 log10 比例。这就是我能够做到的。

df2 <- df[df$x>=1,] # remove annoying warning msgs.

fit2 <- lm(data = df2, log10(y) ~ log10(x))

pred.fits2 <- expand.grid(x=seq(10^0, 10^3  , length=200))

pm2 <- predict(fit2, newdata=pred.fits2, interval="confidence")

pred.fits2$py <-  10^pm2[,1] # convert the predicted y values to linear scale

ggplot(df2, aes(x=x, y=y)) + 
geom_point() + 
geom_smooth(method = lm, formula = y ~ x, se = FALSE, size=1.5) +
geom_line(data=pred.fits2, aes(x=x, y=py), size=1.5, linetype = "longdash") + 
scale_x_log10() +
scale_y_log10()

感谢大家的帮助。

此代码可能有助于您的理解(感谢@BWilliams 的宝贵评论)。您需要对数比例的 x 和 y,因此如果混合具有不同比例的线性模型会弄乱一切。如果你想看到相似的尺度,最好用对数变量训练不同的模型,然后也使用适当的值绘制它。这是我们构建 log-log 模型然后绘制的方法(数据值作为 1 或负数已在新数据框中隔离 df2)。这里的代码:

第一个线性模型:

library(ggplot2)
#Data
df <- data.frame(x= c(0,1,10,100,1000,0,1, 10,100,1000,0,1,10,100,1000), 
                 y=c(7,15,135,1132,6459,-3,11,127,1120,6249,-5,13,126,1208,6208))

#Model 1 all obs
fit <- lm(data = df, y ~ x)
pred.fits <- expand.grid(x=seq(1, 2000, length=2001))
pm <- predict(fit, newdata=pred.fits, interval="confidence")
pred.fits$py <- pm[,1]
#Plot 1
ggplot(df, aes(x=x, y=y)) + 
  geom_point() + 
  geom_smooth(method = lm, formula = y ~ x, se = FALSE, size=1.5) +
  geom_line(data=pred.fits, aes(x=x, y=py), size=.2)

输出:

现在是日志变量的草图,请注意我们如何在主要变量中使用 log() 以及模型是如何构建的:

#First remove issue values
df2 <- df[df$x>1,]
#Train a new model
pred.fits2 <- expand.grid(x=seq(1, 2000, length=2001))
fit2 <- lm(data = df2, log(y) ~ log(x))
pm2 <- predict(fit2, newdata=pred.fits2, interval="confidence")
pred.fits2$py <- pm2[,1]
#Plot 2
ggplot(df2, aes(x=log(x), y=log(y))) + 
  geom_point() + 
  geom_smooth(method = lm, formula = y ~ x, se = FALSE, size=1.5) +
  geom_line(data=pred.fits2, aes(x=log(x), y=py), size=.2)

输出: