aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKen Kellner <ken@kenkellner.com>2020-03-23 12:11:33 -0400
committerKen Kellner <ken@kenkellner.com>2020-03-23 12:11:33 -0400
commit873ad366d0ee70e0c20dbb66f0eebb66f619ddec (patch)
tree38f89be0aceedcfab0db33eb4c667065acedf0e8
parentcebd06f9bca0ae36ddfceaecd629ccd3a2a4989c (diff)
Adjustments to utility functions and compatibility with Julia 1.4
-rw-r--r--.travis.yml2
-rw-r--r--src/design.jl10
-rw-r--r--src/nmix.jl20
-rw-r--r--src/occupancy.jl6
-rw-r--r--src/utils.jl28
-rw-r--r--test/test_design.jl1
6 files changed, 50 insertions, 17 deletions
diff --git a/.travis.yml b/.travis.yml
index ade2244..d8a0029 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -2,6 +2,6 @@ language: julia
julia:
- nightly
- - 1.3
+ - 1.4
codecov: true
diff --git a/src/design.jl b/src/design.jl
index 2d1a309..0b57d20 100644
--- a/src/design.jl
+++ b/src/design.jl
@@ -52,7 +52,7 @@ function add_levels(df::DataFrame)
nlev = [n_levels(col) for col = eachcol(df)]
maxlev = max(nlev...)
ndf = DataFrame([rep_levels(col, maxlev) for col = eachcol(df)])
- names!(ndf, names(df))
+ rename!(ndf, names(df))
return ndf
end
@@ -84,7 +84,13 @@ function add_idx!(dm::Array{UmDesign})
x.idx = idx:(idx+np-1)
idx += np
end
- return idx - 1
+ return nothing
+ #return idx - 1
+end
+
+#Get total parameters
+function get_np(dm::Array{UmDesign})
+ sum(map(x -> length(x.coefnames), dm))
end
#Transform linear predictor back to response scale
diff --git a/src/nmix.jl b/src/nmix.jl
index dd7e54d..8e1c651 100644
--- a/src/nmix.jl
+++ b/src/nmix.jl
@@ -6,7 +6,7 @@ struct Nmix <: UnmarkedModel
end
"""
- nmix(λ_formula::FormulaTerm, p_formula::FormulaTerm, data::UmData, K::Int)
+ nmix(λ_formula::FormulaTerm, p_formula::FormulaTerm, data::UmData; K::Int)
Fit single-season N-mixture models. Covariates on abundance and detection
are specified with `λ_formula` and `p_formula`, respectively. The `UmData`
@@ -17,15 +17,17 @@ enough that the likelihood of true abundance `K` for any site is ≈ 0 (defaults
to maximum observed count + 20). Larger `K` will increase runtime.
"""
function nmix(λ_formula::FormulaTerm, p_formula::FormulaTerm,
- data::UmData, K::Union{Int,Nothing}=nothing)
+ data::UmData; K::Union{Int,Nothing}=nothing)
abun = UmDesign(:Abundance, λ_formula, LogLink(), data.site_covs)
det = UmDesign(:Detection, p_formula, LogitLink(), data.obs_covs)
- np = add_idx!([abun, det])
+ add_idx!([abun, det])
+ np = get_np([abun, det])
y = data.y
N, J = size(y)
- K = isnothing(K) ? maximum(skipmissing(y)) + 20 : K
+ K = check_K(K, y)
+ Kmin = get_Kmin(y)
function loglik(β::Array)
@@ -43,10 +45,7 @@ function nmix(λ_formula::FormulaTerm, p_formula::FormulaTerm,
end
fg = zeros(eltype(β),K+1)
- for k = 0:K
- if maximum(skipmissing(y[i,:])) > k
- continue
- end
+ for k = Kmin[i]:K
fk = pdf(Poisson(λ[i]), k)
gk = 1.0
for j = 1:J
@@ -118,7 +117,8 @@ function simulate(::Type{Nmix}, λ_form::FormulaTerm, p_form::FormulaTerm,
abun = UmDesign(:Abun, λ_form, LogLink(), sc)
det = UmDesign(:Det, p_form, LogitLink(), oc)
- np = add_idx!([abun, det])
+ add_idx!([abun, det])
+ np = get_np([abun, det])
if np != length(coef) error(string("Coef array must be length ",np)) end
@@ -150,7 +150,7 @@ end
# Update ----------------------------------------------------------------------
function update(fit::Nmix, data::UmData)
- nmix(abundance(fit).formula, detection(fit).formula, data, fit.K)
+ nmix(abundance(fit).formula, detection(fit).formula, data, K=fit.K)
end
# Goodness-of-fit -------------------------------------------------------------
diff --git a/src/occupancy.jl b/src/occupancy.jl
index 39fccca..c08a05f 100644
--- a/src/occupancy.jl
+++ b/src/occupancy.jl
@@ -16,7 +16,8 @@ function occu(ψ_form::FormulaTerm, p_form::FormulaTerm, data::UmData)
occ = UmDesign(:Occupancy, ψ_form, LogitLink(), data.site_covs)
det = UmDesign(:Detection, p_form, LogitLink(), data.obs_covs)
- np = add_idx!([occ, det])
+ add_idx!([occ, det])
+ np = get_np([occ, det])
y = data.y
N, J = size(y)
@@ -96,7 +97,8 @@ function simulate(::Type{Occu}, ψ_form::FormulaTerm, p_form::FormulaTerm,
occ = UmDesign(:Occ, ψ_form, LogitLink(), sc)
det = UmDesign(:Det, p_form, LogitLink(), oc)
- np = add_idx!([occ, det])
+ add_idx!([occ, det])
+ np = get_np([occ, det])
if np != length(coef) error(string("Coef array must be length ",np)) end
diff --git a/src/utils.jl b/src/utils.jl
index d4d719b..946e224 100644
--- a/src/utils.jl
+++ b/src/utils.jl
@@ -6,13 +6,13 @@ function ndetects(y::Array)
mapslices(sum2, y, dims=2)
end
-#Simulate random covariate frame based on provided formula"
+#Simulate random covariate frame based on provided formula
function gen_covs(f::FormulaTerm, n::Int)
covs = varnames(f).rhs
if isnothing(covs) return DataFrame(_dummy=ones(n)) end
nc = length(covs)
out = DataFrame(reshape(rand(Normal(0,1), n*nc), n, nc))
- DataFrames.names!(out, covs)
+ DataFrames.rename!(out, covs)
out
end
@@ -22,3 +22,27 @@ function rep_missing!(ynew::Array, y::Array)
if length(na_idx) == 0 return nothing end
ynew[na_idx] = fill(missing, length(na_idx))
end
+
+#Get appropriate value of K
+function check_K(K::Int, y::Array{Union{Missing,Int},2})
+ if(K < (maximum(skipmissing(y))))
+ error("K should be larger than the maximum observed abundance")
+ end
+ K
+end
+
+function check_K(K::Nothing, y::Array{Union{Missing,Int},2})
+ maximum(skipmissing(y)) + 20
+end
+
+#Get minimum bound for integrating over possible abundance values K
+#Minimum bound = maximum abundance ever observed at a site
+function get_Kmin(y::Array{Union{Missing,Int64},2})
+ N = size(y, 1)
+ kmin = zeros(Int64, N)
+ for n in 1:N
+ if(all(ismissing.(y[n, :]))) continue end
+ kmin[n] = maximum(skipmissing(y[n, :]))
+ end
+ kmin
+end
diff --git a/test/test_design.jl b/test/test_design.jl
index c4518c7..9176585 100644
--- a/test/test_design.jl
+++ b/test/test_design.jl
@@ -26,6 +26,7 @@
Unmarked.add_idx!([umd, umd2])
@test isequal(umd.idx, 1:3)
@test isequal(umd2.idx, 4:4)
+ @test isequal(Unmarked.get_np([umd, umd2]), 4)
#Test transform
tr = logistic.(umd.mat * [0,0.3,0.5])