@@ -19,13 +19,12 @@ function regife(
19
19
# #
20
20
# #############################################################################
21
21
df = DataFrame (df; copycols = false )
22
- if (ConstantTerm (0 ) ∉ FixedEffectModels . eachterm (f. rhs)) & (ConstantTerm (1 ) ∉ FixedEffectModels . eachterm (f. rhs))
23
- formula = FormulaTerm (f. lhs, tuple (ConstantTerm (1 ), FixedEffectModels . eachterm (f. rhs)... ))
22
+ if (ConstantTerm (0 ) ∉ eachterm (f. rhs)) & (ConstantTerm (1 ) ∉ eachterm (f. rhs))
23
+ formula = FormulaTerm (f. lhs, tuple (ConstantTerm (1 ), eachterm (f. rhs)... ))
24
24
end
25
25
26
- formula, formula_endo, formula_iv = FixedEffectModels. parse_iv (f)
27
26
28
- m, formula = parse_interactivefixedeffect (df, formula )
27
+ m, formula = parse_interactivefixedeffect (df, f )
29
28
has_weights = (weights != nothing )
30
29
31
30
@@ -60,10 +59,10 @@ function regife(
60
59
weights = uweights (sum (esample))
61
60
sqrtw = ones (length (weights))
62
61
end
63
- for a in FixedEffectModels . eachterm (formula. rhs)
62
+ for a in eachterm (formula. rhs)
64
63
if has_fe (a)
65
64
isa (a, InteractionTerm) && error (" Fixed effects cannot be interacted" )
66
- Symbol (FixedEffectModels . fesymbol (a)) ∉ factor_vars && error (" FixedEffect should correspond to id or time dimension of the factor model" )
65
+ Symbol (fesymbol (a)) ∉ factor_vars && error (" FixedEffect should correspond to id or time dimension of the factor model" )
67
66
end
68
67
end
69
68
fes, ids, formula = FixedEffectModels. parse_fixedeffect (df, formula)
@@ -72,15 +71,15 @@ function regife(
72
71
# # Compute factors, an array of AbtractFixedEffects
73
72
if has_fes
74
73
if any ([isa (fe. interaction, Ones) for fe in fes])
75
- formula = FormulaTerm (formula. lhs, tuple (ConstantTerm (0 ), (t for t in FixedEffectModels . eachterm (formula. rhs) if t!= ConstantTerm (1 )). .. ))
74
+ formula = FormulaTerm (formula. lhs, tuple (ConstantTerm (0 ), (t for t in eachterm (formula. rhs) if t!= ConstantTerm (1 )). .. ))
76
75
has_fes_intercept = true
77
76
end
78
- fes = FixedEffect[FixedEffectModels . _subset (fe, esample) for fe in fes]
79
- feM = FixedEffectModels . AbstractFixedEffectSolver {Float64} (fes, weights, Val{:cpu })
77
+ fes = FixedEffect[fe[ esample] for fe in fes]
78
+ feM = AbstractFixedEffectSolver {Float64} (fes, weights, Val{:cpu })
80
79
end
81
80
82
81
83
- has_intercept = ConstantTerm (1 ) ∈ FixedEffectModels . eachterm (formula. rhs)
82
+ has_intercept = ConstantTerm (1 ) ∈ eachterm (formula. rhs)
84
83
85
84
86
85
iterations = 0
@@ -100,7 +99,7 @@ function regife(
100
99
formula_schema = apply_schema (formula, schema (formula, subdf, contrasts), StatisticalModel)
101
100
102
101
y = convert (Vector{Float64}, response (formula_schema, subdf))
103
- tss_total = FixedEffectModels . tss (y, has_intercept || has_fes_intercept, weights)
102
+ tss_total = tss (y, has_intercept || has_fes_intercept, weights)
104
103
105
104
X = convert (Matrix{Float64}, modelmatrix (formula_schema, subdf))
106
105
@@ -123,8 +122,8 @@ function regife(
123
122
124
123
# demean variables
125
124
if has_fes
126
- FixedEffectModels . solve_residuals! (y, feM)
127
- FixedEffectModels . solve_residuals! (X, feM, progress_bar = false )
125
+ solve_residuals! (y, feM)
126
+ solve_residuals! (X, feM, progress_bar = false )
128
127
end
129
128
130
129
@@ -136,8 +135,8 @@ function regife(
136
135
# #############################################################################
137
136
138
137
# initialize factor models at 0.1
139
- idpool = fill (0.1 , length ( id. pool) , m. rank)
140
- timepool = fill (0.1 , length ( time. pool) , m. rank)
138
+ idpool = fill (0.1 , id. n , m. rank)
139
+ timepool = fill (0.1 , time. n , m. rank)
141
140
142
141
y .= y .* sqrtw
143
142
X .= X .* sqrtw
@@ -169,12 +168,12 @@ function regife(
169
168
# y ~ x + γ1 x factors + γ2 x loadings
170
169
# if not, this means fit! ended up on a a local minimum.
171
170
# restart with randomized coefficients, factors, loadings
172
- newfeM = FixedEffectModels . AbstractFixedEffectSolver {Float64} (getfactors (fp, fs), weights, Val{:cpu })
171
+ newfeM = AbstractFixedEffectSolver {Float64} (getfactors (fp, fs), weights, Val{:cpu })
173
172
ym .= ym ./ sqrtw
174
- FixedEffectModels . solve_residuals! (ym, newfeM, tol = tol, maxiter = maxiter)
173
+ solve_residuals! (ym, newfeM, tol = tol, maxiter = maxiter)
175
174
ym .= ym .* sqrtw
176
175
Xm .= Xm ./ sqrtw
177
- FixedEffectModels . solve_residuals! (Xm, newfeM, tol = tol, maxiter = maxiter)
176
+ solve_residuals! (Xm, newfeM, tol = tol, maxiter = maxiter)
178
177
Xm .= Xm .* sqrtw
179
178
ydiff = Xm * (fs. b - Xm \ ym)
180
179
if iterations >= maxiter || norm (ydiff) <= 0.01 * norm (y)
@@ -226,7 +225,7 @@ function regife(
226
225
# compute various r2
227
226
nobs = sum (esample)
228
227
rss = sum (abs2, residualsm)
229
- _tss = FixedEffectModels . tss (ym ./ sqrtw, has_intercept || has_fes_intercept, weights)
228
+ _tss = tss (ym ./ sqrtw, has_intercept || has_fes_intercept, weights)
230
229
r2_within = 1 - rss / _tss
231
230
232
231
rss = sum (abs2, residuals)
@@ -266,7 +265,7 @@ function regife(
266
265
subtract_factor! (fp, fs)
267
266
axpy! (- 1.0 , residuals, oldresiduals)
268
267
# get fixed effect
269
- newfes, b, c = FixedEffectModels . solve_coefficients! (oldresiduals, feM; tol = tol, maxiter = maxiter)
268
+ newfes, b, c = solve_coefficients! (oldresiduals, feM; tol = tol, maxiter = maxiter)
270
269
for j in 1 : length (fes)
271
270
augmentdf[! , ids[j]] = Vector {Union{Float64, Missing}} (missing , length (esample))
272
271
augmentdf[esample, ids[j]] = newfes[j]
0 commit comments