Skip to content

Commit c39e62b

Browse files
authored
fix up nesting (slash) syntax and add tests for fixefs too (#257)
* fix nesting syntax in formula * add tests and use FullRank schema to make sure promotion works
1 parent f373812 commit c39e62b

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/randomeffectsterm.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ function StatsModels.apply_schema(
2727
if length(t.args_parsed) 2
2828
throw(ArgumentError("malformed nesting term: $t (Exactly two arguments required"))
2929
end
30-
first, second = apply_schema.(t.args_parsed, Ref(sch.schema), Mod)
31-
return first + first & second
30+
31+
first, second = apply_schema.(t.args_parsed, Ref(sch), Mod)
32+
33+
if !(typeof(first) <: CategoricalTerm)
34+
throw(ArgumentError("nesting terms requires categorical grouping term, got $first. Manually specify $first as `CategoricalTerm` in hints/contrasts"))
35+
end
36+
37+
return first + fulldummy(first) & second
3238
end
3339

3440
RandomEffectsTerm(lhs, rhs::NTuple{2,AbstractTerm}) =

test/FactorReTerm.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,20 @@ end
175175

176176
@test_broken fit(MixedModel, @formula(Y ~ 1 + (1|H/c)), dat[:Pastes])
177177

178+
# in fixed effects:
179+
d2 = (a = rand(20), b = repeat([:X, :Y], outer=10), c = repeat([:S,:T],outer=10))
180+
f2 = apply_schema(@formula(0 ~ 1 + b/a), schema(d2), MixedModel)
181+
@test modelcols(f2.rhs, d2) == [ones(20) d2.b .== :Y (d2.b .== :X).*d2.a (d2.b .== :Y).*d2.a]
182+
@test coefnames(f2.rhs) == ["(Intercept)", "b: Y", "b: X & a", "b: Y & a"]
183+
184+
f3 = apply_schema(@formula(0 ~ 0 + b/a), schema(d2), MixedModel)
185+
@test modelcols(f3.rhs, d2) == [d2.b .== :X d2.b .== :Y (d2.b .== :X).*d2.a (d2.b .== :Y).*d2.a]
186+
@test coefnames(f3.rhs) == ["b: X", "b: Y", "b: X & a", "b: Y & a"]
187+
188+
# errors for continuous grouping
189+
@test_throws ArgumentError apply_schema(@formula(0 ~ 1 + a/b), schema(d2), MixedModel)
190+
191+
# errors for too much nesting
192+
@test_throws ArgumentError apply_schema(@formula(0 ~ 1 + b/c/a), schema(d2), MixedModel)
178193
end
179194
end

0 commit comments

Comments
 (0)