Rim class¶
rim
¶
RIM (Raking) weighting algorithm implementation.
This module implements the core RIM (Iterative Proportional Fitting) algorithm used for survey weighting. It provides the Rim class for defining complex weighting schemes and the Rake class for executing the iterative algorithm.
Rim
¶
set_targets(targets, group_name=None)
¶
Quickly set simple weight targets, optionally assigning a group name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
targets
|
dict or list of dict
|
Dictionary mapping of DataFrame columns to target proportion list. |
required |
group_name
|
str
|
A name for the simple weight (group) created. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
add_group(name=None, filter_def=None, targets=None)
¶
Set weight groups using flexible filter and target defintions.
Main method to structure and specify complex weight schemes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the weight group. |
None
|
filter_def
|
str
|
An optional filter defintion given as a boolean expression in string format. Must be a valid input for the pandas DataFrame.query() method. |
None
|
targets
|
dict
|
Dictionary mapping of DataFrame columns to target proportion list. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
report(group=None)
¶
Internal API; will be removed in Weightipy 0.5.
Return the report for the specified group or all groups.
Requires verbose to be set to True on the Rim class. This is disabled by default for performance reasons.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
str
|
Name of the group to get the report for. If None, returns reports for all groups |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing the report(s) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If verbose is not enabled on the Rim class |
dataframe(df, key_column=None)
¶
Internal API; will be removed in Weightipy 0.5.
group_targets(group_targets)
¶
Set inter-group target proportions.
This will scale the weight factors per group to match the desired group proportions and thus effectively change each group's weighted total number of cases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group_targets
|
dict
|
A dictionary mapping of group names to the desired proportions |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If group_targets is not a dictionary |
validate()
¶
Internal API; will be removed in Weightipy 0.5.
Generate summary on scheme target variables to detect and handle missing data.
Returns:
| Type | Description |
|---|---|
DataFrame
|
A summary of missing entries and (rounded) mean/mode/median of value codes per target variable |