# Bug in StatsModels.modelmatrix()

**URL:** <https://discourse.julialang.org/t/bug-in-statsmodels-modelmatrix/69428>\
**Category:** Data\
**Created:** [October 8, 2021, 12:29pm UTC](https://discourse.julialang.org/t/bug-in-statsmodels-modelmatrix/69428 "2021-10-08T12:29:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [October 8, 2021, 12:29pm UTC](https://discourse.julialang.org/t/bug-in-statsmodels-modelmatrix/69428/1 "2021-10-08T12:29:02Z")

</div>

as per below, `modelmatrix()` (also `modelcols()`) fails when the formula is “quadratic” rather than “linear”:

```julia
using DataFrames
using GLM
df = DataFrame(a = 1.0:3.0, b = 2.0:4.0, y = 5.0:7.0)
fml1 = @formula(y ~ 1 + a + b);
fml2 = @formula(y ~ 1 + a * (a + b) + b * b);
modelmatrix(fml1, df); # fine
modelmatrix(fml2, df);
ERROR: MethodError: no method matching iterate(::ContinuousTerm{Float64})
Closest candidates are:
  iterate(::Union{LinRange, StepRangeLen}) at range.jl:664
  iterate(::Union{LinRange, StepRangeLen}, ::Int64) at range.jl:664
  iterate(::T) where T<:Union{Base.KeySet{var"#s79", var"#s78"} where {var"#s79", var"#s78"<:Dict}, Base.ValueIterator{var"#s77"} where var"#s77"<:Dict} at dict.jl:693
  ...
Stacktrace:
  [1] iterate(::Base.Generator{ContinuousTerm{Float64}, StatsModels.var"#32#33"{NamedTuple{(:a, :b, :y), Tuple{Vector{Float64}, Vector{Float64}, Vector{Float64}}}}})
    @ Base ./generator.jl:44
  [2] modelcols(t::InteractionTerm{ContinuousTerm{Float64}}, d::NamedTuple{(:a, :b, :y), Tuple{Vector{Float64}, Vector{Float64}, Vector{Float64}}})
    @ StatsModels ~/.julia/packages/StatsModels/MDeyQ/src/terms.jl:532

```

is it possible to have a quick fix patch on `iterate()` to solve the problem?

please advise, thanks.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [October 8, 2021, 1:26pm UTC](https://discourse.julialang.org/t/bug-in-statsmodels-modelmatrix/69428/2 "2021-10-08T13:26:20Z")

</div>

I’m closer to the problem. The term `a & a` and `b & b` got the type `InteractionTerm{ContinuousTerm{Float64}}`, that cannot be handled properly, instead of `InteractionTerm{Tuple{ContinuousTerm{Float64}, ContinuousTerm{Float64}}}`:

```julia
julia> fml2
FormulaTerm
Response:
  y(unknown)
Predictors:
  1
  a(unknown)
  b(unknown)
  a(unknown) & a(unknown)
  a(unknown) & b(unknown)
  b(unknown) & b(unknown)

julia> typeof(sfml2.rhs.terms[1])
InterceptTerm{true}

julia> typeof(sfml2.rhs.terms[2])
ContinuousTerm{Float64}

julia> typeof(sfml2.rhs.terms[3])
ContinuousTerm{Float64}

julia> typeof(sfml2.rhs.terms[4])
InteractionTerm{ContinuousTerm{Float64}}

julia> typeof(sfml2.rhs.terms[5])
InteractionTerm{Tuple{ContinuousTerm{Float64}, ContinuousTerm{Float64}}}

julia> typeof(sfml2.rhs.terms[6])
InteractionTerm{ContinuousTerm{Float64}}

```

so, for the time being we **need to explicitly create the square terms** like:

```julia
fml3 = @formula(y ~ 1 + a + (a ^ 2) + (a & b) + b + (b ^ 2));

julia> modelmatrix(fml3, df)
3×6 Matrix{Float64}:
 1.0 1.0 1.0 2.0 4.0 2.0
 1.0 2.0 4.0 3.0 9.0 6.0
 1.0 3.0 9.0 4.0 16.0 12.0

```

in order the avoid the problem.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [October 8, 2021, 1:36pm UTC](https://discourse.julialang.org/t/bug-in-statsmodels-modelmatrix/69428/3 "2021-10-08T13:36:43Z")

</div>

Better file this kind of bug directly on GitHub. Cc: @dave.f.kleinschmidt

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [October 8, 2021, 1:56pm UTC](https://discourse.julialang.org/t/bug-in-statsmodels-modelmatrix/69428/4 "2021-10-08T13:56:15Z")

</div>

[done here.](https://github.com/JuliaStats/StatsModels.jl/issues/244)

the problem remains is I don’t know how to “Cc” in GitHub…
