Skip to content

Commit 608bf28

Browse files
committed
update stderr -> stderror
1 parent 6dd13d1 commit 608bf28

File tree

7 files changed

+35
-32
lines changed

7 files changed

+35
-32
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
julia 0.6.0
22
Distributions 0.4.6
3-
StatsBase 0.7.1
3+
StatsBase 0.22.0
44
StatsModels 0.2.4
55
DataArrays 0.3.4
66
CategoricalArrays

benchmark/benchmark.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ id1 = rand(1:(N/K), N)
55
id2 = rand(1:K, N)
66
x1 = randn(N)
77
x2 = randn(N)
8-
w = cos(id1)
9-
y= 3 .* x1 .+ 5 .* x2 .+ cos(id1) .+ cos(id2).^2 .+ randn(N)
8+
w = cos.(id1)
9+
y= 3 .* x1 .+ 5 .* x2 .+ cos.(id1) .+ cos.(id2).^2 .+ randn(N)
1010
df = DataFrame(id1 = pool(id1), id2 = pool(id2), x1 = x1, x2 = x2, w = w, y = y)
11-
@time reg(y ~ x1 + x2, df)
11+
@time reg(df, @model(y ~ x1 + x2))
1212
# 1.258554 seconds (723 allocations: 1.205 GB, 18.70% gc time)
1313
#852MB to obtain the matrix etc, and then 1.205 for Regressions part
14-
@time reg(y ~ x1 + x2, df, VcovCluster(:id2))
14+
@time reg(df, @model(y ~ x1 + x2, vcov = cluster(id2)))
1515
# 1.569679 seconds (843 allocations: 1.293 GB, 25.21% gc time)
16-
@time reg(y ~ x1 + x2 |> id1, df)
16+
@time reg(df, @model(y ~ x1 + x2, fe = id1))
1717
# 1.476390 seconds (890 allocations: 1.175 GB, 20.15% gc time)
18-
@time reg(y ~ x1 + x2 |> id1, df, VcovCluster(:id1))
18+
@time reg(df, @model(y ~ x1 + x2, fe = id1, vcov = cluster(id1)))
1919
# 1.974738 seconds (1.04 k allocations: 1.255 GB, 15.85% gc time)
20-
@time reg(y ~ x1 + x2 |> id1 + id2, df)
20+
@time reg(df, @model(y ~ x1 + x2, fe = id1 + id2))
2121
# 4.554836 seconds (1.01 k allocations: 1.188 GB, 9.56% gc time)
22-
@time reg(y ~ x1 + x2 |> id1, df, weight = :w)
22+
@time reg(df, @model(y ~ x1 + x2, fe = id1, weights = w))
2323
# 2.000850 seconds (20.00 M allocations: 1.010 GB, 18.82% gc time)
24-
@time reg(y ~ x1 + x2 |> id1 + id2, df, weight = :w)
24+
@time reg(df, @model(y ~ x1 + x2, fe = id1 + id2, weights = w))
2525
# 4.118974 seconds (20.00 M allocations: 1.018 GB, 9.60% gc time)

src/FixedEffectModels.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import CategoricalArrays: CategoricalArray, CategoricalVector, compress, categor
1515
import DataFrames: DataFrame, AbstractDataFrame, completecases, names!, ismissing
1616
import StatsModels: ModelMatrix, ModelFrame, Terms, coefnames, Formula, completecases, names!, @formula
1717
using Reexport
18-
import StatsBase: coef, nobs, coeftable, vcov, predict, residuals, var, RegressionModel, model_response, stderr, confint, fit, CoefTable, df_residual
18+
import StatsBase: coef, nobs, coeftable, vcov, predict, residuals, var, RegressionModel, model_response, stderror, confint, fit, CoefTable, df_residual
1919
@reexport using StatsBase
2020
##############################################################################
2121
##

src/RegressionResult.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ df_residual(x::AbstractRegressionResult) = x.df_residual
1616

1717
function confint(x::AbstractRegressionResult)
1818
scale = quantile(TDist(x.df_residual), 1 - (1-0.95)/2)
19-
se = stderr(x)
19+
se = stderror(x)
2020
hcat(x.coef - scale * se, x.coef + scale * se)
2121
end
2222

@@ -66,7 +66,7 @@ function coeftable(x::AbstractRegressionResult)
6666
ctitle = title(x)
6767
ctop = top(x)
6868
cc = coef(x)
69-
se = stderr(x)
69+
se = stderror(x)
7070
coefnms = coefnames(x)
7171
conf_int = confint(x)
7272
# put (intercept) last

src/fixedeffect/FixedEffectProblem_LSMR.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ size(fem::FixedEffectMatrix, dim::Integer) = (dim == 1) ? fem.m :
113113
function A_mul_B_helper!::Number, fe::FixedEffect,
114114
x::Vector{Float64}, y::AbstractVector{Float64}, cache::Vector{Float64})
115115
for (i, j) in zip(1:length(y), eachindex(y))
116-
y[j] += α * x[fe.refs[i]] * cache[i]
116+
@inbounds y[j] += α * x[fe.refs[i]] * cache[i]
117117
end
118118
end
119119
function A_mul_B!::Number, fem::FixedEffectMatrix, fev::FixedEffectVector,
120120
β::Number, y::AbstractVector{Float64})
121121
safe_scale!(y, β)
122-
for i in 1:length(fev._)
122+
Threads.@threads for i in 1:length(fev._)
123123
A_mul_B_helper!(α, fem._[i], fev._[i], y, fem.cache[i])
124124
end
125125
return y
@@ -129,14 +129,13 @@ end
129129
function Ac_mul_B_helper!::Number, fe::FixedEffect,
130130
y::AbstractVector{Float64}, x::Vector{Float64}, cache::Vector{Float64})
131131
for (i, j) in zip(1:length(y), eachindex(y))
132-
x[fe.refs[i]] += α * y[j] * cache[i]
132+
@inbounds x[fe.refs[i]] += α * y[j] * cache[i]
133133
end
134134
end
135135
function Ac_mul_B!::Number, fem::FixedEffectMatrix,
136136
y::AbstractVector{Float64}, β::Number, fev::FixedEffectVector)
137-
safe_scale!(fev, β)
138-
for i in 1:length(fev._)
139-
# @code_warntype Ac_mul_B_helper!(α, fem._[i], y, fev._[i])
137+
safe_scale!(fev, β)
138+
Threads.@threads for i in 1:length(fev._)
140139
Ac_mul_B_helper!(α, fem._[i], y, fev._[i], fem.cache[i])
141140
end
142141
return fev

src/reg.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ function reg(df::AbstractDataFrame, f::Formula;
125125
sqrtw = get_weights(df, esample, weights)
126126

127127
# remove unusused levels
128-
subdf = df[esample, all_vars]
128+
if any(esample)
129+
subdf = df[esample, all_vars]
130+
else
131+
subdf = df[all_vars]
132+
end
129133
main_vars = unique(convert(Vector{Symbol}, vcat(vars, endo_vars, iv_vars)))
130134
for v in main_vars
131135
# in case subdataframe, don't construct subdf[v] if you dont need to do it

test/reg.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,55 +186,55 @@ x = reg(df, m)
186186
# Simple
187187
m = @model y ~ x1
188188
x = reg(df, m)
189-
@test stderr(x) [1.52126, 0.01889] atol = 1e-4
189+
@test stderror(x) [1.52126, 0.01889] atol = 1e-4
190190
# Stata ivreg
191191
m = @model y ~ (x1 ~ z1)
192192
x = reg(df, m)
193-
@test stderr(x) [1.53661, 0.01915] atol = 1e-4
193+
@test stderror(x) [1.53661, 0.01915] atol = 1e-4
194194
# Stata areg
195195
m = @model y ~ x1 fe = pid1
196196
x = reg(df, m)
197-
@test stderr(x) [0.00980] atol = 1e-4
197+
@test stderror(x) [0.00980] atol = 1e-4
198198

199199
# White
200200
# Stata reg
201201
m = @model y ~ x1 vcov = robust
202202
x = reg(df, m)
203-
@test stderr(x) [1.68679, 0.01670] atol = 1e-4
203+
@test stderror(x) [1.68679, 0.01670] atol = 1e-4
204204
# Stata ivreg
205205
m = @model y ~ (x1 ~ z1) vcov = robust
206206
x = reg(df, m)
207-
@test stderr(x) [1.63305, 0.01674] atol = 1e-4
207+
@test stderror(x) [1.63305, 0.01674] atol = 1e-4
208208
# Stata areg
209209
m = @model y ~ x1 fe = pid1 vcov = robust
210210
x = reg(df, m)
211-
@test stderr(x) [0.01100] atol = 1e-4
211+
@test stderror(x) [0.01100] atol = 1e-4
212212

213213
# cluster
214214
m = @model y ~ x1 vcov = cluster(pid1)
215215
x = reg(df, m)
216-
@test stderr(x)[2] 0.03792 atol = 1e-4
216+
@test stderror(x)[2] 0.03792 atol = 1e-4
217217
m = @model y ~ x1 fe = pid1 vcov = cluster(pid2)
218218
x = reg(df, m)
219-
@test stderr(x) [0.02205] atol = 1e-4
219+
@test stderror(x) [0.02205] atol = 1e-4
220220
# stata reghxe
221221
m = @model y ~ x1 fe = pid1 vcov = cluster(pid1)
222222
x = reg(df, m)
223-
@test stderr(x) [0.03573] atol = 1e-4
223+
@test stderror(x) [0.03573] atol = 1e-4
224224

225225
# no reference
226226
m = @model y ~ x1 vcov = cluster(pid1 + pid2)
227227
x = reg(df, m)
228-
@test stderr(x)[1] 6.17025 atol = 1e-4
228+
@test stderror(x)[1] 6.17025 atol = 1e-4
229229
# no reference
230230
m = @model y ~ x1 fe = pid1 vcov = cluster(pid1 + pid2)
231231
x = reg(df, m)
232-
@test stderr(x)[1] 0.04037 atol = 1e-4
232+
@test stderror(x)[1] 0.04037 atol = 1e-4
233233

234234
# TO CHECK WITH STATA
235235
m = @model y ~ x1 fe = pid1 vcov = cluster(pid1&pid2)
236236
x = reg(df, m)
237-
@test stderr(x) [0.0110005] atol = 1e-4
237+
@test stderror(x) [0.0110005] atol = 1e-4
238238

239239

240240
@test_throws ErrorException reg(df, @model(y ~ x1, vcov = cluster(State)))

0 commit comments

Comments
 (0)