关于R 3.6.3(macOS Catalina)运行Zelig及替代方案的技术问询
Great question—let’s break this down step by step for your macOS Catalina 10.15.4 + R 3.6.3 setup:
The latest CRAN releases of Zelig (v5.1.7 and above) are built for R 4.0+, so they won’t work natively with your R 3.6.3 install out of the box.
You could try installing an older, compatible version of Zelig (like v5.1.1) using:
library(devtools) install_version("Zelig", version = "5.1.1")
But heads up: this might require tracking down older versions of dependent packages (e.g., ggplot2, dplyr) that also support R 3.6.3, which can be tedious. On Catalina, you’ll also need Xcode 11.x or its command-line tools to compile these older packages, so ensure those are set up first.
That said, if you want a smoother workflow, switching to MatchIt or the Matching package is far more reliable for your setup.
Both packages work seamlessly with R 3.6.3 on Catalina and make it straightforward to compute treatment effects after nearest-neighbor PSM.
Using MatchIt
MatchIt is user-friendly and integrates well with standard tidy workflows:
- First, run nearest-neighbor propensity score matching:
library(MatchIt) # Replace with your treatment variable, covariates, and dataset match_obj <- matchit(treat ~ cov1 + cov2 + cov3, data = your_dataset, method = "nearest", ratio = 1) # 1:1 matching
- Extract the matched dataset (optional but useful for further analysis):
matched_data <- match.data(match_obj)
- Compute ATT: The
summary()function directly returns the ATT estimate and its standard error:
summary(match_obj, standardize = FALSE)
Look for the Estimate value under the "Average Treatment Effect on the Treated (ATT)" section.
- Compute ATE: Nearest-neighbor matching is optimized for ATT, but you can estimate ATE using propensity score weighting instead:
weighted_match <- matchit(treat ~ cov1 + cov2 + cov3, data = your_dataset, method = "weighting", estimand = "ATE") summary(weighted_match, standardize = FALSE)
The Estimate under "Average Treatment Effect (ATE)" is your result.
Using the Matching Package
The Matching package (often called Match for short) lets you directly specify whether you want ATT or ATE during matching:
- First, estimate propensity scores and run matching:
library(Matching) # Step 1: Fit a propensity score model ps_model <- glm(treat ~ cov1 + cov2 + cov3, data = your_dataset, family = binomial) ps_scores <- predict(ps_model, type = "response") # Step 2: Calculate ATT with nearest-neighbor matching att_results <- Match(Y = your_dataset$outcome_variable, Tr = your_dataset$treat, X = ps_scores, estimand = "ATT", method = "nearest") summary(att_results) # Step 3: Calculate ATE with nearest-neighbor matching ate_results <- Match(Y = your_dataset$outcome_variable, Tr = your_dataset$treat, X = ps_scores, estimand = "ATE", method = "nearest") summary(ate_results)
The summary() output will include the treatment effect estimate, standard error, and p-value for both metrics.
内容的提问来源于stack exchange,提问作者Tanu

