@@ -12,8 +12,7 @@ using Compat
12
12
# - support more transformations with I()?
13
13
14
14
# # Formula parsing
15
- import StatsModels: @~ , Formula
16
- import StatsModels. Terms
15
+ import StatsModels: @formula , Formula, Terms
17
16
18
17
# # totally empty
19
18
t = Terms (Formula (nothing , 0 ))
@@ -23,87 +22,90 @@ t = Terms(Formula(nothing, 0))
23
22
@test t. eterms == []
24
23
25
24
# # empty RHS
26
- t = Terms (y ~ 0 )
25
+ t = Terms (@formula ( y ~ 0 ) )
27
26
@test t. intercept == false
28
27
@test t. terms == []
29
28
@test t. eterms == [:y ]
30
- t = Terms (y ~ - 1 )
29
+ t = Terms (@formula ( y ~ - 1 ) )
31
30
@test t. intercept == false
32
31
@test t. terms == []
33
32
34
33
# # intercept-only
35
- t = Terms (y ~ 1 )
34
+ t = Terms (@formula ( y ~ 1 ) )
36
35
@test t. response == true
37
36
@test t. intercept == true
38
37
@test t. terms == []
39
38
@test t. eterms == [:y ]
40
39
41
40
# # terms add
42
- t = Terms (y ~ 1 + x1 + x2)
41
+ t = Terms (@formula ( y ~ 1 + x1 + x2) )
43
42
@test t. intercept == true
44
43
@test t. terms == [:x1 , :x2 ]
45
44
@test t. eterms == [:y , :x1 , :x2 ]
46
45
47
46
# # implicit intercept behavior:
48
- t = Terms (y ~ x1 + x2)
47
+ t = Terms (@formula ( y ~ x1 + x2) )
49
48
@test t. intercept == true
50
49
@test t. terms == [:x1 , :x2 ]
51
50
@test t. eterms == [:y , :x1 , :x2 ]
52
51
53
52
# # no intercept
54
- t = Terms (y ~ 0 + x1 + x2)
53
+ t = Terms (@formula ( y ~ 0 + x1 + x2) )
55
54
@test t. intercept == false
56
55
@test t. terms == [:x1 , :x2 ]
57
56
58
- @test t == Terms (y ~ - 1 + x1 + x2) == Terms (y ~ x1 - 1 + x2) == Terms (y ~ x1 + x2 - 1 )
57
+ @test t == Terms (@formula ( y ~ - 1 + x1 + x2)) == Terms (@formula ( y ~ x1 - 1 + x2)) == Terms (@formula ( y ~ x1 + x2 - 1 ) )
59
58
60
59
# # can't subtract terms other than 1
61
- @test_throws ErrorException Terms (y ~ x1 - x2)
60
+ @test_throws ErrorException Terms (@formula ( y ~ x1 - x2) )
62
61
63
- t = Terms (y ~ x1 & x2)
62
+ t = Terms (@formula ( y ~ x1 & x2) )
64
63
@test t. terms == [:(x1 & x2)]
65
64
@test t. eterms == [:y , :x1 , :x2 ]
66
65
67
66
# # `*` expansion
68
- t = Terms (y ~ x1 * x2)
67
+ t = Terms (@formula ( y ~ x1 * x2) )
69
68
@test t. terms == [:x1 , :x2 , :(x1 & x2)]
70
69
@test t. eterms == [:y , :x1 , :x2 ]
71
70
72
71
# # associative rule:
73
72
# # +
74
- t = Terms (y ~ x1 + x2 + x3)
73
+ t = Terms (@formula ( y ~ x1 + x2 + x3) )
75
74
@test t. terms == [:x1 , :x2 , :x3 ]
76
75
77
76
# # &
78
- t = Terms (y ~ x1 & x2 & x3)
77
+ t = Terms (@formula ( y ~ x1 & x2 & x3) )
79
78
@test t. terms == [:((& )(x1, x2, x3))]
80
79
@test t. eterms == [:y , :x1 , :x2 , :x3 ]
81
80
82
81
# # distributive property of + and &
83
- t = Terms (y ~ x1 & (x2 + x3))
82
+ t = Terms (@formula ( y ~ x1 & (x2 + x3) ))
84
83
@test t. terms == [:(x1& x2), :(x1& x3)]
85
84
86
85
# # FAILS: ordering of expanded interaction terms is wrong
87
86
# # (only has an observable effect when both terms are categorical and
88
87
# # produce multiple model matrix columns that are multiplied together...)
89
88
# #
90
- # # t = Terms(y ~ (x2 + x3) & x1)
89
+ # # t = Terms(@formula( y ~ (x2 + x3) ) & x1)
91
90
# # @test t.terms == [:(x2&x1), :(x3&x1)]
92
91
93
92
# # three-way *
94
- t = Terms (y ~ x1 * x2 * x3)
93
+ t = Terms (@formula ( y ~ x1 * x2 * x3) )
95
94
@test t. terms == [:x1 , :x2 , :x3 ,
96
95
:(x1& x2), :(x1& x3), :(x2& x3),
97
96
:((& )(x1, x2, x3))]
98
97
@test t. eterms == [:y , :x1 , :x2 , :x3 ]
99
98
100
99
# # Interactions with `1` reduce to main effect. All fail at the moment.
101
- # # t = Terms(y ~ 1 & x1)
100
+ # # t = Terms(@formula( y ~ 1 & x1) )
102
101
# # @test t.terms == [:x1] # == [:(1 & x1)]
103
102
# # @test t.eterms == [:y, :x1]
104
103
105
- # # t = Terms(y ~ (1 + x1) & x2)
104
+ # # t = Terms(@formula( y ~ (1 + x1) ) & x2)
106
105
# # @test t.terms == [:x2, :(x1&x2)] # == [:(1 & x1)]
107
106
# # @test t.eterms == [:y, :x1, :x2]
108
107
108
+ # Incorrect formula separator
109
+ @test_throws ErrorException eval (:(@formula (y => x + 1 )))
110
+
109
111
end
0 commit comments