@@ -47,7 +47,7 @@ function regife(df, f::FormulaTerm, vcov::CovarianceEstimator = Vcov.simple();
47
47
formula = FormulaTerm (f. lhs, tuple (ConstantTerm (1 ), FixedEffectModels. eachterm (f. rhs)... ))
48
48
end
49
49
50
- formula, formula_endo, formula_iv = FixedEffectModels. decompose_iv (f)
50
+ formula, formula_endo, formula_iv = FixedEffectModels. parse_iv (f)
51
51
52
52
m, formula = parse_interactivefixedeffect (df, formula)
53
53
if ifeformula != nothing # remove after depreciation
@@ -114,7 +114,7 @@ function regife(df, f::FormulaTerm, vcov::CovarianceEstimator = Vcov.simple();
114
114
has_fes_intercept = true
115
115
end
116
116
fes = FixedEffect[FixedEffectModels. _subset (fe, esample) for fe in fes]
117
- feM = FixedEffectModels. AbstractFixedEffectSolver {Float64} (fes, weights, Val{:lsmr })
117
+ feM = FixedEffectModels. AbstractFixedEffectSolver {Float64} (fes, weights, Val{:cpu })
118
118
end
119
119
120
120
@@ -138,7 +138,8 @@ function regife(df, f::FormulaTerm, vcov::CovarianceEstimator = Vcov.simple();
138
138
formula_schema = apply_schema (formula, schema (formula, subdf, contrasts), StatisticalModel)
139
139
140
140
y = convert (Vector{Float64}, response (formula_schema, subdf))
141
- oldy = copy (y)
141
+ tss_total = FixedEffectModels. tss (y, has_intercept || has_fes_intercept, weights)
142
+
142
143
X = convert (Matrix{Float64}, modelmatrix (formula_schema, subdf))
143
144
144
145
# change default if has_regressors
@@ -158,14 +159,12 @@ function regife(df, f::FormulaTerm, vcov::CovarianceEstimator = Vcov.simple();
158
159
coef_names = Symbol .(coef_names)
159
160
160
161
161
-
162
+ # demean variables
162
163
if has_fes
163
164
FixedEffectModels. solve_residuals! (y, feM)
164
165
FixedEffectModels. solve_residuals! (X, feM)
165
- end
166
- y .= y .* sqrtw
167
- X .= X .* sqrtw
168
-
166
+ end
167
+
169
168
170
169
171
170
# #############################################################################
@@ -178,23 +177,25 @@ function regife(df, f::FormulaTerm, vcov::CovarianceEstimator = Vcov.simple();
178
177
idpool = fill (0.1 , length (id. pool), m. rank)
179
178
timepool = fill (0.1 , length (time. pool), m. rank)
180
179
180
+ y .= y .* sqrtw
181
+ X .= X .* sqrtw
181
182
if ! has_regressors
183
+ # factor model
182
184
fp = FactorModel (y, sqrtw, id. refs, time. refs, m. rank)
183
185
fs = FactorSolution (idpool, timepool)
184
- # factor model
185
-
186
186
(fs, iterations, converged) =
187
187
fit! (Val{method}, fp, fs; maxiter = maxiter, tol = tol, lambda = lambda)
188
188
else
189
189
# interactive fixed effect
190
- # initialize fs
191
190
coef = X \ y
192
191
fp = FactorModel (y - X * coef, sqrtw, id. refs, time. refs, m. rank)
193
192
fs = FactorSolution (idpool, timepool)
194
193
fit! (Val{:levenberg_marquardt }, fp, fs; maxiter = 100 , tol = 1e-3 , lambda = lambda)
195
194
196
195
fs = InteractiveFixedEffectsSolution (coef, fs. idpool, fs. timepool)
197
196
fp = InteractiveFixedEffectsModel (y, sqrtw, X, id. refs, time. refs, m. rank)
197
+
198
+
198
199
ym = copy (y)
199
200
Xm = copy (X)
200
201
@@ -206,15 +207,14 @@ function regife(df, f::FormulaTerm, vcov::CovarianceEstimator = Vcov.simple();
206
207
# y ~ x + γ1 x factors + γ2 x loadings
207
208
# if not, this means fit! ended up on a a local minimum.
208
209
# restart with randomized coefficients, factors, loadings
209
- newfeM = FixedEffectModels. AbstractFixedEffectSolver {Float64} (getfactors (fp, fs), weights, Val{:lsmr })
210
- ym .= ym ./ sqrtw
210
+ newfeM = FixedEffectModels. AbstractFixedEffectSolver {Float64} (getfactors (fp, fs), weights, Val{:cpu })
211
+ ym .= ym ./ sqrtw
211
212
FixedEffectModels. solve_residuals! (ym, newfeM, tol = tol, maxiter = maxiter)
212
213
ym .= ym .* sqrtw
213
-
214
- Xm .= Xm ./ sqrtw
214
+ Xm .= Xm ./ sqrtw
215
215
FixedEffectModels. solve_residuals! (Xm, newfeM, tol = tol, maxiter = maxiter)
216
216
Xm .= Xm .* sqrtw
217
- ydiff = Xm * (fs. b - Xm \ ym)
217
+ ydiff = Xm * (fs. b - Xm \ ym)
218
218
if iterations >= maxiter || norm (ydiff) <= 0.01 * norm (y)
219
219
break
220
220
end
@@ -264,13 +264,12 @@ function regife(df, f::FormulaTerm, vcov::CovarianceEstimator = Vcov.simple();
264
264
# compute various r2
265
265
nobs = sum (esample)
266
266
rss = sum (abs2, residualsm)
267
- _tss = FixedEffectModels. tss (ym, has_intercept || has_fes_intercept, sqrtw )
267
+ _tss = FixedEffectModels. tss (ym ./ sqrtw , has_intercept || has_fes_intercept, weights )
268
268
r2_within = 1 - rss / _tss
269
269
270
270
rss = sum (abs2, residuals)
271
- _tss = FixedEffectModels. tss (oldy, has_intercept || has_fes_intercept, sqrtw)
272
- r2 = 1 - rss / _tss
273
- r2_a = 1 - rss / _tss * (nobs - has_intercept) / dof_residual
271
+ r2 = 1 - rss / tss_total
272
+ r2_a = 1 - rss / tss_total * (nobs - has_intercept) / dof_residual
274
273
end
275
274
276
275
# #############################################################################
0 commit comments