Skip to content

Commit

Permalink
[2023.8.23]
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce committed Aug 24, 2023
1 parent 97e68f2 commit 22535a2
Show file tree
Hide file tree
Showing 25 changed files with 173 additions and 125 deletions.
6 changes: 3 additions & 3 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 2023.8
Date: 2023-08-08 10:17:47 UTC
SHA: 29fdc28d5ddd19fa51b92b65f61362b3807df98a
Version: 2023.8.23
Date: 2023-08-23 22:24:41 UTC
SHA: 97e68f27f3289f1c419d7a5e631452d74d236ec1
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: bruceR
Title: Broadly Useful Convenient and Efficient R Functions
Version: 2023.8
Date: 2023-08-08
Version: 2023.8.23
Date: 2023-08-23
Authors@R:
c(person(given = "Han-Wu-Shuang",
family = "Bao",
Expand Down
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
**Please check the [latest news (change log)](https://psychbruce.github.io/bruceR/news/index.html) and keep this package updated.**

# bruceR 2023.9

## Minor Changes

- Now `var` and `items` support more flexible input, e.g.:
- `.mean(var="X", items=1:3)` matches `X1`, `X2`, `X3`
- `.mean(var="X.{any_placeholder}.pre", items=1:3)` matches `X.1.pre`, `X.2.pre`, `X.3.pre`
- Use `\donttest{}` in more examples to avoid unnecessary errors.

# bruceR 2023.8

## New Features
Expand Down
5 changes: 2 additions & 3 deletions R/bruceR-stats_1_basic.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ added = function(data, expr, when, by, drop=FALSE) {
dn.drop = base::setdiff(dn, dn.new)
data[, (dn.drop) := NULL][]
}
Print("<<green \u2714>> Raw data has already been changed. Please check.")
invisible(data)
}

Expand Down Expand Up @@ -618,7 +617,7 @@ Freq = function(
#' (2) a \code{ggplot2} object if \code{plot=TRUE}.
#'
#' @examples
#' Corr(airquality)
#' \donttest{Corr(airquality)
#' Corr(airquality, p.adjust="bonferroni",
#' plot.colors=c("#b2182b", "white", "#2166ac"))
#'
Expand All @@ -633,7 +632,7 @@ Freq = function(
#' O = .mean("O", 1:5, rev=c(2,5), range=1:6)
#' })
#' Corr(d[, .(age, gender, education, E, A, C, N, O)])
#'
#' }
#' @seealso
#' \code{\link{Describe}}
#'
Expand Down
70 changes: 47 additions & 23 deletions R/bruceR-stats_2_scale.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' Reverse scoring can also be easily implemented without saving extra variables.
#' \code{\link{Alpha}} function uses a similar method to deal with reverse scoring.
#'
#' Three options to specify variables:
#' Three ways to specify variables:
#' \enumerate{
#' \item \strong{\code{var + items}}: common and unique parts of variable names (suggested).
#' \item \strong{\code{vars}}: a character vector of variable names (suggested).
Expand All @@ -17,30 +17,34 @@
#'
#' @param data Data frame.
#' @param var \strong{[Option 1]}
#' The common part across the variables. e.g., \code{"RSES"}
#' Common part across variables: e.g., \code{"RSES"}, \code{"XX.{i}.pre"}
#' (if \code{var} string has any placeholder in braces \code{{...}},
#' then \code{items} will be pasted into the braces, see examples)
#' @param items \strong{[Option 1]}
#' The unique part across the variables. e.g., \code{1:10}
#' Unique part across variables: e.g., \code{1:10}, \code{c("a", "b", "c")}
#' @param vars \strong{[Option 2]}
#' A character vector specifying the variables. e.g., \code{c("X1", "X2", "X3", "X4", "X5")}
#' Character vector specifying variables: e.g., \code{c("X1", "X2", "X3", "X4", "X5")}
#' @param varrange \strong{[Option 3]}
#' A character string specifying the positions ("starting:stopping") of variables. e.g., \code{"A1:E5"}
#' Character string specifying positions ("start:stop") of variables: e.g., \code{"A1:E5"}
#' @param value [Only for \code{COUNT}] The value to be counted.
#' @param rev [Optional] Variables that need to be reversed. It can be
#' (1) a character vector specifying the reverse-scoring variables (recommended), or
#' (2) a numeric vector specifying the item number of reverse-scoring variables (not recommended).
#' @param range,likert [Optional] Range of likert scale (e.g., \code{1:5}, \code{c(1, 5)}).
#' @param range,likert [Optional] Range of likert scale: e.g., \code{1:5}, \code{c(1, 5)}.
#' If not provided, it will be automatically estimated from the given data (BUT you should use this carefully).
#' @param na.rm Ignore missing values. Defaults to \code{TRUE}.
#' @param values [Only for \code{CONSEC}] Values to be counted as consecutive identical values. Defaults to all numbers (\code{0:9}).
#'
#' @return A vector of computed values.
#'
#' @examples
#' d = data.table(x1=1:5,
#' x4=c(2,2,5,4,5),
#' x3=c(3,2,NA,NA,5),
#' x2=c(4,4,NA,2,5),
#' x5=c(5,4,1,4,5))
#' d = data.table(
#' x1 = 1:5,
#' x4 = c(2,2,5,4,5),
#' x3 = c(3,2,NA,NA,5),
#' x2 = c(4,4,NA,2,5),
#' x5 = c(5,4,1,4,5)
#' )
#' d
#' ## I deliberately set this order to show you
#' ## the difference between "vars" and "varrange".
Expand Down Expand Up @@ -71,11 +75,31 @@
#' }, drop=TRUE)
#' data
#'
#' ## ====== New Feature for `var` & `items` ====== ##
#' d = data.table(
#' XX.1.pre = 1:5,
#' XX.2.pre = 6:10,
#' XX.3.pre = 11:15
#' )
#' add(d, { XX.mean = .mean("XX.{i}.pre", 1:3) })
#' add(d, { XX.mean = .mean("XX.{items}.pre", 1:3) }) # the same
#' add(d, { XX.mean = .mean("XX.{#$%^&}.pre", 1:3) }) # the same
#'
#' @name %%COMPUTE%%
## @aliases COUNT SUM MEAN STD CONSEC
NULL


paste_var_items = function(var, items) {
if(grepl("\\{.*\\}", var)) {
var.split = strsplit(var, "\\{.*\\}")[[1]]
vars = paste0(var.split[1], items, var.split[2])
} else {
vars = paste0(var, items)
}
return(vars)
}


convert2vars = function(data,
var=NULL, items=NULL,
vars=NULL,
Expand All @@ -86,7 +110,7 @@ convert2vars = function(data,
varrange = gsub(" ", "", strsplit(varrange, ":")[[1]])
vars = dn[which(dn==varrange[1]):which(dn==varrange[2])]
}
if(is.null(vars)) vars = paste0(var, items)
if(is.null(vars)) vars = paste_var_items(var, items)
if(is.numeric(rev)) {
if(is.null(var))
stop("Argument `rev` must be character rather than numeric if you specify `vars` or `varrange`.", call.=FALSE)
Expand Down Expand Up @@ -165,8 +189,8 @@ SUM = function(data,
na.rm=TRUE) {
Sum = glue("function(...) sum(..., na.rm={na.rm})")
if(!is.null(varrange))
stop("Please specify either `vars` or `var` + `items`.", call.=FALSE)
if(is.null(vars)) vars = paste0(var, items)
stop("Please specify either `vars` or `var` + `items`. To specify `varrange`, please use `SUM()`.", call.=FALSE)
if(is.null(vars)) vars = paste_var_items(var, items)
if(!is.null(rev) & is.null(range))
stop("Please also specify the `range` argument!", call.=FALSE)
if(is.numeric(rev)) {
Expand Down Expand Up @@ -217,8 +241,8 @@ MEAN = function(data,
na.rm=TRUE) {
Mean = glue("function(...) mean(c(...), na.rm={na.rm})")
if(!is.null(varrange))
stop("Please specify either `vars` or `var` + `items`.", call.=FALSE)
if(is.null(vars)) vars = paste0(var, items)
stop("Please specify either `vars` or `var` + `items`. To specify `varrange`, please use `MEAN()`.", call.=FALSE)
if(is.null(vars)) vars = paste_var_items(var, items)
if(!is.null(rev) & is.null(range))
stop("Please also specify the `range` argument!", call.=FALSE)
if(is.numeric(rev)) {
Expand Down Expand Up @@ -310,7 +334,7 @@ CONSEC = function(data,
#'
#' @examples
#' # ?psych::bfi
#' data = psych::bfi
#' \donttest{data = psych::bfi
#' Alpha(data, "E", 1:5) # "E1" & "E2" should be reversed
#' Alpha(data, "E", 1:5, rev=1:2) # correct
#' Alpha(data, "E", 1:5, rev=cc("E1, E2")) # also correct
Expand All @@ -320,7 +344,7 @@ CONSEC = function(data,
#' # using dplyr::select()
#' data %>% select(E1, E2, E3, E4, E5) %>%
#' Alpha(vars=names(.), rev=cc("E1, E2"))
#'
#' }
#' @seealso
#' \code{\link{MEAN}}, \code{\link{EFA}}, \code{\link{CFA}}
#'
Expand All @@ -334,7 +358,7 @@ Alpha = function(
varrange = gsub(" ", "", strsplit(varrange, ":")[[1]])
vars = dn[which(dn==varrange[1]):which(dn==varrange[2])]
}
if(is.null(vars)) vars = paste0(var, items)
if(is.null(vars)) vars = paste_var_items(var, items)
if(is.numeric(rev)) rev = paste0(var, rev)
n.total = nrow(data)
data = na.omit(as.data.frame(data)[vars])
Expand Down Expand Up @@ -486,7 +510,7 @@ Alpha = function(
#' \code{\link{MEAN}}, \code{\link{Alpha}}, \code{\link{CFA}}
#'
#' @examples
#' data = psych::bfi
#' \donttest{data = psych::bfi
#' EFA(data, "E", 1:5) # var + items
#' EFA(data, "E", 1:5, nfactors=2) # var + items
#'
Expand All @@ -504,7 +528,7 @@ Alpha = function(
#' rotation="varimax", # default
#' nfactors="parallel", # parallel analysis
#' hide.loadings=0.45) # hide loadings < 0.45
#'
#' }
#' @export
EFA = function(
data, var, items, vars=NULL, varrange=NULL, rev=NULL,
Expand All @@ -526,7 +550,7 @@ EFA = function(
varrange = gsub(" ", "", strsplit(varrange, ":")[[1]])
vars = dn[which(dn==varrange[1]):which(dn==varrange[2])]
}
if(is.null(vars)) vars = paste0(var, items)
if(is.null(vars)) vars = paste_var_items(var, items)
if(is.numeric(rev)) rev = paste0(var, rev)
n.total = nrow(data)
data = na.omit(as.data.frame(data)[vars])
Expand Down
20 changes: 10 additions & 10 deletions R/bruceR-stats_3_manova.R
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ fix_long_data = function(data.long, ivs) {
#'
#' @examples
#' #### Between-Subjects Design ####
#'
#' \donttest{
#' between.1
#' MANOVA(between.1, dv="SCORE", between="A")
#'
Expand Down Expand Up @@ -281,7 +281,7 @@ fix_long_data = function(data.long, ivs) {
#' MANOVA(mixed.2_1b1w, dvs="B1:B3", dvs.pattern="B(.)",
#' between="A", within="B", sph.correction="GG")
#'
#' \donttest{mixed.3_1b2w
#' mixed.3_1b2w
#' MANOVA(mixed.3_1b2w, dvs="B1C1:B2C2", dvs.pattern="B(.)C(.)",
#' between="A", within=c("B", "C"))
#'
Expand Down Expand Up @@ -724,24 +724,24 @@ MANOVA = function(
#'
#' @examples
#' #### Between-Subjects Design ####
#'
#' \donttest{
#' between.1
#' MANOVA(between.1, dv="SCORE", between="A") %>%
#' EMMEANS("A")
#' \donttest{MANOVA(between.1, dv="SCORE", between="A") %>%
#' MANOVA(between.1, dv="SCORE", between="A") %>%
#' EMMEANS("A", p.adjust="tukey")
#' MANOVA(between.1, dv="SCORE", between="A") %>%
#' EMMEANS("A", contrast="seq")
#' MANOVA(between.1, dv="SCORE", between="A") %>%
#' EMMEANS("A", contrast="poly")
#' }
#'
#' between.2
#' MANOVA(between.2, dv="SCORE", between=c("A", "B")) %>%
#' EMMEANS("A", by="B") %>%
#' EMMEANS("B", by="A")
#' ## How to create an interaction plot using `emmeans::emmip()`?
#' ## See help page: ?emmeans::emmip()
#' \donttest{m = MANOVA(between.2, dv="SCORE", between=c("A", "B"))
#' m = MANOVA(between.2, dv="SCORE", between=c("A", "B"))
#' emmip(m, ~ A | B, CIs=TRUE)
#' emmip(m, ~ B | A, CIs=TRUE)
#' emmip(m, B ~ A, CIs=TRUE)
Expand All @@ -754,7 +754,7 @@ MANOVA = function(
#' EMMEANS("A", by=c("B", "C"))
#' ## Just to name a few...
#' ## You may test other combinations...
#' }
#'
#'
#' #### Within-Subjects Design ####
#'
Expand All @@ -763,7 +763,7 @@ MANOVA = function(
#' within="A") %>%
#' EMMEANS("A")
#'
#' \donttest{within.2
#' within.2
#' MANOVA(within.2, dvs="A1B1:A2B3", dvs.pattern="A(.)B(.)",
#' within=c("A", "B")) %>%
#' EMMEANS("A", by="B") %>%
Expand All @@ -781,7 +781,7 @@ MANOVA = function(
#' EMMEANS("A", by=c("B", "C"))
#' ## Just to name a few...
#' ## You may test other combinations...
#' }
#'
#'
#' #### Mixed Design ####
#'
Expand All @@ -791,7 +791,7 @@ MANOVA = function(
#' EMMEANS("A", by="B") %>%
#' EMMEANS("B", by="A")
#'
#' \donttest{mixed.3_1b2w
#' mixed.3_1b2w
#' MANOVA(mixed.3_1b2w, dvs="B1C1:B2C2", dvs.pattern="B(.)C(.)",
#' between="A", within=c("B", "C")) %>%
#' EMMEANS("A", by="B") %>%
Expand Down
16 changes: 8 additions & 8 deletions R/bruceR-stats_4_regress.R
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ regress = function(
#' \code{\link{PROCESS}}
#'
#' @examples
#' #### Example 1: Linear Model ####
#' \donttest{#### Example 1: Linear Model ####
#' lm1 = lm(Temp ~ Month + Day, data=airquality)
#' lm2 = lm(Temp ~ Month + Day + Wind + Solar.R, data=airquality)
#' model_summary(lm1)
Expand Down Expand Up @@ -326,7 +326,7 @@ regress = function(
#' model_summary(mn2)
#' model_summary(mn2, file="Multinomial Logistic Model.doc")
#' unlink("Multinomial Logistic Model.doc") # delete file for code check
#'
#' }
#' @export
model_summary = function(
model.list,
Expand Down Expand Up @@ -558,7 +558,7 @@ model_summary = function(
#' @return No return value.
#'
#' @examples
#' ## Example 1: OLS regression
#' \donttest{## Example 1: OLS regression
#' lm = lm(Temp ~ Month + Day + Wind + Solar.R, data=airquality)
#' GLM_summary(lm)
#' GLM_summary(lm, robust="HC1")
Expand All @@ -570,7 +570,7 @@ model_summary = function(
#' data=infert, family=binomial)
#' GLM_summary(glm)
#' GLM_summary(glm, robust="HC1", cluster="stratum")
#'
#' }
#' @seealso
#' \code{\link{print_table}} (print simple table)
#'
Expand Down Expand Up @@ -835,7 +835,7 @@ HLM_ICC = function(model, digits=3) {
#' @return No return value.
#'
#' @examples
#' library(lmerTest)
#' \donttest{library(lmerTest)
#'
#' ## Example 1: data from lme4::sleepstudy
#' # (1) 'Subject' is a grouping/clustering variable
Expand All @@ -858,7 +858,7 @@ HLM_ICC = function(model, digits=3) {
#' (Sweetness | Consumer) + (1 | Product), data=carrots)
#' HLM_summary(hlm.1)
#' HLM_summary(hlm.2)
#'
#' }
#' @references
#' Hox, J. J. (2010).
#' \emph{Multilevel analysis: Techniques and applications} (2nd ed.).
Expand Down Expand Up @@ -1181,7 +1181,7 @@ HLM_summary = function(
#' \href{https://CRAN.R-project.org/package=multilevel}{R package "multilevel"}
#'
#' @examples
#' data = lme4::sleepstudy # continuous variable
#' \donttest{data = lme4::sleepstudy # continuous variable
#' HLM_ICC_rWG(data, group="Subject", icc.var="Reaction")
#'
#' data = lmerTest::carrots # 7-point scale
Expand All @@ -1191,7 +1191,7 @@ HLM_summary = function(
#' HLM_ICC_rWG(data, group="Consumer", icc.var="Preference",
#' rwg.vars=c("Sweetness", "Bitter", "Crisp"),
#' rwg.levels=7)
#'
#' }
#' @export
HLM_ICC_rWG = function(data, group, icc.var,
rwg.vars=icc.var,
Expand Down
Loading

0 comments on commit 22535a2

Please sign in to comment.