R Under development (unstable) (2025-08-19 r88650 ucrt) -- "Unsuffered Consequences" Copyright (C) 2025 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > cat("# bayesqgcomp test\n") # bayesqgcomp test > #devtools::install_github("alexpkeil1/qgcomp@dev") > library("qgcomp") > data("metals", package="qgcomp") > > Xnm <- c( + 'arsenic','barium','cadmium','calcium','chromium','copper', + 'iron','lead','magnesium','manganese','mercury','selenium','silver', + 'sodium','zinc' + ) > > # continuous outcome, example with perfect collinearity > metals$cadmium2 = metals$cadmium > Xnm2 = c(Xnm, "cadmium2") > res = try(qc.fit <- qgcomp.noboot(y~.,dat=metals[,c(Xnm2, 'y')], family=gaussian()), silent=TRUE) Including all model terms as exposures of interest > stopifnot(class(res)=="try-error") > # error > > res = try(qc.fit <- qgcomp.noboot(y~.,dat=metals[,c(Xnm2, 'y')], family=gaussian(), bayes=TRUE)) Including all model terms as exposures of interest > stopifnot(inherits(res, "qgcompfit")) > > # compare to results with colinear exposure removed > res = try(qc.fit2 <- qgcomp.noboot(y~.,dat=metals[,c(Xnm, 'y')], family=gaussian())) Including all model terms as exposures of interest > stopifnot(inherits(res, "qgcompfit")) > > #hitting code coverage just to check > dat = qgcomp:::.dgm_quantized(N=100) > dat$y = as.numeric(dat$y>median(dat$y)) > > qgcomp(y~.,dat=dat, expnms=c("x1", "x2"), family="binomial", rr=FALSE) Scaled effect size (positive direction, sum of positive coefficients = 1.48) x1 x2 0.868 0.132 Scaled effect size (negative direction, sum of negative coefficients = 0) None Mixture log(OR) (delta method CI): Estimate Std. Error Lower CI Upper CI Z value Pr(>|z|) (Intercept) -2.66099 0.74063 -4.11259 -1.2094 -3.5929 0.000327 psi1 1.48188 0.34587 0.80398 2.1598 4.2845 1.832e-05 > > res = try(qgcomp(y~x1+x2,dat=dat, expnms=c("x1", "x2"), family=NULL, rr=FALSE), silent=TRUE) NULL > stopifnot(inherits(res, "try-error")) > > qgcomp(y~x1+x2 | x1+x2,dat=dat, expnms=c("x1", "x2"), family="poisson") Prob(Y ~ count): Scaled effect size (positive direction, sum of positive coefficients = 0.516) x1 x2 0.864 0.136 Scaled effect size (negative direction, sum of negative coefficients = 0) None Prob(Y ~ zero/count): Scaled effect size (positive direction, sum of positive coefficients = 0.12) x2 1 Scaled effect size (negative direction, sum of negative coefficients = -9.77) x1 1 Mixture log(OR/RR) (Delta method CI): Prob(Y ~ count): Estimate Std. Error Lower CI Upper CI Z value Pr(>|z|) (Intercept) -1.54568 0.46985 -2.46657 -0.62479 -3.2897 0.001003 psi1 0.51557 0.21128 0.10147 0.92967 2.4402 0.014678 Prob(Y ~ zero): Estimate Std. Error Lower CI Upper CI Z value Pr(>|z|) (Intercept) -0.46185 2.14243 -4.6609 3.7372 -0.2156 0.8293 psi1 -9.64542 108.69453 -222.6828 203.3919 -0.0887 0.9293 > > res = try(qgcomp(y~x1+x2 | x1+x2 +I(x2^2), B=2,dat=dat, expnms=c("x1", "x2"), bayes=TRUE, family="poisson"), silent=TRUE) > stopifnot(inherits(res, "try-error")) > > > ft = qgcomp(y~x1+x2 + I(x2^2), B=2,dat=dat, expnms=c("x1", "x2"), bayes=TRUE, family="poisson") > pp = msm.predict(ft) > ft = qgcomp(y~x1+x2,dat=dat, expnms=c("x1", "x2"), bayes=TRUE, family="poisson") > res = try(msm.predict(ft), silent = TRUE) > stopifnot(inherits(res, "try-error")) > > > ft = qgcomp(y~x1+x2 + I(x2^2), B=2, q=NULL,dat=dat, expnms=c("x1", "x2"), bayes=TRUE, family=binomial()) Note: using all possible values of exposure as the intervention values > summary(ft) Mixture log(RR) (bootstrap CI): $coefficients Estimate Std. Error Lower CI Upper CI Z value Pr(>|z|) (Intercept) -1.7170560 0.20614702 -2.1210967 -1.3130153 -8.329279 0.000000e+00 psi1 0.5443754 0.09251761 0.3630442 0.7257066 5.884019 4.004213e-09 > qc.fit2 <- qgcomp.boot(y~.,expnms = Xnm, dat=metals[,c(Xnm, 'y')],B=2, q=NULL, family=gaussian()) Note: using quantiles of all exposures combined in order to set proposed intervention values for overall effect (25th, 50th, 75th %ile) You can ensure this is valid by scaling all variables in expnms to have similar ranges. > > > ## slight shrinkage with heavily non-linear models that don't converge > ## non-Bayesian > #results5 = qgcomp(y~. + .^2 + .^3 + arsenic*cadmium, > # expnms=Xnm, > # metals[,c(Xnm, 'y')], family=gaussian(), q=10, B=10, > # seed=125, degree=3) > # > #print(results5) > #plot(results5) > #results5$bootsamps # samples are wack > # > ## Bayesian (takes a few mins) > #results5bayes = qgcomp.boot(y~. + .^2 + .^3 + arsenic*cadmium, > # expnms=Xnm, > # metals[,c(Xnm, 'y')], family=gaussian(), q=10, B=10, > # seed=125, degree=3, > # bayes=TRUE) > # > ## note p-values are not computed due to negative > ## degrees of freedom (can fix by using a z-statistic instead of a t-statistic) > #print(results5bayes) > #plot(results5bayes) > #results5bayes$bootsamps # samples are reasonable > > > cat("done") done> > proc.time() user system elapsed 3.54 0.68 4.20