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
4 changes: 3 additions & 1 deletion src/statmodels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ mutable struct CoefTable
teststatcol::Int
function CoefTable(cols::Vector,colnms::Vector,rownms::Vector,
pvalcol::Int=0,teststatcol::Int=0)
Base.require_one_based_indexing(cols...)
nc = length(cols)
nrs = map(length,cols)
nr = nrs[1]
Expand All @@ -84,7 +85,8 @@ mutable struct CoefTable

function CoefTable(mat::AbstractMatrix,colnms::Vector,rownms::Vector,
pvalcol::Int=0,teststatcol::Int=0)
cols = Any[collect(col) for col in eachcol(mat)]
Base.require_one_based_indexing(mat)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything in this method that relies on one based indexing? Same for the other one.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Methods for CoefTable such as the show method assume 1-based indexing, eg in

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(escape_markdown_pipes(cols[j-1][i])) : cols[j-1][i]
for i in 1:nr, j in 1:nc+1]

@andreasnoack andreasnoack Nov 10, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it can safely assume that because the fields are Arrays.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fields are Arrays, but their elements are indexed as well here, and those are not guaranteed to use 1-based indexing. The assumption also shows up in other places, e.g. in iterate:

cols = getindex.(ct.cols, Ref(i))

cols = collect(Any, eachcol(mat))
CoefTable(cols,colnms,rownms,pvalcol,teststatcol)
end
end
Expand Down
34 changes: 20 additions & 14 deletions test/statmodels.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using StatsBase
using StatsBase: PValue, TestStat
using Test, Random, StatsAPI, LinearAlgebra, Markdown
using Test, Random, StatsAPI, LinearAlgebra, Markdown, OffsetArrays

v1 = [1.45666, -23.14, 1.56734e-13]
v2 = ["Good", "Great", "Bad"]
Expand Down Expand Up @@ -201,7 +201,7 @@ end
aic, aicc, bic, r2, r², adjr2, adjr²)
@test f isa Function
end
# Defined but not reexported
# Defined but not reexported
@test StatsBase.params isa Function
@test StatsBase.params! isa Function
end
Expand All @@ -214,18 +214,24 @@ end
reference = CoefTable(Matrix(matrix), columns, rows)
table = CoefTable(matrix, columns, rows)
@test collect(table) == collect(reference)
@test sprint(show, MIME"text/plain"(), table) ==
sprint(show, MIME"text/plain"(), reference)
@test sprint(show, MIME"text/markdown"(), table) ==
sprint(show, MIME"text/markdown"(), reference)
@test repr("text/plain", table) == repr("text/plain", reference)
@test repr("text/markdown", table) == repr("text/markdown", reference)
@test eltype(table.cols[1]) === eltype(values)
matrix[1, 1] = -99
@test table.cols[1][1] == values[1, 1]
table.cols[1][2] = 77
@test matrix[2, 1] == values[2, 1]
@test_throws ArgumentError CoefTable(matrix, ["a"], rows)
@test_throws ArgumentError CoefTable(matrix, columns, ["x"])
@test_throws ArgumentError CoefTable(matrix, columns, rows, 3)
@test_throws ArgumentError CoefTable(matrix, columns, rows, 0, 3)
@test_throws ArgumentError("colnms should have length 0 or 2") CoefTable(matrix, ["a"], rows)
@test_throws ArgumentError("rownms should have length 0 or 3") CoefTable(matrix, columns, ["x"])
@test_throws ArgumentError("pvalcol should be between 0 and 2") CoefTable(matrix, columns, rows, 3)
@test_throws ArgumentError("teststatcol should be between 0 and 2") CoefTable(matrix, columns, rows, 0, 3)
end
end

@testset "CoefTable rejects offset arrays" begin
offset_error = ArgumentError("offset arrays are not supported but got an array with index other than 1")
values = [1.0 10.0; 2.0 20.0; 3.0 30.0]
columns, rows = ["a", "b"], ["x", "y", "z"]
# matrix inputs, with offset row or column axis
@test_throws offset_error CoefTable(OffsetArray(values, 0:2, 1:2), columns, rows)
@test_throws offset_error CoefTable(OffsetArray(values, 1:3, 0:1), columns, rows)
# vector-of-columns inputs, with an offset column
@test_throws offset_error CoefTable(Any[OffsetArray(values[:, 1], 0:2), values[:, 2]], columns, rows)
@test_throws offset_error CoefTable(Any[values[:, 1], OffsetArray(values[:, 2], 0:2)], columns, rows)
end
Loading