Skip to content

Commit

Permalink
Update to FillArrays 0.12 (#45)
Browse files Browse the repository at this point in the history
* Update Project.toml

* Update Project.toml

* Update fit.jl

* Update InteractiveFixedEffectModels.jl

* Update fit.jl

* Update interactivefixedeffectsmodel.jl
  • Loading branch information
matthieugomez authored Aug 23, 2021
1 parent e7083ca commit 16a2478
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion src/InteractiveFixedEffectModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module InteractiveFixedEffectModels
using DataFrames
using FillArrays
using FixedEffects
using GroupedArrays
using LeastSquaresOptim
using LinearAlgebra
using Printf
Expand Down Expand Up @@ -42,4 +43,4 @@ include("utils/formula.jl")
include("methods/gauss_seidel.jl")
include("methods/ls.jl")
include("fit.jl")
end
end
18 changes: 9 additions & 9 deletions src/fit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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])

##############################################################################
##
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions test/interactivefixedeffectsmodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))


Expand All @@ -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)
Expand All @@ -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
Expand Down

2 comments on commit 16a2478

@matthieugomez
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/43391

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.4 -m "<description of version>" 16a2478a129a515cfe14bd62ecc8e9846c4dddfd
git push origin v1.1.4

Please sign in to comment.