Skip to content

Commit 65f723d

Browse files
committed
update
1 parent 3b1659e commit 65f723d

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
2121
[compat]
2222
DataFrames = "≥ 0.19.1"
2323
FixedEffectModels = "≥ 0.8.0"
24-
FixedEffects = "≥ 0.1.1"
24+
FixedEffects = "≥ 0.2.0"
2525
LeastSquaresOptim = "≥ 0.7.0"
2626
StatsBase = "≥ 0.22.0"
2727
StatsModels = "≥ 0.6.0"

src/regife.jl

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function regife(df::AbstractDataFrame, f::FormulaTerm;
8888
has_absorb_intercept = true
8989
end
9090
fes = FixedEffect[FixedEffectModels._subset(fe, esample) for fe in fes]
91-
pfe = FixedEffectModels.FixedEffectMatrix(fes, sqrtw, Val{:lsmr})
91+
feM = FixedEffectModels.FixedEffectMatrix(fes, sqrtw, Val{:lsmr})
9292
end
9393

9494
has_intercept = ConstantTerm(1) FixedEffectModels.eachterm(formula.rhs)
@@ -123,10 +123,8 @@ function regife(df::AbstractDataFrame, f::FormulaTerm;
123123
formula_schema = apply_schema(formula, schema(formula, subdf, contrasts), StatisticalModel)
124124

125125
y = convert(Vector{Float64}, response(formula_schema, subdf))
126-
y .= y .* sqrtw
127126
oldy = copy(y)
128127
X = convert(Matrix{Float64}, modelmatrix(formula_schema, subdf))
129-
X .= X .* sqrtw
130128

131129
# change default if has_regressors
132130
has_regressors = size(X, 2) > 0
@@ -147,9 +145,11 @@ function regife(df::AbstractDataFrame, f::FormulaTerm;
147145

148146

149147
if has_absorb
150-
FixedEffectModels.solve_residuals!(y, pfe)
151-
FixedEffectModels.solve_residuals!(X, pfe)
148+
FixedEffectModels.solve_residuals!(y, feM)
149+
FixedEffectModels.solve_residuals!(X, feM)
152150
end
151+
y .= y .* sqrtw
152+
X .= X .* sqrtw
153153

154154

155155

@@ -191,9 +191,13 @@ function regife(df::AbstractDataFrame, f::FormulaTerm;
191191
# y ~ x + γ1 x factors + γ2 x loadings
192192
# if not, this means fit! ended up on a a local minimum.
193193
# restart with randomized coefficients, factors, loadings
194-
newpfe = FixedEffectModels.FixedEffectMatrix(getfactors(fp, fs), sqrtw, Val{:lsmr})
195-
FixedEffectModels.solve_residuals!(ym, newpfe, tol = tol, maxiter = maxiter)
196-
FixedEffectModels.solve_residuals!(Xm, newpfe, tol = tol, maxiter = maxiter)
194+
newfeM = FixedEffectModels.FixedEffectMatrix(getfactors(fp, fs), sqrtw, Val{:lsmr})
195+
ym .= ym ./sqrtw
196+
Xm .= Xm ./sqrtw
197+
FixedEffectModels.solve_residuals!(ym, newfeM, tol = tol, maxiter = maxiter)
198+
FixedEffectModels.solve_residuals!(Xm, newfeM, tol = tol, maxiter = maxiter)
199+
ym .= ym .* sqrtw
200+
Xm .= Xm .* sqrtw
197201
ydiff = Xm * (fs.b - Xm \ ym)
198202
if iterations >= maxiter || norm(ydiff) <= 0.01 * norm(y)
199203
break
@@ -285,7 +289,7 @@ function regife(df::AbstractDataFrame, f::FormulaTerm;
285289
subtract_factor!(fp, fs)
286290
axpy!(-1.0, residuals, oldresiduals)
287291
# get fixed effect
288-
newfes, b, c = FixedEffectModels.solve_coefficients!(oldresiduals, pfe; tol = tol, maxiter = maxiter)
292+
newfes, b, c = FixedEffectModels.solve_coefficients!(oldresiduals, feM; tol = tol, maxiter = maxiter)
289293
for j in 1:length(fes)
290294
augmentdf[!, ids[j]] = Vector{Union{Float64, Missing}}(missing, length(esample))
291295
augmentdf[esample, ids[j]] = newfes[j]

0 commit comments

Comments
 (0)