Skip to content

Commit

Permalink
add test to see if "bestmodel" violates hereditary condition
Browse files Browse the repository at this point in the history
  • Loading branch information
merliseclyde committed Dec 8, 2023
1 parent 75a8a36 commit 8e5e99b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: BAS
Version: 1.7.1.9000
Date: 2023-12-6
Date: 2023-12-7
Title: Bayesian Variable Selection and Model Averaging using Bayesian Adaptive Sampling
Authors@R: c(person("Merlise", "Clyde", email="[email protected]",
role=c("aut","cre", "cph"),
Expand Down
28 changes: 6 additions & 22 deletions R/bas_glm.R
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,11 @@ bas.glm <- function(formula, family = binomial(link = "logit"),

if (!inherits(betaprior, "prior")) stop("prior on coeeficients must be an object of type 'prior'")

loglik_null <- as.numeric(-0.5 * glm(Y ~ 1,
weights = weights,
offset = offset,
family = eval(call$family)
)$null.deviance)
null.deviance = glm(Y ~ 1,
weights = weights,
offset = offset,
family = eval(call$family))$null.deviance
loglik_null <- as.numeric(-0.5 * null.deviance)

betaprior$hyper.parameters$loglik_null <- loglik_null
# browser()
Expand Down Expand Up @@ -542,7 +542,7 @@ bas.glm <- function(formula, family = binomial(link = "logit"),

if (betaprior$class == "IC") df <- df - result$size + 1
result$df <- df
result$R2 <- .R2.glm.bas(result$deviance, result$size, call)
result$R2 <- 1.0 - result$deviance/null.deviance
result$n.vars <- p
result$Y <- Yvec
result$X <- X
Expand Down Expand Up @@ -637,19 +637,3 @@ bas.glm <- function(formula, family = binomial(link = "logit"),
return(probs)
}

.R2.glm.bas <- function(deviance, size, call) {
n.models <- length(deviance)
null.model <- (1:n.models)[size == 1]
if (is.null(null.model)) {
null.deviance <- glm(eval(call$formula),
data = eval(call$data),
family = eval(call$family)
)$null.deviance
}
else {
null.deviance <- deviance[null.model]
}

R2 <- 1 - deviance / null.deviance
return(R2)
}
10 changes: 9 additions & 1 deletion tests/testthat/test-bas-glm.R
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,19 @@ test_that("include always MCMC", {
data("Pima.tr", package="MASS")
pima_BAS = bas.glm(type ~ .,
data = Pima.tr, method = "MCMC",
include.always = ~ bp,
include.always = type ~ bp,
betaprior = g.prior(g=100), family = binomial(),
modelprior = beta.binomial(1, 1))
x = pima_BAS$probne0[match(c("Intercept", "bp") ,pima_BAS$namesx)]
expect_equal(2, sum(x), tolerance=.002)

expect_equal(0, sum(pima_BAS$R2 < 0))

expect_warning(bas.glm(type ~ poly(bp,2),
data = Pima.tr, method = "BAS",
force.heredity = TRUE, bestmodel = c(1,0,1),
betaprior = g.prior(g=100), family = binomial(),
modelprior = beta.binomial(1, 1)))

# pima_BAS = bas.glm(type ~ .,
# data = Pima.tr, method = "BAS",
Expand Down

0 comments on commit 8e5e99b

Please sign in to comment.