Skip to content

Commit a966c65

Browse files
nilshgnalimilankleinschmidt
authored
Add string method for term (#190)
* Add & document `AbstractString` method for `term` This simplifies programmatically constructing formulas from DataFrames columns now that `names()` returns strings. * Change docstring * Add test for `term` string method * Mention string method of `term` * Update src/terms.jl Co-authored-by: Milan Bouchet-Valat <[email protected]> * Update docs/src/formula.md Co-authored-by: Dave Kleinschmidt <[email protected]> * Add `AbstractString` method to `term` + related docs changes and tests Co-authored-by: Milan Bouchet-Valat <[email protected]> Co-authored-by: Dave Kleinschmidt <[email protected]>
1 parent 4065093 commit a966c65

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StatsModels"
22
uuid = "3eaba693-59b7-5ba5-a881-562e759f1c8d"
3-
version = "0.6.13"
3+
version = "0.6.14"
44

55
[deps]
66
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"

docs/src/formula.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,11 @@ Predictors:
299299
long as the packages have [implemented support for them](@ref extend-runtime).
300300

301301
The [`term`](@ref) function constructs a term of the appropriate type from
302-
symbols and numbers, which makes it easy to work with collections of mixed type:
302+
symbols or strings (`Term`) and numbers (`ConstantTerm`), which makes it easy to
303+
work with collections of mixed type:
303304

304305
```jldoctest 1
305-
julia> ts = term.((1, :a, :b))
306+
julia> ts = term.((1, :a, "b"))
306307
1
307308
a(unknown)
308309
b(unknown)

src/terms.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,13 +579,14 @@ hasresponse(t::FormulaTerm) =
579579
"""
580580
term(x)
581581
582-
Wrap argument in an appropriate `AbstractTerm` type: `Symbol`s become `Term`s,
583-
and `Number`s become `ConstantTerm`s. Any `AbstractTerm`s are unchanged.
582+
Wrap argument in an appropriate `AbstractTerm` type: `Symbol`s and `AbstractString`s become `Term`s,
583+
and `Number`s become `ConstantTerm`s. Any `AbstractTerm`s are unchanged. `AbstractString`s
584+
are converted to symbols before wrapping.
584585
585586
# Example
586587
587588
```jldoctest
588-
julia> ts = term.((1, :a, :b))
589+
julia> ts = term.((1, :a, "b"))
589590
1
590591
a(unknown)
591592
b(unknown)
@@ -596,4 +597,5 @@ Tuple{ConstantTerm{Int64},Term,Term}
596597
"""
597598
term(n::Number) = ConstantTerm(n)
598599
term(s::Symbol) = Term(s)
600+
term(s::AbstractString) = term(Symbol(s))
599601
term(t::AbstractTerm) = t

test/terms.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ StatsModels.apply_schema(mt::MultiTerm, sch::StatsModels.Schema, Mod::Type) =
1818

1919
@testset "concrete_term" begin
2020
t = term(:aaa)
21+
ts = term("aaa")
22+
@test t == ts
2123
@test string(t) == "aaa"
2224
@test mimestring(t) == "aaa(unknown)"
2325

0 commit comments

Comments
 (0)