WeightedLowess
A C++ library for LOWESS with various weighting schemes
|
This C++ library implements the Locally Weighted Scatterplot Smoothing (LOWESS) method described by Cleveland (1979, 1981). LOWESS is a non-parametric smoothing algorithm that is simple and computationally efficient yet can accommodate a wide variety of curves. The libary itself is header-only and thus can be easily used in any C++ project by adding the relevant #include
directives. This implementation is derived from the limma package and contains some modifications from Cleveland's original code. Of particular interest is the ability to treat the weights as frequencies such that they are involved in the window span calculations for each anchor point - hence the name.
Cleveland's original Fortran implementation is quite simple:
In limma::weightedLowess
, we implemented some (optional) modifications that are also available in this library. See the ?weightedLowess
documentation for more details.
delta
value can be automatically determined from a pre-specified number of anchor points. This provides a convenient way of controlling the approximation fidelity.In this library, we implement some further modifications from limma::weightedLowess
:
limma::weightedLowess
includes the first fit. So 3 iterations here are equivalent to 4 iterations in limma::weightedLowess
.Using this library is as simple as including the header file in your source code:
We can set options via the WeightedLowess::Options
object:
If users already have an appropriate buffer for the fitted values and robustness weights, they can be filled directly with the results:
We can also pre-compute the window locations from x
to re-use them with different y
, e.g., for smoothing different dimensions with a common covariate.
The compute()
function assumes that the input x-coordinates are already sorted. If this is not the case, we can use the SortBy
class to sort the input and unsort the output:
See the reference documentation for more details.
FetchContent
If you're already using CMake, you can add something like this to your CMakeLists.txt
:
And then:
find_package()
You can install the library by cloning a suitable version of this repository and running the following commands:
Then you can use find_package()
as usual:
By default, this will use FetchContent
to fetch all external dependencies. If you want to install them manually, use -DWEIGHTEDLOWESS_FETCH_EXTERN=OFF
. See extern/CMakeLists.txt
to find compatible versions of each dependency.
If you're not using CMake, the simple approach is to just copy the files in the [include/
](include) subdirectory - either directly or with Git submodules - and include their path during compilation with, e.g., GCC's -I
. This requires the external dependencies listed in extern/CMakeLists.txt
, which also need to be made available during compilation.
Cleveland, W.S. (1979). Robust locally weighted regression and smoothing scatterplots. Journal of the American Statistical Association 74(368), 829-836.
Cleveland, W.S. (1981). LOWESS: A program for smoothing scatterplots by robust locally weighted regression. The American Statistician 35(1), 54.