Skip to content

Commit

Permalink
add sorting of treatment values
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierlabayle committed Jan 31, 2024
1 parent a11d0dc commit 69281c6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/counterfactual_mean_based/estimands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,20 @@ function identify(method::BackdoorAdjustment, causal_estimand::T, scm::SCM) wher
)
end

unique_non_missing(dataset, colname) = unique(skipmissing(Tables.getcolumn(dataset, colname)))
function get_treatment_values(dataset, colname)
counts = groupcount(skipmissing(Tables.getcolumn(dataset, colname)))
sorted_counts = sort(collect(pairs(counts)), by = x -> x.second, rev=true)
return first.(sorted_counts)
end

unique_treatment_values(dataset, colnames) =(;(colname => unique_non_missing(dataset, colname) for colname in colnames)...)
"""
unique_treatment_values(dataset, colnames)
We ensure that the values are sorted by frequency to maximize
the number of estimands passing the positivity constraint.
"""
unique_treatment_values(dataset, colnames) =
(;(colname => get_treatment_values(dataset, colname) for colname in colnames)...)

get_transitive_treatments_contrasts(treatments_unique_values) =
[collect(zip(vals[1:end-1], vals[2:end])) for vals in values(treatments_unique_values)]
Expand Down
14 changes: 13 additions & 1 deletion test/counterfactual_mean_based/estimands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module TestEstimands

using Test
using TMLE

@testset "Test StatisticalCMCompositeEstimand" begin
dataset = (
W = [1, 2, 3, 4, 5, 6, 7, 8],
Expand Down Expand Up @@ -208,6 +207,19 @@ end
treatments_unique_values = (T₁=(1, 0, 2), T₂=["AC", "CC"])
@test TMLE.get_transitive_treatments_contrasts(treatments_unique_values) == [[(1, 0), (0, 2)], [("AC", "CC")]]
end

@testset "Test unique_treatment_values" begin
dataset = (
T₁ = ["AC", missing, "AC", "CC", "CC", "AA", "CC"],
T₂ = [1, missing, 1, 2, 2, 3, 2]
)
# most frequent to least frequent
@test TMLE.unique_treatment_values(dataset, (:T₁, :T₂)) == (
T₁ = ["CC", "AC", "AA"],
T₂ = [2, 1, 3],
)
end

@testset "Test factorialATE" begin
dataset = (
T₁ = [0, 1, 2, missing],
Expand Down

2 comments on commit 69281c6

@olivierlabayle
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

Release notes:

  • Fix the "show" method issues arising from the previous version
  • add sorting of treatment values for maximizing joint estimand size

@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/99966

Tagging

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 v0.14.2 -m "<description of version>" 69281c6d8d56dbdbea57ece6add3ae9541c036c2
git push origin v0.14.2

Please sign in to comment.