From f3f7212cda25031fed2b04ceb41ffdb68189f5c0 Mon Sep 17 00:00:00 2001 From: spk57 <3463852+spk57@users.noreply.github.com> Date: Sat, 7 Dec 2024 19:45:39 -0500 Subject: [PATCH 1/4] Added doc/Examples.md --- doc/examples.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 doc/examples.md diff --git a/doc/examples.md b/doc/examples.md new file mode 100644 index 0000000..eb4120d --- /dev/null +++ b/doc/examples.md @@ -0,0 +1,7 @@ +# Examples +Worked examples using GAM.jl from Generalized Additive Models, Simon N. Wood + +## Linear Models + +### Hubble +y ~ x -y From 8c377c379f73029ea0d780df4630e39e0b18a82c Mon Sep 17 00:00:00 2001 From: spk57 <3463852+spk57@users.noreply.github.com> Date: Sat, 7 Dec 2024 22:49:59 -0500 Subject: [PATCH 2/4] Examples, summary init --- doc/examples.md | 19 ++++++++++- grep | 86 +++++++++++++++++++++++++++++++++++++++++++++++ src/GAM.jl | 1 + src/GAMData.jl | 12 ++++++- src/GAMFormula.jl | 3 +- 5 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 grep diff --git a/doc/examples.md b/doc/examples.md index eb4120d..e4378c6 100644 --- a/doc/examples.md +++ b/doc/examples.md @@ -2,6 +2,23 @@ Worked examples using GAM.jl from Generalized Additive Models, Simon N. Wood ## Linear Models +``` +using RDatasets +using GAM +using Plots +``` +### Trees +``` +df=dataset("datasets", "trees") +mod = gam("Volume ~ s(Girth, k=10, degree=3) + s(Height, k=10, degree=3)", df) +``` ### Hubble -y ~ x -y +y ~ x - 1 + +``` +hubble=dataset("gamair", "hubble") +hubMod =gam("y ~ x - 1", hubble) +hubMod1=gam("y ~ x", hubble[Not([3,15]), :]) + +``` \ No newline at end of file diff --git a/grep b/grep new file mode 100644 index 0000000..3529568 --- /dev/null +++ b/grep @@ -0,0 +1,86 @@ +. +./.vscode +./.vscode/settings.json +./doc +./doc/examples.md +./test +./test/Project.toml +./test/runtests.jl +./Project.toml +./README.md +./grep +./Manifest.toml +./src +./src/GAM.jl +./src/dcat.jl +./src/DifferenceMatrix.jl +./src/alpha.jl +./src/FitGAM.jl +./src/diffm.jl +./src/Plots.jl +./src/FitWPS.jl +./src/Predictions.jl +./src/GAMData.jl +./src/PIRLS.jl +./src/GAMFormula.jl +./src/BuildBasis.jl +./src/FitOLS.jl +./src/ModelDiagnostics.jl +./src/Links-Dists.jl +./LICENSE +./.gitignore +./.git +./.git/info +./.git/info/exclude +./.git/index +./.git/objects +./.git/objects/info +./.git/objects/eb +./.git/objects/eb/4120dd653172387cb15d119bd9b9711dbcf04d +./.git/objects/f3 +./.git/objects/f3/f7212cda25031fed2b04ceb41ffdb68189f5c0 +./.git/objects/da +./.git/objects/da/71ec305a605ba1de4657ffbc3beddffb98f464 +./.git/objects/pack +./.git/objects/pack/pack-101bb809d82d5d77673d831d7987be961e216234.rev +./.git/objects/pack/pack-101bb809d82d5d77673d831d7987be961e216234.pack +./.git/objects/pack/pack-101bb809d82d5d77673d831d7987be961e216234.idx +./.git/objects/e6 +./.git/objects/e6/706ce7b74b799565d2639eb78af8c477d7a21c +./.git/refs +./.git/refs/heads +./.git/refs/heads/main +./.git/refs/tags +./.git/refs/remotes +./.git/refs/remotes/origin +./.git/refs/remotes/origin/HEAD +./.git/config +./.git/branches +./.git/FETCH_HEAD +./.git/description +./.git/COMMIT_EDITMSG +./.git/logs +./.git/logs/refs +./.git/logs/refs/heads +./.git/logs/refs/heads/main +./.git/logs/refs/remotes +./.git/logs/refs/remotes/origin +./.git/logs/refs/remotes/origin/HEAD +./.git/logs/HEAD +./.git/packed-refs +./.git/hooks +./.git/hooks/pre-merge-commit.sample +./.git/hooks/pre-rebase.sample +./.git/hooks/sendemail-validate.sample +./.git/hooks/pre-commit.sample +./.git/hooks/post-update.sample +./.git/hooks/update.sample +./.git/hooks/commit-msg.sample +./.git/hooks/pre-push.sample +./.git/hooks/pre-applypatch.sample +./.git/hooks/fsmonitor-watchman.sample +./.git/hooks/pre-receive.sample +./.git/hooks/prepare-commit-msg.sample +./.git/hooks/push-to-checkout.sample +./.git/hooks/applypatch-msg.sample +./.git/HEAD diff --git a/src/GAM.jl b/src/GAM.jl index 47eb1ff..f9f4aaa 100644 --- a/src/GAM.jl +++ b/src/GAM.jl @@ -26,5 +26,6 @@ export GAMData export PartialDependencePlot export plotGAM export gam +export summary end diff --git a/src/GAMData.jl b/src/GAMData.jl index d71fdf1..ca47da8 100644 --- a/src/GAMData.jl +++ b/src/GAMData.jl @@ -29,7 +29,7 @@ mutable struct GAMData CoefIndex::AbstractArray Fitted::AbstractArray Diagnostics::Dict - + #TODO Why is separate constructor needed? spk function GAMData( y::AbstractArray, x::AbstractArray, @@ -44,4 +44,14 @@ mutable struct GAMData ) new(y, x, Basis, Family, Link, Coef, ColMeans, CoefIndex, Fitted, Diagnostics) end +end + +"Print residuals" +printResiduals(data)=println("Residuals: $data[\n ") +"Print summary information of GAMData" +function summary(data::GAMData, out::IO=stdout) + println("Summary:") + println(out, "EDF: $(data.Diagnostics[:EDF])") + println(out, "GCV: $(data.Diagnostics[:GCV])") + println(out, "RSS: $(data.Diagnostics[:RSS])") end \ No newline at end of file diff --git a/src/GAMFormula.jl b/src/GAMFormula.jl index d095e1c..bbba1ba 100644 --- a/src/GAMFormula.jl +++ b/src/GAMFormula.jl @@ -65,7 +65,8 @@ function ParseFormula(formula::String) smooth = true else # no s() wrapping symbol_name = Symbol(component) - k = degree = 0 # Set default values when s() wrapping is absent + k = 10 + degree = 10 # Set default values when s() wrapping is absent smooth = false end push!(df, (symbol_name, k, degree, smooth)) From 888e0f0add04a040f1247386fc339122272cb1e6 Mon Sep 17 00:00:00 2001 From: spk57 <3463852+spk57@users.noreply.github.com> Date: Sun, 8 Dec 2024 06:14:14 -0500 Subject: [PATCH 3/4] Working hubble example, summary function --- doc/examples.md | 10 ++++++++-- src/GAMData.jl | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/doc/examples.md b/doc/examples.md index e4378c6..13d3689 100644 --- a/doc/examples.md +++ b/doc/examples.md @@ -14,11 +14,17 @@ mod = gam("Volume ~ s(Girth, k=10, degree=3) + s(Height, k=10, degree=3)", df) ``` ### Hubble -y ~ x - 1 +y ~ x ``` hubble=dataset("gamair", "hubble") -hubMod =gam("y ~ x - 1", hubble) +hubMod =gam("y ~ x", hubble) hubMod1=gam("y ~ x", hubble[Not([3,15]), :]) +scatter(hubble.x, hubble.y, label="Measured") +scatter!(hubMod.x, hubMod.Fitted, label="Fitted") +scatter(hubble.x, hubble.y, label="Measured") +scatter!(hubMod.x, hubMod.Fitted, label="Fitted") +GAM.summary(hubMod) +plot(fitted(hub.mod), residuals(hub.mod), xlab="fitted values", ylab="residuals") ``` \ No newline at end of file diff --git a/src/GAMData.jl b/src/GAMData.jl index ca47da8..e387c97 100644 --- a/src/GAMData.jl +++ b/src/GAMData.jl @@ -51,6 +51,7 @@ printResiduals(data)=println("Residuals: $data[\n ") "Print summary information of GAMData" function summary(data::GAMData, out::IO=stdout) println("Summary:") + println("Coef $(data.Coef)") println(out, "EDF: $(data.Diagnostics[:EDF])") println(out, "GCV: $(data.Diagnostics[:GCV])") println(out, "RSS: $(data.Diagnostics[:RSS])") From bf4ebd8efa0c42e142248fd822108a8b2d972a73 Mon Sep 17 00:00:00 2001 From: spk57 <3463852+spk57@users.noreply.github.com> Date: Sun, 8 Dec 2024 06:32:33 -0500 Subject: [PATCH 4/4] Cleaned doco, fixed plots --- doc/examples.md | 18 +++++++++++++----- src/GAM.jl | 3 ++- src/GAMData.jl | 8 ++++++-- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/doc/examples.md b/doc/examples.md index 13d3689..a718a7c 100644 --- a/doc/examples.md +++ b/doc/examples.md @@ -14,17 +14,25 @@ mod = gam("Volume ~ s(Girth, k=10, degree=3) + s(Height, k=10, degree=3)", df) ``` ### Hubble +Model:
y ~ x - +#### Load Hubble Dataset ``` hubble=dataset("gamair", "hubble") +``` +#### Fit with GAM +``` hubMod =gam("y ~ x", hubble) +GAM.summary(hubMod) +#Remove outliers hubMod1=gam("y ~ x", hubble[Not([3,15]), :]) +GAM.summary(hubMod1) +``` +#### Plots +``` scatter(hubble.x, hubble.y, label="Measured") scatter!(hubMod.x, hubMod.Fitted, label="Fitted") -scatter(hubble.x, hubble.y, label="Measured") -scatter!(hubMod.x, hubMod.Fitted, label="Fitted") -GAM.summary(hubMod) -plot(fitted(hub.mod), residuals(hub.mod), xlab="fitted values", ylab="residuals") +#Plot fitted data vs residuals +scatter(hubMod.Fitted, residuals(hubMod), label="", xlab="fitted values", ylab="residuals") ``` \ No newline at end of file diff --git a/src/GAM.jl b/src/GAM.jl index f9f4aaa..0284501 100644 --- a/src/GAM.jl +++ b/src/GAM.jl @@ -27,5 +27,6 @@ export PartialDependencePlot export plotGAM export gam export summary - +export residuals +export fitted end diff --git a/src/GAMData.jl b/src/GAMData.jl index e387c97..9b7c0c8 100644 --- a/src/GAMData.jl +++ b/src/GAMData.jl @@ -46,8 +46,12 @@ mutable struct GAMData end end -"Print residuals" -printResiduals(data)=println("Residuals: $data[\n ") +"Calculate residuals from fitted data" +residuals(data)=abs.(data.y - data.Fitted) + +"Return fitted data to match R function" +fitted(data)=data.Fitted + "Print summary information of GAMData" function summary(data::GAMData, out::IO=stdout) println("Summary:")