From 16a2478a129a515cfe14bd62ecc8e9846c4dddfd Mon Sep 17 00:00:00 2001 From: Matthieu Gomez Date: Mon, 23 Aug 2021 17:52:01 +0200 Subject: [PATCH] Update to FillArrays 0.12 (#45) * Update Project.toml * Update Project.toml * Update fit.jl * Update InteractiveFixedEffectModels.jl * Update fit.jl * Update interactivefixedeffectsmodel.jl --- Project.toml | 4 +++- src/InteractiveFixedEffectModels.jl | 3 ++- src/fit.jl | 18 +++++++++--------- test/interactivefixedeffectsmodel.jl | 12 ++++-------- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/Project.toml b/Project.toml index 1f3c2ab..761754e 100644 --- a/Project.toml +++ b/Project.toml @@ -7,6 +7,7 @@ DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b" FixedEffectModels = "9d5cd8c9-2029-5cab-9928-427838db53e3" FixedEffects = "c8885935-8500-56a7-9867-7708b20db0eb" +GroupedArrays = "6407cd72-fade-4a84-8a1e-56e431fc1533" LeastSquaresOptim = "0fc2ff8b-aaa3-5acd-a817-1944a5e08891" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" @@ -20,9 +21,10 @@ Vcov = "ec2bfdc2-55df-4fc9-b9ae-4958c2cf2486" [compat] DataFrames = "0.21, 0.22, 1.0" -FillArrays = "0.7, 0.8, 0.9, 0.10, 0.11" +FillArrays = "0.7, 0.8, 0.9, 0.10, 0.11, 0.12" FixedEffectModels = "1.5" FixedEffects = "2" +GroupedArrays = "0.3" LeastSquaresOptim = "0.7, 0.8" Reexport = "0.2, 1.0" StatsBase = "0.33" diff --git a/src/InteractiveFixedEffectModels.jl b/src/InteractiveFixedEffectModels.jl index f52213a..23f6180 100644 --- a/src/InteractiveFixedEffectModels.jl +++ b/src/InteractiveFixedEffectModels.jl @@ -7,6 +7,7 @@ module InteractiveFixedEffectModels using DataFrames using FillArrays using FixedEffects +using GroupedArrays using LeastSquaresOptim using LinearAlgebra using Printf @@ -42,4 +43,4 @@ include("utils/formula.jl") include("methods/gauss_seidel.jl") include("methods/ls.jl") include("fit.jl") -end \ No newline at end of file +end diff --git a/src/fit.jl b/src/fit.jl index 6ad86ad..2830fef 100644 --- a/src/fit.jl +++ b/src/fit.jl @@ -86,8 +86,8 @@ function regife( converged = false # get two dimensions - id = FixedEffects.group(df[esample, m.id]) - time = FixedEffects.group(df[esample, m.time]) + id = GroupedArray(df[esample, m.id]) + time = GroupedArray(df[esample, m.time]) ############################################################################## ## @@ -135,26 +135,26 @@ function regife( ############################################################################## # initialize factor models at 0.1 - idpool = fill(0.1, id.n, m.rank) - timepool = fill(0.1, time.n, m.rank) + idpool = fill(0.1, id.ngroups, m.rank) + timepool = fill(0.1, time.ngroups, m.rank) y .= y .* sqrtw X .= X .* sqrtw if !has_regressors # factor model - fp = FactorModel(y, sqrtw, id.refs, time.refs, m.rank) + fp = FactorModel(y, sqrtw, id.groups, time.groups, m.rank) fs = FactorSolution(idpool, timepool) (fs, iterations, converged) = fit!(Val{method}, fp, fs; maxiter = maxiter, tol = tol, lambda = lambda) else # interactive fixed effect coef = X \ y - fp = FactorModel(y - X * coef, sqrtw, id.refs, time.refs, m.rank) + fp = FactorModel(y - X * coef, sqrtw, id.groups, time.groups, m.rank) fs = FactorSolution(idpool, timepool) fit!(Val{:levenberg_marquardt}, fp, fs; maxiter = 100, tol = 1e-3, lambda = lambda) fs = InteractiveFixedEffectsSolution(coef, fs.idpool, fs.timepool) - fp = InteractiveFixedEffectsModel(y, sqrtw, X, id.refs, time.refs, m.rank) + fp = InteractiveFixedEffectsModel(y, sqrtw, X, id.groups, time.groups, m.rank) ym = copy(y) @@ -193,7 +193,7 @@ function regife( ############################################################################## # compute residuals - fp = FactorModel(copy(y), sqrtw, id.refs, time.refs, m.rank) + fp = FactorModel(copy(y), sqrtw, id.groups, time.groups, m.rank) if has_regressors LinearAlgebra.BLAS.gemm!('N', 'N', -1.0, X, fs.b, 1.0, fp.y) end @@ -261,7 +261,7 @@ function regife( if has_regressors LinearAlgebra.BLAS.gemm!('N', 'N', -1.0, oldX, coef, 1.0, oldresiduals) end - fp = FactorModel(oldresiduals, sqrtw, id.refs, time.refs, m.rank) + fp = FactorModel(oldresiduals, sqrtw, id.groups, time.groups, m.rank) subtract_factor!(fp, fs) axpy!(-1.0, residuals, oldresiduals) # get fixed effect diff --git a/test/interactivefixedeffectsmodel.jl b/test/interactivefixedeffectsmodel.jl index a865371..1f1ae07 100644 --- a/test/interactivefixedeffectsmodel.jl +++ b/test/interactivefixedeffectsmodel.jl @@ -61,7 +61,7 @@ for method in [:dogleg, :levenberg_marquardt, :gauss_seidel] end # check high dimentional fixed effects are part of factor models -df.State2 = categorical(df.State) +df.State2 = deepcopy(df.State) @test_throws ErrorException regife(df, @formula(Sales ~ Price + ife(State, Year, 2) + fe(State2))) @@ -74,8 +74,6 @@ for method in [:levenberg_marquardt, :dogleg] println(method) local df = DataFrame(CSV.File(joinpath(dirname(pathof(InteractiveFixedEffectModels)), "../dataset/Cigar.csv"))) - df.State = categorical(df.State) - df.Year = categorical(df.Year) model = @formula Sales ~ Price + ife(State, Year, 1) + fe(State) result = regife(df, model, weights = :Pop, method = method, save = true) model = @formula Sales ~ Price + ife(State, Year, 2) + fe(State) @@ -84,18 +82,16 @@ for method in [:levenberg_marquardt, :dogleg] local df = DataFrame(CSV.File(joinpath(dirname(pathof(InteractiveFixedEffectModels)), "../dataset/EmplUK.csv"))) df.id1 = df.Firm df.id2 = df.Year - df.pid1 = categorical(df.id1) - df.pid2 = categorical(df.id2) df.y = df.Wage df.x1 = df.Emp df.w = df.Output - model = @formula y ~ x1 + ife(pid1, pid2, 2) + model = @formula y ~ x1 + ife(id1, id2, 2) result = regife(df, model, method = method, save = true) @test norm(result.coef ./ [4.53965, -0.0160858] .- 1) < precision - model = @formula y ~ x1 + ife(pid1, pid2, 2) + model = @formula y ~ x1 + ife(id1, id2, 2) result = regife(df, model, weights = :w, method = method, save = true) @test norm(result.coef ./ [3.47551,-0.017366] .- 1) < precision - model = @formula y ~ x1 + ife(pid1, pid2, 1) + model = @formula y ~ x1 + ife(id1, id2, 1) result = regife(df, model, weights = :w, method = method, save = true) @test norm(result.coef ./ [ -2.62105, -0.0470005] .- 1) < precision end