Open In App

How to perform Causal Analysis?

Causal analysis is a powerful technique that can help you understand why something happens and how to prevent or improve it, in other words, it helps us understand the relationships between different events or variables. Causal analysis can offer insightful information when doing research, fixing issues, or making judgments.

In this article, we’ll break down the concept of causal analysis, step by step, catering to beginners who are new to this intriguing field.



What is Causal Analysis?

Causal analysis is the process of identifying and addressing the causes and effects of a phenomenon, problem, or event. It is about figuring out how one variable (the cause) affects or determines another variable (the effect), as well as recognizing the relationships between various occurrences and how changes in one variable might affect another. For example, smoking causes lung cancer, or increasing the price of a product reduces its demand. To get useful conclusions from data, this technique is frequently applied in disciplines including science, economics, and medicine. Causal analysis can help you answer questions such as:



To perform causal analysis, you need to collect and analyze data that can support or refute your causal hypotheses. It is important to take into account additional variables that might impact the result, including moderating, mediating, and confounding variables. These are factors that might affect or interfere with the cause-and-effect causal relationship.

Depending on your research topic, data, and context, you may apply one of several methods of causal analysis. Among the most common types are:

How to Perform Causal Analysis?

Depending on the type of causal analysis, the data, and the research topic, there may be differences in the processes involved in doing the analysis. However, a general framework that you can follow is:

Steps to Perform Casual Analysis

  1. Define the Problem: Begin by clearly defining the problem or issue you want to analyze causally. This step sets the foundation for the entire process.
  2. Identify Variables: Break down the problem into different variables. Variables are factors that can change or be changed. For example, if you’re investigating the reasons for low productivity, variables could include workload, employee satisfaction, and work environment.
  3. Collect Data: Gather relevant data for each variable. This can involve surveys, experiments, observations, or even analyzing existing data sets. Make sure your data is accurate and comprehensive.
  4. Establish Relationships: Determine how the variables are related to each other. Use statistical methods or visual tools like graphs and charts to identify patterns and correlations.
  5. Distinguish Correlation from Causation: It is important to realize that correlation does not equal causation. A correlation between two variables does not imply that one causes the other. It is necessary to comprehend the fundamental mechanisms of causation in more detail.
  6. Consider Confounding Variables: Recognize confounding variables, which are elements that may affect the observed connection between variables and skew findings. Precise causal analysis requires accounting for these factors.

What are the Benefits of Causal Analysis?

There are several advantages to using causal analysis, including:

Example Case of Causal Analysis

Here are some examples of causal analysis that you can refer to:

  1. A causal investigation of how social media affects mental health. This study can investigate how various aspects of social media use—such as frequency, length, content, or platform—affect users’ mental health outcomes—such as stress, anxiety, depression, or self-esteem. It can do this by using experimental, quasi-experimental, or correlational approaches. It can also investigate how other factors, such as personality, social support, or coping mechanisms, mediate or moderate the situation.
  2. A causal examination of the variables affecting consumer loyalty and satisfaction. The investigation of how various aspects (such as product quality, service quality, price, or brand image) affect customer satisfaction and loyalty can be done through case studies or correlational approaches in this study. It can also look at how a company’s business success and profitability are affected by customer happiness and loyalty.
  3. A causal analysis of the causes and effects of climate change. This study can use case studies or correlational methods to analyze how human activities (such as greenhouse gas emissions, deforestation, or urbanization) contribute to global warming and the environmental changes (such as rising sea levels, melting glaciers, or extreme weather events) that result from it. It can also assess the impact of climate change on the social and economic aspects of human life, such as health, food security, or migration.

Example 1: Causal Analysis with a Synthetic Dataset

Objective: Explore the causal relationship between the number of study hours and exam scores using a synthetic dataset.

Step 1: Import Necessary Libraries




import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

Step 2: Create a Synthetic Dataset




np.random.seed(42)
study_hours = np.random.normal(30, 10, 100)
exam_scores = 50 + 2 * study_hours + np.random.normal(0, 20, 100)
data = pd.DataFrame({'Study_Hours': study_hours, 'Exam_Scores': exam_scores})

Step 3: Visualize the Data




plt.scatter(data['Study_Hours'], data['Exam_Scores'])
plt.title('Synthetic Dataset: Study Hours vs. Exam Scores')
plt.xlabel('Study Hours')
plt.ylabel('Exam Scores')
plt.show()

Output:

Explanation: This scatter plot visually represents our synthetic dataset, where the x-axis shows study hours, and the y-axis shows exam scores. We can observe a positive trend, suggesting a potential correlation.

Step 4: Split the Dataset




X_train, X_test, y_train, y_test = train_test_split(data[['Study_Hours']], data['Exam_Scores'], test_size=0.2, random_state=42)

Explanation: Splitting the dataset into training and testing sets allows us to train our model on one subset and evaluate its performance on another, ensuring unbiased results.

Step 5: Train a Linear Regression Model




model = LinearRegression()
model.fit(X_train, y_train)

Output:

Linear Regression

Explanation: Linear regression is chosen to model the relationship between study hours and exam scores. Training the model involves finding the best-fit line that minimizes the difference between predicted and actual exam scores.

Step 6: Visualize the Regression Line




plt.scatter(X_test, y_test)
plt.plot(X_test, model.predict(X_test), color='red', linewidth=2)
plt.title('Linear Regression: Study Hours vs. Exam Scores')
plt.xlabel('Study Hours')
plt.ylabel('Exam Scores')
plt.show()

Output:

Explanation: The red line represents the regression model’s prediction. This line summarizes the relationship between study hours and exam scores, showcasing the model’s ability to generalize.

Example 2: Propensity Score Matching

Propensity score matching is a technique that aims to reduce the bias due to confounding variables by matching units that have similar probabilities of receiving the treatment, based on their observed characteristics. For instance, we can match smokers and non-smokers with comparable ages, genders, and health statuses to evaluate the influence of smoking on lung cancer and compare the results.

We will utilize a synthetic dataset that mimics the impact of a training program on employee performance to demonstrate this technique. The four variables in the dataset are outcome, covariate, treatment, and id. Each employee has a unique identifier or ID; the treatment is a binary indicator of whether or not the employee took part in the training program; the outcome, or measure of employee performance, is a continuous variable; the covariate is a continuous variable that represents some confounding factor that influences both the treatment and the outcome.

We will use the sklearn and causal inference libraries to generate and analyze the data. The code and the output are shown below.




# Import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import make_regression
from causalinference import CausalModel
 
# Set random seed for reproducibility
np.random.seed(42)
 
# Generate synthetic data
n = 1000 # number of observations
X, y = make_regression(n_samples=n, n_features=1, n_informative=1, noise=10, random_state=42) # generate covariate and outcome
treatment = np.random.binomial(1, p=0.5, size=n) # generate treatment indicator
y[treatment==1] += 5 # add treatment effect
data = pd.DataFrame({'id': np.arange(n), 'treatment': treatment, 'covariate': X.flatten(), 'outcome': y}) # create dataframe
data.head()

Ouput:

Propensity Score Matching




# Plot the data
plt.figure(figsize=(8,6))
plt.scatter(data['covariate'], data['outcome'], c=data['treatment'], cmap='bwr', alpha=0.5)
plt.xlabel('Covariate')
plt.ylabel('Outcome')
plt.title('Synthetic Data')
plt.show()

Output:

The plot indicates that the covariate and the outcome, as well as the treatment and the outcome, have a positive connection. However, because the treatment assignment may rely on the covariate, a confounding factor, we are unable to deduce the treatment’s causal effect from this connection. Propensity score matching is one technique we can use to account for the covariate to assess the causal influence.

The likelihood of receiving the therapy in light of the observed covariates is known as the propensity score. A logistic regression model can be used to estimate the propensity score. This way, we can create a balanced sample that has similar distributions of the covariates across the treatment groups, and then compare the average outcomes of the matched pairs.

We will use the CausalModel class from the causal inference library to perform the propensity score matching. The code and the output are shown below.




# Create a causal model
cm = CausalModel(
    Y=data['outcome'].values, # outcome variable
    D=data['treatment'].values, # treatment variable
    X=data['covariate'].values # covariate variable
)
 
# Estimate the propensity score
cm.est_propensity_s()
cm.propensity
 
# Perform propensity score matching
cm.trim_s() # trim units with extreme propensity scores
cm.stratify_s() # stratify units into bins based on propensity score
cm.est_via_matching() # estimate the treatment effect via matching
cm.estimates

Output:

{'matching': {'atc': 5.435467575470179, 'att': 5.660317763899948, 'ate': 5.5472181191197745, 'atc_se': 1.1868216799057065, 'att_se': 1.2189135556978998, 'ate_se': 1.0618794080326954}}

The output shows the estimated average treatment effect (ATE), the average treatment effect on the controls (ATC), and the average treatment effect on the treated (ATT), along with their standard errors and confidence intervals. We can see that the estimated effect is very close to the true effect of 5 that we added to the data, and the confidence intervals are fairly narrow. This means that the propensity score matching technique can reduce the bias due to the confounding covariate and estimate the causal effect of the treatment accurately.

Example3: using CasualPY(Public)




# Import libraries
import causalpy as cp
import matplotlib.pyplot as plt
import seaborn as sns
 
# Import and process data
df = (cp.load_data("drinking") # Load the data from the NLSY dataset
      .rename(columns={"agecell": "age"}) # Rename the column for age
      .assign(treated=lambda df_: df_.age > 21) # Assign a binary variable for treatment status
      .dropna()) # Drop the missing values
 
# Make assumptions
# We assume that the outcome variable (all) is continuous and smooth around the cutoff point (21)
# We assume that there is no manipulation or sorting of the running variable (age) around the cutoff point
# We assume that the treatment assignment (treated) is unconfounded, meaning that there are no other variables that affect both the treatment and the outcome
 
# Model the counterfactual
# We use a linear regression model with a constant term, the running variable, and the treatment variable as predictors
# We specify the running variable name, the treatment threshold, and the model object
result = cp.pymc_experiments.RegressionDiscontinuity(df,
                                                     formula="all ~ 1 + age + treated",
                                                     running_variable_name="age",
                                                     model=cp.pymc_models.LinearRegression(),
                                                     treatment_threshold=21)
 
# Estimate the causal effect
# We use the summary method to get the ATE, the standard error, and the confidence interval
result.summary()
# The output shows that the ATE is -0.052, meaning that drinking alcohol reduces the health outcome by 0.052 units on average
# The standard error is 0.017, and the 95% confidence interval is [-0.086, -0.018]
 
# Visualize the results
# We use the plot method to get a scatter plot of the data and the fitted model, with the discontinuity at the cutoff point
fig, ax = result.plot()
plt.show()
# The plot shows that the outcome variable (all) decreases sharply at the cutoff point (21), indicating a negative causal effect of drinking alcohol
# We can also plot the distribution of the running variable (age) and the outcome variable (all), and check for any anomalies or outliers
sns.histplot(data=df, x="age", hue="treated", bins=20)
plt.show()
# The histogram shows that the running variable (age) is roughly balanced on both sides of the cutoff point, with no evidence of manipulation or sorting
sns.histplot(data=df, x="all", hue="treated", bins=20)
plt.show()
# The histogram shows that the outcome variable (all) is skewed to the right, with some outliers on the lower end

Output:

Tips for Performing Causal Analysis

Here are some tips that can help you perform causal analysis effectively:

  1. Be clear and specific about your research question or goal and the variables that you want to analyze. Avoid vague or ambiguous terms that can confuse or mislead your readers or yourself.
  2. Examine the theories and books that have been written on your subject in-depth and critically. Determine the advantages and disadvantages of earlier research, as well as how your findings connect to those of the prior studies.
  3. Make sure your causal theories are reasonable and grounded in reality. Refrain from asserting things or assuming things without reasoning or proof.
  4. Proceed with rigor and ethics when gathering and analyzing your data. Select the techniques and resources that make sense for your data and analysis. Observe the norms and principles that apply to your field or domain in terms of ethics and practicality. Make sure your data and analysis are free of biases or mistakes that could have an impact on your findings.
  5. Analyze and present your findings in a clear, impartial manner. Employ suitable statistical tests and methodologies to validate or refute your causal conjectures. Describe the importance and meaning of your findings concerning the existing theories and literature, your research question or purpose, and both. Recognize the limitations and ramifications of your findings, and make recommendations for future study directions.

FAQs on Casual Analysis

Q1: What is the difference between causal analysis and descriptive analysis?

Descriptive analysis is the process of summarizing and presenting the characteristics and patterns of a data set without making any inferences or predictions. Causal analysis is the process of identifying and explaining the causes and effects of a phenomenon, problem, or event based on a data set.

Q2: What are some of the challenges or difficulties of performing causal analysis?

Some of the challenges or difficulties of performing causal analysis are:

  • Establishing a causal relationship between two or more variables can be complex and uncertain, as there may be other variables or factors that can affect the outcome.
  • Collecting and analyzing data that can support or refute a causal hypothesis can be costly, time-consuming, or impractical, especially when dealing with large, diverse, or sensitive data sets.
  • Interpreting and reporting the results of a causal analysis can be subjective, biased, or misleading, especially when there are conflicting or contradictory results or interpretations.

Q3: How can I improve my causal analysis skills?

Reading and reviewing the literature and theories on your topic of interest and learning from the examples and methods of other researchers or practitioners. Practicing and applying the causal analysis techniques and tools to your data or projects and seeking feedback and guidance from your peers or mentors Exploring and experimenting with different types of causal analysis and data and challenging yourself with new or complex questions or problems.

Q4: Can correlation indicate causation?

A: No, correlation doesn’t necessarily imply causation. It’s important to delve deeper into the relationships between variables to establish causation.

Q5: How can I control for confounding variables?

A: To control for confounding variables, ensure your study design accounts for and mitigates the influence of other factors that may affect your results.


Article Tags :