diff --git a/DESCRIPTION b/DESCRIPTION index 013843b..5198098 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -30,10 +30,10 @@ Suggests: car, coin, ggplot2, + lme4, MASS, pscl, pwr, - sjPlot, survey, testthat URL: https://strengejacke.github.io/sjstats/ diff --git a/NAMESPACE b/NAMESPACE index 558b8f6..3e933bb 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -19,21 +19,15 @@ S3method(plot,sj_inequ_trend) S3method(predict,svyglm.nb) S3method(print,sj_anova_stat) S3method(print,sj_chi2gof) -S3method(print,sj_grpmean) S3method(print,sj_htest_chi) S3method(print,sj_htest_kw) S3method(print,sj_htest_mwu) -S3method(print,sj_mwu) -S3method(print,sj_outliers) S3method(print,sj_resample) S3method(print,sj_ttest) S3method(print,sj_wcor) -S3method(print,sj_wmwu) S3method(print,sj_xtab_stat) -S3method(print,sj_xtab_stat2) S3method(print,svyglm.nb) S3method(print,svyglm.zip) -S3method(print,tidy_stan) S3method(residuals,svyglm.nb) S3method(terms,svyglm.nb) S3method(weighted_correlation,default) @@ -111,11 +105,6 @@ importFrom(bayestestR,equivalence_test) importFrom(datawizard,weighted_mean) importFrom(datawizard,weighted_median) importFrom(datawizard,weighted_sd) -importFrom(insight,export_table) -importFrom(insight,format_p) -importFrom(insight,format_value) importFrom(insight,link_inverse) -importFrom(insight,print_color) importFrom(performance,mse) importFrom(performance,rmse) -importFrom(stats,family) diff --git a/R/S3-methods.R b/R/S3-methods.R index 131f3d9..cff58b4 100644 --- a/R/S3-methods.R +++ b/R/S3-methods.R @@ -24,7 +24,6 @@ print.svyglm.nb <- function(x, se = c("robust", "model"), digits = 4, ...) { } - #' @export print.svyglm.zip <- function(x, se = c("robust", "model"), digits = 4, ...) { se <- match.arg(se) @@ -47,7 +46,6 @@ print.svyglm.zip <- function(x, se = c("robust", "model"), digits = 4, ...) { } - tidy_svyglm.nb <- function(x, digits = 4, v_se = c("robust", "model")) { v_se <- match.arg(v_se) @@ -70,7 +68,6 @@ tidy_svyglm.nb <- function(x, digits = 4, v_se = c("robust", "model")) { } - tidy_svyglm.zip <- function(x, digits = 4, v_se = c("robust", "model")) { v_se <- match.arg(v_se) @@ -92,7 +89,6 @@ tidy_svyglm.zip <- function(x, digits = 4, v_se = c("robust", "model")) { } - #' @export model.frame.svyglm.nb <- function(formula, ...) { pred <- attr(formula, "nb.terms", exact = TRUE) @@ -100,7 +96,6 @@ model.frame.svyglm.nb <- function(formula, ...) { } - #' @export model.frame.svyglm.zip <- function(formula, ...) { pred <- attr(formula, "zip.terms", exact = TRUE) @@ -108,28 +103,24 @@ model.frame.svyglm.zip <- function(formula, ...) { } -#' @importFrom stats family #' @export family.svyglm.nb <- function(object, ...) { attr(object, "family", exact = TRUE) } - #' @export formula.svyglm.nb <- function(x, ...) { attr(x, "nb.formula", exact = TRUE) } - #' @export formula.svyglm.zip <- function(x, ...) { attr(x, "zip.formula", exact = TRUE) } - #' @export predict.svyglm.nb <- function(object, newdata = NULL, type = c("link", "response", "terms"), @@ -212,51 +203,18 @@ deviance.svyglm.nb <- function(object, ...) { } -#' @export -print.tidy_stan <- function(x, ...) { - insight::print_color("\nSummary Statistics of Stan-Model\n\n", "blue") - digits <- attr(x, "digits") - - for (i in x) { - insight::print_color(paste0("# ", attr(i, "main_title")), "blue") - cat(" ") - insight::print_color(attr(i, "sub_title"), "red") - cat("\n\n") - rem <- which(colnames(i) %in% c("Parameter", "Group", "Response", "Function")) - i <- i[, -rem] - colnames(i)[1] <- "Parameter" - i$ESS <- as.character(i$ESS) - i$pd <- sprintf("%.1f%%", 100 * i$pd) - i[] <- lapply(i, function(.j) { - if (is.numeric(.j)) .j <- sprintf("%.*f", digits, .j) - .j - }) - print.data.frame(i, quote = FALSE, row.names = FALSE) - cat("\n\n") - } -} - - -clean_term_name <- function(x) { - x <- insight::trim_ws(x) - format(x, width = max(nchar(x))) -} - - #' @export as.integer.sj_resample <- function(x, ...) { x$id } - #' @export as.data.frame.sj_resample <- function(x, ...) { x$data[x$id, , drop = FALSE] } - #' @export print.sj_resample <- function(x, ...) { n <- length(x$id) @@ -275,7 +233,6 @@ print.sj_resample <- function(x, ...) { } - #' @export plot.sj_inequ_trend <- function(x, ...) { .data <- NULL @@ -319,78 +276,6 @@ plot.sj_inequ_trend <- function(x, ...) { } -#' @export -print.sj_mwu <- function(x, ...) { - insight::print_color("\n# Mann-Whitney-U-Test\n\n", "blue") - # get data - .dat <- x$df - # print to console - for (i in seq_len(nrow(.dat))) { - # get value labels - l1 <- .dat[i, "grp1.label"] - l2 <- .dat[i, "grp2.label"] - # do we have value labels? - if (!is.null(l1) && !is.na(l1) %% !is.null(l2) && !is.na(l2)) { - insight::print_color( - sprintf( - "Groups %i = %s (n = %i) | %i = %s (n = %i):\n", - .dat[i, "grp1"], - l1, - .dat[i, "grp1.n"], - .dat[i, "grp2"], - l2, - .dat[i, "grp2.n"] - ), "cyan" - ) - } else { - insight::print_color( - sprintf("Groups (%i|%i), n = %i/%i:\n", - .dat[i, "grp1"], .dat[i, "grp2"], - .dat[i, "grp1.n"], .dat[i, "grp2.n"]), - "cyan" - ) - } - - cat(sprintf( - " U = %.3f, W = %.3f, %s, Z = %.3f\n", - .dat[i, "u"], .dat[i, "w"], insight::format_p(.dat[i, "p"]), .dat[i, "z"] - )) - - string_es <- "effect-size r" - string_r <- sprintf("%.3f", .dat[i, "r"]) - string_group1 <- sprintf("rank-mean(%i)", .dat[i, "grp1"]) - string_group2 <- sprintf("rank-mean(%i)", .dat[i, "grp2"]) - string_rm1 <- sprintf("%.2f", .dat[i, "rank.mean.grp1"]) - string_rm2 <- sprintf("%.2f", .dat[i, "rank.mean.grp2"]) - - space1 <- max(nchar(c(string_es, string_group1, string_group2))) - space2 <- max(nchar(c(string_r, string_rm1, string_rm2))) - - cat( - sprintf(" %*s = %*s\n", space1, string_es, space2 + 1, string_r), - sprintf(" %*s = %*s\n", space1, string_group1, space2, string_rm1), - sprintf(" %*s = %*s\n\n", space1, string_group2, space2, string_rm2) - ) - } - - # if we have more than 2 groups, also perfom kruskal-wallis-test - if (length(unique(stats::na.omit(x$data$grp))) > 2) { - insight::print_color("# Kruskal-Wallis-Test\n\n", "blue") - kw <- stats::kruskal.test(x$data$dv, x$data$grp) - cat(sprintf("chi-squared = %.3f\n", kw$statistic)) - cat(sprintf("df = %i\n", kw$parameter)) - cat(paste(insight::format_p(kw$p.value, stars = TRUE), "\n")) - } -} - - -#' @export -print.sj_outliers <- function(x, ...) { - print(x$result, ...) -} - - -#' @importFrom insight format_p #' @export print.sj_xtab_stat <- function(x, ...) { # get length of method name, to align output @@ -414,63 +299,6 @@ print.sj_xtab_stat <- function(x, ...) { } - -#' @export -print.sj_xtab_stat2 <- function(x, ...) { - # get length of method name, to align output - l <- max(nchar(c(x$stat.name, "p-value", "Observations"))) - - # headline - insight::print_color(paste0("\n# ", x$method, "\n\n"), "blue") - - # print test statistic - cat(sprintf(" %*s: %.4f\n", l, x$stat.name, x$estimate)) - cat(sprintf(" %*s: %g\n", l, "df", x$df)) - cat(sprintf(" %*s: %s\n", l, "p-value", insight::format_p(x$p.value, stars = TRUE, name = NULL))) - cat(sprintf(" %*s: %g\n", l, "Observations", x$n_obs)) -} - - - -#' @export -print.sj_grpmean <- function(x, ...) { - cat("\n") - print_grpmean(x, digits = attributes(x)$digits, ...) -} - - -#' @importFrom insight export_table print_color format_value format_p -print_grpmean <- function(x, digits = NULL, ...) { - # headline - insight::print_color(sprintf( - "# Grouped Means for %s by %s\n\n", - attr(x, "dv.label", exact = TRUE), - attr(x, "grp.label", exact = TRUE) - ), "blue") - - if (is.null(digits)) { - digits <- 2 - } - - x$mean <- insight::format_value(x$mean, digits = digits) - x$std.dev <- insight::format_value(x$std.dev, digits = digits) - x$std.error <- insight::format_value(x$std.error, digits = digits) - x$p.value <- insight::format_p(x$p.value, name = NULL) - - colnames(x) <- c("Category", "Mean", "N", "SD", "SE", "p") - cat(insight::export_table(x)) - - # statistics - cat(sprintf( - "\nAnova: R2=%.3f; adj.R2=%.3f; F=%.3f; p=%.3f\n", - attr(x, "r2", exact = TRUE), - attr(x, "adj.r2", exact = TRUE), - attr(x, "fstat", exact = TRUE), - attr(x, "p.value", exact = TRUE) - )) -} - - #' @export print.sj_chi2gof <- function(x, ...) { insight::print_color("\n# Chi-squared Goodness-of-Fit Test\n\n", "blue") @@ -538,18 +366,6 @@ print.sj_ttest <- function(x, ...) { } -#' @export -print.sj_wmwu <- function(x, ...) { - insight::print_color(sprintf("\n# %s\n", x$method), "blue") - - group <- attr(x, "group.name", exact = TRUE) - xn <- attr(x, "x.name", exact = TRUE) - - insight::print_color(sprintf("\n comparison of %s by %s\n", xn, group), "cyan") - cat(sprintf(" Chisq=%.2f df=%i p-value=%.3f\n\n", x$statistic, as.integer(x$parameter), x$p.value)) -} - - #' @export print.sj_wcor <- function(x, ...) { insight::print_color(sprintf("\nWeighted %s\n\n", x$method), "blue") @@ -565,7 +381,6 @@ print.sj_wcor <- function(x, ...) { } -#' @importFrom insight export_table #' @export print.sj_anova_stat <- function(x, digits = 3, ...) { x$p.value <- insight::format_p(x$p.value, name = NULL) diff --git a/R/boot_ci.R b/R/boot_ci.R index 4645031..33b8b9f 100644 --- a/R/boot_ci.R +++ b/R/boot_ci.R @@ -8,7 +8,7 @@ #' @param data A data frame that containts the vector with bootstrapped #' estimates, or directly the vector (see 'Examples'). #' @param ci.lvl Numeric, the level of the confidence intervals. -#' @param Select Optional, unquoted names of variables (as character vector) +#' @param select Optional, unquoted names of variables (as character vector) #' with bootstrapped estimates. Required, if either `data` is a data frame #' (and no vector), and only selected variables from `data` should be processed. #' @param method Character vector, indicating if confidence intervals should be diff --git a/R/helpfunctions.R b/R/helpfunctions.R index e3f2a69..21d54f6 100644 --- a/R/helpfunctions.R +++ b/R/helpfunctions.R @@ -33,7 +33,7 @@ get_glm_family <- function(fit) { # create logical for family binom_fam <- fitfam %in% c("binomial", "quasibinomial") poisson_fam <- fitfam %in% c("poisson", "quasipoisson") || - grepl("negative binomial", fitfram, ignore.case = TRUE, fixed = TRUE) + grepl("negative binomial", fitfam, ignore.case = TRUE, fixed = TRUE) list(is_bin = binom_fam, is_pois = poisson_fam, is_logit = logit_link) } diff --git a/R/se_ybar.R b/R/se_ybar.R index a0066fa..642dc85 100644 --- a/R/se_ybar.R +++ b/R/se_ybar.R @@ -12,11 +12,9 @@ #' #' @references Gelman A, Hill J. 2007. Data analysis using regression and multilevel/hierarchical models. Cambridge, New York: Cambridge University Press #' -#' @examples -#' if (require("lme4")) { -#' fit <- lmer(Reaction ~ 1 + (1 | Subject), sleepstudy) -#' se_ybar(fit) -#' } +#' @examplesIf require("lme4") +#' fit <- lmer(Reaction ~ 1 + (1 | Subject), sleepstudy) +#' se_ybar(fit) #' @export se_ybar <- function(fit) { # get model icc diff --git a/R/select_helpers.R b/R/select_helpers.R index 72c01f9..87d6104 100644 --- a/R/select_helpers.R +++ b/R/select_helpers.R @@ -31,5 +31,5 @@ obj_has_name <- function(x, name) { } obj_has_rownames <- function(x) { - !identical(as.character(1:nrow(x)), rownames(x)) + !identical(as.character(seq_len(nrow(x))), rownames(x)) } diff --git a/man/boot_ci.Rd b/man/boot_ci.Rd index eec0835..893080e 100644 --- a/man/boot_ci.Rd +++ b/man/boot_ci.Rd @@ -19,16 +19,16 @@ boot_est(data, select = NULL) \item{data}{A data frame that containts the vector with bootstrapped estimates, or directly the vector (see 'Examples').} +\item{select}{Optional, unquoted names of variables (as character vector) +with bootstrapped estimates. Required, if either \code{data} is a data frame +(and no vector), and only selected variables from \code{data} should be processed.} + \item{method}{Character vector, indicating if confidence intervals should be based on bootstrap standard error, multiplied by the value of the quantile function of the t-distribution (default), or on sample quantiles of the bootstrapped values. See 'Details' in \code{boot_ci()}. May be abbreviated.} \item{ci.lvl}{Numeric, the level of the confidence intervals.} - -\item{Select}{Optional, unquoted names of variables (as character vector) -with bootstrapped estimates. Required, if either \code{data} is a data frame -(and no vector), and only selected variables from \code{data} should be processed.} } \value{ A data frame with either bootstrap estimate, standard error, the diff --git a/man/se_ybar.Rd b/man/se_ybar.Rd index 3b976fc..5a16baf 100644 --- a/man/se_ybar.Rd +++ b/man/se_ybar.Rd @@ -19,10 +19,10 @@ May be used as part of the multilevel power calculation for cluster sampling (see \cite{Gelman and Hill 2007, 447ff}). } \examples{ -if (require("lme4")) { - fit <- lmer(Reaction ~ 1 + (1 | Subject), sleepstudy) - se_ybar(fit) -} +\dontshow{if (require("lme4")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +fit <- lmer(Reaction ~ 1 + (1 | Subject), sleepstudy) +se_ybar(fit) +\dontshow{\}) # examplesIf} } \references{ Gelman A, Hill J. 2007. Data analysis using regression and multilevel/hierarchical models. Cambridge, New York: Cambridge University Press