Skip to content

Commit

Permalink
Merge pull request #42 from sfcheung/devel
Browse files Browse the repository at this point in the history
Fix some typos
  • Loading branch information
sfcheung committed Apr 18, 2022
2 parents 4026ba0 + 0493d2f commit 3043019
Show file tree
Hide file tree
Showing 19 changed files with 365 additions and 40 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Generated by roxygen2: do not edit by hand

S3method(confint,std_selected)
S3method(print,summary.std_selected)
S3method(summary,std_selected)
S3method(vcov,std_selected)
export(std_selected)
export(std_selected_boot)
export(stdmod)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
- Change the column order of the coefficient table
in `std_selected_boot` output. Bootstrapping confidence
intervals are placed next to parameter estimates.
- Add `vcov` method for `std_selected` output. If bootstrapping is used,
it can return the variance-covariance matrix of the bootstrap estimates.
- Add `confint` method for `std_selected` output. If bootstrapping is used,
it can return the bootstrap percentile confidence intervals if requested.

- 0.1.3

Expand Down
68 changes: 68 additions & 0 deletions R/confint_stdmod.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#'@title confint method for \code{std_selected} class output
#'
#'@description Return the confidence intervals of estimates
#' in a \code{std_selected} class output
#'
#'@details If bootstrapping is used to form the confidence interval,
#' users can request the percentile confidence intervals of
#' the bootstrap estimates.
#'
#'@return
#' A matrix of the confidence intervals.
#'
#'@param object The output of the class \code{std_selected}.
#'@param parm The parameters for which confidence intervals should be returned.
#' If missing, the confidence intervals of all parameters will be
#' returned.
#'@param level The level of confidence. For the confidence intervals returned
#' by [lm], default is .95. For the bootstrapping confidence
#' intervals, default is the level used in calling
#' [std_selected_boot]. If a level different from that in the
#' original
#' call is specified, `full_output` needs to be set in the call
#' to [std_selected_boot] such that the original bootstrap output
#' is stored.
#'@param type The type of the confidence intervals. Default is "lm",
#' returned by the [confint] method of [lm]. If set to "boot",
#' the bootstrap percentile confidence intervals are
#' returned.
#'@param ... Arguments to be passed to \code{summary.lm}.
#'
#'@examples
#' # See examples for std_selected.
#' @export


confint.std_selected <- function(object, parm, level = .95, type = "lm", ...) {
if (!(type %in% c("lm", "boot"))) {
stop("type must be either lm or boot.")
}
if (type == "boot") {
if (is.null(object$boot_est)) {
stop("Bootstrap estimates not available in the object.")
}
if ((level != object$conf) && is.null(object$boot_out)) {
stop("level is different form conf in std_selected_boot but full_output is FALSE.")
}
if (level == object$conf) {
out <- object$boot_ci
} else {
p <- nrow(object$boot_ci)
out <- t(sapply(seq_len(p), function(x) {
boot::boot.ci(object$boot_out, conf = level,
type = "perc", index = x)$percent[4:5]
}))
rownames(out) <- names(object$boot_out$t0)
}
tmp <- object
class(tmp) <- "lm"
colnames(out) <- colnames(stats::confint(tmp, level = level))
if (!missing(parm)) {
out <- out[rownames(out) %in% parm, , drop = FALSE]
}
return(out)
}
if (type == "lm") {
NextMethod()
}
}
22 changes: 13 additions & 9 deletions R/print_summary_stdmod.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@

print.summary.std_selected <- function(x, ...) {
cat("\nSelected variable(s) are centered and/or scaled")
cat("\nVariable(s) centered:", x$centered_terms)
cat("\nVariable(s) scaled:", x$scaled_terms)
cat("\n- Variable(s) centered:", x$centered_terms)
cat("\n- Variable(s) scaled:", x$scaled_terms)
cat("\n")
dat_sc <- data.frame(centered_by = x$centered_by,
scaled_by = x$scaled_by)
print(dat_sc)
cat("Note:")
cat("\nCentered by 0 = No centering; Scaled by 1: No scaling.")
cat("\nNote:")
cat("\n- Centered by 0 or NA: No centering\n- Scaled by 1 or NA: No scaling")
if (!is.null(x$nboot)) {
cat("\nNonparametric bootstrapping 95% confidence intervals computed.")
cat("\nNumber of bootstrap samples is", x$nboot)
cat("\n- Nonparametric bootstrapping 95% confidence intervals computed.")
cat("\n- The number of bootstrap samples is", x$nboot)
}
cat("\n")
xlm <- x
class(xlm) <- "summary.lm"
print(xlm, ...)
NextMethod()
if (!is.null(x$nboot)) {
cat("Note:")
cat("\n- [CI Lower, CI Upper] are bootstrap percentile confidence intervals.")
cat("\n- Std. Error are standard errors in the original analysis, not bootstrap SEs.")
cat("\n")
}
}
2 changes: 1 addition & 1 deletion R/std_selected.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ std_selected <- function(lm_out,
lm_out_mod$centered_terms <- center_terms
lm_out_mod$scaled_by <- var_b
lm_out_mod$centered_by <- var_a

lm_out_mod$std_selected_call <- match.call()
lm_out_mod

}
11 changes: 10 additions & 1 deletion R/std_selected_boot.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' random number. Users are recommended to set the seed, e.g., using [set.seed()]
#' before calling it, to ensure reproducibility.
#'
# The updated \code{lm} output, with the class \code{stdmod} added. It will be
# The updated \code{lm} output, with the class \code{std_selected} added. It will be
# treated as a usual \code{lm} object by most functions. It has these additional elements:
#
# - \code{scaled}: The terms scaled.
Expand All @@ -30,6 +30,9 @@
#' @param nboot The number of bootstrap samples. Default is 100.
#' @param boot_args A named list of arguments to be passed to [boot::boot()]. Default
#' is `NULL`.
#' @param save_boot_est If `TRUE`, the default, the bootstrap estimates will be saved
#' in the element
#' `boot_est` of the output.
#' @param full_output Whether the full output from [boot::boot()] is return. Default is
#' `FALSE`.
#'
Expand Down Expand Up @@ -61,6 +64,7 @@ std_selected_boot <- function(lm_out,
conf = .95,
nboot = 100,
boot_args = NULL,
save_boot_est = TRUE,
full_output = FALSE) {
if (missing(lm_out)) {
stop("The arguments lm_out cannot be empty.")
Expand Down Expand Up @@ -108,6 +112,11 @@ std_selected_boot <- function(lm_out,

std_selected_out$boot_ci <- cis
std_selected_out$nboot <- nboot
std_selected_out$conf <- conf
tmp <- boot_out$t
colnames(tmp) <- names(boot_out$t0)
std_selected_out$boot_est <- tmp
std_selected_out$std_selected_boot_call <- match.call()
if (full_output) {
std_selected_out$boot_out <- boot_out
}
Expand Down
38 changes: 38 additions & 0 deletions R/vcov_stdmod.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#'@title vcov method for \code{std_selected} class output
#'
#'@description Compute the variance-covariance matrix
#' of estimates in a \code{std_selected} class output
#'
#'@details If bootstrapping is used to form the confidence interval,
#' users can request the variance-covariance matrix of the bootstrap estimates.
#'
#'@return
#' A matrix of the variances and covariances.
#'
#'@param object The output of the class \code{std_selected}.
#'@param type The type of variance-covariance matrix. Default is "lm",
#' returned by the [vcov] method of [lm]. If set to "boot",
#' the variance-covariance matrix of the bootstrap estimates
#' is returned.
#'@param ... Arguments to be passed to \code{summary.lm}.
#'
#'@examples
#' # See examples for std_selected.
#' @export


vcov.std_selected <- function(object, type = "lm", ...) {
if (!(type %in% c("lm", "boot"))) {
stop("type must be either lm or boot.")
}
if (type == "boot") {
if (is.null(object$boot_est)) {
stop("Bootstrap estimates not available in the object.")
}
out <- stats::cov(object$boot_est)
return(out)
}
if (type == "lm") {
NextMethod()
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ remotes::install_github("sfcheung/stdmod")

# Implementation

The main function, `stdmod_selected()`, accepts an `lm()`
The main function, `std_selected()`, accepts an `lm()`
output, standardizes variables by users, and update the
results. If interaction terms are presented, they will be
formed after the standardization. If bootstrapping
Expand Down
41 changes: 41 additions & 0 deletions man/confint.std_selected.Rd

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

5 changes: 5 additions & 0 deletions man/std_selected.Rd

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

32 changes: 32 additions & 0 deletions man/vcov.std_selected.Rd

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

10 changes: 6 additions & 4 deletions tests/testthat/test_stdmod_selected_boot.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ library(stdmod)

set.seed(8970808)

nboot <- 1000

context("Check standardizing selected variables with std_selected with bootstrapping")

dat <- test_x_1_w_1_v_1_cat1_n_500
Expand All @@ -27,13 +29,13 @@ lm_cxsw <- lm(dv ~ iv*mod + v1 + cat1, dplyr::mutate(dat, iv = scale(iv, scale
mod = scale(mod, scale = sd(dat$mod), center = FALSE)[, 1]))


stdmod_wy <- std_selected_boot(lm_raw, to_scale = ~ mod + dv, to_center = ~ mod + dv, nboot = 2000)
stdmod_xwy <- std_selected_boot(lm_raw, to_scale = ~ mod + iv + dv, to_center = ~ iv + mod + dv, nboot = 2000)
stdmod_wy <- std_selected_boot(lm_raw, to_scale = ~ mod + dv, to_center = ~ mod + dv, nboot = nboot)
stdmod_xwy <- std_selected_boot(lm_raw, to_scale = ~ mod + iv + dv, to_center = ~ iv + mod + dv, nboot = nboot)

stdmod2_wy <- stdmod_boot(lm_raw, x = iv, y = dv, w =mod,
x_rescale = FALSE, y_rescale = TRUE, w_rescale = TRUE, nboot = 2000)
x_rescale = FALSE, y_rescale = TRUE, w_rescale = TRUE, nboot = nboot)
stdmod2_xwy <- stdmod_boot(lm_raw, x = iv, y = dv, w =mod,
x_rescale = TRUE, y_rescale = TRUE, w_rescale = TRUE, nboot = 2000)
x_rescale = TRUE, y_rescale = TRUE, w_rescale = TRUE, nboot = nboot)

stdmod3_wy <- std_selected(lm_raw, to_scale = ~ mod + dv, to_center = ~ dv + mod)
stdmod3_xwy <- std_selected(lm_raw, to_scale = ~ dv + iv + mod, to_center = ~ mod + iv + dv)
Expand Down
50 changes: 50 additions & 0 deletions tests/testthat/test_stdmod_selected_confint.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
library(testthat)
library(stdmod)

context("Check setting confint in summary if bootstrapping is done")

dat <- test_x_1_w_1_v_1_cat1_n_500

lm_raw <- lm(dv ~ iv*mod + v1 + cat1, dat)

set.seed(649831074)
stdmod_wy <- std_selected_boot(lm_raw, to_scale = ~ mod + dv, to_center = ~ mod + dv,
nboot = 100,
full_output = TRUE)
confint_def <- confint(stdmod_wy)
confint_boot <- confint(stdmod_wy, type = "boot")
confint_lm <- confint(stdmod_wy, type = "lm")
confint_boot_parm <- confint(stdmod_wy, parm = "iv:mod", type = "boot")
confint_boot_conf90 <- confint(stdmod_wy, parm = "iv:mod", level = .90, type = "boot")

stdmod_wy_lm <- std_selected(lm_raw, to_scale = ~ mod + dv, to_center = ~ mod + dv)
confint_lm_no_boot <- confint(stdmod_wy_lm)

confint_boot_check <- summary(stdmod_wy)$coefficients[, c("CI Lower", "CI Upper")]
confint_boot_conf90_check <- boot::boot.ci(stdmod_wy$boot_out, conf = .90,
type = "perc", index = 7)$percent[4:5]

test_that("confint_def == confint by lm", {
expect_equivalent(
confint_def, confint_lm_no_boot
)
})

test_that("confint_boot == confint from summary", {
expect_equivalent(
confint_boot, confint_boot_check
)
})

test_that("confint_boot != confint from lm", {
expect_false(
identical(confint_boot, confint_lm_no_boot)
)
})

test_that("confint_boot with user confidence level", {
expect_true(
all(confint_boot_conf90 == confint_boot_conf90_check)
)
})

Loading

0 comments on commit 3043019

Please sign in to comment.