tests/testthat/test-joint-tests.R

test_that("term-specific proportional-odds tests share a null fit and match separate tests", {
    fit <- fastglm_ordinal(Sat~Infl+Type+Cont,MASS::housing,weights=Freq,firth=TRUE)
    count <- 0L; original <- .ordinal_fit_core
    local_mocked_bindings(.ordinal_fit_core=function(...) {
        count <<- count+1L; original(...)
    })
    for(estimation in c("ML","meanBR")) {
        count <- 0L
        combined <- fastglm_po_test(fit,by.term=TRUE,null.estimation=estimation)
        expect_equal(count,1L)
        expect_named(combined$tests,c("Joint","Infl","Type","Cont"))
        expect_output(print(combined),"Hypothesis.*Statistic.*df.*p-value")
        expect_true(grepl("unadjusted for multiple\\s+testing",
            paste(capture.output(print(combined)),collapse=" ")))
        for(term in names(combined$tests)) {
            separate <- fastglm_po_test(fit,terms=if(term=="Joint") NULL else term,null.estimation=estimation)
            expect_equal(combined$tests[[term]]$statistic,separate$statistic,tolerance=1e-9)
            expect_equal(combined$tests[[term]]$p.value,separate$p.value,tolerance=1e-10)
            expect_identical(combined$tests[[term]]$df,separate$df)
        }
        table <- as.data.frame(combined)
        expect_equal(nrow(table),4)
        expect_identical(table$statistic,unname(vapply(combined$tests,`[[`,numeric(1),"statistic")))
        if(requireNamespace("broom",quietly=TRUE)) expect_identical(broom::tidy(combined),table)
        failed <- fastglm_po_test(fit,by.term=TRUE,control=list(maxit=1))
        expect_true(all(!as.data.frame(failed)$available))
        expect_true(all(as.data.frame(failed,details=TRUE)$reason=="null_fit_not_converged"))
    }
    subset <- fastglm_po_test(fit,terms=c("Type","Cont"),by.term=TRUE)
    expect_equal(as.data.frame(subset)$df,c(4,3,1))
    expect_error(fastglm_po_test(fit,by.term=NA),"by.term")
})

test_that("joint Wald tests agree with Gaussian F tests and GLM coefficient tests", {
    set.seed(111401)
    d <- data.frame(x=rnorm(180),g=factor(rep(letters[1:3],60)))
    d$y <- .2+.4*d$x+.3*(d$g=="c")+rnorm(180)
    fit <- fastglm(y~x+g,d)
    joint <- fastglm_wald_test(fit,terms="g")
    comparison <- anova(lm(y~x,d),lm(y~x+g,d))
    expect_true(joint$available)
    expect_equal(joint$statistic,comparison$F[2]*2,tolerance=1e-8)
    expect_equal(joint$df,2)
    expect_equal(joint$p.value,pchisq(joint$statistic,2,lower.tail=FALSE))
    expect_identical(as.data.frame(joint)$statistic,joint$statistic)
    expect_output(print(joint),"Tested contrasts")
    d$y <- rpois(180,exp(.2+.4*d$x))
    fit <- fastglm(y~x*g,d,poisson(),tol=1e-12)
    reference <- summary(glm(y~x*g,data=d,family=poisson(),control=glm.control(epsilon=1e-12)))$coefficients
    one <- fastglm_wald_test(fit,terms="x")
    expect_equal(one$statistic,reference["x","z value"]^2,tolerance=1e-7)
    interaction <- fastglm_wald_test(fit,terms="x:g")
    expect_named(interaction$estimate,c("x:gb","x:gc"))
    expect_error(fastglm_wald_test(fit,terms="gb"),"unknown")
    source <- fastglm_source(function(k) d[((k-1)*60+1):(k*60),],3,
        formula=y~x*g,prototype=d)
    streaming <- fastglm_streaming(source,family=poisson(),tol=1e-12)
    expect_equal(fastglm_wald_test(streaming,terms="x:g")$statistic,interaction$statistic,tolerance=1e-7)
})

test_that("Wald contrasts align covariance and retain nonzero nulls and precision", {
    fit <- fastglm_ordinal(Sat~Infl+Type+Cont,MASS::housing,weights=Freq,firth=TRUE)
    b <- coef(fit); V <- vcov(fit)
    L <- rbind(difference=c(InflHigh=1,InflMedium=-1),contact=c(InflHigh=0,InflMedium=0))
    L <- cbind(L,ContHigh=c(0,1))
    out <- fastglm_wald_test(fit,contrast=L,null=c(.5,.1))
    index <- match(colnames(L),names(b))
    delta <- drop(L%*%b[index])-c(.5,.1)
    C <- L%*%V[index,index]%*%t(L)
    expect_equal(out$statistic,drop(crossprod(delta,solve(C,delta))),tolerance=1e-11)
    permutation <- rev(seq_along(b))
    reordered <- fastglm_wald_test(fit,contrast=L[,3:1],null=c(.5,.1),vcov.=V[permutation,permutation])
    expect_equal(reordered$statistic,out$statistic,tolerance=1e-11)
    transformed <- L*c(1e-8,1e8)
    scaled <- fastglm_wald_test(fit,contrast=transformed,null=c(.5,.1)*c(1e-8,1e8))
    expect_equal(scaled$statistic,out$statistic,tolerance=1e-10)
    robust <- fastglm_vcov(fit,type="HC0")
    a <- fastglm_wald_test(fit,terms="Type",vcov.=robust)
    c <- fastglm_wald_test(fit,terms="Type",vcov.=function(model) fastglm_vcov(model,type="HC0"))
    expect_true(a$available); expect_equal(a$statistic,c$statistic,tolerance=1e-12)
    expect_error(fastglm_wald_test(fit,terms="Type",contrast=L),"either")
    expect_error(fastglm_wald_test(fit,contrast=c(unknown=1)),"name fitted")
    expect_error(fastglm_wald_test(fit,contrast=L,null=1:3),"one value")
    bad <- V; bad[1,2] <- bad[1,2]+1
    expect_error(fastglm_wald_test(fit,contrast=diag(length(b)),vcov.=bad),"symmetric")
})

test_that("Wald tests report nonestimability and covariance failure explicitly", {
    set.seed(111402)
    x <- rnorm(100); design <- cbind("(Intercept)"=1,x=x,duplicate=x,z=rnorm(100))
    y <- rpois(100,exp(.2+.3*x))
    fit <- fastglm(design,y,poisson(),method="auto")
    expect_true(anyNA(coef(fit)))
    expect_false(fastglm_wald_test(fit,terms="x")$available)
    sum <- fastglm_wald_test(fit,contrast=c(x=1,duplicate=1))
    reduced <- fastglm(design[,c(1,2,4)],y,poisson())
    expect_true(sum$available)
    expect_equal(sum$statistic,fastglm_wald_test(reduced,terms="x")$statistic,tolerance=1e-8)
    expect_true(fastglm_wald_test(fit,terms="z")$available)
    repeated <- rbind(c(x=1),c(x=1)); rownames(repeated) <- c("first","same")
    singular <- fastglm_wald_test(reduced,contrast=repeated)
    expect_false(singular$available); expect_identical(singular$df,2L)
    expect_true(is.na(singular$p.value))
    expect_identical(singular$reason,"singular_contrast_covariance")
    compact <- fastglm(design[,c(1,2,4)],y,poisson(),inference="none",retain.data=FALSE)
    expect_identical(fastglm_wald_test(compact,terms="x")$reason,"covariance_unavailable")
    reduced$converged <- FALSE
    expect_identical(fastglm_wald_test(reduced,terms="x")$reason,"fit_not_converged")
})

Try the fastglm package in your browser

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

fastglm documentation built on Nov. 11, 2026, 5:07 p.m.