Skip to content

Commit 16a2478

Browse files
Update to FillArrays 0.12 (#45)
* Update Project.toml * Update Project.toml * Update fit.jl * Update InteractiveFixedEffectModels.jl * Update fit.jl * Update interactivefixedeffectsmodel.jl
1 parent e7083ca commit 16a2478

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
77
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
88
FixedEffectModels = "9d5cd8c9-2029-5cab-9928-427838db53e3"
99
FixedEffects = "c8885935-8500-56a7-9867-7708b20db0eb"
10+
GroupedArrays = "6407cd72-fade-4a84-8a1e-56e431fc1533"
1011
LeastSquaresOptim = "0fc2ff8b-aaa3-5acd-a817-1944a5e08891"
1112
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1213
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
@@ -20,9 +21,10 @@ Vcov = "ec2bfdc2-55df-4fc9-b9ae-4958c2cf2486"
2021

2122
[compat]
2223
DataFrames = "0.21, 0.22, 1.0"
23-
FillArrays = "0.7, 0.8, 0.9, 0.10, 0.11"
24+
FillArrays = "0.7, 0.8, 0.9, 0.10, 0.11, 0.12"
2425
FixedEffectModels = "1.5"
2526
FixedEffects = "2"
27+
GroupedArrays = "0.3"
2628
LeastSquaresOptim = "0.7, 0.8"
2729
Reexport = "0.2, 1.0"
2830
StatsBase = "0.33"

src/InteractiveFixedEffectModels.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module InteractiveFixedEffectModels
77
using DataFrames
88
using FillArrays
99
using FixedEffects
10+
using GroupedArrays
1011
using LeastSquaresOptim
1112
using LinearAlgebra
1213
using Printf
@@ -42,4 +43,4 @@ include("utils/formula.jl")
4243
include("methods/gauss_seidel.jl")
4344
include("methods/ls.jl")
4445
include("fit.jl")
45-
end
46+
end

src/fit.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ function regife(
8686
converged = false
8787
# get two dimensions
8888

89-
id = FixedEffects.group(df[esample, m.id])
90-
time = FixedEffects.group(df[esample, m.time])
89+
id = GroupedArray(df[esample, m.id])
90+
time = GroupedArray(df[esample, m.time])
9191

9292
##############################################################################
9393
##
@@ -135,26 +135,26 @@ function regife(
135135
##############################################################################
136136

137137
# initialize factor models at 0.1
138-
idpool = fill(0.1, id.n, m.rank)
139-
timepool = fill(0.1, time.n, m.rank)
138+
idpool = fill(0.1, id.ngroups, m.rank)
139+
timepool = fill(0.1, time.ngroups, m.rank)
140140

141141
y .= y .* sqrtw
142142
X .= X .* sqrtw
143143
if !has_regressors
144144
# factor model
145-
fp = FactorModel(y, sqrtw, id.refs, time.refs, m.rank)
145+
fp = FactorModel(y, sqrtw, id.groups, time.groups, m.rank)
146146
fs = FactorSolution(idpool, timepool)
147147
(fs, iterations, converged) =
148148
fit!(Val{method}, fp, fs; maxiter = maxiter, tol = tol, lambda = lambda)
149149
else
150150
# interactive fixed effect
151151
coef = X \ y
152-
fp = FactorModel(y - X * coef, sqrtw, id.refs, time.refs, m.rank)
152+
fp = FactorModel(y - X * coef, sqrtw, id.groups, time.groups, m.rank)
153153
fs = FactorSolution(idpool, timepool)
154154
fit!(Val{:levenberg_marquardt}, fp, fs; maxiter = 100, tol = 1e-3, lambda = lambda)
155155

156156
fs = InteractiveFixedEffectsSolution(coef, fs.idpool, fs.timepool)
157-
fp = InteractiveFixedEffectsModel(y, sqrtw, X, id.refs, time.refs, m.rank)
157+
fp = InteractiveFixedEffectsModel(y, sqrtw, X, id.groups, time.groups, m.rank)
158158

159159

160160
ym = copy(y)
@@ -193,7 +193,7 @@ function regife(
193193
##############################################################################
194194

195195
# compute residuals
196-
fp = FactorModel(copy(y), sqrtw, id.refs, time.refs, m.rank)
196+
fp = FactorModel(copy(y), sqrtw, id.groups, time.groups, m.rank)
197197
if has_regressors
198198
LinearAlgebra.BLAS.gemm!('N', 'N', -1.0, X, fs.b, 1.0, fp.y)
199199
end
@@ -261,7 +261,7 @@ function regife(
261261
if has_regressors
262262
LinearAlgebra.BLAS.gemm!('N', 'N', -1.0, oldX, coef, 1.0, oldresiduals)
263263
end
264-
fp = FactorModel(oldresiduals, sqrtw, id.refs, time.refs, m.rank)
264+
fp = FactorModel(oldresiduals, sqrtw, id.groups, time.groups, m.rank)
265265
subtract_factor!(fp, fs)
266266
axpy!(-1.0, residuals, oldresiduals)
267267
# get fixed effect

test/interactivefixedeffectsmodel.jl

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ for method in [:dogleg, :levenberg_marquardt, :gauss_seidel]
6161
end
6262

6363
# check high dimentional fixed effects are part of factor models
64-
df.State2 = categorical(df.State)
64+
df.State2 = deepcopy(df.State)
6565
@test_throws ErrorException regife(df, @formula(Sales ~ Price + ife(State, Year, 2) + fe(State2)))
6666

6767

@@ -74,8 +74,6 @@ for method in [:levenberg_marquardt, :dogleg]
7474
println(method)
7575

7676
local df = DataFrame(CSV.File(joinpath(dirname(pathof(InteractiveFixedEffectModels)), "../dataset/Cigar.csv")))
77-
df.State = categorical(df.State)
78-
df.Year = categorical(df.Year)
7977
model = @formula Sales ~ Price + ife(State, Year, 1) + fe(State)
8078
result = regife(df, model, weights = :Pop, method = method, save = true)
8179
model = @formula Sales ~ Price + ife(State, Year, 2) + fe(State)
@@ -84,18 +82,16 @@ for method in [:levenberg_marquardt, :dogleg]
8482
local df = DataFrame(CSV.File(joinpath(dirname(pathof(InteractiveFixedEffectModels)), "../dataset/EmplUK.csv")))
8583
df.id1 = df.Firm
8684
df.id2 = df.Year
87-
df.pid1 = categorical(df.id1)
88-
df.pid2 = categorical(df.id2)
8985
df.y = df.Wage
9086
df.x1 = df.Emp
9187
df.w = df.Output
92-
model = @formula y ~ x1 + ife(pid1, pid2, 2)
88+
model = @formula y ~ x1 + ife(id1, id2, 2)
9389
result = regife(df, model, method = method, save = true)
9490
@test norm(result.coef ./ [4.53965, -0.0160858] .- 1) < precision
95-
model = @formula y ~ x1 + ife(pid1, pid2, 2)
91+
model = @formula y ~ x1 + ife(id1, id2, 2)
9692
result = regife(df, model, weights = :w, method = method, save = true)
9793
@test norm(result.coef ./ [3.47551,-0.017366] .- 1) < precision
98-
model = @formula y ~ x1 + ife(pid1, pid2, 1)
94+
model = @formula y ~ x1 + ife(id1, id2, 1)
9995
result = regife(df, model, weights = :w, method = method, save = true)
10096
@test norm(result.coef ./ [ -2.62105, -0.0470005] .- 1) < precision
10197
end

0 commit comments

Comments
 (0)