2

I did piecewise regressions, found the breakpoints and plotted following this example. According to the results, there is only one breakpoint at 107.4733, so 2 lines are expected to be presented: one before and one after this value of x. However, when plotting 3 lines appear, indicating 3 segments. How can I solve this? Where is the error?

library(ggplot2)
library(segmented)

x <- c(0, 60, 90, 120, 180, 240)
y <- c(0.0000000, 0.6900891, 1.9523416, 2.3142669, 1.8167140, 2.6999062)
data <- data.frame(x, y)
my.lm <- lm(y ~ x, data = data)
my.seg <- segmented(my.lm, 
                    seg.Z = ~ x)

my.fitted <- fitted(my.seg)
my.model <- data.frame(x = data$x, y = my.fitted)

ggplot(my.model, aes(x = x, y = y)) + geom_line() +
  geom_vline(xintercept = my.seg$psi[2])

enter image description here

Daniel Valencia C.
  • 2,159
  • 2
  • 19
  • 38
  • 1
    Not completely on-topic, but if you were to plot this with uncertainty, you'd get a very wide range of fits (including inferred change point locations) to such a small dataset. Try `library(mcp); model = list(y ~ 0 + x, ~ 0 + x); fit = mcp(model, data); plot(fit, q_fit = 0.5)` and read more in `?mcp::plot.mcpfit`. – Jonas Lindeløv Nov 29 '21 at 20:05
  • @Daniel Valencia C. I am also facing the same problem. Did you able to solve the problem? – UseR10085 Jul 03 '23 at 09:51

0 Answers0