balance: transformations and formulas¶
This tutorial focuses on the ways transformations, formulas and penalty can be included in your pre-processing of the coveriates before adjusting for them.
Example dataset - preparing the objects¶
The following is a toy simulated dataset.
For a more basic walkthrough of the elements in the next code block, please take a look at the tutorial: balance Quickstart: Analyzing and adjusting the bias on a simulated toy dataset
from balance import load_data
target_df, sample_df = load_data()
from balance import Sample
sample = Sample.from_frame(sample_df, outcome_columns=["happiness"])
target = Sample.from_frame(target_df, outcome_columns=["happiness"])
sample_with_target = sample.set_target(target)
sample_with_target
INFO (2025-03-20 17:31:40,810) [__init__/<module> (line 54)]: Using balance version 0.10.0
WARNING (2025-03-20 17:31:40,997) [util/guess_id_column (line 113)]: Guessed id column name id for the data
WARNING (2025-03-20 17:31:41,006) [sample_class/from_frame (line 261)]: No weights passed. Adding a 'weight' column and setting all values to 1
WARNING (2025-03-20 17:31:41,015) [util/guess_id_column (line 113)]: Guessed id column name id for the data
WARNING (2025-03-20 17:31:41,029) [sample_class/from_frame (line 261)]: No weights passed. Adding a 'weight' column and setting all values to 1
(balance.sample_class.Sample) balance Sample object with target set 1000 observations x 3 variables: gender,age_group,income id_column: id, weight_column: weight, outcome_columns: happiness target: balance Sample object 10000 observations x 3 variables: gender,age_group,income id_column: id, weight_column: weight, outcome_columns: happiness 3 common variables: gender,age_group,income
When trying to understand what an adjustment does, we can look at the model_coef items collected from the diagnostics method.
adjusted = sample_with_target.adjust(
# method="ipw", # default method
# transformations=None,
# formula=None,
# penalty_factor=None, # all 1s
# max_de=None,
)
adj_diag = adjusted.diagnostics()
adj_diag.query("metric == 'model_coef'")
INFO (2025-03-20 17:31:41,047) [ipw/ipw (line 399)]: Starting ipw function
INFO (2025-03-20 17:31:41,051) [adjustment/apply_transformations (line 305)]: Adding the variables: []
INFO (2025-03-20 17:31:41,052) [adjustment/apply_transformations (line 306)]: Transforming the variables: ['gender', 'age_group', 'income']
INFO (2025-03-20 17:31:41,061) [adjustment/apply_transformations (line 343)]: Final variables in output: ['gender', 'age_group', 'income']
INFO (2025-03-20 17:31:41,068) [ipw/ipw (line 433)]: Building model matrix
INFO (2025-03-20 17:31:41,165) [ipw/ipw (line 455)]: The formula used to build the model matrix: ['income + gender + age_group + _is_na_gender']
INFO (2025-03-20 17:31:41,166) [ipw/ipw (line 458)]: The number of columns in the model matrix: 16
INFO (2025-03-20 17:31:41,167) [ipw/ipw (line 459)]: The number of rows in the model matrix: 11000
INFO (2025-03-20 17:31:57,794) [ipw/ipw (line 578)]: Done with sklearn
INFO (2025-03-20 17:31:57,795) [ipw/ipw (line 584)]: max_de: None
INFO (2025-03-20 17:31:57,795) [ipw/ipw (line 605)]: Starting model selection
INFO (2025-03-20 17:31:57,799) [ipw/ipw (line 638)]: Chosen lambda: 0.041158338186664825
INFO (2025-03-20 17:31:57,800) [ipw/ipw (line 654)]: Proportion null deviance explained 0.17265121909892267
INFO (2025-03-20 17:31:57,804) [sample_class/diagnostics (line 839)]: Starting computation of diagnostics of the fitting
INFO (2025-03-20 17:31:58,072) [sample_class/diagnostics (line 1049)]: Done computing diagnostics
metric | val | var | |
---|---|---|---|
44 | model_coef | 0.142821 | intercept |
45 | model_coef | 0.043803 | _is_na_gender[T.True] |
46 | model_coef | -0.203801 | age_group[T.25-34] |
47 | model_coef | -0.428742 | age_group[T.35-44] |
48 | model_coef | -0.529629 | age_group[T.45+] |
49 | model_coef | 0.332477 | gender[T.Male] |
50 | model_coef | 0.043803 | gender[T._NA] |
51 | model_coef | 0.168359 | income[Interval(-0.0009997440000000001, 0.44, ... |
52 | model_coef | 0.152978 | income[Interval(0.44, 1.664, closed='right')] |
53 | model_coef | 0.11004 | income[Interval(1.664, 3.472, closed='right')] |
54 | model_coef | -0.042502 | income[Interval(11.312, 15.139, closed='right')] |
55 | model_coef | -0.162192 | income[Interval(15.139, 20.567, closed='right')] |
56 | model_coef | -0.212078 | income[Interval(20.567, 29.504, closed='right')] |
57 | model_coef | -0.358711 | income[Interval(29.504, 128.536, closed='right')] |
58 | model_coef | 0.092556 | income[Interval(3.472, 5.663, closed='right')] |
59 | model_coef | 0.071799 | income[Interval(5.663, 8.211, closed='right')] |
60 | model_coef | 0.00469 | income[Interval(8.211, 11.312, closed='right')] |
As we can see from the glm coefficients, the age and gender groups got an extra NA column. And the income variable is bucketed into 10 buckets.
We can change these defaults by deciding on the specific transformation we want.
Let's start with NO transformations.
The transformation argument accepts either a dict or None. None indicates no transformations.
adjusted = sample_with_target.adjust(
# method="ipw",
transformations=None,
# formula=formula,
# penalty_factor=penalty_factor,
# max_de=None,
)
adj_diag = adjusted.diagnostics()
adj_diag.query("metric == 'model_coef'")
INFO (2025-03-20 17:31:58,087) [ipw/ipw (line 399)]: Starting ipw function
INFO (2025-03-20 17:31:58,089) [ipw/ipw (line 433)]: Building model matrix
INFO (2025-03-20 17:31:58,156) [ipw/ipw (line 455)]: The formula used to build the model matrix: ['income + gender + age_group + _is_na_gender']
INFO (2025-03-20 17:31:58,157) [ipw/ipw (line 458)]: The number of columns in the model matrix: 8
INFO (2025-03-20 17:31:58,157) [ipw/ipw (line 459)]: The number of rows in the model matrix: 11000
INFO (2025-03-20 17:32:13,569) [ipw/ipw (line 578)]: Done with sklearn
INFO (2025-03-20 17:32:13,570) [ipw/ipw (line 584)]: max_de: None
INFO (2025-03-20 17:32:13,570) [ipw/ipw (line 605)]: Starting model selection
INFO (2025-03-20 17:32:13,574) [ipw/ipw (line 638)]: Chosen lambda: 0.0368353720078807
INFO (2025-03-20 17:32:13,574) [ipw/ipw (line 654)]: Proportion null deviance explained 0.17353587606008936
INFO (2025-03-20 17:32:13,579) [sample_class/diagnostics (line 839)]: Starting computation of diagnostics of the fitting
INFO (2025-03-20 17:32:13,846) [sample_class/diagnostics (line 1049)]: Done computing diagnostics
metric | val | var | |
---|---|---|---|
44 | model_coef | 0.998089 | intercept |
45 | model_coef | 0.000043 | _is_na_gender[T.True] |
46 | model_coef | -0.212989 | age_group[T.25-34] |
47 | model_coef | -0.440492 | age_group[T.35-44] |
48 | model_coef | -0.545653 | age_group[T.45+] |
49 | model_coef | -0.188196 | gender[Female] |
50 | model_coef | 0.18171 | gender[Male] |
51 | model_coef | 0.000043 | gender[_NA] |
52 | model_coef | -0.570551 | income |
In this setting, income was treated as a numeric variable, with no transformations (e.g.: bucketing) on it. Regardless of the transformations, the model matrix made sure to turn the gender and age_group into dummy variables (including a column for NA).
Next we can fit a simple transformation.
Let's say we wanted to bucket age_groups groups that are smaller than 25% of the data, and use different bucketing on income, here is how we'd do it:
from balance.util import fct_lump, quantize
transformations = {
"age_group": lambda x: fct_lump(x, 0.25),
"gender": lambda x: x,
"income": lambda x: quantize(x.fillna(x.mean()), q=3),
}
adjusted = sample_with_target.adjust(
# method="ipw",
transformations=transformations,
# formula=formula,
# penalty_factor=penalty_factor,
# max_de=None,
)
adj_diag = adjusted.diagnostics()
adj_diag.query("metric == 'model_coef'")
INFO (2025-03-20 17:32:13,861) [ipw/ipw (line 399)]: Starting ipw function
INFO (2025-03-20 17:32:13,863) [adjustment/apply_transformations (line 305)]: Adding the variables: []
INFO (2025-03-20 17:32:13,864) [adjustment/apply_transformations (line 306)]: Transforming the variables: ['age_group', 'gender', 'income']
INFO (2025-03-20 17:32:13,871) [adjustment/apply_transformations (line 343)]: Final variables in output: ['age_group', 'gender', 'income']
INFO (2025-03-20 17:32:13,878) [ipw/ipw (line 433)]: Building model matrix
INFO (2025-03-20 17:32:13,973) [ipw/ipw (line 455)]: The formula used to build the model matrix: ['income + gender + age_group + _is_na_gender']
INFO (2025-03-20 17:32:13,974) [ipw/ipw (line 458)]: The number of columns in the model matrix: 8
INFO (2025-03-20 17:32:13,975) [ipw/ipw (line 459)]: The number of rows in the model matrix: 11000
INFO (2025-03-20 17:32:27,611) [ipw/ipw (line 578)]: Done with sklearn
INFO (2025-03-20 17:32:27,611) [ipw/ipw (line 584)]: max_de: None
INFO (2025-03-20 17:32:27,612) [ipw/ipw (line 605)]: Starting model selection
INFO (2025-03-20 17:32:27,615) [ipw/ipw (line 638)]: Chosen lambda: 0.11811067639400605
INFO (2025-03-20 17:32:27,615) [ipw/ipw (line 654)]: Proportion null deviance explained 0.09420328935831213
WARNING (2025-03-20 17:32:27,617) [ipw/ipw (line 662)]: The propensity model has low fraction null deviance explained (0.09420328935831213). Results may not be accurate
INFO (2025-03-20 17:32:27,620) [sample_class/diagnostics (line 839)]: Starting computation of diagnostics of the fitting
INFO (2025-03-20 17:32:27,883) [sample_class/diagnostics (line 1049)]: Done computing diagnostics
metric | val | var | |
---|---|---|---|
44 | model_coef | -0.327112 | intercept |
45 | model_coef | 0.031737 | _is_na_gender[T.True] |
46 | model_coef | -0.181989 | age_group[T.35-44] |
47 | model_coef | 0.098871 | age_group[T._lumped_other] |
48 | model_coef | 0.241586 | gender[T.Male] |
49 | model_coef | 0.031737 | gender[T._NA] |
50 | model_coef | 0.19926 | income[Interval(-0.0009997440000000001, 4.194,... |
51 | model_coef | -0.283084 | income[Interval(13.693, 128.536, closed='right')] |
52 | model_coef | 0.048146 | income[Interval(4.194, 13.693, closed='right')] |
As we can see - we managed to change the bucket sizes of income to have only 3 buckets, and we lumped the age_group to two groups (and collapsed together "small" buckets into the _lumped_other bucket).
Lastly, notice that if we omit a variable from transformations, it will not be available for the model construction (This behavior might change in the future).
transformations = {
# "age_group": lambda x: fct_lump(x, 0.25),
"gender": lambda x: x,
# "income": lambda x: quantize(x.fillna(x.mean()), q=3),
}
adjusted = sample_with_target.adjust(
# method="ipw",
transformations=transformations,
# formula=formula,
# penalty_factor=penalty_factor,
# max_de=None,
)
adj_diag = adjusted.diagnostics()
adj_diag.query("metric == 'model_coef'")
INFO (2025-03-20 17:32:27,897) [ipw/ipw (line 399)]: Starting ipw function
INFO (2025-03-20 17:32:27,900) [adjustment/apply_transformations (line 305)]: Adding the variables: []
INFO (2025-03-20 17:32:27,900) [adjustment/apply_transformations (line 306)]: Transforming the variables: ['gender']
WARNING (2025-03-20 17:32:27,901) [adjustment/apply_transformations (line 340)]: Dropping the variables: ['age_group', 'income']
INFO (2025-03-20 17:32:27,902) [adjustment/apply_transformations (line 343)]: Final variables in output: ['gender']
INFO (2025-03-20 17:32:27,904) [ipw/ipw (line 433)]: Building model matrix
INFO (2025-03-20 17:32:27,941) [ipw/ipw (line 455)]: The formula used to build the model matrix: ['gender + _is_na_gender']
INFO (2025-03-20 17:32:27,941) [ipw/ipw (line 458)]: The number of columns in the model matrix: 4
INFO (2025-03-20 17:32:27,942) [ipw/ipw (line 459)]: The number of rows in the model matrix: 11000
INFO (2025-03-20 17:32:36,547) [ipw/ipw (line 578)]: Done with sklearn
INFO (2025-03-20 17:32:36,548) [ipw/ipw (line 584)]: max_de: None
INFO (2025-03-20 17:32:36,548) [ipw/ipw (line 605)]: Starting model selection
INFO (2025-03-20 17:32:36,551) [ipw/ipw (line 638)]: Chosen lambda: 0.20570886693214954
INFO (2025-03-20 17:32:36,552) [ipw/ipw (line 654)]: Proportion null deviance explained 0.0264854344569313
WARNING (2025-03-20 17:32:36,553) [ipw/ipw (line 662)]: The propensity model has low fraction null deviance explained (0.0264854344569313). Results may not be accurate
INFO (2025-03-20 17:32:36,557) [sample_class/diagnostics (line 839)]: Starting computation of diagnostics of the fitting
INFO (2025-03-20 17:32:36,819) [sample_class/diagnostics (line 1049)]: Done computing diagnostics
metric | val | var | |
---|---|---|---|
44 | model_coef | -0.035465 | intercept |
45 | model_coef | 0.001051 | _is_na_gender[T.True] |
46 | model_coef | -0.141695 | gender[Female] |
47 | model_coef | 0.136225 | gender[Male] |
48 | model_coef | 0.001051 | gender[_NA] |
As we can see, only gender was included in the model.
# TODO: add more examples about how add_na works
# TODO: add more examples about rare values in categorical variables and how they are grouped together.
Creating new variables¶
In the next example we will create several new transformations of income.
The info gives information on which variables were added, which were transformed, and what is the final variables in the output.
The x in the lambda function can have one of two meanings:
- When the keys in the dict match the exact names of the variables in the DataFrame (e.g.: "income"), then the lambda function treats x as the pandas.Series of that variable.
- If the name of the key does NOT exist in the DataFrame (e.g.: "income_squared"), then x will become the DataFrame of the data.
from balance.util import fct_lump, quantize
transformations = {
"age_group": lambda x: x,
"gender": lambda x: x,
"income": lambda x: x,
"income_squared": lambda x: x.income**2,
"income_buckets": lambda x: quantize(x.income.fillna(x.income.mean()), q=3),
}
adjusted = sample_with_target.adjust(
# method="ipw",
transformations=transformations,
# formula=formula,
# penalty_factor=penalty_factor,
# max_de=None,
)
adj_diag = adjusted.diagnostics()
adj_diag.query("metric == 'model_coef'")
INFO (2025-03-20 17:32:36,838) [ipw/ipw (line 399)]: Starting ipw function
INFO (2025-03-20 17:32:36,840) [adjustment/apply_transformations (line 305)]: Adding the variables: ['income_squared', 'income_buckets']
INFO (2025-03-20 17:32:36,841) [adjustment/apply_transformations (line 306)]: Transforming the variables: ['age_group', 'gender', 'income']
INFO (2025-03-20 17:32:36,846) [adjustment/apply_transformations (line 343)]: Final variables in output: ['income_squared', 'income_buckets', 'age_group', 'gender', 'income']
INFO (2025-03-20 17:32:36,857) [ipw/ipw (line 433)]: Building model matrix
INFO (2025-03-20 17:32:36,955) [ipw/ipw (line 455)]: The formula used to build the model matrix: ['income_squared + income_buckets + income + gender + age_group + _is_na_gender']
INFO (2025-03-20 17:32:36,956) [ipw/ipw (line 458)]: The number of columns in the model matrix: 11
INFO (2025-03-20 17:32:36,956) [ipw/ipw (line 459)]: The number of rows in the model matrix: 11000
INFO (2025-03-20 17:32:58,350) [ipw/ipw (line 578)]: Done with sklearn
INFO (2025-03-20 17:32:58,351) [ipw/ipw (line 584)]: max_de: None
INFO (2025-03-20 17:32:58,352) [ipw/ipw (line 605)]: Starting model selection
INFO (2025-03-20 17:32:58,354) [ipw/ipw (line 638)]: Chosen lambda: 0.043506507030756265
INFO (2025-03-20 17:32:58,355) [ipw/ipw (line 654)]: Proportion null deviance explained 0.17223252304635372
INFO (2025-03-20 17:32:58,359) [sample_class/diagnostics (line 839)]: Starting computation of diagnostics of the fitting
INFO (2025-03-20 17:32:58,620) [sample_class/diagnostics (line 1049)]: Done computing diagnostics
metric | val | var | |
---|---|---|---|
44 | model_coef | 0.41745 | intercept |
45 | model_coef | 0.044654 | _is_na_gender[T.True] |
46 | model_coef | -0.194436 | age_group[T.25-34] |
47 | model_coef | -0.421207 | age_group[T.35-44] |
48 | model_coef | -0.521732 | age_group[T.45+] |
49 | model_coef | 0.325806 | gender[T.Male] |
50 | model_coef | 0.044654 | gender[T._NA] |
51 | model_coef | -0.26602 | income |
52 | model_coef | 0.113306 | income_buckets[Interval(-0.0009997440000000001... |
53 | model_coef | -0.166366 | income_buckets[Interval(13.693, 128.536, close... |
54 | model_coef | 0.032296 | income_buckets[Interval(4.194, 13.693, closed=... |
55 | model_coef | -0.185931 | income_squared |
Formula¶
The formula can accept a list of strings indicating how to combine the transformed variables together. It follows the formula notation from patsy.
For example, we can have an interaction between age_group and gender:
from balance.util import fct_lump_by, quantize
transformations = {
"age_group": lambda x: x,
"gender": lambda x: x,
"income": lambda x: quantize(x.fillna(x.mean()), q=20),
}
formula = ["age_group * gender"]
# the penalty is per elemnt in the list of formula:
# penalty_factor = [0.1, 0.1, 0.1]
adjusted = sample_with_target.adjust(
method="ipw",
transformations=transformations,
formula=formula,
# penalty_factor=penalty_factor,
# max_de=None,
)
adj_diag = adjusted.diagnostics()
adj_diag.query("metric == 'model_coef'")
INFO (2025-03-20 17:32:58,635) [ipw/ipw (line 399)]: Starting ipw function
INFO (2025-03-20 17:32:58,638) [adjustment/apply_transformations (line 305)]: Adding the variables: []
INFO (2025-03-20 17:32:58,638) [adjustment/apply_transformations (line 306)]: Transforming the variables: ['age_group', 'gender', 'income']
INFO (2025-03-20 17:32:58,643) [adjustment/apply_transformations (line 343)]: Final variables in output: ['age_group', 'gender', 'income']
INFO (2025-03-20 17:32:58,650) [ipw/ipw (line 433)]: Building model matrix
INFO (2025-03-20 17:32:58,714) [ipw/ipw (line 455)]: The formula used to build the model matrix: ['age_group * gender']
INFO (2025-03-20 17:32:58,714) [ipw/ipw (line 458)]: The number of columns in the model matrix: 12
INFO (2025-03-20 17:32:58,715) [ipw/ipw (line 459)]: The number of rows in the model matrix: 11000
INFO (2025-03-20 17:33:15,969) [ipw/ipw (line 578)]: Done with sklearn
INFO (2025-03-20 17:33:15,970) [ipw/ipw (line 584)]: max_de: None
INFO (2025-03-20 17:33:15,971) [ipw/ipw (line 605)]: Starting model selection
INFO (2025-03-20 17:33:15,973) [ipw/ipw (line 638)]: Chosen lambda: 0.0894967426547247
INFO (2025-03-20 17:33:15,974) [ipw/ipw (line 654)]: Proportion null deviance explained 0.11496302030801142
INFO (2025-03-20 17:33:15,979) [sample_class/diagnostics (line 839)]: Starting computation of diagnostics of the fitting
INFO (2025-03-20 17:33:16,238) [sample_class/diagnostics (line 1049)]: Done computing diagnostics
metric | val | var | |
---|---|---|---|
44 | model_coef | -0.348132 | intercept |
45 | model_coef | 0.334726 | age_group[18-24] |
46 | model_coef | 0.005414 | age_group[25-34] |
47 | model_coef | -0.160449 | age_group[35-44] |
48 | model_coef | -0.280501 | age_group[45+] |
49 | model_coef | 0.032004 | age_group[T.25-34]:gender[T.Male] |
50 | model_coef | 0.016476 | age_group[T.25-34]:gender[T._NA] |
51 | model_coef | -0.037461 | age_group[T.35-44]:gender[T.Male] |
52 | model_coef | -0.046181 | age_group[T.35-44]:gender[T._NA] |
53 | model_coef | -0.031939 | age_group[T.45+]:gender[T.Male] |
54 | model_coef | -0.042359 | age_group[T.45+]:gender[T._NA] |
55 | model_coef | 0.271811 | gender[T.Male] |
56 | model_coef | 0.06233 | gender[T._NA] |
As we can see, the formula makes it so that we have combinations of age_group and gender, as well as a main effects of age_group and gender. Since income was not in the formula, it is not included in the model.
Formula and penalty_factor¶
The formula can be provided as several strings, and then the penalty factor can indicate how much the model should focus to adjust to that element of the formula. Larger penalty factors means that element will be less corrected.
The next two examples shows how in one case we focus on correcting for income, and in the second case we focus to correct for age and gender.
transformations = {
"age_group": lambda x: x,
"gender": lambda x: x,
"income": lambda x: x,
}
formula = ["age_group + gender", "income"]
# the penalty is per elemnt in the list of formula:
penalty_factor = [10, 0.1]
adjusted = sample_with_target.adjust(
method="ipw",
transformations=transformations,
formula=formula,
penalty_factor=penalty_factor,
# max_de=None,
)
adj_diag = adjusted.diagnostics()
adj_diag.query("metric == 'model_coef'")
INFO (2025-03-20 17:33:16,253) [ipw/ipw (line 399)]: Starting ipw function
INFO (2025-03-20 17:33:16,256) [adjustment/apply_transformations (line 305)]: Adding the variables: []
INFO (2025-03-20 17:33:16,257) [adjustment/apply_transformations (line 306)]: Transforming the variables: ['age_group', 'gender', 'income']
INFO (2025-03-20 17:33:16,258) [adjustment/apply_transformations (line 343)]: Final variables in output: ['age_group', 'gender', 'income']
INFO (2025-03-20 17:33:16,264) [ipw/ipw (line 433)]: Building model matrix
INFO (2025-03-20 17:33:16,329) [ipw/ipw (line 455)]: The formula used to build the model matrix: ['age_group + gender', 'income']
INFO (2025-03-20 17:33:16,330) [ipw/ipw (line 458)]: The number of columns in the model matrix: 7
INFO (2025-03-20 17:33:16,331) [ipw/ipw (line 459)]: The number of rows in the model matrix: 11000
INFO (2025-03-20 17:33:44,246) [ipw/ipw (line 578)]: Done with sklearn
INFO (2025-03-20 17:33:44,247) [ipw/ipw (line 584)]: max_de: None
INFO (2025-03-20 17:33:44,247) [ipw/ipw (line 605)]: Starting model selection
INFO (2025-03-20 17:33:44,251) [ipw/ipw (line 638)]: Chosen lambda: 0.0009460271806598614
INFO (2025-03-20 17:33:44,251) [ipw/ipw (line 654)]: Proportion null deviance explained 0.17289162681789683
INFO (2025-03-20 17:33:44,256) [sample_class/diagnostics (line 839)]: Starting computation of diagnostics of the fitting
INFO (2025-03-20 17:33:44,518) [sample_class/diagnostics (line 1049)]: Done computing diagnostics
metric | val | var | |
---|---|---|---|
44 | model_coef | 0.243384 | intercept |
45 | model_coef | 3.238671 | age_group[18-24] |
46 | model_coef | 0.394707 | age_group[25-34] |
47 | model_coef | -1.759083 | age_group[35-44] |
48 | model_coef | -2.919449 | age_group[45+] |
49 | model_coef | 2.599977 | gender[T.Male] |
50 | model_coef | 0.487265 | gender[T._NA] |
51 | model_coef | -0.073744 | income |
The above example corrected more to income. As we can see, age and gender got 0 correction (since their penalty was so high). Let's now over correct for age and gender:
transformations = {
"age_group": lambda x: x,
"gender": lambda x: x,
"income": lambda x: x,
}
formula = ["age_group + gender", "income"]
# the penalty is per elemnt in the list of formula:
penalty_factor = [0.1, 10] # this is flipped
adjusted = sample_with_target.adjust(
method="ipw",
transformations=transformations,
formula=formula,
penalty_factor=penalty_factor,
# max_de=None,
)
adj_diag = adjusted.diagnostics()
adj_diag.query("metric == 'model_coef'")
INFO (2025-03-20 17:33:44,533) [ipw/ipw (line 399)]: Starting ipw function
INFO (2025-03-20 17:33:44,536) [adjustment/apply_transformations (line 305)]: Adding the variables: []
INFO (2025-03-20 17:33:44,536) [adjustment/apply_transformations (line 306)]: Transforming the variables: ['age_group', 'gender', 'income']
INFO (2025-03-20 17:33:44,538) [adjustment/apply_transformations (line 343)]: Final variables in output: ['age_group', 'gender', 'income']
INFO (2025-03-20 17:33:44,543) [ipw/ipw (line 433)]: Building model matrix
INFO (2025-03-20 17:33:44,608) [ipw/ipw (line 455)]: The formula used to build the model matrix: ['age_group + gender', 'income']
INFO (2025-03-20 17:33:44,609) [ipw/ipw (line 458)]: The number of columns in the model matrix: 7
INFO (2025-03-20 17:33:44,610) [ipw/ipw (line 459)]: The number of rows in the model matrix: 11000
INFO (2025-03-20 17:34:10,532) [ipw/ipw (line 578)]: Done with sklearn
INFO (2025-03-20 17:34:10,533) [ipw/ipw (line 584)]: max_de: None
INFO (2025-03-20 17:34:10,534) [ipw/ipw (line 605)]: Starting model selection
INFO (2025-03-20 17:34:10,537) [ipw/ipw (line 638)]: Chosen lambda: 0.0012484913627071772
INFO (2025-03-20 17:34:10,537) [ipw/ipw (line 654)]: Proportion null deviance explained 0.17058848391571657
INFO (2025-03-20 17:34:10,542) [sample_class/diagnostics (line 839)]: Starting computation of diagnostics of the fitting
INFO (2025-03-20 17:34:10,806) [sample_class/diagnostics (line 1049)]: Done computing diagnostics
metric | val | var | |
---|---|---|---|
44 | model_coef | -0.436751 | intercept |
45 | model_coef | 0.053355 | age_group[18-24] |
46 | model_coef | 0.014525 | age_group[25-34] |
47 | model_coef | -0.014981 | age_group[35-44] |
48 | model_coef | -0.037504 | age_group[45+] |
49 | model_coef | 0.041851 | gender[T.Male] |
50 | model_coef | 0.011851 | gender[T._NA] |
51 | model_coef | -3.824697 | income |
In the above case, income basically got 0 correction.
We can add two versions of income, and give each of them a higher penalty than the age and gender:
from balance.util import fct_lump_by, quantize
transformations = {
"age_group": lambda x: x,
"gender": lambda x: x,
"income": lambda x: x,
"income_buckets": lambda x: quantize(x.income.fillna(x.income.mean()), q=4),
}
formula = ["age_group + gender", "income", "income_buckets"]
# the penalty is per elemnt in the list of formula:
penalty_factor = [1, 2, 2]
adjusted = sample_with_target.adjust(
method="ipw",
transformations=transformations,
formula=formula,
penalty_factor=penalty_factor,
# max_de=None,
)
adj_diag = adjusted.diagnostics()
adj_diag.query("metric == 'model_coef'")
INFO (2025-03-20 17:34:10,821) [ipw/ipw (line 399)]: Starting ipw function
INFO (2025-03-20 17:34:10,824) [adjustment/apply_transformations (line 305)]: Adding the variables: ['income_buckets']
INFO (2025-03-20 17:34:10,825) [adjustment/apply_transformations (line 306)]: Transforming the variables: ['age_group', 'gender', 'income']
INFO (2025-03-20 17:34:10,830) [adjustment/apply_transformations (line 343)]: Final variables in output: ['income_buckets', 'age_group', 'gender', 'income']
INFO (2025-03-20 17:34:10,838) [ipw/ipw (line 433)]: Building model matrix
INFO (2025-03-20 17:34:10,937) [ipw/ipw (line 455)]: The formula used to build the model matrix: ['age_group + gender', 'income', 'income_buckets']
INFO (2025-03-20 17:34:10,937) [ipw/ipw (line 458)]: The number of columns in the model matrix: 11
INFO (2025-03-20 17:34:10,938) [ipw/ipw (line 459)]: The number of rows in the model matrix: 11000
INFO (2025-03-20 17:34:30,287) [ipw/ipw (line 578)]: Done with sklearn
INFO (2025-03-20 17:34:30,288) [ipw/ipw (line 584)]: max_de: None
INFO (2025-03-20 17:34:30,288) [ipw/ipw (line 605)]: Starting model selection
INFO (2025-03-20 17:34:30,291) [ipw/ipw (line 638)]: Chosen lambda: 0.043506507030756265
INFO (2025-03-20 17:34:30,292) [ipw/ipw (line 654)]: Proportion null deviance explained 0.17297124065566938
INFO (2025-03-20 17:34:30,296) [sample_class/diagnostics (line 839)]: Starting computation of diagnostics of the fitting
INFO (2025-03-20 17:34:30,557) [sample_class/diagnostics (line 1049)]: Done computing diagnostics
metric | val | var | |
---|---|---|---|
44 | model_coef | -0.286191 | intercept |
45 | model_coef | 0.378645 | age_group[18-24] |
46 | model_coef | 0.047595 | age_group[25-34] |
47 | model_coef | -0.199745 | age_group[35-44] |
48 | model_coef | -0.350982 | age_group[45+] |
49 | model_coef | 0.321907 | gender[T.Male] |
50 | model_coef | 0.074456 | gender[T._NA] |
51 | model_coef | -0.418356 | income |
52 | model_coef | 0.216575 | income_buckets[Interval(-0.0009997440000000001... |
53 | model_coef | -0.366924 | income_buckets[Interval(17.694, 128.536, close... |
54 | model_coef | 0.12985 | income_buckets[Interval(2.53, 8.211, closed='r... |
55 | model_coef | -0.05101 | income_buckets[Interval(8.211, 17.694, closed=... |
Another way is to create a formula for several variations of each variable, and give each a penalty of 1. For example:
from balance.util import fct_lump_by, quantize
transformations = {
"age_group": lambda x: x,
"gender": lambda x: x,
"income": lambda x: x,
"income_buckets": lambda x: quantize(x.income.fillna(x.income.mean()), q=4),
}
formula = ["age_group", "gender", "income + income_buckets"]
# the penalty is per elemnt in the list of formula:
penalty_factor = [1, 1, 1]
adjusted = sample_with_target.adjust(
method="ipw",
transformations=transformations,
formula=formula,
penalty_factor=penalty_factor,
# max_de=None,
)
adj_diag = adjusted.diagnostics()
adj_diag.query("metric == 'model_coef'")
INFO (2025-03-20 17:34:30,573) [ipw/ipw (line 399)]: Starting ipw function
INFO (2025-03-20 17:34:30,576) [adjustment/apply_transformations (line 305)]: Adding the variables: ['income_buckets']
INFO (2025-03-20 17:34:30,576) [adjustment/apply_transformations (line 306)]: Transforming the variables: ['age_group', 'gender', 'income']
INFO (2025-03-20 17:34:30,582) [adjustment/apply_transformations (line 343)]: Final variables in output: ['income_buckets', 'age_group', 'gender', 'income']
INFO (2025-03-20 17:34:30,591) [ipw/ipw (line 433)]: Building model matrix
INFO (2025-03-20 17:34:30,691) [ipw/ipw (line 455)]: The formula used to build the model matrix: ['age_group', 'gender', 'income + income_buckets']
INFO (2025-03-20 17:34:30,692) [ipw/ipw (line 458)]: The number of columns in the model matrix: 12
INFO (2025-03-20 17:34:30,693) [ipw/ipw (line 459)]: The number of rows in the model matrix: 11000
INFO (2025-03-20 17:34:46,410) [ipw/ipw (line 578)]: Done with sklearn
INFO (2025-03-20 17:34:46,411) [ipw/ipw (line 584)]: max_de: None
INFO (2025-03-20 17:34:46,412) [ipw/ipw (line 605)]: Starting model selection
INFO (2025-03-20 17:34:46,415) [ipw/ipw (line 638)]: Chosen lambda: 0.0894967426547247
INFO (2025-03-20 17:34:46,416) [ipw/ipw (line 654)]: Proportion null deviance explained 0.17297566853458446
INFO (2025-03-20 17:34:46,420) [sample_class/diagnostics (line 839)]: Starting computation of diagnostics of the fitting
INFO (2025-03-20 17:34:46,683) [sample_class/diagnostics (line 1049)]: Done computing diagnostics
metric | val | var | |
---|---|---|---|
44 | model_coef | 0.084942 | intercept |
45 | model_coef | 0.327596 | age_group[18-24] |
46 | model_coef | 0.039282 | age_group[25-34] |
47 | model_coef | -0.176394 | age_group[35-44] |
48 | model_coef | -0.296639 | age_group[45+] |
49 | model_coef | -0.163857 | gender[Female] |
50 | model_coef | 0.158443 | gender[Male] |
51 | model_coef | -0.000375 | gender[_NA] |
52 | model_coef | -0.258364 | income |
53 | model_coef | 0.122028 | income_buckets[Interval(-0.0009997440000000001... |
54 | model_coef | -0.213971 | income_buckets[Interval(17.694, 128.536, close... |
55 | model_coef | 0.076064 | income_buckets[Interval(2.53, 8.211, closed='r... |
56 | model_coef | -0.025416 | income_buckets[Interval(8.211, 17.694, closed=... |
# Defaults from the package
adjusted = sample_with_target.adjust(
# max_de=None,
)
print(adjusted.summary())
print(adjusted.outcomes().summary())
adjusted.covars().plot(library = "seaborn", dist_type = "kde")
INFO (2025-03-20 17:34:46,697) [ipw/ipw (line 399)]: Starting ipw function
INFO (2025-03-20 17:34:46,700) [adjustment/apply_transformations (line 305)]: Adding the variables: []
INFO (2025-03-20 17:34:46,701) [adjustment/apply_transformations (line 306)]: Transforming the variables: ['gender', 'age_group', 'income']
INFO (2025-03-20 17:34:46,709) [adjustment/apply_transformations (line 343)]: Final variables in output: ['gender', 'age_group', 'income']
INFO (2025-03-20 17:34:46,715) [ipw/ipw (line 433)]: Building model matrix
INFO (2025-03-20 17:34:46,809) [ipw/ipw (line 455)]: The formula used to build the model matrix: ['income + gender + age_group + _is_na_gender']
INFO (2025-03-20 17:34:46,810) [ipw/ipw (line 458)]: The number of columns in the model matrix: 16
INFO (2025-03-20 17:34:46,810) [ipw/ipw (line 459)]: The number of rows in the model matrix: 11000
INFO (2025-03-20 17:35:03,133) [ipw/ipw (line 578)]: Done with sklearn
INFO (2025-03-20 17:35:03,134) [ipw/ipw (line 584)]: max_de: None
INFO (2025-03-20 17:35:03,134) [ipw/ipw (line 605)]: Starting model selection
INFO (2025-03-20 17:35:03,137) [ipw/ipw (line 638)]: Chosen lambda: 0.041158338186664825
INFO (2025-03-20 17:35:03,137) [ipw/ipw (line 654)]: Proportion null deviance explained 0.17265121909892267
Covar ASMD reduction: 63.4%, design effect: 1.880 Covar ASMD (7 variables): 0.327 -> 0.119 Model performance: Model proportion deviance explained: 0.173 1 outcomes: ['happiness'] Mean outcomes (with 95% confidence intervals): source self target unadjusted self_ci target_ci unadjusted_ci happiness 53.297 56.278 48.559 (52.097, 54.496) (55.961, 56.595) (47.669, 49.449) Response rates (relative to number of respondents in sample): happiness n 1000.0 % 100.0 Response rates (relative to notnull rows in the target): happiness n 1000.0 % 10.0 Response rates (in the target): happiness n 10000.0 % 100.0
# No transformations at all
# transformations = None is just like using:
# transformations = {
# "age_group": lambda x: x,
# "gender": lambda x: x,
# "income": lambda x: x,
# }
adjusted = sample_with_target.adjust(
method="ipw",
transformations=None,
# formula=formula,
# penalty_factor=penalty_factor,
# max_de=None,
)
print(adjusted.summary())
print(adjusted.outcomes().summary())
adjusted.covars().plot(library = "seaborn", dist_type = "kde")
# slightly smaller design effect, slightly better ASMD reduction.
INFO (2025-03-20 17:35:04,101) [ipw/ipw (line 399)]: Starting ipw function
INFO (2025-03-20 17:35:04,103) [ipw/ipw (line 433)]: Building model matrix
INFO (2025-03-20 17:35:04,168) [ipw/ipw (line 455)]: The formula used to build the model matrix: ['income + gender + age_group + _is_na_gender']
INFO (2025-03-20 17:35:04,169) [ipw/ipw (line 458)]: The number of columns in the model matrix: 8
INFO (2025-03-20 17:35:04,169) [ipw/ipw (line 459)]: The number of rows in the model matrix: 11000
INFO (2025-03-20 17:35:19,399) [ipw/ipw (line 578)]: Done with sklearn
INFO (2025-03-20 17:35:19,399) [ipw/ipw (line 584)]: max_de: None
INFO (2025-03-20 17:35:19,400) [ipw/ipw (line 605)]: Starting model selection
INFO (2025-03-20 17:35:19,403) [ipw/ipw (line 638)]: Chosen lambda: 0.0368353720078807
INFO (2025-03-20 17:35:19,403) [ipw/ipw (line 654)]: Proportion null deviance explained 0.17353587606008936
Covar ASMD reduction: 68.5%, design effect: 2.087 Covar ASMD (7 variables): 0.327 -> 0.103 Model performance: Model proportion deviance explained: 0.174 1 outcomes: ['happiness'] Mean outcomes (with 95% confidence intervals): source self target unadjusted self_ci target_ci unadjusted_ci happiness 53.731 56.278 48.559 (52.513, 54.949) (55.961, 56.595) (47.669, 49.449) Response rates (relative to number of respondents in sample): happiness n 1000.0 % 100.0 Response rates (relative to notnull rows in the target): happiness n 1000.0 % 10.0 Response rates (in the target): happiness n 10000.0 % 100.0
# No transformations at all
transformations = None
# But passing a squared term of income to the formula:
formula = ["age_group + gender + income + income**2"]
# the penalty is per elemnt in the list of formula:
# penalty_factor = [1]
adjusted = sample_with_target.adjust(
method="ipw",
transformations=transformations,
formula=formula,
# penalty_factor=penalty_factor,
# max_de=None,
)
print(adjusted.summary())
print(adjusted.outcomes().summary())
adjusted.covars().plot(library = "seaborn", dist_type = "kde")
# Adding income**2 to the formula led to lower Deff but also lower ASMD reduction.
INFO (2025-03-20 17:35:20,345) [ipw/ipw (line 399)]: Starting ipw function
INFO (2025-03-20 17:35:20,346) [ipw/ipw (line 433)]: Building model matrix
INFO (2025-03-20 17:35:20,411) [ipw/ipw (line 455)]: The formula used to build the model matrix: ['age_group + gender + income + income**2']
INFO (2025-03-20 17:35:20,411) [ipw/ipw (line 458)]: The number of columns in the model matrix: 7
INFO (2025-03-20 17:35:20,412) [ipw/ipw (line 459)]: The number of rows in the model matrix: 11000
INFO (2025-03-20 17:35:34,923) [ipw/ipw (line 578)]: Done with sklearn
INFO (2025-03-20 17:35:34,924) [ipw/ipw (line 584)]: max_de: None
INFO (2025-03-20 17:35:34,925) [ipw/ipw (line 605)]: Starting model selection
INFO (2025-03-20 17:35:34,928) [ipw/ipw (line 638)]: Chosen lambda: 0.0574164245593571
INFO (2025-03-20 17:35:34,929) [ipw/ipw (line 654)]: Proportion null deviance explained 0.17296221715396187
Covar ASMD reduction: 60.7%, design effect: 1.925 Covar ASMD (7 variables): 0.327 -> 0.128 Model performance: Model proportion deviance explained: 0.173 1 outcomes: ['happiness'] Mean outcomes (with 95% confidence intervals): source self target unadjusted self_ci target_ci unadjusted_ci happiness 53.258 56.278 48.559 (52.072, 54.445) (55.961, 56.595) (47.669, 49.449) Response rates (relative to number of respondents in sample): happiness n 1000.0 % 100.0 Response rates (relative to notnull rows in the target): happiness n 1000.0 % 10.0 Response rates (in the target): happiness n 10000.0 % 100.0
transformations = {
"age_group": lambda x: x,
"gender": lambda x: x,
"income": lambda x: x,
"income_buckets": lambda x: quantize(x.income.fillna(x.income.mean()), q=20),
}
formula = ["age_group + gender", "income_buckets"]
# the penalty is per elemnt in the list of formula:
penalty_factor = [1, 0.1]
adjusted = sample_with_target.adjust(
method="ipw",
transformations=transformations,
formula=formula,
penalty_factor=penalty_factor,
# max_de=None,
)
print(adjusted.summary())
print(adjusted.outcomes().summary())
adjusted.covars().plot(library = "seaborn", dist_type = "kde")
# By adding income_buckets and using it instead of income, as well as putting more weight in it in terms of penalty
# we managed to correct income quite well, but at the expense of age and gender.
INFO (2025-03-20 17:35:35,935) [ipw/ipw (line 399)]: Starting ipw function
INFO (2025-03-20 17:35:35,938) [adjustment/apply_transformations (line 305)]: Adding the variables: ['income_buckets']
INFO (2025-03-20 17:35:35,939) [adjustment/apply_transformations (line 306)]: Transforming the variables: ['age_group', 'gender', 'income']
INFO (2025-03-20 17:35:35,944) [adjustment/apply_transformations (line 343)]: Final variables in output: ['income_buckets', 'age_group', 'gender', 'income']
INFO (2025-03-20 17:35:35,953) [ipw/ipw (line 433)]: Building model matrix
INFO (2025-03-20 17:35:36,051) [ipw/ipw (line 455)]: The formula used to build the model matrix: ['age_group + gender', 'income_buckets']
INFO (2025-03-20 17:35:36,051) [ipw/ipw (line 458)]: The number of columns in the model matrix: 26
INFO (2025-03-20 17:35:36,052) [ipw/ipw (line 459)]: The number of rows in the model matrix: 11000
INFO (2025-03-20 17:35:58,420) [ipw/ipw (line 578)]: Done with sklearn
INFO (2025-03-20 17:35:58,421) [ipw/ipw (line 584)]: max_de: None
INFO (2025-03-20 17:35:58,422) [ipw/ipw (line 605)]: Starting model selection
INFO (2025-03-20 17:35:58,424) [ipw/ipw (line 638)]: Chosen lambda: 0.09460271806598614
INFO (2025-03-20 17:35:58,425) [ipw/ipw (line 654)]: Proportion null deviance explained 0.17682033095496275
Covar ASMD reduction: 70.0%, design effect: 2.391 Covar ASMD (7 variables): 0.327 -> 0.098 Model performance: Model proportion deviance explained: 0.177 1 outcomes: ['happiness'] Mean outcomes (with 95% confidence intervals): source self target unadjusted self_ci target_ci unadjusted_ci happiness 52.289 56.278 48.559 (51.06, 53.519) (55.961, 56.595) (47.669, 49.449) Response rates (relative to number of respondents in sample): happiness n 1000.0 % 100.0 Response rates (relative to notnull rows in the target): happiness n 1000.0 % 10.0 Response rates (in the target): happiness n 10000.0 % 100.0
CBPS¶
Let's see if we can improve on CBPS a bit.
# Defaults from the package
adjusted = sample_with_target.adjust(
method = "cbps",
# max_de=None,
)
print(adjusted.summary())
print(adjusted.outcomes().summary())
adjusted.covars().plot(library = "seaborn", dist_type = "kde")
# CBPS already corrects a lot. Let's see if we can make it correct a tiny bit more.
INFO (2025-03-20 17:35:59,343) [cbps/cbps (line 411)]: Starting cbps function
INFO (2025-03-20 17:35:59,346) [adjustment/apply_transformations (line 305)]: Adding the variables: []
INFO (2025-03-20 17:35:59,346) [adjustment/apply_transformations (line 306)]: Transforming the variables: ['gender', 'age_group', 'income']
INFO (2025-03-20 17:35:59,355) [adjustment/apply_transformations (line 343)]: Final variables in output: ['gender', 'age_group', 'income']
INFO (2025-03-20 17:35:59,455) [cbps/cbps (line 461)]: The formula used to build the model matrix: ['income + gender + age_group + _is_na_gender']
INFO (2025-03-20 17:35:59,457) [cbps/cbps (line 473)]: The number of columns in the model matrix: 16
INFO (2025-03-20 17:35:59,457) [cbps/cbps (line 474)]: The number of rows in the model matrix: 11000
INFO (2025-03-20 17:35:59,462) [cbps/cbps (line 543)]: Finding initial estimator for GMM optimization
INFO (2025-03-20 17:35:59,517) [cbps/cbps (line 570)]: Finding initial estimator for GMM optimization that minimizes the balance loss
INFO (2025-03-20 17:35:59,773) [cbps/cbps (line 605)]: Running GMM optimization
INFO (2025-03-20 17:36:00,219) [cbps/cbps (line 734)]: Done cbps function
Covar ASMD reduction: 77.4%, design effect: 2.754 Covar ASMD (7 variables): 0.327 -> 0.074 1 outcomes: ['happiness'] Mean outcomes (with 95% confidence intervals): source self target unadjusted self_ci target_ci unadjusted_ci happiness 54.366 56.278 48.559 (53.003, 55.73) (55.961, 56.595) (47.669, 49.449) Response rates (relative to number of respondents in sample): happiness n 1000.0 % 100.0 Response rates (relative to notnull rows in the target): happiness n 1000.0 % 10.0 Response rates (in the target): happiness n 10000.0 % 100.0
import numpy as np
# No transformations at all
transformations = {
"age_group": lambda x: x,
"gender": lambda x: x,
# "income": lambda x: x,
"income_log": lambda x: np.log(x.income.fillna(x.income.mean())),
"income_buckets": lambda x: quantize(x.income.fillna(x.income.mean()), q=5),
}
formula = ["age_group + gender + income_log * income_buckets"]
adjusted = sample_with_target.adjust(
method="cbps",
transformations=transformations,
formula=formula,
# penalty_factor=penalty_factor, # CBPS seems to ignore the penalty factor.
# max_de=None,
)
print(adjusted.summary())
print(adjusted.outcomes().summary())
adjusted.covars().plot(library="seaborn", dist_type="kde")
# Trying various transformations gives slightly different results (some effect on the outcome, Deff and ASMD) - but nothing too major here.
INFO (2025-03-20 17:36:01,197) [cbps/cbps (line 411)]: Starting cbps function
INFO (2025-03-20 17:36:01,200) [adjustment/apply_transformations (line 305)]: Adding the variables: ['income_log', 'income_buckets']
INFO (2025-03-20 17:36:01,200) [adjustment/apply_transformations (line 306)]: Transforming the variables: ['age_group', 'gender']
WARNING (2025-03-20 17:36:01,206) [adjustment/apply_transformations (line 340)]: Dropping the variables: ['income']
INFO (2025-03-20 17:36:01,206) [adjustment/apply_transformations (line 343)]: Final variables in output: ['income_log', 'income_buckets', 'age_group', 'gender']
INFO (2025-03-20 17:36:01,313) [cbps/cbps (line 461)]: The formula used to build the model matrix: ['age_group + gender + income_log * income_buckets']
INFO (2025-03-20 17:36:01,314) [cbps/cbps (line 473)]: The number of columns in the model matrix: 15
INFO (2025-03-20 17:36:01,315) [cbps/cbps (line 474)]: The number of rows in the model matrix: 11000
INFO (2025-03-20 17:36:01,320) [cbps/cbps (line 543)]: Finding initial estimator for GMM optimization
INFO (2025-03-20 17:36:01,420) [cbps/cbps (line 570)]: Finding initial estimator for GMM optimization that minimizes the balance loss
INFO (2025-03-20 17:36:01,730) [cbps/cbps (line 605)]: Running GMM optimization
INFO (2025-03-20 17:36:02,161) [cbps/cbps (line 734)]: Done cbps function
Covar ASMD reduction: 82.1%, design effect: 3.030 Covar ASMD (7 variables): 0.327 -> 0.059 1 outcomes: ['happiness'] Mean outcomes (with 95% confidence intervals): source self target unadjusted self_ci target_ci unadjusted_ci happiness 54.432 56.278 48.559 (53.042, 55.822) (55.961, 56.595) (47.669, 49.449) Response rates (relative to number of respondents in sample): happiness n 1000.0 % 100.0 Response rates (relative to notnull rows in the target): happiness n 1000.0 % 10.0 Response rates (in the target): happiness n 10000.0 % 100.0
# Sessions info
import session_info
session_info.show(html=False, dependencies=True)
----- balance 0.10.0 numpy 1.26.4 pandas 2.0.3 session_info 1.0.0 ----- PIL 11.1.0 anyio NA arrow 1.3.0 asttokens NA attr 25.3.0 attrs 25.3.0 babel 2.17.0 certifi 2025.01.31 charset_normalizer 3.4.1 comm 0.2.2 cycler 0.12.1 cython_runtime NA dateutil 2.9.0.post0 debugpy 1.8.13 decorator 5.2.1 defusedxml 0.7.1 exceptiongroup 1.2.2 executing 2.2.0 fastjsonschema NA fqdn NA idna 3.10 importlib_metadata NA importlib_resources NA ipfn NA ipykernel 6.29.5 isoduration NA jedi 0.19.2 jinja2 3.1.6 joblib 1.4.2 json5 0.10.0 jsonpointer 3.0.0 jsonschema 4.23.0 jsonschema_specifications NA jupyter_events 0.12.0 jupyter_server 2.15.0 jupyterlab_server 2.27.3 kiwisolver 1.4.7 markupsafe 3.0.2 matplotlib 3.9.4 matplotlib_inline 0.1.7 mpl_toolkits NA narwhals 1.31.0 nbformat 5.10.4 overrides NA packaging 24.2 parso 0.8.4 patsy 1.0.1 pexpect 4.9.0 platformdirs 4.3.7 plotly 6.0.1 prometheus_client NA prompt_toolkit 3.0.50 psutil 7.0.0 ptyprocess 0.7.0 pure_eval 0.2.3 pydev_ipython NA pydevconsole NA pydevd 3.2.3 pydevd_file_utils NA pydevd_plugins NA pydevd_tracing NA pygments 2.19.1 pyparsing 3.2.1 pythonjsonlogger NA pytz 2025.1 referencing NA requests 2.32.3 rfc3339_validator 0.1.4 rfc3986_validator 0.1.1 rpds NA scipy 1.10.1 seaborn 0.13.2 send2trash NA six 1.17.0 sklearn 1.2.2 sniffio 1.3.1 sphinxcontrib NA stack_data 0.6.3 statsmodels 0.14.4 threadpoolctl 3.6.0 tornado 6.4.2 traitlets 5.14.3 typing_extensions NA uri_template NA urllib3 2.3.0 wcwidth 0.2.13 webcolors NA websocket 1.8.0 yaml 6.0.2 zipp NA zmq 26.3.0 zoneinfo NA ----- IPython 8.18.1 jupyter_client 8.6.3 jupyter_core 5.7.2 jupyterlab 4.3.6 notebook 7.3.3 ----- Python 3.9.21 (main, Dec 12 2024, 19:08:08) [GCC 13.2.0] Linux-6.8.0-1021-azure-x86_64-with-glibc2.39 ----- Session information updated at 2025-03-20 17:36
/opt/hostedtoolcache/Python/3.9.21/x64/lib/python3.9/site-packages/session_info/main.py:213: UserWarning: The '__version__' attribute is deprecated and will be removed in MarkupSafe 3.1. Use feature detection, or `importlib.metadata.version("markupsafe")`, instead.