|
| 1 | +""" |
| 2 | + LikelihoodRatioTest |
| 3 | +
|
| 4 | +Results of MixedModels.likelihoodratiotest |
| 5 | +
|
| 6 | +## Fields |
| 7 | +* `formulas`: Vector of model formulae |
| 8 | +* `models`: NamedTuple of the `dof` and `deviance` of the models |
| 9 | +* `tests`: NamedTuple of the sequential `dofdiff`, `deviancediff`, and resulting `pvalues` |
| 10 | +
|
| 11 | +## Properties |
| 12 | +* `deviance` |
| 13 | +* `pvalues` |
| 14 | +
|
| 15 | +""" |
| 16 | +struct LikelihoodRatioTest |
| 17 | + formulas::AbstractVector{String} |
| 18 | + models::NamedTuple{(:dof,:deviance)} |
| 19 | + tests::NamedTuple{(:dofdiff,:deviancediff,:pvalues)} |
| 20 | +end |
| 21 | + |
| 22 | +Base.propertynames(lrt::LikelihoodRatioTest, private = false) = ( |
| 23 | + :deviance, |
| 24 | + :formulas, |
| 25 | + :models, |
| 26 | + :pvalues, |
| 27 | + :tests, |
| 28 | +) |
| 29 | + |
| 30 | +function Base.getproperty(lrt::LikelihoodRatioTest, s::Symbol) |
| 31 | + if s == :dof |
| 32 | + lrt.models.dof |
| 33 | + elseif s == :deviance |
| 34 | + lrt.models.deviance |
| 35 | + elseif s == :pvalues |
| 36 | + lrt.tests.pvalues |
| 37 | + elseif s == :formulae |
| 38 | + lrt.formulas |
| 39 | + else |
| 40 | + getfield(lrt, s) |
| 41 | + end |
| 42 | +end |
| 43 | + |
| 44 | +# backward syntactic but not type compatibility |
| 45 | +Base.getindex(lrt::LikelihoodRatioTest, s::Symbol) = getfield(lrt,s) |
| 46 | + |
| 47 | +""" |
| 48 | + likelihoodratiotest(m::LinearMixedModel...) |
| 49 | +
|
| 50 | +Likeihood ratio test applied to a set of nested models. |
| 51 | +
|
| 52 | +Note that nesting of the models is not checked. It is incumbent on the user to check this. |
| 53 | +""" |
| 54 | +function likelihoodratiotest(m::LinearMixedModel...) |
| 55 | + if any(getproperty.(getproperty.(m,:optsum),:REML)) |
| 56 | + reduce(==,coefnames.(m)) || |
| 57 | + throw(ArgumentError("Likelihood-ratio tests for REML-fitted models are only valid when the fixed-effects specifications are identical")) |
| 58 | + end |
| 59 | + m = collect(m) # change the tuple to an array |
| 60 | + dofs = dof.(m) |
| 61 | + formulas = String.(Symbol.(getproperty.(m,:formula))) |
| 62 | + ord = sortperm(dofs) |
| 63 | + dofs = dofs[ord] |
| 64 | + formulas = formulas[ord] |
| 65 | + devs = objective.(m)[ord] |
| 66 | + dofdiffs = diff(dofs) |
| 67 | + devdiffs = .-(diff(devs)) |
| 68 | + pvals = ccdf.(Chisq.(dofdiffs), devdiffs) |
| 69 | + |
| 70 | + LikelihoodRatioTest( |
| 71 | + formulas, |
| 72 | + (dof = dofs, deviance = devs), |
| 73 | + (dofdiff = dofdiffs, deviancediff = devdiffs, pvalues = pvals) |
| 74 | + ) |
| 75 | +end |
| 76 | + |
| 77 | +function _array_union_nothing(arr::Array{T}) where T |
| 78 | + Array{Union{T,Nothing}}(arr) |
| 79 | +end |
| 80 | + |
| 81 | +function _prepend_0(arr::Array{T}) where T |
| 82 | + pushfirst!(copy(arr), -zero(T)) |
| 83 | +end |
| 84 | + |
| 85 | +function Base.show(io::IO, lrt::LikelihoodRatioTest; digits=2) |
| 86 | + println(io, "Model Formulae") |
| 87 | + |
| 88 | + for (i, f) in enumerate(lrt.formulas) |
| 89 | + println("$i: $f") |
| 90 | + end |
| 91 | + cols = hcat(lrt.models.dof, lrt.models.deviance, |
| 92 | + _prepend_0(lrt.tests.deviancediff), |
| 93 | + _prepend_0(lrt.tests.dofdiff), |
| 94 | + _prepend_0(lrt.tests.pvalues)) |
| 95 | + |
| 96 | + ct = CoefTable( |
| 97 | + cols, # cols |
| 98 | + ["model-dof", "deviance", "χ²", "χ²-dof", "P(>χ²)"], # colnms |
| 99 | + string.(1:length(lrt.formulas)), # rownms |
| 100 | + 5, # pvalcol |
| 101 | + 3 # teststatcol |
| 102 | + ) |
| 103 | + show(io, ct) |
| 104 | + |
| 105 | + nothing |
| 106 | +end |
0 commit comments