# example data
d <- data.frame(
x = 1:8,
y = c(1, 2, 3, 4, 4, 3, 2, 1))
# solve for break points
soln <- solve_for_partition(d$x, d$y)
# show solution
print(soln)
# label each point
d$group <- base::findInterval(
d$x,
soln$x[soln$what=='left'])
# apply piecewise approximation
d$estimate <- stats::approx(
soln$x,
soln$pred,
xout = d$x,
method = 'linear',
rule = 2)$y
# show result
print(d)
Run the code above in your browser using DataLab