【问题标题】:How to plot linear and inverse regression fit lines in R?如何在 R 中绘制线性和逆回归拟合线?
【发布时间】:2019-12-06 06:30:36
【问题描述】:

我正在尝试在散点图上绘制线性和逆拟合线。 我尝试了很多事情都没有成功。

我曾尝试使用具有平滑函数规范的 ggplot,但反向线仍然完全看起来像 y=0。 我试过添加ablines,没有运气。 我没有成功使用 lm(y=1/x)。

【问题讨论】:

  • 你所说的逆向拟合线是什么意思?请注意,lm(y=1/x) 不是有效的 R,它可能是 lm(y ~ I(1/x)),但我仍然不明白这一点。
  • 逆向拟合是指指数衰减模型,例如stats.stackexchange.com/questions/136269/…?
  • 喜欢ggplot(mtcars, aes(hp, mpg)) + geom_point() + geom_smooth(method = 'glm', method.args = list(family = quasi('inverse')))A reproducible example 会有很大帮助。

标签: r plot regression line inverse


【解决方案1】:

我从逆拟合线的理解是;

y = a + bx

data <- data.frame("y"=mtcars$disp,"x"=mtcars$wt)

fit <-  lm(data$y ~  data$x)
fit_inverse <-  lm(data$y ~  I(1/data$x))

但是,由于 x 轴不同(x 和 1/x),要将它们放在同一个图表上,您必须使用不同的 x 轴。否则,您需要单独绘制它们。

plot(data$x,data$y,col = "blue",bty="l",pch=20,ylab = "",xlab="")
lines(data$x,fitted(fit) ,type="l",lty = 29,col="blue") 
par(new = TRUE)
plot(1/data$x,data$y, xaxt = "n", yaxt = "n",col = "red", lty = 
2,bty="l",pch=10,ylab = "",xlab="")
lines(1/data$x,fitted(fit_inverse) ,type="l",lty = "29",col="red") 
axis(side = 3)
legend("top", c("Fitted", "Inverse Fitted"),col = c("blue", "red"), lty 
= c(29, 2))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    相关资源
    最近更新 更多