Skip to content

Commit

Permalink
add check and revdep workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
bcallaway11 committed Jan 31, 2025
1 parent 3baca52 commit 5a9dd01
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 81 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/check-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: R Package Checks and Coverage

on:
push:
pull_request:
workflow_dispatch:

jobs:
check:
runs-on: ${{ matrix.config.os }}

strategy:
fail-fast: false
matrix:
config:
- {os: windows-latest, r: 'release'}
- {os: windows-latest, r: 'devel'}
- {os: macOS-latest, r: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'devel'}

name: ${{ matrix.config.os }} (R ${{ matrix.config.r }})
steps:
- uses: actions/checkout@v4
- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
- name: Install dependencies
run: |
Rscript -e "install.packages('remotes')"
Rscript -e "remotes::install_deps(dependencies = TRUE)"
- name: Install system dependencies (Ubuntu only)
if: runner.os == 'Linux'
run: sudo apt-get install -y libcurl4-openssl-dev libssl-dev libxml2-dev libfreetype6-dev libfontconfig1-dev libharfbuzz-dev libfribidi-dev libpng-dev libjpeg-dev libtiff5-dev
- name: Setup Check
run: |
Rscript -e "install.packages('rcmdcheck')"
mkdir -p "check-${{ matrix.config.os }}"
- name: Check
run: |
Rscript -e "rcmdcheck::rcmdcheck(args = '--no-manual', error_on = 'error', check_dir = 'check-${{ matrix.config.os }}')"
- name: Setup Test
run: |
mkdir -p "test-results-${{ matrix.config.os }}"
Rscript -e "install.packages('testthat')"
Rscript -e "install.packages('devtools')"
Rscript -e "devtools::install('.', dependencies = TRUE)"
- name: Test
run: |
Rscript -e "testthat::test_dir('tests', reporter = testthat::SummaryReporter)"
- name: Code Coverage
if: matrix.config.os == 'ubuntu-latest' && matrix.config.r == 'release'
run: |
Rscript -e "install.packages('covr')"
Rscript -e "covr::codecov()"
- name: Upload check results
if: always()
uses: actions/upload-artifact@v4
with:
name: R-CMD-check-results-${{ matrix.config.os }}-${{ matrix.config.r }}
path: check-*
- name: Upload test results
if: always()
uses: actions/upload-artifact@v4
with:
name: R-test-results-${{ matrix.config.os }}-${{ matrix.config.r }}
path: test-results-*
98 changes: 98 additions & 0 deletions .github/workflows/revdep-check-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Reverse Dependency Check

on:
workflow_dispatch:

jobs:
revdep_check:
runs-on: ubuntu-latest

name: Reverse check ${{ inputs.which }} dependents

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up R
uses: r-lib/actions/setup-r@v2
with:
r-version: 'release'

- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y \
libcurl4-openssl-dev \
libssl-dev \
libxml2-dev \
libfreetype6-dev \
libfontconfig1-dev \
libharfbuzz-dev \
libfribidi-dev \
libpng-dev \
libjpeg-dev \
libtiff5-dev \
gfortran \
libblas-dev \
liblapack-dev
- name: Install R dependencies
run: |
Rscript -e "install.packages(c('remotes', 'revdepcheck', 'testthat', 'rcmdcheck'))"
Rscript -e "remotes::install_deps(dependencies = TRUE)"
- name: Setup Reverse Dependency Check
run: |
Rscript -e "
rev_deps <- tools::package_dependencies(
packages = 'ptetools',
db = available.packages(),
reverse = TRUE
)
rev_deps <- rev_deps[['ptetools']]
cat('Testing the following reverse dependencies:', rev_deps, '\n')
for (pkg in rev_deps) {
cat('Installing package:', pkg, '\n')
install.packages(pkg, dependencies = TRUE)
}
"
- name: Reverse Dependency Check
run: |
Rscript -e "
rev_deps <- tools::package_dependencies(
packages = 'BMisc',
db = available.packages(),
reverse = TRUE
)
rev_deps <- rev_deps[['BMisc']]
dir.create('revdep_tarballs', showWarnings = FALSE)
for (pkg in rev_deps) {
cat('Checking package:', pkg, '\n')
# Download the source tarball
tarball <- tryCatch({
tarball_info <- download.packages(pkg, destdir = 'revdep_tarballs', type = 'source')
# Extract the correct tarball file path
tarball_path <- tarball_info[, 2] # The second column contains the file path
tarball_path # Return the correct path
}, error = function(e) {
cat('Error downloading source tarball for package:', pkg, '\n', conditionMessage(e), '\n')
next
})
pkg_path <- find.package(pkg, quiet = TRUE)
tryCatch({
rcmdcheck::rcmdcheck(tarball, error_on = 'never', args = '--no-manual')
}, error = function(e) {
cat('Error checking package:', pkg, '\n', conditionMessage(e), '\n')
})
}
"
- name: Upload Results
if: always()
uses: actions/upload-artifact@v4
with:
name: Reverse-Dependency-Check-Results
path: revdep/
16 changes: 8 additions & 8 deletions R/attgt_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
#'
#' @param gt_data data that is "local" to a particular group-time average
#' treatment effect
#' @param xformla one-sided formula for covariates used in the propensity score
#' @param xformula one-sided formula for covariates used in the propensity score
#' and outcome regression models
#' @param ... extra function arguments; not used here
#'
#' @return attgt_if
#'
#' @export
did_attgt <- function(gt_data, xformla, ...) {
did_attgt <- function(gt_data, xformula = ~1, ...) {
#-----------------------------------------------------------------------------
# handle covariates
#-----------------------------------------------------------------------------
# for outcome regression, get pre-treatment values
Xpre <- model.frame(xformla, data = subset(gt_data, name == "pre"))
Xpre <- model.frame(xformula, data = subset(gt_data, name == "pre"))

# convert two period panel into one period
gt_data_outcomes <- tidyr::pivot_wider(gt_data[, c("D", "id", "period", "name", "Y")],
Expand All @@ -54,7 +54,7 @@ did_attgt <- function(gt_data, xformla, ...) {
y1 = Y_post,
y0 = Y_pre,
D = D,
covariates = model.matrix(xformla,
covariates = model.matrix(xformula,
data = gt_dataX
),
inffunc = TRUE
Expand Down Expand Up @@ -83,7 +83,7 @@ did_attgt <- function(gt_data, xformla, ...) {
#'
#' @param gt_data data that is "local" to a particular group-time average
#' treatment effect
#' @param xformla one-sided formula for covariates used in the propensity score
#' @param xformula one-sided formula for covariates used in the propensity score
#' and outcome regression models
#' @param d_outcome Whether or not to take the first difference of the outcome.
#' The default is FALSE. To use difference-in-differences, set this to be TRUE.
Expand All @@ -101,7 +101,7 @@ did_attgt <- function(gt_data, xformla, ...) {
#' @export
pte_attgt <- function(
gt_data,
xformla,
xformula,
d_outcome = FALSE,
d_covs_formula = ~ -1, lagged_outcome_cov = FALSE,
est_method = "dr",
Expand All @@ -114,7 +114,7 @@ pte_attgt <- function(
#-----------------------------------------------------------------------------

# pre-treatment covariates
Xpre <- model.frame(xformla, data = subset(gt_data, name == "pre"))
Xpre <- model.frame(xformula, data = subset(gt_data, name == "pre"))
.w <- subset(gt_data, name == "pre")$.w

# change in covariates
Expand Down Expand Up @@ -144,7 +144,7 @@ pte_attgt <- function(
# to work in levels by just setting outcomes in "first period"
# to be equal to 0 for all units
gt_dataX <- droplevels(gt_dataX)
use_formula <- BMisc::toformula("", c(BMisc::rhs.vars(xformla), colnames(dX)))
use_formula <- BMisc::toformula("", c(BMisc::rhs.vars(xformula), colnames(dX)))
if (lagged_outcome_cov) use_formula <- BMisc::addCovToFormla("pre", use_formula)
covmat <- model.matrix(use_formula, data = gt_dataX)
covmat2 <- covmat[D == 0, ]
Expand Down
4 changes: 2 additions & 2 deletions R/pte.R
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ pte_default <- function(yname,
tname,
idname,
data,
xformla = ~1,
xformula = ~1,
d_outcome = FALSE,
d_covs_formula = ~ -1,
lagged_outcome_cov = FALSE,
Expand All @@ -685,7 +685,7 @@ pte_default <- function(yname,
setup_pte_fun = setup_pte,
subset_fun = two_by_two_subset,
attgt_fun = pte_attgt,
xformla = xformla,
xformula = xformula,
d_outcome = d_outcome,
d_covs_formula = d_covs_formula,
lagged_outcome_cov = lagged_outcome_cov,
Expand Down
2 changes: 1 addition & 1 deletion man/did_attgt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions man/pte_default.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
library(testthat)
library(ptetools)

test_check("ptetool")
test_check("ptetools")
24 changes: 13 additions & 11 deletions tests/testthat/test-did-inference.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ test_that("tests for inference", {
sp <- did::reset.sim()
data <- did::build_sim_dataset(sp)

res <- pte2(
yname = "Y",
gname = "G",
tname = "period",
idname = "id",
data = data,
setup_pte_fun = setup_pte,
subset_fun = two_by_two_subset,
attgt_fun = did_attgt,
xformla = ~X
res <- suppressWarnings(
pte2(
yname = "Y",
gname = "G",
tname = "period",
idname = "id",
data = data,
setup_pte_fun = setup_pte,
subset_fun = two_by_two_subset,
attgt_fun = did_attgt,
xformula = ~X
)
)
# truth is that att = 1
tstat <- (res$overall_att$overall.att - 1) / res$overall_att$overall.se
Expand All @@ -37,5 +39,5 @@ test_that("tests for inference", {

rej_frac <- mean(unlist(rejs))

expect_equal(rej_frac, 0.06, tolerance = .05) # make test fail if reject 0
expect_equal(rej_frac, 0.06, tolerance = 0.05) # make test fail if reject 0
})
Loading

0 comments on commit 5a9dd01

Please sign in to comment.