【发布时间】:2020-09-12 06:29:02
【问题描述】:
我有一些数据:
library(ggplot2)
x <-c(2600248.25,1303899.14285714,1370136.33333333,353105.857142857, 145446.952380952,299032,75142.2631578947,40381.1818181818,6133.93103448276,975.234567901235,779.341463414634)
y <- c(4,7,6,14,21,9,19,22,29,81,41)
我试图对此进行回归和绘图。我的问题是我想进行回归并根据我的数据绘制它,但是当我在对数值上使用 lm 时,预测和绘制与 stat_smooth 相比,我得到了一些不同的结果。考虑代码:
fit0 <- lm(log(y) ~ log(x))
summary(fit0)
newx <- x
lm.fit <- predict(fit0, newdata = data.frame(x=newx), interval = "confidence")
df <- as.data.frame(cbind(x,y,lm.fit))
p <- ggplot(df, aes(x,y)) + geom_point() + geom_smooth(method = "lm", formula ="y~x") + scale_x_log10() + scale_y_log10()
p <- p + geom_line(aes(y=fit)) # result too low
p <- p + geom_line(aes(y=10^fit)) # result too high
如所见,我已尝试使用日志结果并使用 10^x 转换回来。照原样,两个线性模型应该显示相同的值吗?这里有什么问题,我如何获得正确的值?
(我的最终目标是能够绘制预测区间)
【问题讨论】:
标签: r ggplot2 regression linear-regression lm