如何在回归线上找到相交点 [英] How to find the intersecting point in the regression line

查看:72
本文介绍了如何在回归线上找到相交点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上图中,绘制了两条垂直线段和水平线段,它们将与回归线相交.如何编写代码以画出这些线以找到相交点?

In the above graph two vertical and horizontal line segments are drawn which will intersect the regression line. How to write code to draw those lines to find the intersecting point?

推荐答案

每条回归线的公式都打印在绘图上,因此我们可以通过简单的代数获得信息.

The formula for each regression line is printed on the plot, so we can get the information by simple algebra.

首先,我们将绘制每个回归线:

First we will plot each regression line:

x   <- c(0, 22)
y0  <- 27.46 + 0.31 * x
y5  <- 40.18 + 0.49 * x
y10 <- 55.54 + 0.67 * x
y15 <- 71.63 + 0.84 * x

plot(x, y0, type = "l", ylim = c(0, 105), xlim = c(0, 25),
     ylab = "Percentage of VO2max", xlab = "Load (kg)")
lines(x, y5)
lines(x, y10)
lines(x, y15)

现在,我们只用y = 50,y = 60和y = 75重新排列适当的回归线公式:

Now we just rearrange the appropriate regression line formulas with y = 50, y = 60, and y = 75:

x5  <- (50 - 40.18) / 0.49
x10 <- (60 - 55.54) / 0.67
x15 <- (75 - 71.63) / 0.84

因此我们可以将它们添加到图中以表明我们具有交点:

So we can add these to our plot to show that we have the intersections:

abline(h = 50, lty = 2, col = "red")
abline(h = 60, lty = 2, col = "blue")
abline(h = 75, lty = 2, col = "green")
lines(c(x5, x5), c(50, 0), lty = 2, col = "red")
lines(c(x10, x10), c(60, 0), lty = 2, col = "blue")
lines(c(x15, x15), c(75, 0), lty = 2, col = "green")
points(c(x5, x10, x15), c(50, 60, 75))

这看起来不错.因此,我们的三个交叉点是:

This looks good. So our three intersections are:

data.frame(x = c(x5, x10, x15), y = c(50, 60, 75))
          x  y
1 20.040816 50
2  6.656716 60
3  4.011905 75


编辑

在注释中添加了一些数据:

With some data added in the comments:

df <- data.frame(load = rep(c(0,4.4,10.7,17,21.4), each = 4), 
                 Gradient = c(0,5,10,15), 
                 VO2max= c(28.0,41.0,56.3,71.3,28.2,41.1,57.0,
                           75.0,31.0,45.4,63.6,82.1, 32.0,48.8,
                           66.8,85.5,34.6,50.5,69.9,89.3))

df$Gradient <- as.factor(df$Gradient)

可以在ggplot2中执行此操作:

It is possible to do this in ggplot2:

library(ggplot2)

ggplot(df, aes(load, VO2max, group = Gradient)) + 
  geom_point(aes(shape = Gradient), size = 3) + 
  geom_abline(aes(slope = 0.31, intercept = 27.46)) +
  geom_abline(aes(slope = 0.49, intercept = 40.18)) +
  geom_abline(aes(slope = 0.67, intercept = 55.54)) +
  geom_abline(aes(slope = 0.84, intercept = 71.63)) +
  geom_segment(data = data.frame(x = c(x5, x10, x15),
                                 y = c(50, 60, 75),
                                 Gradient = factor(c(50, 60, 75))),
               aes(x, y, xend = x, yend = 0, colour = Gradient),
               linetype = 2) +
  geom_point(data = data.frame(load = c(x5, x10, x15), 
                               VO2max = c(50, 60, 75),
                               Gradient = 1)) +
  coord_cartesian(ylim = c(0, 105), xlim = c(0, 25),
                  expand = 0) +
  geom_hline(data = data.frame(y = c(50, 60, 75), 
                               Gradient = factor(c(50, 60, 75))),
             aes(yintercept = y, colour = Gradient), linetype = 2) +
  theme_minimal() +
  theme(axis.line = element_line()) +
  guides(colour = "none")

这篇关于如何在回归线上找到相交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆