loess

Syntax

loess(X, Y, resampleRule, [closed='left'], [origin='start_day'], [outputX=false], [bandwidth=0.3], [robustnessIter=4], [accuracy=1e-12])

Parameters

X is a strictly increasing vector of temporal type.

Y is a numeric vector of the same length as X.

resampleRule is a string. See the parameter rule of function resample for the optional values.

closed and origin are the same as the parameters closed and origin of function resample.

outputX is a Boolean value indicating whether to output the resampled X. The default value is false.

bandwidth is a numeric scalar in (0,1]. when computing the loess fit at a particular point, this fraction of source points closest to the current point is taken into account for computing a least-squares regression.

robustnessIter is a postive interger indicating how many robustness iterations are done.

accuracy is a number greater than 1. If the median residual at a certain robustness iteration is less than this amount, no more iterations are done.

Details

Resample X based on the specified resampleRule, closed and origin. Implement Local Regression Algorithm (Loess) for interpolation on Y based on the resampled X.

If outputX is unspecified, return a vector of Y after the interpolation.

If outputX=true, return a tuple where the first element is the vector of resampled X and the second element is a vector of Y after the interpolation.

Examples

Example 1
loess([2016.02.14 00:00:00, 2016.02.15 00:00:00, 2016.02.16 00:00:00], [1.0, 2.0, 4.0], resampleRule=`60min, bandwidth=1)

/* output:
[1,1.0521,1.104,1.1558,1.2072,1.2582,1.3086,1.3584,1.4074,1.4556,
1.5027,1.5488,1.5937,1.6374,1.6795,1.7202,1.7593,1.7966,1.832,1.8655,
1.897,1.9263,1.9533,1.9779,2,2.0195,2.0366,2.0513,2.0637,2.0739,
2.082,2.0882,2.0926,2.0952,2.0962,2.0957,2.0938,2.0905,2.0861,2.0806,
2.0741,2.0667,2.0586,2.0498,2.0405,2.0308,2.0207,2.0104,2]
*/
Example 2 Different values of closed affect the result.
X = 2022.01.01T00:00:00 + [0, 3, 6, 9] * 60
Y = [1.0, 3.0, 7.0, 13.0]

// closed='left' (default): 00:03 belongs to the start of [00:03, 00:06)
loess(X, Y, `3min, closed=`left, outputX=true, bandwidth=1)

// closed='right': 00:03 belongs to the end of (00:00, 00:03]
// The fitted point positions differ, so the output also changes
loess(X, Y, `3min, closed=`right, outputX=true, bandwidth=1)
Example 3 Different values of origin affect the result.
X = 2022.01.01T00:00:30 + (0..4) * 60
Y = [2.0, 4.0, 7.0, 11.0, 16.0]

// origin='start_day' (default): align to 00:00:00 of the same day
loess(X, Y, `1min, origin=`start_day, outputX=true, bandwidth=1)

// origin='start': align to the first data point 00:00:30
loess(X, Y, `1min, origin=`start, outputX=true, bandwidth=1)

// origin=custom timestamp: align to 00:00:10
loess(X, Y, `1min, origin=2022.01.01T00:00:10, outputX=true, bandwidth=1)
Example 4 Different values of outputX affect the result.
X = [2016.02.14T00:00:00, 2016.02.15T00:00:00, 2016.02.16T00:00:00]
Y = [1.0, 2.0, 4.0]

// outputX=false (default): return only the LOESS-interpolated Y vector
loess(X, Y, `60min, bandwidth=1)

// outputX=true: return a tuple; [0] is the resampled timestamp vector, [1] is the interpolated Y
result = loess(X, Y, `60min, outputX=true, bandwidth=1)
result[0]
result[1]