Skip to content
Open
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
38 changes: 38 additions & 0 deletions doc/examples.md
Original file line number Diff line number Diff line change
@@ -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: </br>
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")

```
86 changes: 86 additions & 0 deletions grep
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion src/GAM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ export GAMData
export PartialDependencePlot
export plotGAM
export gam

export summary
export residuals
export fitted
end
17 changes: 16 additions & 1 deletion src/GAMData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
3 changes: 2 additions & 1 deletion src/GAMFormula.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down