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