Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Loess.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Loess

import Distances: euclidean
import StatsAPI: fitted, predict, residuals
import StatsAPI: fitted, modelmatrix, predict, residuals, response

using Statistics, LinearAlgebra

export loess, fitted, predict, residuals
export loess, fitted, modelmatrix, predict, residuals, response

include("kd.jl")

Expand All @@ -17,6 +17,10 @@ struct LoessModel{T <: AbstractFloat}
kdtree::KDTree{T}
end

modelmatrix(model::LoessModel) = model.xs

response(model::LoessModel) = model.ys

"""
loess(xs, ys; normalize=true, span=0.75, degree=2)

Expand Down Expand Up @@ -199,9 +203,9 @@ function predict(model::LoessModel, zs::AbstractMatrix)
end
end

fitted(model::LoessModel) = predict(model, model.xs)
fitted(model::LoessModel) = predict(model, modelmatrix(model))

residuals(model::LoessModel) = fitted(model) .- model.ys
residuals(model::LoessModel) = fitted(model) .- response(model)

"""
tricubic(u)
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ using RDatasets
x = [13.0,14.0,14.35,15.0,16.0]
y = [0.369486, 0.355579, 0.3545, 0.356952, 0.36883]
model = loess(x,y)
@test response(model) == y
@test modelmatrix(model) == reshape(x, :, 1)
@test fitted(model) ≈ y
@test all(isapprox(0; atol=1e-12), residuals(model))
end
Expand Down