0

I try to find two breakpoints in a piecewise linear regression. Not use segmented package. When I store R2 value to a matrix, it goes wrong? Could some one help me?

Here is my code:

x <- c(40:90)
mse <- matrix(nrow = length(x),ncol = length(x))
for(i in 1:length(x)){
  for(j in 1:length(x)){
    piecewise <- lm(Meter ~ Temperature*(Temperature <= x[i])
                    +Temperature*(Temperature > x[i] & Temperature < x[j])
                    +Temperature*(Temperature >= x[j]))
  }
  mse[i,j] <- summary(piecewise)[8]
}

Error in mse[i, j] <- summary(piecewise)[8] : incorrect number of subscripts on matrix

XF Ye
  • 1
  • Just put `mse[i, j] <- summary(piecewise)[[8]]` or `mse[i, j] <- summary(piecewise)$r.squared` as the last instruction of the ***inner*** loop. See [Difference between `[` and `[[`](https://stackoverflow.com/questions/1169456/the-difference-between-bracket-and-double-bracket-for-accessing-the-el) – Rui Barradas Jan 12 '23 at 06:46
  • Also, are you looking for [segmented regression](https://CRAN.R-project.org/package=segmented)? – Rui Barradas Jan 12 '23 at 06:51

0 Answers0