# creating a linear + linear function
x <- seq(0,10, length.out=201)
knot <- 5
set.seed(12543)
mu <- ifelse(x<=knot,5+0.5*x,5+0.5*x+(x-knot))
y <- rNO(201, mu=mu, sigma=.5)
# plot the data
plot(y~x, xlim=c(-1,13), ylim=c(3,17))
# fit model using fixed break points
m1 <- fitFixBP(x, y, knots=5, degree=1)
knots(m1)
lines(fitted(m1)~x, col="red")
m2 <- fitFreeKnots(x, y, knots=5, degree=1)
knots(m2)
lines(fitted(m2)~x, col="green", lwd=3)
points(0:13,predict(m2, newdata=0:13), col="red",pch = 21, bg="blue")
points(0:13,predict(m2, newdata=0:13, old.x.range=FALSE), col="red",pch = 21, bg="grey")Run the code above in your browser using DataLab