if( !require('alr3') ){ install.packages('alr3', dependencies=TRUE, repos='http://cran.rstudio.com/') } library(alr3) plot(C ~ Temp, data=segreg, xlab='Temperature', ylab='Energy consumption') grid() # I found how to use plinear with two (and more) linear terms here: # # https://stat.ethz.ch/pipermail/r-help/2008-July/167107.html # segreg.m1 = nls(C ~ cbind(1, pmax(0, Temp - gamma)), data=segreg, algorithm='plinear', start=list(gamma=mean(segreg$Temp))) print(summary(segreg.m1)) #postscript("../../WriteUp/Graphics/Chapter4/chap_4_prob_1.eps", onefile=FALSE, horizontal=FALSE) Temp_grid = with(segreg, seq(min(Temp), max(Temp), length.out=100)) C_hat = predict(segreg.m1, newdata=data.frame(Temp=Temp_grid)) plot(segreg$Temp, segreg$C, type='p', pch=19, cex=1.25, xlab='Temperature', ylab='Energy consumption') lines(Temp_grid, C_hat, type='l') grid() #dev.off()