Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial build #1

Merged
merged 6 commits into from
Dec 5, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/Manifest.toml
/docs/Manifest.toml
/docs/build/
.DS_Store
1,117 changes: 1,117 additions & 0 deletions data/bradypus.tsv

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion src/maxnet.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module Maxnet
module MaxNet
using Lasso
using StatsModels
using Missings

# Write your package code here.

include("utils.jl")
include("terms.jl")
include("maxnet_function.jl")
end
31 changes: 31 additions & 0 deletions src/maxnet_function.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function addsamples(presences, predictors) # this should be a general sdm thing
ret = Int[]
for i in eachindex(presences)
if presences[i]
add = true
for j in eachindex(presences)
if (!presences[j]) && predictors[j, :] == predictors[i, :]
add = false
break
end
end
add && push!(ret, i)
end
end
vcat(presences, zeros(length(ret))), vcat(predictors, predictors[ret, :])
end

function maxnet(presences, predictors,
formula = maxnet_formula(presences, predictors),
regularization_multiplier = 1.0,
regularization_function = maxnet_default_regularization,
addsamplestobackground = true)

disallowmissing!(predictors)
if addsamplestobackground
presences, predictors = addsamples(presences, predictors)
end

#... TODO: This function is not yet finished

end
37 changes: 37 additions & 0 deletions src/terms.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using StatsBase, StatsModels

struct ThresholdTerm{T,N} <: AbstractTerm
term::T
nknots::N
end

thresholds(t::Symbol, nknots::Int) = ThresholdTerm(term(t), term(nknots))

Base.show(io::IO, t::ThresholdTerm) = print(io, "thresholds($(t.term), nknots = $(t.nknots))")

function StatsModels.apply_schema(t::FunctionTerm{typeof(thresholds)},
sch::StatsModels.Schema,
Mod::Type{<:Any})
apply_schema(ThresholdsTerm(t.args_parsed...), sch, Mod)
end

# apply_schema to internal Terms and check for proper types
function StatsModels.apply_schema(t::ThresholdTerm,
sch::StatsModels.Schema,
Mod::Type{<:Any})
term = apply_schema(t.term, sch, Mod)
isa(term, ContinuousTerm) ||
throw(ArgumentError("ThresholdTerm only works with continuous terms (got $term)"))
isa(t.nknots, ConstantTerm) ||
throw(ArgumentError("ThresholdTerm nknots must be a number (got $t.nknots)"))
ThresholdTerm(term, t.nknots.n)
end

StatsModels.terms(p::ThresholdTerm) = terms(p.term)
StatsModels.termvars(p::ThresholdTerm) = StatsModels.termvars(p.term)
StatsModels.width(p::ThresholdTerm) = 1

function StatsModels.modelcols(p::ThresholdTerm, d::NamedTuple)
col = modelcols(p.term, d)
reduce(hcat, [col.^n for n in 1:p.deg])
end
22 changes: 22 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function hinge(x, nknots = 50)
mi, ma = extrema(x)
k = range(mi, ma, length = nknots)
lh = hingeval.(x, k[1:end-1]', [ma])
rh = hingeval.(x, [mi], k[2:end]')
[lh rh]
end
hingeval(x, mi, ma) = clamp((x - mi) / (ma - mi), 0, 1)

function thresholds(x, nknots = 50)
k = range(extrema(x)..., length = nknots + 2)[2:nknots + 1]
Int.(x .>= k')
end
thresholdval(x, knot) = Int.(x .>= knot)

categorical(x) = Int.(x .== unique(x)')
categoricalval(x, category) = Int.(x .== category)

function thresholds(x, nknots = 50)
k = range(extrema(x)..., length = nknots + 2)[2:nknots + 1]
Int.(x .>= k')
end
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,14 @@ using Test
@testset "Maxnet.jl" begin
# Write your tests here.
end

# Test stub based on the R package
using DataFrames, CSV, CategoricalArrays
bradypus = CSV.read("data/bradypus.tsv", DataFrame)
bradypus.ecoreg = categorical(bradypus.ecoreg)
p = bradypus.presence
data = bradypus[:, 2:end]
mod = maxnet(p, data)
plot(mod, type = "cloglog")
mod = amxnet(p, data, maxnet_formula(p, data, classes = "lq"))
plot(mod, "tmp6190_ann")
Loading