Skip to content

Commit eddd207

Browse files
committed
add methods
1 parent 46930f7 commit eddd207

File tree

2 files changed

+93
-22
lines changed

2 files changed

+93
-22
lines changed

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ S3method(convert_efa_to_cfa,fa)
120120
S3method(convert_efa_to_cfa,fa.ci)
121121
S3method(convert_efa_to_cfa,parameters_efa)
122122
S3method(convert_efa_to_cfa,parameters_pca)
123+
S3method(convert_efa_to_cfa,psych_efa)
123124
S3method(display,compare_parameters)
124125
S3method(display,equivalence_test_lm)
125126
S3method(display,parameters_brms_meta)
@@ -567,6 +568,7 @@ S3method(predict,parameters_clusters)
567568
S3method(predict,parameters_efa)
568569
S3method(predict,parameters_pca)
569570
S3method(predict,parameters_sem)
571+
S3method(predict,psych_efa)
570572
S3method(principal_components,data.frame)
571573
S3method(principal_components,lm)
572574
S3method(principal_components,merMod)
@@ -602,6 +604,7 @@ S3method(print,parameters_random)
602604
S3method(print,parameters_sem)
603605
S3method(print,parameters_simulate)
604606
S3method(print,parameters_standardized)
607+
S3method(print,psych_efa)
605608
S3method(print_html,compare_parameters)
606609
S3method(print_html,parameters_brms_meta)
607610
S3method(print_html,parameters_model)
@@ -898,6 +901,7 @@ S3method(summary,parameters_efa)
898901
S3method(summary,parameters_model)
899902
S3method(summary,parameters_omega)
900903
S3method(summary,parameters_pca)
904+
S3method(summary,psych_efa)
901905
S3method(visualisation_recipe,cluster_analysis)
902906
S3method(visualisation_recipe,cluster_analysis_summary)
903907
S3method(visualisation_recipe,n_clusters_dbscan)

R/factor_analysis.R

Lines changed: 89 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,101 @@ factor_analysis.data.frame <- function(x,
3535
correlation_matrix = correlation_matrix
3636
)
3737

38-
.factor_analysis_rotate(
38+
out <- .factor_analysis_rotate(
3939
x,
4040
n,
4141
rotation = rotation,
42-
sort = sort,
43-
threshold = threshold,
4442
correlation_matrix = correlation_matrix,
4543
...
4644
)
45+
46+
attr(out, "dataset") <- x
47+
attr(out, "type") <- "fa"
48+
attr(out, "n") <- n
49+
attr(out, "sort") <- out
50+
attr(out, "threshold") <- threshold
51+
class(out) <- c("psych_efa", class(out))
52+
53+
out
54+
}
55+
56+
57+
# methods -------------------------
58+
#' @export
59+
print.psych_efa <- function(x,
60+
sort = FALSE,
61+
threshold = NULL,
62+
labels = NULL,
63+
...) {
64+
if (is.null(threshold)) {
65+
threshold <- attributes(x)$threshold
66+
}
67+
if (is.null(sort)) {
68+
sort <- attributes(x)$sort
69+
}
70+
model_parameters(x, sort = sort, threshold = threshold, labels = labels, ...)
71+
}
72+
73+
74+
#' @export
75+
summary.psych_efa <- function(object,
76+
sort = FALSE,
77+
threshold = NULL,
78+
labels = NULL,
79+
...) {
80+
if (is.null(threshold)) {
81+
threshold <- attributes(object)$threshold
82+
}
83+
if (is.null(sort)) {
84+
sort <- attributes(object)$sort
85+
}
86+
out <- model_parameters(object, sort = sort, threshold = threshold, labels = labels, ...)
87+
summary(out, ...)
4788
}
4889

4990

91+
#' @export
92+
predict.psych_efa <- function(object,
93+
newdata = NULL,
94+
names = NULL,
95+
keep_na = TRUE,
96+
sort = FALSE,
97+
threshold = NULL,
98+
labels = NULL,
99+
verbose = TRUE,
100+
...) {
101+
if (is.null(threshold)) {
102+
threshold <- attributes(object)$threshold
103+
}
104+
if (is.null(sort)) {
105+
sort <- attributes(object)$sort
106+
}
107+
out <- model_parameters(object, sort = sort, threshold = threshold, labels = labels, ...)
108+
predict(out, newdata = newdata, names = names, keep_na = keep_na, verbose = verbose, ...)
109+
}
110+
111+
112+
#' @export
113+
convert_efa_to_cfa.psych_efa <- function(model,
114+
threshold = NULL,
115+
names = NULL,
116+
max_per_dimension = NULL,
117+
...) {
118+
if (is.null(threshold)) {
119+
threshold <- attributes(object)$threshold
120+
}
121+
sort <- attributes(object)$sort
122+
out <- model_parameters(object, sort = sort, threshold = threshold, ...)
123+
convert_efa_to_cfa(out, names = names, max_per_dimension = max_per_dimension, ...)
124+
}
125+
126+
127+
# internals -----------------------
128+
50129
#' @keywords internal
51130
.factor_analysis_rotate <- function(x,
52131
n,
53132
rotation,
54-
sort = FALSE,
55-
threshold = NULL,
56133
correlation_matrix = NULL,
57134
...) {
58135
if (!inherits(x, "data.frame")) {
@@ -66,25 +143,15 @@ factor_analysis.data.frame <- function(x,
66143

67144
# Pass correlation_matrix if available
68145
if (is.null(correlation_matrix)) {
69-
out <- model_parameters(
70-
psych::fa(x, nfactors = n, rotate = rotation, ...),
71-
sort = sort,
72-
threshold = threshold
73-
)
146+
out <- psych::fa(x, nfactors = n, rotate = rotation, ...)
74147
} else {
75-
out <- model_parameters(
76-
psych::fa(
77-
correlation_matrix,
78-
nfactors = n,
79-
rotate = rotation,
80-
n.obs = nrow(x),
81-
...
82-
),
83-
sort = sort,
84-
threshold = threshold
148+
out <- psych::fa(
149+
correlation_matrix,
150+
nfactors = n,
151+
rotate = rotation,
152+
n.obs = nrow(x),
153+
...
85154
)
86155
}
87-
88-
attr(out, "dataset") <- x
89156
out
90157
}

0 commit comments

Comments
 (0)