diff --git a/doc/examples.md b/doc/examples.md new file mode 100644 index 0000000..a718a7c --- /dev/null +++ b/doc/examples.md @@ -0,0 +1,38 @@ +# Examples +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 +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") +#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/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..0284501 100644 --- a/src/GAM.jl +++ b/src/GAM.jl @@ -26,5 +26,7 @@ export GAMData export PartialDependencePlot export plotGAM export gam - +export summary +export residuals +export fitted end diff --git a/src/GAMData.jl b/src/GAMData.jl index d71fdf1..9b7c0c8 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,19 @@ mutable struct GAMData ) new(y, x, Basis, Family, Link, Coef, ColMeans, CoefIndex, Fitted, Diagnostics) end +end + +"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:") + println("Coef $(data.Coef)") + 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))