Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
PharmCat committed Jan 9, 2021
1 parent 8bd70e6 commit d49efdf
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
src/temp.jl
#test/precompile/
test/precompile/
test/devtest.jl
examples/
docs/build/
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Metida"
uuid = "a1dec852-9fe5-11e9-361f-8d9fde67cfa2"
authors = ["Vladimir Arnautov <[email protected]>"]
version = "0.1.9"
version = "0.1.10"

[deps]
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
Expand All @@ -23,7 +23,7 @@ Optim = "0.19, 0.20, 0.21, 0.22, 1.0, 1.1, 1.2"
StatsBase = "0.29, 0.30, 0.31, 0.32, 0.33"
StatsModels = "0.6"
Tables = "1"
julia = "1.1, 1.2, 1.3, 1.4, 1.5"
julia = "1.1, 1.2, 1.3, 1.4, 1.5, 1.6"

[extras]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ categorical!(df, :formulation);
lmm = LMM(@formula(var~sequence+period+formulation), df;
random = VarEffect(@covstr(formulation), CSH),
repeated = VarEffect(@covstr(formulation), VC),
repeated = VarEffect(@covstr(formulation), DIAG),
subject = :subject)
fit!(lmm)
Expand Down
47 changes: 25 additions & 22 deletions src/fit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Fit LMM model.
"""
function fit!(lmm::LMM{T}; verbose::Symbol = :auto, varlinkf = :exp, rholinkf = :sigm) where T
function fit!(lmm::LMM{T}; verbose::Symbol = :auto, varlinkf = :exp, rholinkf = :sigm, aifirst = false) where T

#Make varlink function
fv = varlinkvec(lmm.covstr.ct)
Expand Down Expand Up @@ -41,32 +41,35 @@ function fit!(lmm::LMM{T}; verbose::Symbol = :auto, varlinkf = :exp, rholinkf =
for i = 1:length(θ)
if lmm.covstr.ct[i] == :rho θ[i] = 0.0 end
end

push!(lmm.log, "Initial θ: "*string(θ))
#Initial step with modified Newton method
############################################################################
aif(x) = optfunc(lmm, x)[4]
grf(x) = optfunc(lmm, x)[1]
ai = ForwardDiff.hessian(aif, θ)
gr = ForwardDiff.gradient(grf, θ)
println("vec: ", θ)
try
θ .-= (inv(ai) ./4 )*gr
catch
θ .-= (pinv(ai) ./4 )*gr
end
for i = 1:length(θ)
if lmm.covstr.ct[i] == :rho
if θ[i] > 0.99
θ[i] = 0.9
elseif θ[i] < 0.0
θ[i] = 0.0
if aifirst
aif(x) = optfunc(lmm, x)[4]
grf(x) = optfunc(lmm, x)[1]
ai = ForwardDiff.hessian(aif, θ)
gr = ForwardDiff.gradient(grf, θ)
#println("vec: ", θ)
try
θ .-= (inv(ai) ./4 )*gr
catch
θ .-= (pinv(ai) ./4 )*gr
end
for i = 1:length(θ)
if lmm.covstr.ct[i] == :rho
if θ[i] > 0.99
θ[i] = 0.9
elseif θ[i] < 0.0
θ[i] = 0.0
end
else
if θ[i] < 0.01 θ[i] = initθ / 2 end
if θ[i] > initθ*1.25 θ[i] = initθ*1.25 end
end
else
if θ[i] < 0.01 θ[i] = initθ / 2 end
if θ[i] > initθ*1.25 θ[i] = initθ*1.25 end
end
push!(lmm.log, "First step with AI like method, θ: "*string(θ))
end
println("new: ", θ)
#println("new: ", θ)
#varlinkvecapply!(θ, fvr)
varlinkrvecapply2!(θ, lmm.covstr.ct)
#Twice differentiable object
Expand Down
6 changes: 3 additions & 3 deletions src/reml.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ end
"""
-2 log Restricted Maximum Likelihood; β calculation inside
"""
function reml_sweep_β(lmm::LMM{T2}, θ::Vector{T}) where T <: Number where T2 <: Number
function reml_sweep_β(lmm::LMM{T2}, @nospecialize θ::Vector{T}) where T <: Number where T2 <: Number
n::Int = length(lmm.data.block)
N::Int = length(lmm.data.yv)
G::Matrix{T} = gmat_base(θ, lmm.covstr)
Expand Down Expand Up @@ -95,7 +95,7 @@ function reml_sweep_β(lmm::LMM{T2}, θ::Vector{T}) where T <: Number where T2 <
return θ₁ + logdetθ₂ + θ₃ + c, β, θ₂, θ₃
end

function reml_sweep_β2(lmm::LMM{T2}, θ::Vector{T}) where T <: Number where T2 <: Number
function reml_sweep_β2(lmm::LMM{T2}, @nospecialize θ::Vector{T}) where T <: Number where T2 <: Number
n::Int = length(lmm.data.block)
N::Int = length(lmm.data.yv)
#G::Matrix{T} = gmat_base(θ, lmm.covstr)
Expand Down Expand Up @@ -150,7 +150,7 @@ function reml_sweep_β2(lmm::LMM{T2}, θ::Vector{T}) where T <: Number where T2
return θ₁ + logdetθ₂ + θ₃ + c, β, θ₂, θ₃
end

function reml_sweep_β3(lmm::LMM{T2}, θ::Vector{T}) where T <: Number where T2 <: Number
function reml_sweep_β3(lmm::LMM{T2}, @nospecialize θ::Vector{T}) where T <: Number where T2 <: Number
n::Int = length(lmm.data.block)
N::Int = length(lmm.data.yv)
G::Matrix{T} = gmat_base(θ, lmm.covstr)
Expand Down
18 changes: 12 additions & 6 deletions test/berds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ remlsb = [536.2011487949176,-28.588171019268458,436.5116765557768,316.0505671599
SPSS
REML G - CSH, R - DIAG
13 - 2087.4810139727 / 2087.481017533285
15 - / 2087.481017533285
15 - / 2087.481017533285 / 2087.4810192819373
16 - 323.99766668073 / 323.9976737508006
29 - / 26.96606070210349
30 - 14.944038069158
Expand All @@ -37,19 +37,25 @@ for i = 1:30
df.lnpk = log.(df.PK)

@testset " Test $(i)" begin
atol=1E-6

lmm = Metida.LMM(@formula(lnpk~sequence+period+treatment), df;
random = Metida.VarEffect(Metida.@covstr(treatment), Metida.CSH),
repeated = Metida.VarEffect(Metida.@covstr(treatment), Metida.DIAG),
random = Metida.VarEffect(Metida.@covstr(1), Metida.SI),
subject = :subject
)
Metida.fit!(lmm)
@test lmm.result.reml remls[i] atol=1E-6
@test lmm.result.reml remlsb[i] atol=atol


if i == 13 || i == 15 atol = 1E-4 end
lmm = Metida.LMM(@formula(lnpk~sequence+period+treatment), df;
random = Metida.VarEffect(Metida.@covstr(1), Metida.SI),
random = Metida.VarEffect(Metida.@covstr(treatment), Metida.CSH),
repeated = Metida.VarEffect(Metida.@covstr(treatment), Metida.DIAG),
subject = :subject
)
Metida.fit!(lmm)
@test lmm.result.reml remlsb[i] atol=1E-6
@test lmm.result.reml remls[i] atol=atol


end
end
10 changes: 10 additions & 0 deletions test/snoopy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ end
data = SnoopCompile.read("$path/precompile/metida_compiles.log")
pc = SnoopCompile.parcel(reverse!(data[2]))
SnoopCompile.write("$path/precompile", pc)


using SnoopCompileCore
invalidations = @snoopr begin
using Metida
end
using SnoopCompile
ui = uinvalidated(invalidations)
trees = invalidation_trees(invalidations)
ftrees = filtermod(Metida, trees)
1 change: 1 addition & 0 deletions test/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,4 @@ end
include("ar.jl")
include("lme4.jl")
include("norand.jl")
include("berds.jl")

2 comments on commit d49efdf

@PharmCat
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/27638

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.10 -m "<description of version>" d49efdfc0f956f0b8962f9e732304946e26d87cf
git push origin v0.1.10

Please sign in to comment.