@@ -91,6 +91,43 @@ using DataStructures
9191 @test_throws InexactError apply_schema (bad_f, schema (bad_f, df))
9292 end
9393 end # Unhappy Path testset
94+
95+ @testset " Programmatic construction" begin
96+ using StatsModels: LeadLagTerm
97+
98+ df = (y= 1 : 10 , x= 1 : 10 )
99+
100+ @testset " schema" begin
101+ t = lag (term (:x ))
102+ @test schema (t, df). schema == schema (term (:x ), df). schema
103+ end
104+
105+ @testset " one-arg" begin
106+ f = @formula (y ~ lag (x))
107+ sch = schema (f, df)
108+ ff = apply_schema (f, sch)
109+ t1 = ff. rhs. terms[1 ]
110+ t2 = apply_schema (LeadLagTerm {Term, typeof(lag)} (term (:x ), 1 ), sch)
111+ t3 = apply_schema (lag (term (:x )), sch)
112+
113+ @test isequal (modelcols (t1, df), modelcols (t2, df))
114+ @test isequal (modelcols (t1, df), modelcols (t3, df))
115+ @test coefnames (t1) == coefnames (t2) == coefnames (t3)
116+ end
117+
118+ @testset " two-arg" begin
119+ f = @formula (y ~ lag (x, 3 ))
120+ sch = schema (f, df)
121+ ff = apply_schema (f, sch)
122+ t1 = ff. rhs. terms[1 ]
123+ t2 = apply_schema (LeadLagTerm {Term, typeof(lag)} (term (:x ), 3 ), sch)
124+ t3 = apply_schema (lag (term (:x ), 3 ), sch)
125+
126+ @test isequal (modelcols (t1, df), modelcols (t2, df))
127+ @test isequal (modelcols (t1, df), modelcols (t3, df))
128+ @test coefnames (t1) == coefnames (t2) == coefnames (t3)
129+ end
130+ end
94131 end # Lag testset
95132
96133 # The code for lag and lead is basically the same, as we tested lag comprehensively above
@@ -109,5 +146,39 @@ using DataStructures
109146
110147 @test coefnames (f)[2 ] == [" x_lead0" , " x_lead1" , " x_lead3" , " x_lead11" ]
111148 end
149+
150+ @testset " Programmatic construction" begin
151+ using StatsModels: LeadLagTerm
152+
153+ df = (y= 1 : 10 , x= 1 : 10 )
154+
155+ @testset " one-arg" begin
156+ f = @formula (y ~ lead (x))
157+ sch = schema (f, df)
158+ ff = apply_schema (f, sch)
159+ t1 = ff. rhs. terms[1 ]
160+ t2 = apply_schema (LeadLagTerm {Term, typeof(lead)} (term (:x ), 1 ), sch)
161+ t3 = apply_schema (lead (term (:x )), sch)
162+
163+ @test isequal (modelcols (t1, df), modelcols (t2, df))
164+ @test isequal (modelcols (t1, df), modelcols (t3, df))
165+ @test coefnames (t1) == coefnames (t2) == coefnames (t3)
166+ end
167+
168+ @testset " two-arg" begin
169+ f = @formula (y ~ lead (x, 3 ))
170+ sch = schema (f, df)
171+ ff = apply_schema (f, sch)
172+ t1 = ff. rhs. terms[1 ]
173+ t2 = apply_schema (LeadLagTerm {Term, typeof(lead)} (term (:x ), 3 ), sch)
174+ t3 = apply_schema (lead (term (:x ), 3 ), sch)
175+
176+ @test isequal (modelcols (t1, df), modelcols (t2, df))
177+ @test isequal (modelcols (t1, df), modelcols (t3, df))
178+ @test coefnames (t1) == coefnames (t2) == coefnames (t3)
179+ end
180+ end
181+
182+
112183 end
113184end
0 commit comments