Skip to content

Commit

Permalink
release 0.17.0 on CRAN
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Aug 20, 2018
1 parent 8e25711 commit f88eb62
Show file tree
Hide file tree
Showing 74 changed files with 4,525 additions and 1,818 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ Package: sjstats
Type: Package
Encoding: UTF-8
Title: Collection of Convenient Functions for Common Statistical Computations
Version: 0.16.0.9000
Date: 2018-08-02
Version: 0.17.0.9000
Date: 2018-08-20
Authors@R: person("Daniel", "Lüdecke", role = c("aut", "cre"), email = "d.luedecke@uke.de", comment = c(ORCID = "0000-0002-8895-3206"))
Maintainer: Daniel Lüdecke <d.luedecke@uke.de>
Description: Collection of convenient functions for common statistical computations,
Expand Down
1 change: 1 addition & 0 deletions R/overdisp.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#' zeros to considered as over- or underfitting zero-counts. A ratio
#' between 1 +/- \code{tolerance} are considered as OK, while a ratio
#' beyond or below this treshold would indicate over- or underfitting.
#' @param ... Currently not used.
#'
#' @return For \code{overdisp()}, information on the overdispersion test; for
#' \code{zero_count()}, the amount of predicted and observed zeros in
Expand Down
60 changes: 47 additions & 13 deletions R/robust.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#' @title Robust standard errors for regression models
#' @name robust
#' @description \code{robust()} computes robust standard error for regression models.
#' This method calls one of the \code{vcov*()}-functions from the \pkg{sandwich}
#' package for robust covariance matrix estimators. Results are returned
#' as tidy data frame.
#' This method calls one of the \code{vcov*()}-functions from the
#' \pkg{sandwich}-package for robust covariance matrix estimators. Results are
#' returned as tidy data frame.
#' \cr \cr
#' \code{svy()} is intended to compute standard errors for survey
#' designs (complex samples) fitted with regular \code{lm} or
Expand All @@ -16,10 +16,10 @@
#' from the \pkg{sandwich} package. For \code{svy()}, \code{x} must be
#' \code{lm} object, fitted with weights.
#' @param vcov.fun String, indicating the name of the \code{vcov*()}-function
#' from the \pkg{sandwich} package, e.g. \code{vcov.fun = "vcovCL"}.
#' from the \pkg{sandwich}-package, e.g. \code{vcov.fun = "vcovCL"}.
#' @param vcov.type Character vector, specifying the estimation type for the
#' heteroskedasticity-consistent covariance matrix estimation
#' (see \code{\link[sandwich]{vcovHC}} for details).
#' robust covariance matrix estimation (see \code{\link[sandwich]{vcovHC}}
#' for details).
#' @param vcov.args List of named vectors, used as additional arguments that
#' are passed down to \code{vcov.fun}.
#' @param conf.int Logical, \code{TRUE} if confidence intervals based on robust
Expand Down Expand Up @@ -54,7 +54,7 @@
#'
#' confint(fit)
#' robust(fit, conf.int = TRUE)
#' robust(fit, vcov = "HC1", conf.int = TRUE) # "HC1" should be Stata default
#' robust(fit, vcov.type = "HC1", conf.int = TRUE) # "HC1" should be Stata default
#'
#' library(sjmisc)
#' # dichtomozize service usage by "service usage yes/no"
Expand All @@ -65,7 +65,7 @@
#' robust(fit)
#' robust(fit, conf.int = TRUE, exponentiate = TRUE)
#'
#' @importFrom stats qt
#' @importFrom stats qt pt df.residual qnorm pnorm nobs coef
#' @export
robust <- function(x, vcov.fun = "vcovHC", vcov.type = c("HC3", "const", "HC", "HC0", "HC1", "HC2", "HC4", "HC4m", "HC5"), vcov.args = NULL, conf.int = FALSE, exponentiate = FALSE) {

Expand All @@ -85,8 +85,43 @@ robust <- function(x, vcov.fun = "vcovHC", vcov.type = c("HC3", "const", "HC", "

se <- sqrt(diag(.vcov))

dendf <- tryCatch(
stats::df.residual(x),
error = function(x) { NULL },
warning = function(x) { NULL },
finally = function(x) { NULL }
)

# 2nd try
if (is.null(dendf)) {
dendf <- tryCatch(
summary(x)$df[2],
error = function(x) { NULL },
warning = function(x) { NULL },
finally = function(x) { NULL }
)
}

# 3rd try
if (is.null(dendf)) {
dendf <- tryCatch(
stats::nobs(x) - length(est),
error = function(x) { NULL },
warning = function(x) { NULL },
finally = function(x) { NULL }
)
}


t.stat <- est / se
p.value <- 2 * pt(abs(t.stat), df = 3, lower.tail = F)

if (is.null(dendf)) {
p.value <- 2 * stats::pnorm(abs(t.stat), lower.tail = FALSE)
se.factor <- stats::qnorm(.975)
} else {
p.value <- 2 * stats::pt(abs(t.stat), df = dendf, lower.tail = FALSE)
se.factor <- stats::qt(.975, df = dendf)
}


# create tidy data frame
Expand All @@ -100,13 +135,12 @@ robust <- function(x, vcov.fun = "vcovHC", vcov.type = c("HC3", "const", "HC", "

# add CI
if (conf.int) {
# denominator df
dendf <- summary(x)$df[2]
# add columns with CI
result <- add_cols(
result,
conf.low = result$estimate - (stats::qt(.975, df = dendf) * result$std.error),
conf.high = result$estimate + (stats::qt(.975, df = dendf) * result$std.error)
conf.low = result$estimate - se.factor * result$std.error,
conf.high = result$estimate + se.factor * result$std.error,
.after = "std.error"
)
}

Expand Down
2 changes: 2 additions & 0 deletions R/select_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ add_cols <- function(data, ..., .after = 1) {

if (.after < 1) {
cbind(dat, data)
} else if (is.infinite(.after)) {
cbind(data, dat)
} else {
c1 <- 1:.after
c2 <- (.after + 1):ncol(data)
Expand Down
15 changes: 7 additions & 8 deletions R/typical.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
#' vector to apply other different functions to numeric and categorical
#' \code{x}, where factors are first converted to numeric vectors, e.g.
#' \code{fun = c(numeric = "median", factor = "mean")}. See 'Examples'.
#' @param weight.by Name of variable in \code{x} that indicated the vector of
#' @param weights Name of variable in \code{x} that indicated the vector of
#' weights that will be applied to weight all observations. Default is
#' \code{NULL}, so no weights are used.
#' @param weight.by Deprecated.
#' @param ... Further arguments, passed down to \code{fun}.
#'
#' @inheritParams grpmean
Expand Down Expand Up @@ -65,14 +66,12 @@
#'
#'
#' @export
typical_value <- function(x, fun = "mean", weight.by = NULL, ...) {
typical_value <- function(x, fun = "mean", weights = NULL, weight.by, ...) {

## TODO activate later

# if (!missing(weight.by)) {
# # message("Argument `weight.by` is deprecated. Please use `weights`.")
# weights <- weight.by
# }
if (!missing(weight.by)) {
message("Argument `weight.by` is deprecated. Please use `weights`.")
weights <- weight.by
}

# check if we have named vectors and find the requested function
# for special functions for factors, convert to numeric first
Expand Down
Loading

0 comments on commit f88eb62

Please sign in to comment.