From 8d4dc17e05c0561044570a94af4258da21332a0d Mon Sep 17 00:00:00 2001 From: Fabian Greimel Date: Thu, 29 Jun 2023 01:09:14 +0200 Subject: [PATCH] Fix tests (#16) --- Project.toml | 2 +- src/DINA.jl | 2 +- test/runtests.jl | 17 +++++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Project.toml b/Project.toml index 95091e8..aac231e 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "DINA" uuid = "2e9d5fb1-712f-4dff-9379-c18f12b3746d" authors = ["Fabian Greimel and contributors"] -version = "0.2.0" +version = "0.2.1" [deps] CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597" diff --git a/src/DINA.jl b/src/DINA.jl index e1b9dcd..95d09db 100644 --- a/src/DINA.jl +++ b/src/DINA.jl @@ -93,7 +93,7 @@ function aggregate_quantiles(var, byvar, ngroups, year; wgt, by_taxunit) # Aggregate by group agg_df = combine( groupby(df, [:group_id, :age]), - ([v, wgt] => ((x,w) -> mean(x, weights(w))) => v for v in var)..., wgt => sum => wgt + ([v, wgt] => ((x,w) -> dot(x, w) / sum(w)) => v for v in var)..., wgt => sum => wgt ) agg_df[!,:year] .= year diff --git a/test/runtests.jl b/test/runtests.jl index 38192ea..1a9c85c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,9 +2,7 @@ using TestItemRunner @run_package_tests -@testitem "DINA.jl" begin - import CSV - +@testitem "DINA.jl" begin tbl = get_dina(1980) @test tbl isa DINA.DataFrames.DataFrame @@ -14,13 +12,16 @@ using TestItemRunner df_1980 = aggregate_quantiles(var, :fiinc, 10, 1980; wgt = :dweght, by_taxunit = true) - df = dina_quantile_panel(var, :fiinc, 10, [1980;2007]) + df = dina_quantile_panel(var, :fiinc, 10, [1962; 1980;2007]) + sort!(df, [:year, :group_id, :age]) + @test abs(df.fiinc[1] - (-175.761)) < 0.01 @test df isa DINA.DataFrames.DataFrame - @test size(df) == (60, 11) - + @test size(df) == (80, 11) end @testitem "Income group panel" begin + using CSV: CSV + var = [:fiinc, :fninc, :ownermort, :ownerhome, :rentalmort, :rentalhome] df = dina_quantile_panel(var, :fiinc, 10) @@ -28,8 +29,8 @@ end @test size(df) == (1530, 11) filename = joinpath(@__DIR__(), "dina-aggregated.csv") + sort!(df, [:year, :group_id, :age]) df |> CSV.write(filename) - @show filename - + @show filename end \ No newline at end of file