Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 2.2.1

- Reduce the latency of the first results display (about 1.3s to 0.07s): the display is precompiled and no longer compiles for each type of output stream.
- Avoid invalidations from packages loaded by CSV (WeakRefStrings, FilePathsBase): fixed-effect names, coefficient names, and the display use concretely typed strings.

## 2.2.0

- Fix regressors and the constant that were dropped as collinear because of the units of another regressor, e.g. the constant with a regressor around 1e12. Fix standard errors in the same cases.
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FixedEffectModels"
uuid = "9d5cd8c9-2029-5cab-9928-427838db53e3"
version = "2.2.0"
version = "2.2.1"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Expand Down
34 changes: 25 additions & 9 deletions src/FixedEffectModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,20 +343,36 @@ function top(m::FixedEffectModel)
end


# Render into a buffer, so that the display compiles once (in the precompile
# workload) rather than once per type of io (terminal, pipe, IOContext, ...)
function Base.show(io::IO, m::FixedEffectModel)
buf = IOContext(IOBuffer(), io)
_show(buf, m)
write(io, take!(buf.io))
return nothing
end

function _show(io::IOContext{IOBuffer}, m::FixedEffectModel)
ct = coeftable(m)
#copied from show(iio,cf::Coeftable)
cols = ct.cols; rownms = ct.rownms; colnms = ct.colnms;
cols = ct.cols
nc = length(cols)
nr = length(cols[1])
if length(rownms) == 0
rownms = [lpad("[$i]",floor(Integer, log10(nr))+3) for i in 1:nr]
nr = length(cols[1])::Int
# concretely typed names: untyped ones compile String conversions that
# packages adding string types (e.g. WeakRefStrings) invalidate
rownms = (isempty(ct.rownms) ? String[lpad("[$i]", floor(Int, log10(nr))+3) for i in 1:nr] :
String[string(x)::String for x in ct.rownms])::Vector{String}
colnms = String[string(x)::String for x in ct.colnms]::Vector{String}
mat = Matrix{Any}(undef, nr, nc + 1)
for i in 1:nr
mat[i, 1] = NoQuote(rownms[i])
for j in 1:nc
x = cols[j][i]
mat[i, j + 1] = j == ct.pvalcol ? NoQuote(sprint(show, PValue(x))::String) :
j in ct.teststatcol ? TestStat(x) :
x isa AbstractString ? NoQuote(string(x)::String) : x
end
end
mat = [j == 1 ? NoQuote(rownms[i]) :
j-1 == ct.pvalcol ? NoQuote(sprint(show, PValue(cols[j-1][i]))) :
j-1 in ct.teststatcol ? TestStat(cols[j-1][i]) :
cols[j-1][i] isa AbstractString ? NoQuote(cols[j-1][i]) : cols[j-1][i]
for i in 1:nr, j in 1:nc+1]
io = IOContext(io, :compact=>true, :limit=>false)
A = Base.alignment(io, mat, 1:size(mat, 1), 1:size(mat, 2),
typemax(Int), typemax(Int), 3)
Expand Down
2 changes: 2 additions & 0 deletions src/FixedEffectModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Vcov
# cover the categorical and interaction term types (the vector-based
# machinery compiles per term type, not per formula shape)
reg(df, @formula(y ~ x1 + s + x1&x2 + x1&s))
# the REPL displays every result
show(IOBuffer(), MIME"text/plain"(), reg(df, @formula(y ~ x1 + fe(id))))
end


Expand Down
6 changes: 4 additions & 2 deletions src/utils/formula.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@ function _parse_fixedeffect(data, @nospecialize(t::InteractionTerm))
isempty(fe_terms) && return nothing

# x1&x2 from (x1&x2)*id
fe_names = [fesymbol(x) for x in fe_terms]
fe_names = Symbol[fesymbol(x) for x in fe_terms]
slope_names = _validate_fe_interactions(data, slope_terms)
interaction = _multiply(data, slope_names)
fe = FixedEffect((Tables.getcolumn(data, fe_name) for fe_name in fe_names)...; interaction = interaction)
pieces = vcat(["fe_" * string(fe_name) for fe_name in fe_names], string.(slope_terms))
# a concretely typed vector: joining an untyped one compiles IO calls that
# packages adding IO methods (e.g. FilePathsBase) invalidate, along with fit
pieces = vcat(String["fe_" * string(fe_name) for fe_name in fe_names], String[string(s) for s in slope_names])
return fe, Symbol(join(pieces, "&")), fe_names
end

Expand Down
7 changes: 6 additions & 1 deletion src/utils/vectorterms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ function coefnames_vector(ts::Vector{AbstractTerm})
out = String[]
for t in ts
StatsModels.width(t) > 0 || continue
append!(out, StatsModels.vectorize(coefnames(t)))
# push concretely typed names: appending the untyped coefnames result
# compiles String conversions that packages adding string types (e.g.
# WeakRefStrings) invalidate, along with fit
for nm in StatsModels.vectorize(coefnames(t))
push!(out, string(nm)::String)
end
end
return out
end
Loading