Chera Maan
Chera Maan

Reputation: 73

How to find the intersecting point in the regression line

enter image description here

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?

Upvotes: 2

Views: 898

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173793

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)

enter image description here

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))

enter image description here

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

EDIT

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)

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")

enter image description here

Upvotes: 3

Related Questions