Solve for a good set of right-exclusive x-cuts such that the overall graph of y~x is well-approximated by a piecewise linear function. Solution is a ready for use with with base::findInterval()
and stats::approx()
(demonstrated in the examples).
solve_for_partition( x, y, ..., w = NULL, penalty = 0, min_n_to_chunk = 1000, min_seg = 1, max_k = length(x) )Arguments x
numeric, input variable (no NAs).
ynumeric, result variable (no NAs, same length as x).
...not used, force later arguments by name.
wnumeric, weights (no NAs, positive, same length as x).
penaltyper-segment cost penalty.
min_n_to_chunkminimum n to subdivied problem.
min_segpositive integer, minimum segment size.
max_kmaximum segments to divide into.
Valuea data frame appropriate for stats::approx().
Examples# 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)#> x pred group what #> 1 1 1 1 left #> 2 4 4 1 right #> 3 5 4 2 left #> 4 8 1 2 right
# 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)#> x y group estimate #> 1 1 1 1 1 #> 2 2 2 1 2 #> 3 3 3 1 3 #> 4 4 4 1 4 #> 5 5 4 2 4 #> 6 6 3 2 3 #> 7 7 2 2 2 #> 8 8 1 2 1
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4