tests/testthat/test-scdataMulti.R

library(scpi)

###############################################################################
###############################################################################
## Auxiliary functions for testing

test.data <- function(df,
                      features = NULL,
                      cov.adj = NULL,
                      cointegrated.data = FALSE,
                      post.est = NULL,
                      units.est = NULL,
                      donors.est = NULL,
                      anticipation = 0,
                      effect = "unit-time",
                      constant = FALSE,
                      verbose = FALSE,
                      sparse.matrices = FALSE) {
  data <- scpi_germany
  
  # Create a second placebo treated unit
  data$treatment <- 0
  data[(data$country == "West Germany" & data$year >= 1991), "treatment"] <- 1
  data[(data$country == "Italy" & data$year >= 1992), "treatment"] <- 1
  
  df <- scdataMulti(data, id.var = "country", outcome.var = "gdp",
                    treatment.var = "treatment", time.var = "year", constant = constant,
                    cointegrated.data = cointegrated.data, features = features,
                    cov.adj = cov.adj, verbose = verbose, post.est = post.est,
                    units.est = units.est, donors.est = donors.est, anticipation = anticipation,
                    effect = effect, sparse.matrices = sparse.matrices)
  
  return(df)
}

###############################################################################
###############################################################################
## Auxiliary functions for testing

test_that("no error is returned", {
  expect_no_error(test.data(constant = TRUE, sparse.matrices=FALSE, effect="unit-time",
                  cointegrated.data = TRUE, features = list(c("gdp", "trade")),
                  cov.adj = list(c("constant", "trend"))))
  expect_no_error(test.data(constant = TRUE, sparse.matrices=FALSE, effect="time",
                  cointegrated.data = TRUE, features = list(c("gdp", "trade")),
                  cov.adj = list(c("constant", "trend"))))
  expect_no_error(test.data(constant = TRUE, sparse.matrices=FALSE, effect="unit",
                  cointegrated.data = TRUE, features = list(c("gdp", "trade")),
                  cov.adj = list(c("constant", "trend"))))
  expect_no_error(test.data(constant = TRUE, sparse.matrices=FALSE, effect="unit", post=5,
                            cointegrated.data = TRUE, features = list(c("gdp", "trade")),
                            cov.adj = list(c("constant", "trend"))))
})

test_that("unit-specific data options are accepted", {
  features <- list("Italy" = c("gdp", "trade"),
                   "West Germany" = c("gdp", "infrate"))
  cov.adj <- list("Italy" = list(c("constant", "trend")),
                  "West Germany" = list(c("constant", "trend"),
                                        c("constant", "trend")))
  constant <- list("Italy" = TRUE, "West Germany" = FALSE)
  cointegrated.data <- list("Italy" = TRUE, "West Germany" = FALSE)

  df <- test.data(features = features, cov.adj = cov.adj,
                  constant = constant,
                  cointegrated.data = cointegrated.data,
                  effect = "unit")

  expect_equal(df$specs$features[["Italy"]], c("gdp", "trade"))
  expect_equal(df$specs$features[["West Germany"]], c("gdp", "infrate"))
  expect_true(df$specs$constant[["Italy"]])
  expect_false(df$specs$constant[["West Germany"]])
  expect_true(df$specs$cointegrated.data[["Italy"]])
  expect_false(df$specs$cointegrated.data[["West Germany"]])
})

Try the scpi package in your browser

Any scripts or data that you put into this service are public.

scpi documentation built on June 3, 2026, 9:06 a.m.