Skip to content

Instantly share code, notes, and snippets.

@mingjiphd
mingjiphd / InterrupedTimeSeries.R
Created January 25, 2026 17:54
Causal Analysis using R Interrupted Time Series Analysis
#### Causal Analysis using R Interrupted Time Series
### Brief Overview of ITS
# - Interrupted time series (ITS) is a quasi-experimental method used to evaluate the impact
# of an intervention by analyzing an outcome measured over time before and after the
# intervention occurs.
# - The technique works by modeling changes in both the level and slope of the time series
# that follow the intervention, estimating both immediate and long-term effects.
# - ITS is valuable when randomization is not feasible and can provide strong evidence
# for causal inference, but requires multiple data points on both sides of the intervention.
@mingjiphd
mingjiphd / Readme.txt
Last active January 26, 2026 00:02
Dimensionaltiy Reduction using Shallow Autoencoders
This scirpt shows how to perform dimensionality reduction using Shallow Autoencoder in R. The R packages neuralnet and mlbench are used to show how ot fit an autoencoder (which is a simple neural network) for a synthetic data set. Subsequent analyses such as visualizing the latent space, computing the reconstruction errors, anamoly detection, clustering in latent space and comparing autoencoder to PCA.
@mingjiphd
mingjiphd / Readme.txt
Created January 26, 2026 19:46
Causal Analysis using R How to Implement the DO Calculus by Judea Pearl
This R script is a step by step tutorial on how to implement the DO Calculus developed by Judea Pearl. The R packages used include: causaleffect, mediation, igraph and dplyr. We demonstrate how to create a simulated data set, how to define a DAG, how to estimate the expression identified by the DO Calculus and how to use that expression to calculate average causal effect (ACE).
A link to a video demonstration on YouTube is: https://youtu.be/yNzpoWRgZdE
@mingjiphd
mingjiphd / R Script
Last active January 27, 2026 18:47
Causal Analysis using R The Weak Instrument Test
#### Causal Analysis using R The Weak Instrument Test
# Load necessary packages
library(AER) # for ivreg
library(car) # for linearHypothesis (optional)
set.seed(11152025)
# Generate synthetic data
n <- 500
@mingjiphd
mingjiphd / R Script
Last active January 27, 2026 18:46
Causal Analysis using R Instrument Variables
### Causal Analysis using R Instrumental Variables
## Instrumental variables (IVs) are special variables used in statistics and econometrics
## to estimate causal effects when ordinary regression methods would be biased due to
## endogeneity—meaning the explanatory variable is correlated with unobserved factors or the error term.
## Two Key Criterias of IVs
## It is correlated with the problematic (endogenous) explanatory variable.
## It is not correlated with the error term and only affects the outcome through
## the explanatory variable (called the exclusion restriction).
@mingjiphd
mingjiphd / R Script
Last active January 27, 2026 18:45
Causal Analysis using R with a More Sophisticated Example of Bayesian Networks
### Causal Analysis using R: A More Sophiscated Example of Bayesian Networks
##In this video, we present an example of using the R package bnlearn
##for Bayesian Network Analysis
## The Following topics are covered:
#Generates a mixed-type synthetic data set
#Applies multiple structure learning algorithms
@mingjiphd
mingjiphd / R Script
Last active January 27, 2026 18:44
Causal Analysis using R A Simple Case Beaysian Network Analysis
### Causal Analysis using R A Simple Case Bayesian Network Analysis
##Bayesian Networks are directed acyclic graphs representing variables and their dependencies.
##Nodes represent variables; edges show conditional dependencies between them.
A
##Each node has a probability function based on its parent nodes.
## They use Bayes' theorem to update probabilities with new evidence.
@mingjiphd
mingjiphd / R Script
Last active January 27, 2026 18:43
Causal Analysis using R The Difference in Difference Method
### Causal Analysis using R The Difference in Difference Method
##DiD estimates causal effects by comparing changes over time between treatment and control groups.
##It uses before-and-after differences in outcomes to isolate the treatment effect.
##The critical assumption is parallel trends, meaning both groups would follow similar outcome paths
#if untreated.
##Suitable for observational data where randomization is not possible.
@mingjiphd
mingjiphd / R Script
Last active January 27, 2026 18:42
Causal Analysis using R with More Sophisticated DAGs
### Causal Analysis using R: Sophisticated DAGs
###Generate Data
# Install if needed
#if (!requireNamespace("simDAG", quietly = TRUE)) {
# install.packages("simDAG")
#}
@mingjiphd
mingjiphd / R Script
Last active January 27, 2026 18:41
Causal Analysis in R: Implementing DAGs with a Simple Example
### How to Implement Directed Acyclic Graphs for Causal Inference in R: The Simple Case
##A Directed Acyclic Graph (DAG) is a finite graph composed of nodes connected
## by directed edges with no cycles, meaning it's impossible to start at any node
## and follow a sequence of edge directions that returns to the same node.
##in DAGs, nodes to be arranged in a linear order that respects the direction of
## edges and represents dependencies or causal relationships.
##Directed Acyclic Graphs (DAGs) are used in causal inference to visually represent
## assumptions about causal relationships, identify confounders, guide variable
## selection for adjustment, and clarify the pathways affecting causal effect estimation