Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: parameters
Title: Processing of Model Parameters
Version: 0.26.0.4
Version: 0.26.0.5
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ S3method(display,parameters_brms_meta)
S3method(display,parameters_efa)
S3method(display,parameters_efa_summary)
S3method(display,parameters_model)
S3method(display,parameters_omega)
S3method(display,parameters_omega_summary)
S3method(display,parameters_pca)
S3method(display,parameters_pca_summary)
S3method(display,parameters_sem)
Expand Down Expand Up @@ -621,6 +623,8 @@ S3method(print_md,parameters_brms_meta)
S3method(print_md,parameters_efa)
S3method(print_md,parameters_efa_summary)
S3method(print_md,parameters_model)
S3method(print_md,parameters_omega)
S3method(print_md,parameters_omega_summary)
S3method(print_md,parameters_p_function)
S3method(print_md,parameters_pca)
S3method(print_md,parameters_pca_summary)
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* New function `factor_scores()` to extract factor scores from EFA (`psych::fa()`
or `factor_analysis()`).

* Added and/or improved print-methods for all functions around PCA and FA.
* Added and/or improved print-methods for all functions around PCA, FA and Omega.

* Improved efficiency in `model_parameters()` for models from packages *brms*
and *rstanarm*.
Expand Down
6 changes: 6 additions & 0 deletions R/display.R
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,14 @@
#' @export
display.parameters_pca_summary <- display.parameters_efa_summary

#' @export
display.parameters_omega_summary <- display.parameters_efa_summary


#' @inheritParams model_parameters.principal
#' @rdname display.parameters_model
#' @export
display.parameters_efa <- function(object, format = "markdown", digits = 2, sort = FALSE, threshold = NULL, labels = NULL, ...) {

Check warning on line 242 in R/display.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/display.R,line=242,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 129 characters.
format <- insight::validate_argument(format, c("markdown", "html", "md", "tt"))

fun_args <- list(
Expand All @@ -257,6 +260,9 @@
#' @export
display.parameters_pca <- display.parameters_efa

#' @export
display.parameters_omega <- display.parameters_efa


# Equivalence tests ------------------------

Expand Down
104 changes: 86 additions & 18 deletions R/methods_psych.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,16 @@
data_summary <- .get_fa_variance_summary(model)

# Get loadings
loadings <- as.data.frame(unclass(model$loadings))

Check warning on line 161 in R/methods_psych.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/methods_psych.R,line=161,col=3,[object_overwrite_linter] 'loadings' is an exported object from package 'stats'. Avoid re-using such symbols.

# Format
loadings <- cbind(data.frame(Variable = row.names(loadings)), loadings)

Check warning on line 164 in R/methods_psych.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/methods_psych.R,line=164,col=3,[object_overwrite_linter] 'loadings' is an exported object from package 'stats'. Avoid re-using such symbols.
row.names(loadings) <- NULL

# Labels
if (!is.null(labels)) {

Check warning on line 168 in R/methods_psych.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/methods_psych.R,line=168,col=7,[if_not_else_linter] Prefer `if (A) x else y` to the less-readable `if (!A) y else x` in a simple if/else statement.
loadings$Label <- labels
loadings <- loadings[c("Variable", "Label", names(loadings)[!names(loadings) %in% c("Variable", "Label")])]

Check warning on line 170 in R/methods_psych.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/methods_psych.R,line=170,col=5,[object_overwrite_linter] 'loadings' is an exported object from package 'stats'. Avoid re-using such symbols.
loading_cols <- 3:(n + 2)
} else {
loading_cols <- 2:(n + 1)
Expand All @@ -192,7 +192,7 @@

# Sorting
if (isTRUE(sort)) {
loadings <- .sort_loadings(loadings)

Check warning on line 195 in R/methods_psych.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/methods_psych.R,line=195,col=5,[object_overwrite_linter] 'loadings' is an exported object from package 'stats'. Avoid re-using such symbols.
}

# Add some more attributes
Expand Down Expand Up @@ -224,29 +224,77 @@


#' @export
model_parameters.omega <- function(model, verbose = TRUE, ...) {
# Table of omega coefficients
table_om <- model$omega.group
colnames(table_om) <- c("Omega_Total", "Omega_Hierarchical", "Omega_Group")
table_om$Composite <- row.names(table_om)
row.names(table_om) <- NULL
table_om <- table_om[c("Composite", names(table_om)[names(table_om) != "Composite"])]
model_parameters.omega <- function(model,
sort = FALSE,
threshold = NULL,
labels = NULL,
verbose = TRUE,
...) {
# n
n <- model$stats$factors

# Get summary: Table of Variance
table_var <- as.data.frame(unclass(model$omega.group))
table_var$Composite <- rownames(model$omega.group)
table_var$Total <- table_var$total * 100
table_var$General <- table_var$general * 100
table_var$Group <- table_var$group * 100
table_var <- table_var[c("Composite", "Total", "General", "Group")]
# Get summary
data_summary <- .get_omega_variance_summary(model)

# Get omega coefficients
omega_coefficients <- .get_omega_coefficients_summary(model)

# Get loadings
loadings <- as.data.frame(unclass(model$schmid$sl))

Check warning on line 243 in R/methods_psych.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/methods_psych.R,line=243,col=3,[object_overwrite_linter] 'loadings' is an exported object from package 'stats'. Avoid re-using such symbols.

# Format
loadings <- cbind(data.frame(Variable = row.names(loadings)), loadings)

Check warning on line 246 in R/methods_psych.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/methods_psych.R,line=246,col=3,[object_overwrite_linter] 'loadings' is an exported object from package 'stats'. Avoid re-using such symbols.
row.names(loadings) <- NULL

# Labels
if (!is.null(labels)) {

Check warning on line 250 in R/methods_psych.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/methods_psych.R,line=250,col=7,[if_not_else_linter] Prefer `if (A) x else y` to the less-readable `if (!A) y else x` in a simple if/else statement.
loadings$Label <- labels
loadings <- loadings[c("Variable", "Label", names(loadings)[!names(loadings) %in% c("Variable", "Label")])]

Check warning on line 252 in R/methods_psych.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/methods_psych.R,line=252,col=5,[object_overwrite_linter] 'loadings' is an exported object from package 'stats'. Avoid re-using such symbols.
loading_cols <- 3:(n + 4)
} else {
loading_cols <- 2:(n + 3)
}

# Add information
colnames(loadings)[colnames(loadings) == "com"] <- "Complexity"

out <- table_om
attr(out, "summary") <- table_var
class(out) <- c("parameters_omega", class(out))
out
# Add attributes
attr(loadings, "summary") <- data_summary
attr(loadings, "omega_coefficients") <- omega_coefficients
attr(loadings, "model") <- model
attr(loadings, "rotation") <- model$rotation
attr(loadings, "scores") <- model$scores
attr(loadings, "additional_arguments") <- list(...)
attr(loadings, "n") <- n
attr(loadings, "threshold") <- threshold
attr(loadings, "sort") <- sort
attr(loadings, "loadings_columns") <- loading_cols

# Sorting
if (isTRUE(sort)) {
loadings <- .sort_loadings(loadings)
}

# Add some more attributes
attr(loadings, "loadings_long") <- .long_loadings(loadings, threshold = threshold, loadings_columns = loading_cols)
# here we match the original columns in the data set with the assigned components
# for each variable, so we know which column in the original data set belongs
# to which extracted component...
attr(loadings, "closest_component") <- .closest_component(
loadings,
loadings_columns = loading_cols,
variable_names = rownames(model$schmid$sl)
)

# add class-attribute for printing
class(loadings) <- c("parameters_omega", class(loadings))
loadings
}


# helper ------------------------------------------------


.get_fa_variance_summary <- function(model) {
n <- model$factors
variance <- as.data.frame(unclass(model$Vaccounted))
Expand All @@ -268,3 +316,23 @@

data_summary
}


.get_omega_variance_summary <- function(model) {
# Get summary: Table of Variance
table_var <- as.data.frame(unclass(model$omega.group))
table_var$Composite <- rownames(model$omega.group)
table_var$Total <- table_var$total * 100
table_var$General <- table_var$general * 100
table_var$Group <- table_var$group * 100
table_var[c("Composite", "Total", "General", "Group")]
}

.get_omega_coefficients_summary <- function(model) {
# Table of omega coefficients
table_om <- model$omega.group
colnames(table_om) <- c("Omega_Total", "Omega_Hierarchical", "Omega_Group")
table_om$Composite <- row.names(table_om)
row.names(table_om) <- NULL
table_om[c("Composite", names(table_om)[names(table_om) != "Composite"])]
}
35 changes: 35 additions & 0 deletions R/print_md.R
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,38 @@ print_md.parameters_efa_summary <- function(x, digits = 3, ...) {
#' @export
print_md.parameters_pca_summary <- print_md.parameters_efa_summary

#' @export
print_md.parameters_omega_summary <- function(x, ...) {
orig_x <- x

# extract summary tables
omega_coefficients <- attributes(x)$omega_coefficients
variance_summary <- attributes(x)$summary

# rename columns
if (!is.null(omega_coefficients)) {
names(omega_coefficients) <- c(
"Composite", "Omega (total)", "Omega (hierarchical)", "Omega (group)"
)
caption1 <- "Omega Coefficients"
}
if (!is.null(variance_summary)) {
names(variance_summary) <- c(
"Composite", "Total (%)", "General Factor (%)",
"Group Factor (%)"
)
caption2 <- "Variances"
}

# list for export
out <- insight::compact_list(list(omega_coefficients, variance_summary))
captions <- insight::compact_list(list(caption1, caption2))

cat(insight::export_table(out, caption = captions, format = "markdown", ...))
invisible(orig_x)
}


#' @export
print_md.parameters_efa <- function(x,
digits = 2,
Expand All @@ -298,6 +330,9 @@ print_md.parameters_efa <- function(x,
#' @export
print_md.parameters_pca <- print_md.parameters_efa

#' @export
print_md.parameters_omega <- print_md.parameters_efa


# Equivalence test ----------------------------

Expand Down
56 changes: 38 additions & 18 deletions R/utils_pca_efa.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,8 @@ summary.parameters_pca <- summary.parameters_efa

#' @export
summary.parameters_omega <- function(object, ...) {
table_var <- attributes(object)$summary
class(table_var) <- c("parameters_omega_summary", class(table_var))
table_var
class(object) <- c("parameters_omega_summary", "data.frame")
object
}


Expand Down Expand Up @@ -284,28 +283,41 @@ print.parameters_efa <- function(x,
invisible(x)
}


#' @export
print.parameters_pca <- print.parameters_efa


#' @export
print.parameters_omega <- function(x, ...) {
orig_x <- x
names(x) <- c("Composite", "Omega (total)", "Omega (hierarchical)", "Omega (group)")
cat(insight::export_table(x))
invisible(orig_x)
}
print.parameters_omega <- print.parameters_efa


#' @export
print.parameters_omega_summary <- function(x, ...) {
orig_x <- x
names(x) <- c(
"Composite", "Total Variance (%)", "Variance due to General Factor (%)",
"Variance due to Group Factor (%)"
)
cat(insight::export_table(x))

# extract summary tables
omega_coefficients <- attributes(x)$omega_coefficients
variance_summary <- attributes(x)$summary

# rename columns
if (!is.null(omega_coefficients)) {
names(omega_coefficients) <- c(
"Composite", "Omega (total)", "Omega (hierarchical)", "Omega (group)"
)
caption1 <- c("# Omega Coefficients", "blue")
}
if (!is.null(variance_summary)) {
names(variance_summary) <- c(
"Composite", "Total (%)", "General Factor (%)",
"Group Factor (%)"
)
caption2 <- c("# Variances", "blue")
}

# list for export
out <- insight::compact_list(list(omega_coefficients, variance_summary))
captions <- insight::compact_list(list(caption1, caption2))

cat(insight::export_table(out, caption = captions, format = "text", ...))
invisible(orig_x)
}

Expand All @@ -317,8 +329,10 @@ print.parameters_omega_summary <- function(x, ...) {
# Method
if (inherits(x, "parameters_pca")) {
method <- "Principal Component Analysis"
} else {
} else if (inherits(x, "parameters_efa")) {
method <- "Factor Analysis"
} else {
method <- "Omega"
}

# Rotation
Expand All @@ -341,7 +355,13 @@ print.parameters_omega_summary <- function(x, ...) {
}

# table caption
if (is.null(rotation_name) || rotation_name == "none") {
if (method == "Omega") {
if (format %in% c("markdown", "html")) {
table_caption <- "Omega"
} else {
table_caption <- c("# Omega", "blue")
}
} else if (is.null(rotation_name) || rotation_name == "none") {
if (format %in% c("markdown", "html")) {
table_caption <- sprintf("Loadings from %s (no rotation)", method)
} else {
Expand Down
Loading