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

0.0.1.15 #23

Merged
merged 2 commits into from
Oct 30, 2024
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: betaselectr
Title: Selective Standardization in Structural Equation Models
Version: 0.0.1.14
Version: 0.0.1.15
Authors@R:
c(person(given = "Shu Fai",
family = "Cheung",
Expand Down
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# betaselectr 0.0.1.14
# betaselectr 0.0.1.15

- Added `lm_betaselect()` and related
methods and helper functions.
Expand Down Expand Up @@ -77,4 +77,8 @@
to print confidence intervals by
default, if available. (0.0.1.13)

- Added `skip_response` to
`lm_betaselect()` and `glm_betaselect()`
as a convenient way to skip standardizing
the response variables. (0.0.1.15)

18 changes: 17 additions & 1 deletion R/lm_betaselect.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@
#' at the same time. Default is `NULL`,
#' and only `to_standardize` is used.
#'
#' @param skip_response Logical. If
#' `TRUE`, will not standardize the
#' response (outcome) variable even if
#' it appears in `to_standardize` or
#' `to_standardize` is not specified.
#' Used for models such as logistic
#' regression models in which there are
#' some restrictions on the response
#' variables (e.g., only 0 or 1 for
#' logistic regression).
#'
#' @param do_boot Whether bootstrapping
#' will be conducted. Default is `TRUE`.
#'
Expand Down Expand Up @@ -352,6 +363,7 @@
lm_betaselect <- function(...,
to_standardize = NULL,
not_to_standardize = NULL,
skip_response = FALSE,
do_boot = TRUE,
bootstrap = 100L,
iseed = NULL,
Expand Down Expand Up @@ -408,7 +420,10 @@ lm_betaselect <- function(...,
input_data = input_data,
to_standardize = to_standardize,
not_to_standardize = not_to_standardize,
skip_categorical_x = TRUE
skip_categorical_x = TRUE,
skip_response = skip_response,
model_call = model_call,
org_call = my_call
)
# Do standardization
input_data_z <- input_data
Expand Down Expand Up @@ -510,6 +525,7 @@ lm_betaselect <- function(...,
glm_betaselect <- function(...,
to_standardize = NULL,
not_to_standardize = NULL,
skip_response = FALSE,
do_boot = TRUE,
bootstrap = 100L,
iseed = NULL,
Expand Down
11 changes: 10 additions & 1 deletion R/lm_betaselect_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fix_to_standardize_lm <- function(object,
# if (length(prods) > 0) {
# to_standardize <- setdiff(to_standardize, names(prods))
# }
browser()
to_standardize
}

Expand All @@ -58,7 +59,10 @@ fix_to_standardize_lm_data <- function(object,
input_data,
to_standardize = ".all.",
not_to_standardize = NULL,
skip_categorical_x = TRUE) {
skip_categorical_x = TRUE,
skip_response = FALSE,
model_call = NULL,
org_call = NULL) {

if (!identical(to_standardize, ".all.") && !is.null(not_to_standardize)) {
stop("Do not specify both to_standardize and not_to_standardize.")
Expand Down Expand Up @@ -96,6 +100,11 @@ fix_to_standardize_lm_data <- function(object,
if ((length(cat_vars) > 0) && skip_categorical_x) {
to_standardize <- setdiff(to_standardize, cat_vars)
}
if (skip_response) {
lm_terms <- stats::terms(object)
lm_y <- all.vars(lm_terms)[attr(lm_terms, "response")]
to_standardize <- setdiff(to_standardize, lm_y)
}
# if (length(prods) > 0) {
# to_standardize <- setdiff(to_standardize, names(prods))
# }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Not ready for use.

# betaselectr: Do selective standardization in structural equation models and regression models

(Version 0.0.1.14, updated on 2024-10-30, [release history](https://sfcheung.github.io/betaselectr/news/index.html))
(Version 0.0.1.15, updated on 2024-10-30, [release history](https://sfcheung.github.io/betaselectr/news/index.html))

It computes Beta_Select, standardization
in structural equation models with only
Expand Down
13 changes: 13 additions & 0 deletions man/lm_betaselect.Rd

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

14 changes: 14 additions & 0 deletions tests/testthat/test_glm_betaselect_skip_def.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
library(testthat)

dat <- data_test_mod_cat

dat$dv <- ifelse(dat$dv > mean(dat$dv),
yes = 1,
no = 0)
test_that("skip response", {
expect_error(glm_betaselect(dv ~ iv*mod + cov1 + cat1, dat, to_standardize = "dv", do_boot = FALSE, family = binomial),
"y values", fixed = TRUE)
expect_error(glm_betaselect(dv ~ iv*mod + cov1 + cat1, dat, do_boot = FALSE, family = binomial),
"y values", fixed = TRUE)
expect_no_error(glm_betaselect(dv ~ iv*mod + cov1 + cat1, dat, skip_response = TRUE, do_boot = FALSE, family = binomial))
})
22 changes: 13 additions & 9 deletions vignettes/betaselectr_glm.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ including product terms.

Suppose we only need to
solve the first problem, standardizing all
numeric variables,
numeric variables except for the
response variable (which is binary),
with the product
term computed after `iv` and `mod`
are standardized.
Expand All @@ -242,7 +243,7 @@ are standardized.
``` r
glm_beta_select <- glm_betaselect(dv ~ iv*mod + cov1 + cat1,
data = data_test_mod_cat_binary,
not_to_standardize = "dv",
skip_response = TRUE,
family = binomial(),
do_boot = FALSE)
```
Expand All @@ -262,9 +263,9 @@ variable (`dv` in this example) may not
be meaningful or may even be not allowed.
In the case of logistic regression, the
outcome variable need to be 0 or 1 only.
Therefore, `not_to_standardize` is set to
`"dv"`, the name of the outcome variable,
to request that it is *not* standardized.
Therefore, `skip_response` is set to
`TRUE`, to request that the response
(outcome) variable is *not* standardized.

Moreover, categorical variables (factors and
string variables) will not be standardized.
Expand All @@ -286,7 +287,7 @@ summary(glm_beta_select)
#> Waiting for profiling to be done...
#> Call to glm_betaselect():
#> betaselectr::lm_betaselect(formula = dv ~ iv * mod + cov1 + cat1,
#> family = binomial(), data = data_test_mod_cat_binary, not_to_standardize = "dv",
#> family = binomial(), data = data_test_mod_cat_binary, skip_response = TRUE,
#> do_boot = FALSE, model_call = "glm")
#>
#> Variable(s) standardized: iv, mod, cov1
Expand Down Expand Up @@ -367,7 +368,7 @@ set:
glm_beta_select_boot <- glm_betaselect(dv ~ iv*mod + cov1 + cat1,
data = data_test_mod_cat_binary,
family = binomial(),
not_to_standardize = "dv",
skip_response = TRUE,
bootstrap = 5000,
iseed = 4567)
```
Expand All @@ -392,7 +393,7 @@ This is the output of `summary()`
summary(glm_beta_select_boot)
#> Call to glm_betaselect():
#> betaselectr::lm_betaselect(formula = dv ~ iv * mod + cov1 + cat1,
#> family = binomial(), data = data_test_mod_cat_binary, not_to_standardize = "dv",
#> family = binomial(), data = data_test_mod_cat_binary, skip_response = TRUE,
#> bootstrap = 5000, iseed = 4567, model_call = "glm")
#>
#> Variable(s) standardized: iv, mod, cov1
Expand Down Expand Up @@ -470,6 +471,7 @@ this, setting
glm_beta_select_boot_1 <- glm_betaselect(dv ~ iv*mod + cov1 + cat1,
data = data_test_mod_cat_binary,
to_standardize = c("iv", "cov1"),
skip_response = TRUE,
family = binomial(),
bootstrap = 5000,
iseed = 4567)
Expand All @@ -485,6 +487,7 @@ this call, and set
glm_beta_select_boot_2 <- glm_betaselect(dv ~ iv*mod + cov1 + cat1,
data = data_test_mod_cat_binary,
not_to_standardize = c("dv", "mod"),
skip_response = TRUE,
family = binomial(),
bootstrap = 5000,
iseed = 4567)
Expand All @@ -500,7 +503,8 @@ summary(glm_beta_select_boot_1)
#> Call to glm_betaselect():
#> betaselectr::lm_betaselect(formula = dv ~ iv * mod + cov1 + cat1,
#> family = binomial(), data = data_test_mod_cat_binary, to_standardize = c("iv",
#> "cov1"), bootstrap = 5000, iseed = 4567, model_call = "glm")
#> "cov1"), skip_response = TRUE, bootstrap = 5000, iseed = 4567,
#> model_call = "glm")
#>
#> Variable(s) standardized: iv, cov1
#>
Expand Down
15 changes: 9 additions & 6 deletions vignettes/betaselectr_glm.Rmd.original
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,16 @@ including product terms.

Suppose we only need to
solve the first problem, standardizing all
numeric variables,
numeric variables except for the
response variable (which is binary),
with the product
term computed after `iv` and `mod`
are standardized.

```{r, results = FALSE}
glm_beta_select <- glm_betaselect(dv ~ iv*mod + cov1 + cat1,
data = data_test_mod_cat_binary,
not_to_standardize = "dv",
skip_response = TRUE,
family = binomial(),
do_boot = FALSE)
```
Expand All @@ -209,9 +210,9 @@ variable (`dv` in this example) may not
be meaningful or may even be not allowed.
In the case of logistic regression, the
outcome variable need to be 0 or 1 only.
Therefore, `not_to_standardize` is set to
`"dv"`, the name of the outcome variable,
to request that it is *not* standardized.
Therefore, `skip_response` is set to
`TRUE`, to request that the response
(outcome) variable is *not* standardized.

Moreover, categorical variables (factors and
string variables) will not be standardized.
Expand Down Expand Up @@ -269,7 +270,7 @@ set:
glm_beta_select_boot <- glm_betaselect(dv ~ iv*mod + cov1 + cat1,
data = data_test_mod_cat_binary,
family = binomial(),
not_to_standardize = "dv",
skip_response = TRUE,
bootstrap = 5000,
iseed = 4567)
```
Expand Down Expand Up @@ -330,6 +331,7 @@ this, setting
glm_beta_select_boot_1 <- glm_betaselect(dv ~ iv*mod + cov1 + cat1,
data = data_test_mod_cat_binary,
to_standardize = c("iv", "cov1"),
skip_response = TRUE,
family = binomial(),
bootstrap = 5000,
iseed = 4567)
Expand All @@ -344,6 +346,7 @@ this call, and set
glm_beta_select_boot_2 <- glm_betaselect(dv ~ iv*mod + cov1 + cat1,
data = data_test_mod_cat_binary,
not_to_standardize = c("dv", "mod"),
skip_response = TRUE,
family = binomial(),
bootstrap = 5000,
iseed = 4567)
Expand Down
Loading