Skip to content

Commit

Permalink
update confint
Browse files Browse the repository at this point in the history
  • Loading branch information
Liming Li (李黎明) committed Jan 23, 2024
1 parent daed4b0 commit fd20ad0
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ S3method(as.cov_struct,cov_struct)
S3method(as.cov_struct,formula)
S3method(augment,mmrm)
S3method(coef,mmrm_tmb)
S3method(confint,mmrm)
S3method(deviance,mmrm_tmb)
S3method(fitted,mmrm_tmb)
S3method(format,cov_struct)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- `Anova` is implemented for `mmrm` models and available upon loading the `car` package. It supports type II and III hypothesis testing.
- The argument `start` for `mmrm_control()` is updated to allow function/numeric input for better
choices of initial values.
- `confint` on `mmrm` models will give t-based confidence interval now, instead of the normal approximation.

### Miscellaneous

Expand Down
32 changes: 32 additions & 0 deletions R/mmrm-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#' 2. If the covariance structure is spatial, the covariance matrix of two time points with unit distance
#' will be displayed.
#'
#' `confint` is used to obtain the confidence intervals for the coefficients.
#' Please note that this is different from the confidence interval of difference
#' of least square means from `emmeans`.
#'
#' @name mmrm_methods
#'
#' @seealso [`mmrm_tmb_methods`], [`mmrm_tidiers`] for additional methods.
Expand Down Expand Up @@ -227,3 +231,31 @@ print.summary.mmrm <- function(x,
cat("\n")
invisible(x)
}


#' @describeIn mmrm_methods obtain the confidence interval for the MMRM fit results.
#' @exportS3Method
#' @examples
#' # Confidence Interval:
#' confint(object)
confint.mmrm <- function(object, parm, level = 0.95, ...) {
cf <- coef(object)
pnames <- names(cf)
if (missing(parm))
parm <- pnames
assert(
check_subset(parm, pnames),
check_integerish(parm, lower = 1L, upper = length(cf))
)
if (is.numeric(parm)) parm <- pnames[parm]
assert_number(level, lower = 0, upper = 1)
a <- (1 - level) / 2
pct <- paste(format(100 * c(a, 1 - a), trim = TRUE, scientific = FALSE, digits = 3), "%")
coef_table <- h_coef_table(object)
df <- coef_table[parm, "df"]
ses <- coef_table[parm, "Std. Error"]
fac <- stats::qt(a, df = df)
ci <- array(NA, dim = c(length(parm), 2L), dimnames = list(parm, pct))
ci[] <- cf[parm] + (ses * fac) %o% c(1, -1)
ci
}
11 changes: 11 additions & 0 deletions man/mmrm_methods.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions tests/testthat/test-mmrm-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,40 @@ test_that("print.summary.mmrm works as expected for spatial fits", {
result <- summary(object)
expect_snapshot_output(print(result, digits = 1), cran = FALSE)
})

# confint ----

test_that("confint works for different significance levels", {
object <- get_mmrm()
expect_silent(confint(object, level = 1))
expect_silent(confint(object, level = 0))
expect_silent(confint(object, level = 0.5))
})

test_that("confint works for different `parm` input", {
object <- get_mmrm()
res <- expect_silent(confint(object, parm = c(1, 2, 3)))
expect_identical(row.names(res), names(coef(object))[c(1, 2, 3)])

res <- expect_silent(confint(object, parm = c("ARMCDTRT", "AVISITVIS4")))
expect_identical(row.names(res), c("ARMCDTRT", "AVISITVIS4"))

expect_error(
confint(object, parm = 100),
"Element 1 is not <= 11"
)
expect_error(
confint(object, parm = c("a", "b")),
"Must be a subset of"
)
})

test_that("confint give same result as emmeans if no ", {
object <- mmrm(FEV1 ~ ARMCD + us(AVISIT |USUBJID), data = fev_data)

Check warning on line 135 in tests/testthat/test-mmrm-methods.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=tests/testthat/test-mmrm-methods.R,line=135,col=43,[infix_spaces_linter] Put spaces around all infix operators.
emm <- emmeans(object, ~ ARMCD)
emm_pair <- pairs(emm, reverse = TRUE)
conf <- confint(emm_pair)
conf_coef <- confint(object, 2)
expect_equal(conf$lower.CL, conf_coef[, 1])
expect_equal(conf$upper.CL, conf_coef[, 2])
})
9 changes: 9 additions & 0 deletions vignettes/subsections/_intro-hypothesis_testing.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ pairs(lsmeans_by_visit, reverse = TRUE)
Note that we use here the `reverse` argument to obtain treatment minus
placebo results, instead of placebo minus treatment results.

To further obtain the confidence interval of the least square mean differences, we can apply
`confint()` on the result returned by `pairs()` .

This is similar to the `LSMEANS` in SAS, with `CL` and `DIFF` options.

```{r pdiffci}
confint(pairs(lsmeans_by_visit, reverse = TRUE))
```

## Support for car

This package includes methods that allow `mmrm` objects to be used with the
Expand Down

0 comments on commit fd20ad0

Please sign in to comment.