If poweruse
is a continuous function of temp
which starts out in the horizontal leg for lower temperatures and then transitions to an increasing linear leg at higher temperatures then we can represent it as the maximum of the two legs at each temperature.
Given ascending temperatures we can assume that at least the first k are in the horizontal leg and that at least the last k are in the linear leg. We fit these portions of the two legs separately using mean
and lm
respectively to get starting values and then use those to get the overall fit using nls
.
Note that T0 is at the intersection of the two legs so T0 is the solution to the equation C = A + T0 * B which is T0 = (C-A)/B .
No packages are used.
# test data
set.seed(123)
temp <- 1:100
poweruse <- pmax(50, 1 + 2 * temp) + rnorm(100)
# starting values
k <- 10
fm0 <- lm(poweruse ~ temp, subset = seq(to = length(temp), length = k))
st <- list(C = mean(head(poweruse, k)), A = coef(fm0)[[1]], B = coef(fm0)[[2]])
fm <- nls(poweruse ~ pmax(C, A + B * temp), start = st)
fm
## Nonlinear regression model
## model: poweruse ~ pmax(C, A + B * temp)
## data: parent.frame()
## C A B
## 49.9913 0.8876 2.0037
## residual sum-of-squares: 81.67
##
## Number of iterations to convergence: 2
## Achieved convergence tolerance: 2.874e-07
# calculate T0
with(as.list(coef(fm)), (C - A)/B)
## [1] 24.50596
plot(poweruse ~ temp, type = "l")
lines(fitted(fm) ~ temp, col = "red")
