Nothing
library( testthat )
library( hettx )
context("detect_idiosyncratic() functions test.")
test_that("FRTCI Runs", {
data(ToyData)
## Set parameters
B <- 20
grid.size = 11
## Test for ideosyncratic treatment effect variation without covariates
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, B=B, grid.size = grid.size, verbose=FALSE)
expect_false( is.null( tst ) )
expect_s3_class( tst, "FRTCI.test" )
expect_equal( tst$test.stat, "SKS_stat" )
s = summary( tst )
s
expect_equal( length( s$p.value.CI ), 2 )
})
test_that("Passing function for test stat works", {
data(ToyData)
## Set parameters
B <- 20
grid.size = 11
## Test for ideosyncratic treatment effect variation without covariates
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, B=B, grid.size = grid.size, verbose=FALSE,
test.stat = SKS_stat)
expect_false( is.null( tst ) )
expect_s3_class( tst, "FRTCI.test" )
expect_equal( tst$test.stat, "SKS_stat" )
summary( tst )
})
test_that( "Example code from documentation, copied over", {
data(ToyData)
B <- 20
grid.size = 51
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, B=B, grid.size = grid.size, verbose=FALSE)
tst
plot( tst )
expect_warning( tst <- detect_idiosyncratic(Y ~ Z, data = ToyData, plugin = TRUE, verbose=FALSE) )
tst <- detect_idiosyncratic(Y ~ Z, data = ToyData, interaction.formula = ~ x1, B=B, verbose=FALSE )
tst
expect_equal( c(500,1), dim( tst$W ) )
summary( tst )
})
test_that( "Variance ratio test works", {
data( ToyData )
vrt <- variance_ratio_test( Y, Z, data=ToyData )
expect_true( vrt$pvalue <= 0.05 )
})
test_that( "get_p_value works", {
data( ToyData )
tst <- detect_idiosyncratic(Y ~ Z, data = ToyData, interaction.formula = ~ x1, B=10, verbose=FALSE )
pv = get_p_value( tst )
expect_equal( length( pv ), 4 )
})
test_that( "Every non-rq test statistic works", {
data(ToyData)
B <- 20
grid.size = 11
## -------------
## Test defaults
## -------------
ToyData$W.fact <- as.factor(sample(c("A", "B"), nrow(ToyData), replace = TRUE))
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, B=B, grid.size=grid.size, verbose=FALSE)
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, control.formula = ~ x1 + x2, B=B, grid.size=grid.size, verbose=FALSE)
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, interaction.formula = ~ x3 + x4, B=B, grid.size=grid.size, verbose=FALSE)
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, interaction.formula = ~ x3 + x4, control.formula = ~ x1 + x2, B=B, grid.size=grid.size, verbose=FALSE)
## ------------------------------------------------
## Test statistics for no adjustment or interaction
## ------------------------------------------------
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, test.stat="KS_stat", B=B, grid.size=grid.size, verbose=FALSE)
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, test.stat="SKS_stat", B=B, grid.size=grid.size, verbose=FALSE)
## ----------------------------------------------
## Test statistics for adjustment, no interaction
## ----------------------------------------------
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, control.formula = ~ x1 + x2, test.stat="SKS_stat_cov_pool", B=B, grid.size=grid.size, verbose=FALSE)
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, control.formula = ~ x1 + x2, test.stat="SKS_stat_cov", B=B, grid.size=grid.size, verbose=FALSE)
## ----------------------------------------------
## Test statistics for interaction, no adjustment
## ----------------------------------------------
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, interaction.formula = ~ x3 + x4, test.stat="SKS_stat_int_cov_pool", B=B, grid.size=grid.size, verbose=FALSE)
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, interaction.formula = ~ x3 + x4, test.stat="SKS_stat_int_cov", B=B, grid.size=grid.size, verbose=FALSE)
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, interaction.formula = ~ W.fact, test.stat="WSKS_t", B=B, grid.size=grid.size, verbose=FALSE)
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, interaction.formula = ~ W.fact, test.stat="SKS_pool_t", B=B, grid.size=grid.size, verbose=FALSE)
## ----------------------------------------------
## Test statistics for interaction and adjustment
## ----------------------------------------------
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, interaction.formula = ~ x3 + x4, control.formula = ~ x1 + x2, test.stat="SKS_stat_int_cov_pool", B=B, grid.size=grid.size, verbose=FALSE)
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, interaction.formula = ~ x3 + x4, control.formula = ~ x1 + x2, test.stat="SKS_stat_int_cov", B=B, grid.size=grid.size, verbose=FALSE)
expect_equal( class( tst ), "FRTCI.test" )
})
test_that( "rq test statistic works", {
data(ToyData)
B <- 20
grid.size = 11
## -------------
## Test defaults
## -------------
X <- model.matrix(~ x1 + x2, data = ToyData)[,-1]
W <- model.matrix(~ x3 + x4, data = ToyData)[,-1]
W.fact <- as.factor(sample(c("A", "B"), nrow(W), replace = TRUE))
## ------------------------------------------------
## Test statistics for no adjustment or interaction
## ------------------------------------------------
tst = detect_idiosyncratic(Y ~ Z, data = ToyData,
test.stat="rq_stat", B=B,
grid.size=grid.size, verbose=FALSE)
## ----------------------------------------------
## Test statistics for adjustment, no interaction
## ----------------------------------------------
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, control.formula = ~ x1 + x2,
test.stat="rq_stat_cond_cov", B=B,
grid.size=grid.size, verbose=FALSE)
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, control.formula = ~ x1 + x2,
test.stat="rq_stat_uncond_cov", B=B,
grid.size=grid.size, verbose=FALSE)
tst = detect_idiosyncratic(Y ~ Z, data = ToyData, control.formula = ~ x1 + x2,
test.stat="SKS_stat_cov_rq", B=B,
grid.size=grid.size, verbose=FALSE)
expect_s3_class( tst, "FRTCI.test" )
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.