### Install SyntheticControlMethods via pip Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/README.md Command to install the package from the Python Package Index. ```bash pip install SyntheticControlMethods ``` -------------------------------- ### POST /Synth Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Initializes a new Synthetic Control model instance with the provided dataset and configuration parameters. ```APIDOC ## POST /Synth ### Description Initializes the Synthetic Control model. This constructor processes the input dataframe and prepares the optimization parameters for the synthetic control estimation. ### Method POST ### Endpoint /Synth ### Parameters #### Request Body - **data** (Pandas DataFrame) - Required - Dataset ordered by unit then time. - **outcome_var** (str) - Required - Name of outcome column. - **id_var** (str) - Required - Name of unit indicator column. - **time_var** (str) - Required - Name of time column. - **treatment_period** (int) - Required - First observation affected by treatment. - **treated_unit** (str) - Required - Name of the unit that received treatment. - **n_optim** (int) - Optional - Default: 10. Number of initialization values for optimization. - **pen** (float/str) - Optional - Default: 0. Penalization coefficient or "auto". - **exclude_columns** (list) - Optional - Default: []. Columns to exclude from analysis. - **random_seed** (int) - Optional - Default: 0. Seed for reproducibility. ### Request Example { "data": "df", "outcome_var": "gdp", "id_var": "country", "time_var": "year", "treatment_period": 1990, "treated_unit": "Germany" } ### Response #### Success Response (200) - **Synth Object** (Object) - Returns an instance containing original_data and model methods. #### Response Example { "status": "success", "object_type": "Synth" } ``` -------------------------------- ### Fit and Visualize Synthetic Control Model Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/README.md Demonstrates how to load panel data, fit a classic Synthetic Control model to estimate the impact of an intervention, and generate visualization plots for the results. ```python #Import packages import pandas as pd from SyntheticControlMethods import Synth #Import data data = pd.read_csv("examples/german_reunification.csv") data = data.drop(columns="code", axis=1) #Fit classic Synthetic Control sc = Synth(data, "gdp", "country", "year", 1990, "West Germany", pen=0) #Visualize synthetic control sc.plot(["original", "pointwise", "cumulative"], treated_label="West Germany", synth_label="Synthetic West Germany", treatment_label="German Reunification") ``` -------------------------------- ### Synth Methods and Attributes Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Available methods and attributes for an initialized Synth object to perform analysis and validation. ```APIDOC ## Synth Methods and Attributes ### Description Once a Synth object is initialized, these methods and attributes are available for analysis, testing, and data retrieval. ### Methods - **Synth.plot(...)**: Generates visualizations for the synthetic control results. - **Synth.in_time_placebo(...)**: Executes in-time placebo tests to validate model robustness. - **Synth.in_space_placebo(...)**: Executes in-space placebo tests to validate model robustness. ### Attributes - **original_data**: Stores variables and results, including `weight_df` (weights of control units) and `comparison_df` (summary tables). ``` -------------------------------- ### Import necessary Python packages for Synthetic Control Methods Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Imports pandas for data manipulation and numpy for numerical operations. It also imports the Synth and DiffSynth classes from the SyntheticControlMethods library, which are essential for performing synthetic control analysis. ```python #Import packages import pandas as pd import numpy as np from SyntheticControlMethods import Synth, DiffSynth ``` -------------------------------- ### Visualize and Extract Model Results Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Provides methods to visualize the synthetic control performance and extract internal model data including weights, constants, and RMSPE metrics. ```python # Visualize results dsc.plot(["original", "pointwise", "cumulative"], treated_label="West Germany", synth_label="Synthetic West Germany", treatment_label="German Reunification") # Extract weights dsc.original_data.weight_df # Extract constant offset dsc.original_data.synth_constant # Extract RMSPE metrics dsc.original_data.rmspe_df # Extract comparison table dsc.original_data.comparison_df ``` -------------------------------- ### Examine Synthetic Control Weights Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Displays the weight distribution for the synthetic control. This output shows which control units contribute to the synthetic control and their respective weights. ```python sc_pen.original_data.weight_df ``` -------------------------------- ### Fit Synthetic Control Model Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Fits a synthetic control model using the Synth library. It requires the data, outcome variable, treatment unit identifier, time identifier, pre-treatment period end year, and the treated unit name. The n_optim parameter controls the optimization iterations. ```python sc = Synth(data, "gdp", "country", "year", 1990, "West Germany", n_optim=100) ``` -------------------------------- ### Visualize Synthetic Control Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Visualizes the original data, pointwise, and cumulative synthetic control. It requires the 'sc_pen' object and specifies labels for the treated and synthetic units, as well as the treatment period. ```python sc_pen.plot(["original", "pointwise", "cumulative"], treated_label="West Germany", synth_label="Synthetic West Germany", treatment_label="German Reunification") ``` -------------------------------- ### Perform In-time Placebo Test with Python Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Executes an in-time placebo test by shifting the treatment period to an earlier date and visualizes the results. This helps assess the stability of the synthetic control model over time. ```python #In-time placebo #Placebo treatment period is 1982, 8 years earlier dsc.in_time_placebo(1982) #Visualize dsc.plot(['in-time placebo'], treated_label="West Germany", synth_label="Synthetic West Germany") ``` -------------------------------- ### Compare Treated Unit with Synthetic Control Variables Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Presents a comparison of key variables between the treated unit and its synthetic control. It includes Weighted Absolute Mean Percentage Error (WMAPE) and importance scores for each variable. ```python sc_pen.original_data.comparison_df ``` -------------------------------- ### Perform and Visualize In-space Placebo Studies Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Executes an in-space placebo test by reassigning treatment to comparison units and visualizes the results using an RMSPE ratio plot to determine statistical significance. ```python #Compute in-space placebos sc.in_space_placebo(15) #Visualize sc.plot(['rmspe ratio']) ``` -------------------------------- ### Initialize and Fit Differenced Synthetic Control Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Initializes a DiffSynth model. The not_diff_cols parameter is used to exclude variables with high missingness from the first-difference transformation. ```python dsc = DiffSynth(data, "gdp", "country", "year", 1990, "West Germany", not_diff_cols=["schooling", "invest60", "invest70", "invest80"], n_optim=100) ``` -------------------------------- ### Perform and Visualize In-Time Placebo Test Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Conducts an in-time placebo test by shifting the treatment period to an earlier point in time. It then visualizes the results of this placebo test, comparing the treated unit with its synthetic counterpart under the altered treatment timeline. ```python sc_pen.in_time_placebo(1982, n_optim=10) #Visualize sc_pen.plot(['in-time placebo'], treated_label="West Germany", synth_label="Synthetic West Germany") ``` -------------------------------- ### Perform and Visualize In-Time Placebo Test in Python Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Executes an in-time placebo test by shifting the treatment period to a pre-treatment date to verify model predictive power. The function requires the placebo year as input and generates a plot to compare the synthetic control against the treated unit. ```python #In-time placebo #Placebo treatment period is 1982, 8 years earlier sc.in_time_placebo(1982, n_optim=10) #Visualize sc.plot(['in-time placebo'], treated_label="West Germany", synth_label="Synthetic West Germany") ``` -------------------------------- ### Fit Penalized Synthetic Control Model Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Initializes and fits a synthetic control model with automatic penalization. This approach balances pre-mixing and post-mixing similarity to reduce bias in non-linear covariate relationships. ```python #Fit synthetic control sc_pen = Synth(data, "gdp", "country", "year", 1990, "West Germany", n_optim=30, pen="auto", exclude_columns=["code"]) ``` -------------------------------- ### Perform In-space Placebo Test with Python Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Computes in-space placebo tests to evaluate the significance of the synthetic control estimate by comparing the treated unit against placebo units. Visualizes the RMSPE ratio to determine statistical significance. ```python #Compute in-space placebos dsc.in_space_placebo(15) #Visualize dsc.plot(['rmspe ratio']) ``` -------------------------------- ### Retrieve Synthetic Control Weight Matrix Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Retrieves the weight matrix for the fitted synthetic control solution. This matrix shows the proportion of each control unit contributing to the synthetic control. Weights are non-negative and sum to 1. Units with zero weight are excluded by default. ```python sc.original_data.weight_df ``` -------------------------------- ### Perform and Visualize In-Space Placebo Test Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Executes an in-space placebo test by randomly selecting control units to act as treated units. The results, specifically the ratio of RMSPEs, are then visualized to assess the robustness of the synthetic control. ```python sc_pen.in_space_placebo(15) #Visualize sc_pen.plot(['rmspe ratio']) ``` -------------------------------- ### Visualize Synthetic Control Results Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Visualizes the results of a fitted synthetic control model. It can display the original data, pointwise differences, and cumulative effects. Labels for the treated unit, synthetic unit, and treatment event can be customized. ```python sc.plot(["original", "pointwise", "cumulative"], treated_label="West Germany", synth_label="Synthetic West Germany", treatment_label="German Reunification") ``` -------------------------------- ### Load and prepare German Reunification data Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Loads the German Reunification dataset from a CSV file into a pandas DataFrame. It then removes the 'code' column, as it's a redundant unit identifier, and displays the first few rows of the cleaned data. ```python #Import German Reunification data from paper #Can be found in /datasets folder in repo data = pd.read_csv("datasets/german_reunification.csv") data = data.drop(columns="code", axis=1) data.head() ``` -------------------------------- ### Calculate RMSPE for Treated vs. Synthetic Unit Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Computes and displays the Root Mean Squared Prediction Error (RMSPE) for the treated unit compared to its synthetic counterpart, both before and after the treatment period. The 'post/pre' ratio indicates the relative change in prediction error. ```python sc_pen.original_data.rmspe_df ``` -------------------------------- ### Compare Covariate Values Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Compares the covariate values of the treated unit against the synthetic control unit. It also provides the Weighted Mean Absolute Pairwise Error (WMAPE) for each covariate and an 'Importance' metric indicating how crucial each covariate was in the optimization process. ```python sc.original_data.comparison_df ``` -------------------------------- ### Inspect Penalization Coefficient Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Retrieves and displays the penalization coefficient used in the synthetic control model. This value influences the regularization applied during the model fitting process. ```python sc_pen.original_data.pen ``` -------------------------------- ### Calculate Root Mean Square Prediction Error (RMSPE) Source: https://github.com/oscarengelbrektson/syntheticcontrolmethods/blob/master/examples/user_guide.ipynb Calculates the Root Mean Square Prediction Error (RMSPE) for the synthetic control compared to the treated unit, both in the pre-treatment and post-treatment periods. The post/pre ratio indicates the relative increase in prediction error after the intervention. ```python sc.original_data.rmspe_df ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.