Ox version of /netlib/a/dloess

description: port to Ox of the loess and stl code
for: smoothing of multivariate scattered data (LOESS);
decompose time series into trend + seasonal + remainder (STL)
required header file: loess.h
namespace: ---
location: ox/packages/netlib/loess
examples: ethanol.ox, gas.ox, and for STL: co2.ox
source: /netlib/a/dloess, /netlib/a/stl
documentation cloess.pdf, cloess.ps


Loess Example

#include <oxstd.h> #include <packages/netlib/loess/loess.h> printsummary(const s, const y, const rest) { println("\n", s); println("Number of Observations: ", columns(y)); println("Equivalent Number of Parameters: ", "%.1f", rest[0]); println("Residual Standard Error: ", "%.4f", rest[1]); } main() { decl NOx = loadmat("ethanol_y.mat"); decl C_E = loadmat("ethanol_x.mat"); decl newdata = <7.5, 9.0, 12.0, 15.0, 18.0; 0.6, 0.8, 1.0, 0.8, 0.6>; decl fit, rest, iret; iret = Loess(C_E, NOx, <>, 0.5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, &fit, 0, 0, 0, 0, 0, &rest); printsummary("Loess(ethanol) (span = 0.5):", C_E, rest); println("Loess return code:", iret); LoessFree(); iret = Loess(C_E, NOx, <>, 0.05, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, &fit, 0, 0, 0, 0, 0, &rest); println("\nLoess(ethanol) (span = 0.05) fails, return code:", iret); if (iret == 0) println("Loess errors:\n", LoessError()); LoessFree(); iret = Loess(C_E, NOx, <>, 0.25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, &fit, 0, 0, 0, 0, 0, &rest); printsummary("Loess(ethanol) (span = 0.25):", C_E, rest); println("Loess return code:", iret); if (iret == 2) println("Loess warnings:\n", LoessWarning()); LoessFree(); } which prints: Loess(ethanol) (span = 0.5): Number of Observations: 88 Equivalent Number of Parameters: 13.0 Residual Standard Error: 0.2599 Loess return code:1 Loess(ethanol) (span = 0.05) fails, return code:0 Loess errors: span too small. fewer data values than degrees of freedom. Loess(ethanol) (span = 0.25): Number of Observations: 88 Equivalent Number of Parameters: 21.6 Residual Standard Error: 0.1761 Loess return code:2 Loess warnings: Warning: pseudoinverse used at 2.1375 5.7452 neighborhood radius 1.0379 reciprocal condition number 1.8687e-016 There are other near singularities as well. 2.893


STL Example

#include <oxstd.h> #include <oxdraw.h> #include <packages/netlib/loess/loess.h> main() { decl co2 = loadmat("co2.mat"); decl robust, seas, trend, no, irreg; no = StLoessEz(co2, 12, 35, 1, 1, 1, &robust, &seas, &trend); irreg = co2 - seas - trend; print("%c", {"data","Seasonal","Trend","Irregular"}, co2' ~ seas' ~ trend' ~ irreg'); DrawTMatrix(0, co2 | trend, {"CO$_2$","Trend"}, 1, 1, 12); DrawTMatrix(1, seas | irreg, {"Seasonal","Irregular"}, 1, 1, 12); ShowDrawWindow(); StLoess(co2, 12, 35, 19, 13, 1, 1, 1, 4, 2, 2, 1, 2, &robust, &seas, &trend); }