Nothing
# Influence functions
test_that("GEE", {
if (requireNamespace("geepack",quietly=TRUE)) {
d <- lvm(y ~ x, ~ id) |>
distribution(~id, uniform.lvm(value=seq(1:20))) |>
sim(100, seed=1)
d0 <- d[order(d$id), ]
g <- geepack::geeglm(y ~ x, data=d0, id=d0$id)
V <- summary(g)$cov.scaled
g0 <- glm(y ~ x, data=d)
V0 <- vcov(estimate(g0, id = d$id))
testthat::expect_true(sum((V - V0)^2) < 1e-12)
}
})
test_that("merge, IC, estimate with 'id' argument", {
d <- data.frame(id=c("a","a","b","b","b","b","c","c","d"),
id1=c("a","a","b1","b1","b2","b2","c","c","d"),
y=rnorm(9), x=rnorm(9))
d$id0 <- as.numeric(as.factor(d$id))
l <- glm(y ~ x, data=d)
e1 <- estimate(l, id=d$id1)
V1 <- vcov(e1)
e0 <- estimate(e1, id=c(1,2,2,3,4))
V0 <- vcov(e0)
if (requireNamespace("geepack",quietly=TRUE)) {
V <- summary(geepack::geeglm(y ~ x, id=d$id0, data=d))$cov.scaled
testthat::expect_true(sum((V - V0)^2) < 1e-12)
}
e <- merge(estimate(l), estimate(l), id=list(d$id, d$id1))
testthat::expect_true(sum((V1 - vcov(e)[3:4,3:4])^2) < 1e-12)
testthat::expect_true(sum((V0 - vcov(e)[1:2,1:2])^2) < 1e-12)
ee <- estimate(e, id=c(1,2,3,4,2,2))
VV <- vcov(ee)
testthat::expect_true(sum((VV[1:2,1:2] - V0)^2) < 1e-12)
testthat::expect_true(sum((VV[3:4,3:4] - V0)^2) < 1e-12)
})
test_that("negative binomial regression (glm.nb)", {
set.seed(1)
n <- 500
z <- rgamma(n, .5, .5)
x <- rnorm(n)
lam <- z * exp(x)
y <- rpois(n, lam)
if (requireNamespace("MASS",quietly=TRUE)) {
m <- MASS::glm.nb(y ~ x)
testthat::expect_true(abs(lava:::logL.glm(m) - logLik(m)) < 1e-6)
p <- coef(m)+1
u1 <- as.vector(numDeriv::jacobian(function(p) lava:::logL.glm(m, p = p), p))
u2 <- score(m, p = p)
testthat::expect_true(sum((u1 - u2)^2) < 1e-6)
p <- coef(m)
u1 <- as.vector(numDeriv::jacobian(function(p) lava:::logL.glm(m, p = p), p))
u2 <- score(m, p = p)
testthat::expect_true(sum((u1 - u2)^2) < 1e-6)
}
})
set.seed(1)
n <- 500
z <- rgamma(n, .5, .5)
x <- rnorm(n)
lam <- z * exp(x)
y <- rpois(n, lam)
d <- data.frame(y,x,z)
test_that("quasipossion", {
m1 <- glm(y ~ x, family=poisson, data=d)
m2 <- glm(y ~ x, family = quasipoisson, data=d)
i1 <- IC(m1)
i2 <- IC(m2)
testthat::expect_true(sum((i1 - i2)^2) < 1e-6)
# check that iid decomposition is correctly scaled
ii <- iid(m1)
expect_equal(crossprod(ii), var_ic(i1), ignore_attr = TRUE)
})
test_that("back.transform", {
tmp <- summary(estimate(coef = 1, IC = c(1,-1,0), id = 1:3, f = log),
transform=exp)
tmp2 <- summary(estimate(coef = 2, IC = c(0,1,-1), id = 1:3, f = exp),
transform=log)
expect_equal(coef(tmp), 1, ignore_attr = TRUE)
expect_equal(coef(tmp2), 2, ignore_attr = TRUE)
})
test_that("summary transform keeps null hypothesis", {
e <- estimate(coef = c(a = 0.5, b = 1), vcov = diag(2))
s0 <- summary(e)
s <- summary(e, transform = exp, labels = c("A", "B"))
# Wald test is unchanged, null displayed on the transformed scale
expect_equal(s$compare$p.value, s0$compare$p.value)
expect_true(all(c("[A] = 1", "[B] = 1") %in% s$compare$method))
# General contrast: null kept on the original scale
s2 <- summary(e, contrast = cbind(1, -1), transform = exp)
expect_true("Null Hypothesis (untransformed scale):" %in% s2$compare$method)
expect_true("[a] - [b] = 0" %in% s2$compare$method)
# No Wald test
s3 <- summary(e, contrast = NULL, transform = exp)
expect_null(s3$compare)
# Row-wise transformation operating on matrices
rowtrans <- function(x) {
rbind(exp(x[1, , drop = FALSE]), x[2, , drop = FALSE])
}
s4 <- summary(e, transform = rowtrans)
expect_equal(unname(coef(s4)), c(exp(0.5), 1))
expect_true(all(c("[a] = 1", "[b] = 0") %in% s4$compare$method))
})
test_that("IC.numeric", {
x <- rnorm(100)
res <- IC(x)
expect_true(ncol(res) == 2L)
ic1 <- IC(lm(x ~ 1))
expect_true(mean((ic1-res[,1])^2) < 1e-9)
expect_equal(mean(x), unname(attr(res, "coef")["mean"]))
expect_true(var(x)*(length(x)-1)/length(x) == attr(res, "coef")["var"])
res <- IC(cbind(x1=x, x2=x))
expect_true(NCOL(res) == 5) # 2 mean, 2 var, 1 cov
expect_equal(mean(x), unname(attr(res, "coef")["x1"]))
expect_equal(mean(x), unname(attr(res, "coef")["x2"]))
expect_equal(var(x)*(length(x)-1)/length(x), unname(attr(res, "coef")[3]))
expect_equal(var(x)*(length(x)-1)/length(x), unname(attr(res, "coef")[5]))
})
y <- rnorm(100)
x <- rnorm(100)
d <- data.frame(y=y,x=x)
e <- estimate(l <- lm(y ~ x, data=d))
test_that("influence", {
expect_equal(influence(e), IC(e), ignore_attr = TRUE)
})
test_that("IC misc", {
ic0 <- IC(l)
cl <- paste0("c", rep(c(3, 1, 10, 2, 4:9), length.out = nrow(d)))
ic1 <- IC(l, id = cl)
# aggregation within clusters in first-appearance order
expect_equal(rownames(ic1), unique(cl))
expect_equal(ic1, rowsum(ic0, cl, reorder = FALSE) * 10 / nrow(d), ignore_attr = TRUE)
expect_equal(attr(ic1, "N"), nrow(d))
expect_equal(colnames(ic1), colnames(ic0))
expect_false(is.null(attr(ic1, "bread")))
expect_equal(var_ic(ic1), vcov(estimate(l, id = cl)), ignore_attr = TRUE)
# missing values in id
cl[1] <- NA
expect_error(IC(l, id = cl), "Missing values in 'id'")
})
test_that("IC.coxph", {
library("survival")
bladder1 <- bladder[bladder$enum < 5, ]
a1 <- coxph(formula = Surv(stop, event) ~ (rx + size + number) * strata(enum),
data=bladder1, robust=TRUE)
b1 <- estimate(a1)
a2 <- coxph(formula = Surv(stop, event) ~ (rx + size + number) * strata(enum),
data=bladder1)
b2 <- estimate(a2)
coxph_se <- summary(a1)$coefficients[, "robust se"]
estimate_se <- parameter(b1)[, 2]
estimate_se2 <- parameter(b2)[, 2]
expect_equal(coxph_se, estimate_se, ignore_attr=TRUE)
expect_equal(estimate_se2, estimate_se, ignore_attr=TRUE)
})
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.