Skip to content

Commit

Permalink
code style
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jul 22, 2024
1 parent 1cc4358 commit 1807fc9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
22 changes: 11 additions & 11 deletions R/3_p_value.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,20 @@ p_value.default <- function(model,
})
}

# output
if (!is.null(p)) {
params <- insight::get_parameters(model, component = component)
if (length(p) == nrow(params) && "Component" %in% colnames(params)) {
p <- .data_frame(Parameter = params$Parameter, p = as.vector(p), Component = params$Component)
} else {
p <- .data_frame(Parameter = names(p), p = as.vector(p))
# failure warning
if (is.null(p)) {
if (isTRUE(verbose)) {
insight::format_warning("Could not extract p-values from model object.")
}
return(p)
return(NULL)
}

# failure warning
if (is.null(p) && isTRUE(verbose)) {
insight::format_warning("Could not extract p-values from model object.")
# output
params <- insight::get_parameters(model, component = component)
if (length(p) == nrow(params) && "Component" %in% colnames(params)) {
.data_frame(Parameter = params$Parameter, p = as.vector(p), Component = params$Component)
} else {
.data_frame(Parameter = names(p), p = as.vector(p))
}
}

Expand Down
50 changes: 31 additions & 19 deletions R/4_standard_error.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ standard_error.default <- function(model,
dots <- list(...)
se <- NULL

# if a vcov is provided, we calculate standard errors based on that matrix
# this is usually the case for HC (robust) standard errors
# ------------------------------------------------------------------------

# vcov: matrix
if (is.matrix(vcov)) {
se <- sqrt(diag(vcov))
Expand All @@ -105,7 +109,9 @@ standard_error.default <- function(model,
se <- sqrt(diag(.vcov))
}

# classical se from summary()
# classical SE from summary()
# ------------------------------------------------------------------------

if (is.null(se)) {
se <- .safe({
if (grepl("Zelig-", class(model)[1], fixed = TRUE)) {
Expand All @@ -116,7 +122,10 @@ standard_error.default <- function(model,
})
}

# classical se from get_varcov()
# if retrieving SE from summary() failed, we try to calculate SE based
# on classical se from get_varcov()
# ------------------------------------------------------------------------

if (is.null(se)) {
se <- .safe({
varcov <- insight::get_varcov(model, component = component)
Expand Down Expand Up @@ -150,27 +159,30 @@ standard_error.default <- function(model,


.get_se_from_summary <- function(model, component = NULL) {
cs <- suppressWarnings(stats::coef(summary(model)))
cs <- .safe(suppressWarnings(stats::coef(summary(model))))
se <- NULL

if (is.list(cs) && !is.null(component)) cs <- cs[[component]]

if (!is.null(cs)) {
# do we have a se column?
se_col <- which(colnames(cs) == "Std. Error")

# if not, default to 2
if (length(se_col) == 0) se_col <- 2

se <- as.vector(cs[, se_col])

if (is.null(names(se))) {
coef_names <- rownames(cs)
if (length(coef_names) == length(se)) names(se) <- coef_names
if (!is.null(se)) {
if (is.list(cs) && !is.null(component)) {
cs <- cs[[component]]
}
if (!is.null(cs)) {
# do we have a se column?
se_col <- which(colnames(cs) == "Std. Error")
# if not, default to 2
if (length(se_col) == 0) {
se_col <- 2
}
se <- as.vector(cs[, se_col])
if (is.null(names(se))) {
coef_names <- rownames(cs)
if (length(coef_names) == length(se)) {
names(se) <- coef_names
}
}
}
names(se) <- .remove_backticks_from_string(names(se))
}

names(se) <- .remove_backticks_from_string(names(se))
se
}

Expand Down
13 changes: 11 additions & 2 deletions R/ci_generic.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,14 @@
}
method <- tolower(method)

# Fist, we want standard errors for parameters
# --------------------------------------------

# if we have adjusted SE, e.g. from kenward-roger, don't recompute
# standard errors to save time...
if (is.null(se)) {
if (!is.null(vcov) || isTRUE(list(...)[["robust"]])) {
# robust (HC) standard errors?
stderror <- standard_error(model,
component = component,
vcov = vcov,
Expand All @@ -109,6 +113,7 @@
...
)
} else {
# normal standard errors, including small-sample approximations
stderror <- switch(method,
kenward = se_kenward(model),
kr = se_kenward(model),
Expand All @@ -133,6 +138,9 @@
se <- stderror$SE
}

# Next, we need degrees of freedom
# --------------------------------

# check if we have a valid dof vector
if (is.null(dof)) {
# residual df
Expand All @@ -146,7 +154,9 @@
}
}

# calculate CIs
# Now we can calculate CIs
# ------------------------

alpha <- (1 + ci) / 2
fac <- suppressWarnings(stats::qt(alpha, df = dof))
out <- cbind(
Expand All @@ -171,7 +181,6 @@
}



.is_chi2_model <- function(model, dof) {
statistic <- insight::find_statistic(model)
(all(dof == 1) && identical(statistic, "chi-squared statistic"))
Expand Down

0 comments on commit 1807fc9

Please sign in to comment.