Skip to content

Commit

Permalink
Merge pull request #9 from yufongpeng/release-0.2.3
Browse files Browse the repository at this point in the history
Release 0.2.3
  • Loading branch information
yufongpeng committed Mar 19, 2023
2 parents 49589e8 + abe39ae commit 55d35f0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AnovaFixedEffectModels"
uuid = "e4965305-65d6-464d-9c03-ae3e5cffadab"
authors = ["yufongpeng <54415349+yufongpeng@users.noreply.github.com> and contributors"]
version = "0.2.2"
version = "0.2.3"

[deps]
AnovaBase = "946dddda-6a23-4b48-8e70-8e60d9b8d680"
Expand Down
2 changes: 1 addition & 1 deletion src/AnovaFixedEffectModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import StatsBase: fit!, fit
import StatsModels: TableRegressionModel, vectorize, width, apply_schema,
ModelFrame, ModelMatrix, columntable, asgn

using AnovaBase: select_super_interaction, extract_contrasts, canonicalgoodnessoffit, subformula, dof_asgn, lrt_nested, ftest_nested, _diff, _diffn
using AnovaBase: select_super_interaction, extract_contrasts, canonicalgoodnessoffit, subformula, dof_asgn, lrt_nested, ftest_nested, _diff, _diffn, has_intercept
import AnovaBase: anova, nestedmodels, anovatable, prednames, predictors, formula
using Tables: columntable

Expand Down
2 changes: 1 addition & 1 deletion src/anova.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function anova(::Type{FTest},
assign = asgn(predictors(aovm))
fullpred = predictors(aovm.model)
fullasgn = asgn(fullpred)
df = filter(>(0), dof_asgn(assign))
df = tuple(filter(>(0), dof_asgn(assign))...)
# May exist some floating point error from dof_residual
varβ = vcov(aovm.model)
β = aovm.model.coef
Expand Down
22 changes: 21 additions & 1 deletion src/fit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,24 @@ predictors(model::T) where {T <: FixedEffectModel} = model.formula_schema.rhs.te

# Variable dispersion
dof_pred(model::FixedEffectModel) = nobs(model) - dof_residual(model)
# define dof on NestedModels?
# define dof on NestedModels?

isfe(::AbstractTerm) = false
isfe(::FunctionTerm{typeof(fe)}) = true
isfe(term::InteractionTerm) = any(isfe, term.terms)

"""
nestedmodels(modeltype::Type{FixedEffectModel}, f::FormulaTerm, data; null = true, kwargs...)
Generate nested models from modeltype, formula and data. The null model will be an empty model if the keyword argument null is true (default).
"""
function nestedmodels(modeltype::Type{FixedEffectModel}, f::FormulaTerm, data; null = true, kwargs...)
fullm = lfe(f, data; kwargs...)
predterms = predictors(fullm)
has_intercept(predterms) || (length(predterms) > 1 ? (predterms = predterms[2:end]) : throw(ArgumentError("Empty model is given!")))
feterms = filter(isfe, f.rhs)
subm = ntuple(length(predterms) - 1) do i
lfe(FormulaTerm(f.lhs, (predterms[1:i]..., feterms...)), data; kwargs...)
end
NestedModels{FixedEffectModel}(null ? (lfe(FormulaTerm(f.lhs, (ConstantTerm(0), feterms...)), data; kwargs...), subm..., fullm) : (subm..., fullm))
end
4 changes: 2 additions & 2 deletions test/Manifest.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is machine-generated - editing it directly is not advised

julia_version = "1.8.3"
julia_version = "1.8.5"
manifest_format = "2.0"
project_hash = "49564b7d6a8af6473e51894f1112145c59b07d7c"

Expand Down Expand Up @@ -71,7 +71,7 @@ version = "4.6.0"
[[deps.CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
version = "0.5.2+0"
version = "1.0.1+0"

[[deps.Crayons]]
git-tree-sha1 = "249fe38abf76d48563e2f4556bebd215aa317e15"
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,12 @@ isapprox(x::NTuple{N, Float64}, y::NTuple{N, Float64}, atol::NTuple{N, Float64}
@test isapprox(first(teststat(aovf2)), first(teststat(aovl2)))
@test isapprox(teststat(aovfs)[2], teststat(aovfs2)[2])
end
@testset "nestedmodels" begin
df = DataFrame(y = randn(1000), x = rand(1:5, 1000), z = rand(["1", "2"], 1000), t = 1:1000)
fems1 = nestedmodels(FixedEffectModel, @formula(y ~ t + fe(z) + fe(x)), df)
fems2 = nestedmodels(FixedEffectModel, @formula(y ~ z + t & fe(x)), df)
@test formula(fems1.model[1]).rhs == @formula(y ~ 0 + fe(z) + fe(x)).rhs
@test AFE.predictors(fems2.model[2])[1] == InterceptTerm{true}()
end
end
end

2 comments on commit 55d35f0

@yufongpeng
Copy link
Owner Author

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/79886

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.2.3 -m "<description of version>" 55d35f0f425001f5f28be6b2151bb77f7c3c6f6c
git push origin v0.2.3

Please sign in to comment.