Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Nov 17, 2024
1 parent bd0b0f9 commit 2fe0715
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 175 deletions.
2 changes: 1 addition & 1 deletion R/get_varcov.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ get_varcov.fixest <- function(x,
get_varcov.asym <- function(x, ...) {
out <- get_varcov.default(x, ...)
colnames(out) <- gsub("^plus__", "+", colnames(out))
rownames(out) <- gsub("^plus__", "+", rownames(out))
rownames(out) <- gsub("^minus__", "-", rownames(out))
out
}

Expand Down
203 changes: 29 additions & 174 deletions tests/testthat/test-panelr-asym.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
skip_if_not_installed("panelr")
skip_if_not_installed("clubSandwich")
skip_if_not_installed("car")

data("teen_poverty", package = "panelr")
teen <- panelr::long_panel(teen_poverty, begin = 1, end = 5)
Expand All @@ -9,138 +11,38 @@ test_that("model_info", {
})

test_that("id_mixed", {
expect_true(is_mixed_model(m1))
expect_true(is_mixed_model(m2))
expect_false(is_mixed_model(m1))
})

test_that("find_predictors", {
expect_identical(
find_predictors(m1),
list(
conditional = c("union", "wks"),
instruments = c("blk", "fem"),
interactions = c("blk", "union")
)
list(conditional = c("pov", "spouse", "wave"))
)
expect_identical(
find_predictors(m1, flatten = TRUE),
c("union", "wks", "blk", "fem")
c("pov", "spouse", "wave")
)
expect_null(find_predictors(m1, effects = "random"))

expect_identical(
find_predictors(m2),
list(
conditional = c("union", "wks"),
instruments = c("blk", "t")
)
)
expect_identical(find_predictors(m2, effects = "random"), list(random = "id"))
})

test_that("find_random", {
expect_null(find_random(m1))
expect_identical(find_random(m2), list(random = "id"))
})

test_that("get_random", {
expect_warning(expect_null(get_random(m1)))
expect_identical(get_random(m2)[[1]], model.frame(m2)$id)
})

test_that("find_response", {
expect_identical(find_response(m1), "lwage")
})

test_that("get_response", {
expect_identical(get_response(m1), model.frame(m1)$lwage)
})

test_that("get_predictors", {
expect_identical(
colnames(get_predictors(m1)),
c("lag(union)", "wks", "blk", "fem")
)
expect_identical(
colnames(get_predictors(m2)),
c("lag(union)", "wks", "blk", "t")
)
expect_identical(find_response(m1), "hours")
})

test_that("link_inverse", {
expect_equal(link_inverse(m1)(0.2), 0.2, tolerance = 1e-5)
})

test_that("clean_parameters", {
cp <- clean_parameters(m1)
expect_identical(
cp$Cleaned_Parameter,
c(
"union", "wks", "(Intercept)", "imean(lag(union))", "imean(wks)",
"blk", "fem", "union:blk"
)
)
expect_identical(
cp$Component,
c(
"conditional", "conditional", "instruments", "instruments",
"instruments", "instruments", "instruments", "interactions"
)
)
})

test_that("get_data", {
expect_identical(nrow(get_data(m1)), 3570L)
expect_identical(
colnames(get_data(m1)),
c(
"lwage",
"id",
"t",
"lag(union)",
"wks",
"blk",
"fem",
"imean(lag(union))",
"imean(wks)",
"imean(lag(union):blk)",
"lag(union):blk"
)
)
expect_identical(
colnames(get_data(m2)),
c(
"lwage",
"id",
"t",
"lag(union)",
"wks",
"blk",
"imean(lag(union))",
"imean(wks)"
)
)
})

test_that("find_formula", {
expect_length(find_formula(m1), 3)
expect_length(find_formula(m1), 1)
expect_equal(
find_formula(m1),
list(
conditional = as.formula("lwage ~ lag(union) + wks"),
instruments = as.formula("~blk + fem"),
interactions = as.formula("~blk * lag(union)")
),
ignore_attr = TRUE
)

expect_equal(
find_formula(m2),
list(
conditional = as.formula("lwage ~ lag(union) + wks"),
instruments = as.formula("~blk + t"),
random = as.formula("~t | id")
),
list(conditional = as.formula("hours ~ lag(pov) + spouse + wave")),
ignore_attr = TRUE
)
})
Expand All @@ -149,35 +51,18 @@ test_that("find_variables", {
expect_identical(
find_variables(m1),
list(
response = "lwage",
conditional = c("union", "wks"),
instruments = c("blk", "fem"),
interactions = c("blk", "union")
response = "hours",
conditional = c("pov", "spouse", "wave")
)
)
expect_identical(
find_variables(m1, flatten = TRUE),
c("lwage", "union", "wks", "blk", "fem")
)

expect_identical(
find_variables(m2),
list(
response = "lwage",
conditional = c("union", "wks"),
instruments = c("blk", "t"),
random = "id"
)
)
expect_identical(
find_variables(m2, flatten = TRUE),
c("lwage", "union", "wks", "blk", "t", "id")
c("hours", "pov", "spouse", "wave")
)
})

test_that("n_obs", {
expect_identical(n_obs(m1), 3570L)
expect_identical(n_obs(m2), 3570L)
expect_identical(n_obs(m1), 3453L)
})

test_that("linkfun", {
Expand All @@ -188,21 +73,10 @@ test_that("find_parameters", {
expect_identical(
find_parameters(m1),
list(
conditional = c("lag(union)", "wks"),
instruments = c("(Intercept)", "imean(lag(union))", "imean(wks)", "blk", "fem"),
random = "lag(union):blk"
)
)

expect_identical(nrow(get_parameters(m1)), 8L)

expect_identical(
find_parameters(m2),
list(
conditional = c("lag(union)", "wks"),
instruments = c("(Intercept)", "imean(lag(union))", "imean(wks)", "blk", "t")
conditional = c("(Intercept)", "+lag(pov)", "-lag(pov)", "+spouse", "-spouse", "wave")
)
)
expect_identical(nrow(get_parameters(m1)), 6L)
})


Expand All @@ -211,27 +85,17 @@ test_that("get_parameters", {
get_parameters(m1),
data.frame(
Parameter = c(
"lag(union)",
"wks",
"(Intercept)",
"imean(lag(union))",
"imean(wks)",
"blk",
"fem",
"lag(union):blk"
"(Intercept)", "+lag(pov)", "-lag(pov)",
"+spouse", "-spouse", "wave"
),
Estimate = c(
0.0582474262882615, -0.00163678667081885, 6.59813245629044,
-0.0279959204722801, 0.00438047648390025, -0.229414915661438,
-0.441756913071962, -0.127319623945541
),
Component = c(
"within", "within", "between", "between",
"between", "between", "between", "interactions"
5.07629091766534, -0.695146196282601, 2.74491742707041,
-2.99740859292258, -0.399625620609947, -0.812955475294268
),
stringsAsFactors = FALSE
),
tolerance = 1e-4
tolerance = 1e-4,
ignore_attr = TRUE
)
})

Expand All @@ -240,19 +104,8 @@ test_that("find_terms", {
expect_identical(
find_terms(m1),
list(
response = "lwage",
conditional = c("lag(union)", "wks"),
instruments = c("blk", "fem"),
interactions = c("blk", "lag(union)")
)
)
expect_identical(
find_terms(m2),
list(
response = "lwage",
conditional = c("lag(union)", "wks"),
instruments = c("blk", "t"),
random = c("t", "id")
response = "hours",
conditional = c("lag(pov)", "spouse", "wave")
)
)
})
Expand All @@ -263,12 +116,14 @@ test_that("is_multivariate", {

test_that("find_statistic", {
expect_identical(find_statistic(m1), "t-statistic")
expect_identical(find_statistic(m2), "t-statistic")
})

test_that("get_variance", {
test_that("get_vcov", {
skip_on_cran()
v <- get_variance(m1)
expect_equal(v$var.intercept, c(id = 0.125306895731005), tolerance = 1e-4)
expect_equal(v$var.fixed, 0.0273792999320531, tolerance = 1e-4)
v <- get_varcov(m1)
expect_identical(dim(v), c(6L, 6L))
expect_identical(
colnames(v),
c("(Intercept)", "+lag(pov)", "-lag(pov)", "+spouse", "-spouse", "wave")
)
})

0 comments on commit 2fe0715

Please sign in to comment.