-
Notifications
You must be signed in to change notification settings - Fork 33
coefnames returns symbols #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -543,18 +543,48 @@ vectorize(x) = [x] | |
coefnames(term::AbstractTerm) | ||
|
||
Return the name(s) of column(s) generated by a term. Return value is either a | ||
`String` or an iterable of `String`s. | ||
`Symbol` or an iterable of `String`s. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missed a String |
||
""" | ||
StatsBase.coefnames(t::Term) = t.sym | ||
StatsBase.coefnames(t::FormulaTerm) = (coefnames(t.lhs), coefnames(t.rhs)) | ||
StatsBase.coefnames(::InterceptTerm{H}) where {H} = H ? "(Intercept)" : [] | ||
StatsBase.coefnames(t::ContinuousTerm) = string(t.sym) | ||
StatsBase.coefnames(t::CategoricalTerm) = | ||
["$(t.sym): $name" for name in t.contrasts.termnames] | ||
StatsBase.coefnames(t::FunctionTerm) = string(t.exorig) | ||
StatsBase.coefnames(ts::TupleTerm) = reduce(vcat, coefnames.(ts)) | ||
StatsBase.coefnames(::InterceptTerm{H}) where {H} = H ? Symbol(:Intercept) : [] # this seems like the wrong thing to return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we keep the parens? Then you'd have |
||
StatsBase.coefnames(t::ContinuousTerm) = t.sym | ||
StatsBase.coefnames(t::CategoricalTerm) = [Symbol("$(t.sym): $name") for name in t.contrasts.termnames] | ||
StatsBase.coefnames(t::FunctionTerm) = Symbol(string(t.exorig)) | ||
StatsBase.coefnames(t::MatrixTerm) = mapreduce(coefnames, vcat, t.terms) | ||
#function StatsBase.coefnames(t::InteractionTerm) | ||
# Symbol.(kron_insideout((args...) -> join(args, " & "), vectorize.(coefnames.(t.terms))...)) | ||
#end | ||
StatsBase.coefnames(t::InteractionTerm) = | ||
kron_insideout((args...) -> join(args, " & "), vectorize.(coefnames.(t.terms))...) | ||
Symbol.(kron_insideout((args...) -> join(args, " & "), vectorize.(coefnames.(t.terms))...)) | ||
StatsBase.coefnames(ts::TupleTerm) = _coefnames(ts.terms) | ||
_coefnames(ts::Tuple) = (coefnames(first(ts)), _coefnames(tail(ts))...) | ||
_coefnames(ts::Tuple{}) = () | ||
|
||
""" | ||
coef(term::AbstractTerm, s::Symbol) | ||
""" | ||
function StatsBase.coef(f::FormulaTerm, s::Symbol) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see what the point of this is, or rather why it's called also, it doesn't seem like this works for categorical terms or for interactions, even if you specify the full coefficient name, since and the RHS can have other things than just a |
||
if coefname(f.lhs) === s | ||
c = f.lhs | ||
else | ||
c = _coef(f.rhs, s) | ||
end | ||
if c isa AbstractTerm | ||
return c | ||
else | ||
error("$c is not a coefficient within $term") | ||
end | ||
end | ||
_coef(t::AbstractTerm, s::Symbol) = coefnames(t) === s ? t : false | ||
function _coef(t::MatrixTerm, s::Symbol) | ||
for t_i in t | ||
coefname(t_i) === s && return t_i | ||
end | ||
return false | ||
end | ||
|
||
|
||
|
||
################################################################################ | ||
# old Terms features: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why can't you use the original broadcast based solution?