Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing logisticCoefs double dot bug #221

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# simstudy (development version)

## Minor fix

* Function `logisticCoefs` now correctly handles double dot notation.

# simstudy 0.7.1

## Breaking Changes
Expand Down
12 changes: 12 additions & 0 deletions R/utility.R
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,18 @@ logisticCoefs <- function(defCovar, coefs, popPrev, rr = NULL, rd = NULL,
if (!is.null(rd)) targetStat <- "rd"
if (!is.null(auc)) targetStat <- "auc"

# Get any double dot vars from calling environment into this environment

.formvars <- all.vars(parse(text = defCovar$formula))
.dotVars <- .formvars[startsWith(.formvars, "..")]
.vars <- gsub("^\\.{2}", "", .dotVars)

for (i in seq_along(.vars)) {
assign(.vars[i], get(.vars[i], envir = parent.frame()))
}

#

dd <- genData(sampleSize, defCovar)

if (targetStat == "prev") {
Expand Down
50 changes: 50 additions & 0 deletions tests/testthat/test-generate_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -836,3 +836,53 @@ test_that("genSpline throws errors", {
noise.var = .025), regexp = "newrange should be a numeric!")
})

#logisticCoefs

test_that("logistiCoefs works", {

skip_on_cran()

f <- function(n, prevalence, mu_x, coefs_y) {

defs <- defData(varname = "x", formula = "..mu_x", variance = 1, dist = "normal")

intercept <- logisticCoefs(defCovar = defs, coefs = coefs_y, popPrev = prevalence)[['B0']]

defs <- defData(
defs,
varname = "y",
formula = "t(c(..intercept, ..coefs_y)) %*% c(1, x)",
dist = "binary", link = "logit")

genData(n, defs)[, .(x= mean(x), y= mean(y) )]

}

p <- rbeta(1, 6, 3)
mu_x <- rnorm(1, 5, 1)
coefs_y <- c(rnorm(1, 0,.5))

expect_equal(
abs(sum(f(n = 999, prevalence = p, mu_x = mu_x, coefs_y = coefs_y) - c(mu_x, p))),
0,
tolerance = 0.25
)

defs <- defData(varname = "x", formula = "..mu_x", variance = 1, dist = "normal")

intercept <- logisticCoefs(defCovar = defs, coefs = coefs_y, popPrev = p)[['B0']]

defs <- defData(
defs,
varname = "y",
formula = "t(c(..intercept, ..coefs_y)) %*% c(1, x)",
dist = "binary", link = "logit")

expect_equal(
abs(sum(genData(n = 999, defs)[, .(x= mean(x), y= mean(y) )] - c(mu_x, p))),
0,
tolerance = 0.25
)

})

Loading