Skip to content

Commit

Permalink
Fixed issue #39 and #56
Browse files Browse the repository at this point in the history
  • Loading branch information
merliseclyde committed Nov 9, 2022
1 parent d33f2bf commit bb881c2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 28 deletions.
12 changes: 9 additions & 3 deletions R/coefficients.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,16 @@ coef.bas <- function(object, n.models, estimator = "BMA", ...) {
models <- rep(0, nvar + 1)
models[bestmodel + 1] <- 1
if (sum(models) > 1) {

# fix for issue #39 and #56
modelform = as.formula(eval(object$call$formula, parent.frame()))
environment(modelform) = environment()
data = eval(object$call$data)
weights = eval(object$call$weights)
object <- bas.lm(
eval(object$call$formula),
data = eval(object$call$data),
weights = eval(object$call$weights),
formula=modelform,
data = data,
weights = weights,
n.models = 1,
alpha = object$g,
initprobs = object$probne0,
Expand Down
25 changes: 0 additions & 25 deletions tests/testthat/test-bas-lm.R
Original file line number Diff line number Diff line change
Expand Up @@ -317,29 +317,4 @@ test_that("as.matrix tools", {
})


# GitHub issue #56 and #42

test_that("formula and env issues with MPM",{
data(UScrime, package = "MASS")
UScrime <- UScrime[, 1:5]

crime.bic1 <- bas.lm(formula = M ~ So + Ed + Po1 + Po2,
data = UScrime,
prior = "JZS",
initprobs = c(1, 0.5, 0.5, 0.5, 0.5),
renormalize = TRUE)
coef.mpm1 <- coef(crime.bic1, estimator = "MPM")

form <- M ~ So + Ed + Po1 + Po2
crime.bic2 <- BAS::bas.lm(formula = form,
data = UScrime,
prior = "JZS",
initprobs = c(1, 0.5, 0.5, 0.5, 0.5),
renormalize = TRUE)

expect_error(coef.mpm2 = coef(crime.bic2, estimator = "MPM"))
# expect_equal(coef.mpm1, coef.mpm2)
}
)


54 changes: 54 additions & 0 deletions tests/testthat/test-coefficients.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,57 @@ test_that("plot posterior coefficients", {
expect_null(plot(crime_coef, ask=FALSE))
})

# GitHub issue #56 and #39

test_that("formula and env issues with MPM",{
data(UScrime, package = "MASS")
UScrime <- UScrime[, 1:5]

crime.bic1 <- bas.lm(formula = M ~ So + Ed + Po1 + Po2,
data = UScrime,
prior = "JZS",
initprobs = c(1, 0.5, 0.5, 0.5, 0.5),
renormalize = TRUE)
coef.mpm1 <- coef(crime.bic1, estimator = "MPM")

form <- M ~ So + Ed + Po1 + Po2
crime.bic2 <- bas.lm(formula = form,
data = UScrime,
prior = "JZS",
initprobs = c(1, 0.5, 0.5, 0.5, 0.5),
renormalize = TRUE)

coef.mpm2 = coef(crime.bic2, estimator = "MPM")
expect_equal(coef.mpm1, coef.mpm2)
}
)


test_that("env and lm", {
data(UScrime, package = "MASS")
UScrime <- UScrime[, 1:5]

mylm = function(object) {
modelform = as.formula(eval(object$call$formula, parent.frame()))
environment(modelform) = environment()
data = eval(object$call$data)
weights = eval(object$call$weights)

object = lm(formula = modelform,
data = data,
weights = weights)
return(object) }


crime.lm1 <- lm(formula = M ~ So + Ed + Po1 + Po2, data = UScrime)
tmp1 = mylm(crime.lm1)

form = M ~ So + Ed + Po1 + Po2
crime.lm2 <- lm(formula = form, data = UScrime)

tmp = mylm(crime.lm2)

expect_equal(coef(tmp), coef(tmp1))

})

0 comments on commit bb881c2

Please sign in to comment.