Skip to content
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
6 changes: 6 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Collate:
'summary.resi.R'
'resi.R'
'resi_pe.R'
'resi_pe_mixed.R'
'Anova.resi.R'
'data.R'
'chisq2S.R'
Expand All @@ -36,6 +37,8 @@ Collate:
'print.R'
'omnibus.R'
'simulations.R'
'mixed_simulations.R'
'resi_mixed_ci.R'
'resi_asymptotic.R'
'resi_pe_gam.R'
Imports:
Expand All @@ -60,6 +63,9 @@ Suggests:
glmtoolbox,
knitr,
lme4,
Matrix,
mlmRev,
numDeriv,
pscl,
R.rsp,
regtools,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export(chisq2S)
export(d2S)
export(f2S)
export(fsq2S)
export(hybridPlasmodeSim)
export(insurancePlasmodeSim)
export(omnibus)
export(resi)
Expand Down
411 changes: 411 additions & 0 deletions R/mixed_simulations.R

Large diffs are not rendered by default.

29 changes: 23 additions & 6 deletions R/print.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# Round only numeric columns so tables with character columns (e.g. the
# mixed-model `Term` and `Variation level`) can be printed.
# print.anova mangles character columns, so such anova tables are printed as
# plain data frames after their heading.
.roundNumeric <- function(x, digits) {
if (!is.data.frame(x)) return(round(x, digits = digits))
num <- vapply(x, is.numeric, logical(1L))
if (all(num)) return(round(x, digits = digits))
if (inherits(x, "anova")) {
if (!is.null(attr(x, "heading"))) cat(attr(x, "heading"), sep = "\n")
attr(x, "heading") <- NULL
class(x) <- "data.frame"
}
x[num] <- lapply(x[num], round, digits = digits)
x
}

#' @export
print.resi <- function(x, digits = max(3L, getOption("digits") - 3L), ...) {
cat("\nAnalysis of effect sizes based on RESI:")
Expand All @@ -14,20 +31,20 @@ print.resi <- function(x, digits = max(3L, getOption("digits") - 3L), ...) {
# coefficients table
if (!is.null(x$coefficients)){
cat("\nCoefficient Table \n")
print(round(x$coefficients, digits = digits))
print(.roundNumeric(x$coefficients, digits = digits))
}

# anova table
if (!is.null(x$anova)){
cat("\n\n")
print(round(x$anova, digits = digits))
print(.roundNumeric(x$anova, digits = digits))
}

# overall
if (!(is.null(x$overall))){
if (is.null(x$model.reduced$formula)){
cat("\nOverall RESI comparing model to intercept-only model:\n\n")
print(round(x$overall, digits = digits))
print(.roundNumeric(x$overall, digits = digits))
} else{
if ((x$model.reduced$formula == as.formula(paste(format(formula(x$model.full)[[2]]), "~ 1")))){
cat("\nOverall RESI comparing model to intercept-only model:\n\n")
Expand All @@ -37,7 +54,7 @@ print.resi <- function(x, digits = max(3L, getOption("digits") - 3L), ...) {
overall = as.data.frame(x$overall)
overall = overall[nrow(overall),]
rownames(overall) = NULL
print(round(overall, digits = digits))
print(.roundNumeric(overall, digits = digits))
}
}

Expand Down Expand Up @@ -70,15 +87,15 @@ print.summary_resi <- function(x, digits = max(3L, getOption("digits") - 3L), ..
cat("\nConfidence level = ", x$alpha)
cat("\nCall: ", paste(deparse(x$model.full$call), sep = "\n", collapse = "\n"), "\n",sep = "")
cat("\nCoefficient Table \n")
print(round(x$coefficients, digits = digits))
print(.roundNumeric(x$coefficients, digits = digits))
invisible(x)
}

#' @export
print.omnibus_resi <- function(x, digits = max(3L, getOption("digits") - 3L), ...) {
cat("\nAnalysis of effect sizes based on RESI:")
cat("\nConfidence level = ", x$alpha, "\n")
print(round(x$overall, digits = digits))
print(.roundNumeric(x$overall, digits = digits))
invisible(x)
}

Expand Down
104 changes: 89 additions & 15 deletions R/resi.R
Original file line number Diff line number Diff line change
Expand Up @@ -737,29 +737,103 @@ resi.gee = function(model.full, data, nboot = 1000, alpha = 0.05,
return(output)
}

#' @describeIn resi RESI point and interval estimation for LME (nlme) models
#' @describeIn resi RESI point and asymptotic interval estimation for LME
#' (nlme) models whose random effects are nested in an outermost cluster;
#' identical method and arguments to the `lmerMod` method. Supported fits
#' have independent homoscedastic within-group errors (no `weights` or
#' `correlation`) and pdSymm/pdLogChol/pdDiag/pdIdent random-effect
#' covariances; other fits return point estimates only, with a warning.
#' @importFrom nlme getGroups
#' @export
resi.lme = function(model.full, alpha = 0.05, nboot = 1000, anova = TRUE, vcovfunc = clubSandwich::vcovCR,
vcov.args = list(), ...){
warning("\nConfidence Interval procedure not developed for lme, returning point estimates only")
output = list(alpha = alpha, nboot = 0)
# RESI point estimates
output = c(output, resi_pe(model.full = model.full, vcovfunc = vcovfunc, vcov.args = vcov.args,
anova = anova, ...))
output$boot.method = "nonparam"
class(output) = "resi"
return(output)
resi.lme = function(model.full, alpha = 0.05, nboot = 1000, anova = TRUE,
vcovfunc = clubSandwich::vcovCR, vcov.args = list(),
ci.method = c("normal", "qf", "cf", "none"),
ci.design = TRUE, ...){
ci.method <- match.arg(ci.method)
why <- .mixedCiLmeUnsupported(model.full)
if (ci.method != "none" && !is.null(why)) {
warning("\nConfidence intervals not available for this lme fit (", why,
"); returning point estimates only")
output = list(alpha = alpha, nboot = 0)
output = c(output, resi_pe(model.full = model.full, vcovfunc = vcovfunc, vcov.args = vcov.args,
anova = anova, ...))
output$boot.method = "nonparam"
class(output) = "resi"
return(output)
}
.resiMixedWithCi(model.full, "lme", alpha, anova, vcovfunc, vcov.args,
ci.method, ci.design, ...)
}

#' @describeIn resi RESI point and interval estimation for lmerMod models
#' @describeIn resi RESI point and asymptotic interval estimation for lmerMod
#' models whose random effects are nested in an outermost cluster. CIs use
#' cluster-level influence functions (`ci.method` one of `"normal"`, `"qf"`,
#' `"cf"`; coefficients get the signed normal CI for `"qf"`). Use
#' `vcovfunc = stats::vcov` for model-based or `clubSandwich::vcovCR` (e.g.
#' `vcov.args = list(type = "CR2")`) for cluster-robust intervals;
#' cluster-robust intervals use t / F references with g - 1 denominator
#' degrees of freedom (g = number of clusters).
#' `ci.design = TRUE` (default) treats the cluster covariate profiles as
#' sampled; `FALSE` gives intervals conditional on the observed design.
#' `ci.method = "none"` returns point estimates only. ANOVA CIs are for the
#' joint Wald test of each term's columns (Type II without interactions).
#' @param ci.design Logical; for `lmerMod` and `lme` CIs, include the
#' variability of the cluster-level design.
#' @export
resi.lmerMod = function(model.full, alpha = 0.05, nboot = 1000, anova = TRUE,
vcovfunc = clubSandwich::vcovCR, vcov.args = list(), ...){
warning("\nConfidence Interval procedure not developed for lmerMod, returning point estimates only")
vcovfunc = clubSandwich::vcovCR, vcov.args = list(),
ci.method = c("normal", "qf", "cf", "none"),
ci.design = TRUE, ...){
ci.method <- match.arg(ci.method)
.resiMixedWithCi(model.full, "lmerMod", alpha, anova, vcovfunc, vcov.args,
ci.method, ci.design, ...)
}

.resiMixedWithCi <- function(model.full, engine, alpha, anova, vcovfunc, vcov.args,
ci.method, ci.design, ...) {
dots <- list(...)
robust <- !identical(vcovfunc, stats::vcov)
pe_vcovfunc <- vcovfunc
if (ci.method != "none" && robust) {
design <- .resiMixedModelDesign(model.full, engine)
Vhat <- .resiMixedVcov(model.full, vcovfunc, vcov.args,
.resiMixedCluster(dots$cluster, design, model.full, engine))(model.full)
pe_vcovfunc <- function(object, ...) Vhat
}
output = list(alpha = alpha, nboot = 0)
output = c(output, resi_pe(model.full, vcovfunc = vcovfunc, vcov.args = vcov.args, anova = anova, ...)) # RESI point estimates
output = c(output, resi_pe(model.full, vcovfunc = pe_vcovfunc, vcov.args = vcov.args, anova = anova, ...)) # RESI point estimates
output$boot.method = "nonparam"
if (ci.method != "none") {
if (!robust) Vhat <- as.matrix(stats::vcov(model.full))
ci_cluster <- dots$cluster
if (!is.null(ci_cluster) && !(is.character(ci_cluster) && length(ci_cluster) == 1L)) ci_cluster <- NULL
cis <- .resi_ci_mixed(model.full, Vhat, robust,
n_coef = setNames(output$coefficients$n, rownames(output$coefficients)),
n_anova = if (anova) setNames(output$anova$n, rownames(output$anova)),
ci.method = ci.method, alpha = alpha, design = ci.design,
cluster = ci_cluster,
n_level_coef = setNames(output$coefficients$`n level`, rownames(output$coefficients)),
n_level_anova = if (anova) setNames(output$anova$`n level`, rownames(output$anova)))[[ci.method]]
ci_cols <- paste0(sort(c(alpha / 2, 1 - alpha / 2)) * 100, "%")
pos <- match("RESI", names(output$coefficients))
output$coefficients <- cbind(output$coefficients[seq_len(pos)],
setNames(as.data.frame(cis$coefficients), ci_cols),
output$coefficients[-seq_len(pos)])
if (anova) {
anova_class <- class(output$anova)
anova_heading <- attr(output$anova, "heading")
pos <- match("RESI", names(output$anova))
anova_df <- as.data.frame(output$anova)
output$anova <- cbind(anova_df[seq_len(pos)],
setNames(as.data.frame(cis$anova), ci_cols),
anova_df[-seq_len(pos)])
class(output$anova) <- anova_class
attr(output$anova, "heading") <- anova_heading
}
output$ci.method <- ci.method
} else {
warning("\nci.method = 'none': returning point estimates only")
}
class(output) = "resi"
return(output)
}
Expand Down
Loading
Loading