Skip to content

Commit 4f60a2b

Browse files
fix n_factors and styler
1 parent 5ad67a4 commit 4f60a2b

File tree

154 files changed

+870
-950
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+870
-950
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ S3method(analyze,lmerModLmerTest)
1414
S3method(analyze,principal)
1515
S3method(analyze,stanreg)
1616
S3method(as.data.frame,density)
17+
S3method(find_best_model,lavaan)
1718
S3method(find_best_model,lmerModLmerTest)
1819
S3method(find_best_model,stanreg)
1920
S3method(find_combinations,formula)

R/analyze.anova.R

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,29 @@
1111
#' @examples
1212
#' \dontrun{
1313
#' library(psycho)
14-
#'
14+
#'
1515
#' df <- psycho::affective
16-
#'
16+
#'
1717
#' x <- aov(df$Tolerating ~ df$Salary)
1818
#' x <- aov(df$Tolerating ~ df$Salary * df$Sex)
19-
#'
19+
#'
2020
#' x <- anova(lm(df$Tolerating ~ df$Salary * df$Sex))
21-
#'
22-
#'
21+
#'
22+
#'
2323
#' summary(analyze(x))
2424
#' print(analyze(x))
25-
#'
25+
#'
2626
#' df <- psycho::emotion %>%
2727
#' mutate(Recall = ifelse(Recall == TRUE, 1, 0)) %>%
2828
#' group_by(Participant_ID, Emotion_Condition) %>%
2929
#' summarise(Recall = sum(Recall) / n())
30-
#'
31-
#' x <- aov(Recall ~ Emotion_Condition + Error(Participant_ID), data=df)
32-
#' x <- anova(lmerTest::lmer(Recall ~ Emotion_Condition + (1|Participant_ID), data=df))
30+
#'
31+
#' x <- aov(Recall ~ Emotion_Condition + Error(Participant_ID), data = df)
32+
#' x <- anova(lmerTest::lmer(Recall ~ Emotion_Condition + (1 | Participant_ID), data = df))
3333
#' analyze(x)
3434
#' summary(x)
3535
#' }
36-
#'
37-
#'
36+
#'
3837
#' @references
3938
#' \itemize{
4039
#' \item{Levine, T. R., & Hullett, C. R. (2002). Eta squared, partial eta squared, and misreporting of effect size in communication research. Human Communication Research, 28(4), 612-625.}
@@ -289,14 +288,13 @@ analyze.aovlist <- analyze.aov
289288
#'
290289
#' @examples
291290
#' library(psycho)
292-
#'
291+
#'
293292
#' df <- psycho::affective
294-
#'
293+
#'
295294
#' x <- aov(df$Tolerating ~ df$Salary)
296295
#' x <- aov(df$Tolerating ~ df$Salary * df$Sex)
297-
#'
296+
#'
298297
#' omega_sq(x)
299-
#'
300298
#' @seealso http://stats.stackexchange.com/a/126520
301299
#'
302300
#' @author Arnoud Plantinga

R/analyze.blavaan.R

Lines changed: 68 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@
1212
#' @examples
1313
#' library(psycho)
1414
#' library(lavaan)
15-
#'
16-
#' model <- ' visual =~ x1 + x2 + x3
17-
#' textual =~ x4 + x5 + x6
18-
#' speed =~ x7 + x8 + x9 '
19-
#' x <- lavaan::cfa(model, data=HolzingerSwineford1939)
20-
#'
15+
#'
16+
#' model <- " visual =~ x1 + x2 + x3\ntextual =~ x4 + x5 + x6\nspeed =~ x7 + x8 + x9 "
17+
#' x <- lavaan::cfa(model, data = HolzingerSwineford1939)
18+
#'
2119
#' rez <- analyze(x)
2220
#' print(rez)
23-
#'
24-
#'
2521
#' @author \href{https://dominiquemakowski.github.io/}{Dominique Makowski}
2622
#'
2723
#' @seealso
@@ -30,14 +26,14 @@
3026
#' @importFrom lavaan parameterEstimates fitmeasures
3127
#'
3228
#' @export
33-
analyze.blavaan <- function(x, CI=90, standardize=FALSE,...) {
29+
analyze.blavaan <- function(x, CI = 90, standardize = FALSE, ...) {
3430
fit <- x
3531

3632

3733
# Processing
3834
# -------------
3935
values <- list()
40-
values$CI = CI
36+
values$CI <- CI
4137

4238
# Fit measures
4339
values$Fit_Measures <- interpret_lavaan(fit)
@@ -47,14 +43,16 @@ analyze.blavaan <- function(x, CI=90, standardize=FALSE,...) {
4743
# -------------
4844
computations <- .get_info_computations(fit)
4945
fitmeasures <- values$Fit_Measures$text
50-
text <- paste0("A Bayesian model was fitted (",
51-
computations,
52-
"). The fit indices are as following: ",
53-
fitmeasures)
46+
text <- paste0(
47+
"A Bayesian model was fitted (",
48+
computations,
49+
"). The fit indices are as following: ",
50+
fitmeasures
51+
)
5452

5553
# Summary
5654
# -------------
57-
summary <- .summary_blavaan(fit, CI=CI, standardize=standardize)
55+
summary <- .summary_blavaan(fit, CI = CI, standardize = standardize)
5856

5957
# Plot
6058
# -------------
@@ -74,30 +72,36 @@ analyze.blavaan <- function(x, CI=90, standardize=FALSE,...) {
7472
#' @keywords internal
7573
.get_info_computations <- function(fit) {
7674
chains <- blavaan::blavInspect(fit, "n.chains")
77-
sample = fit@external$sample
78-
warmup = fit@external$burnin
79-
text = paste0(chains,
80-
" chains, each with iter = ",
81-
sample,
82-
"; warmup = ",
83-
warmup)
75+
sample <- fit@external$sample
76+
warmup <- fit@external$burnin
77+
text <- paste0(
78+
chains,
79+
" chains, each with iter = ",
80+
sample,
81+
"; warmup = ",
82+
warmup
83+
)
8484
return(text)
8585
}
8686

8787

8888

8989

9090
#' @keywords internal
91-
.process_blavaan <- function(fit, standardize=FALSE, CI=90){
91+
.process_blavaan <- function(fit, standardize = FALSE, CI = 90) {
9292
# Get relevant rows
93-
PE <- parameterEstimates(fit, se = FALSE, ci=FALSE, remove.eq = FALSE, remove.system.eq = TRUE,
94-
remove.ineq = FALSE, remove.def = FALSE,
95-
add.attributes = TRUE)
96-
if(!("group" %in% names(PE))) PE$group <- 1
93+
PE <- parameterEstimates(fit,
94+
se = FALSE, ci = FALSE, remove.eq = FALSE, remove.system.eq = TRUE,
95+
remove.ineq = FALSE, remove.def = FALSE,
96+
add.attributes = TRUE
97+
)
98+
if (!("group" %in% names(PE))) PE$group <- 1
9799
newpt <- fit@ParTable
98100
pte2 <- which(newpt$free > 0)
99-
relevant_rows <- match(with(newpt, paste(lhs[pte2], op[pte2], rhs[pte2], group[pte2], sep="")),
100-
paste(PE$lhs, PE$op, PE$rhs, PE$group, sep=""))
101+
relevant_rows <- match(
102+
with(newpt, paste(lhs[pte2], op[pte2], rhs[pte2], group[pte2], sep = "")),
103+
paste(PE$lhs, PE$op, PE$rhs, PE$group, sep = "")
104+
)
101105

102106
# Priors
103107
priors <- rep(NA, nrow(PE))
@@ -108,12 +112,12 @@ analyze.blavaan <- function(x, CI=90, standardize=FALSE,...) {
108112

109113

110114
# Posterior
111-
if(standardize == FALSE){
115+
if (standardize == FALSE) {
112116
posteriors <- blavaan::blavInspect(fit, "draws") %>%
113117
as.matrix() %>%
114118
as.data.frame()
115119
names(posteriors) <- names(lavaan::coef(fit))
116-
} else{
120+
} else {
117121
posteriors <- blavaan::standardizedposterior(fit) %>%
118122
as.data.frame()
119123
}
@@ -127,7 +131,7 @@ analyze.blavaan <- function(x, CI=90, standardize=FALSE,...) {
127131
Effect <- c()
128132
CI_lower <- c()
129133
CI_higher <- c()
130-
for(effect in names(posteriors)){
134+
for (effect in names(posteriors)) {
131135
posterior <- posteriors[[effect]]
132136
Effect <- c(Effect, effect)
133137
MPE <- c(MPE, mpe(posterior)$MPE)
@@ -137,10 +141,9 @@ analyze.blavaan <- function(x, CI=90, standardize=FALSE,...) {
137141
CI_values <- HDI(posterior, prob = CI / 100)
138142
CI_lower <- c(CI_lower, CI_values$values$HDImin)
139143
CI_higher <- c(CI_higher, CI_values$values$HDImax)
140-
141144
}
142145

143-
if(standardize == FALSE){
146+
if (standardize == FALSE) {
144147
Effects <- rep(NA, nrow(PE))
145148
Effects[relevant_rows] <- Effect
146149
MPEs <- rep(NA, nrow(PE))
@@ -153,7 +156,7 @@ analyze.blavaan <- function(x, CI=90, standardize=FALSE,...) {
153156
CI_lowers[relevant_rows] <- CI_lower
154157
CI_highers <- rep(NA, nrow(PE))
155158
CI_highers[relevant_rows] <- CI_higher
156-
} else{
159+
} else {
157160
Effects <- Effect
158161
MPEs <- MPE
159162
Medians <- Median
@@ -162,45 +165,51 @@ analyze.blavaan <- function(x, CI=90, standardize=FALSE,...) {
162165
CI_highers <- CI_higher
163166
}
164167

165-
data <- data.frame("Effect" = Effects,
166-
"Median" = Medians,
167-
"MAD" = MADs,
168-
"MPE" = MPEs,
169-
"CI_lower" = CI_lowers,
170-
"CI_higher" = CI_highers,
171-
"Prior" = priors)
168+
data <- data.frame(
169+
"Effect" = Effects,
170+
"Median" = Medians,
171+
"MAD" = MADs,
172+
"MPE" = MPEs,
173+
"CI_lower" = CI_lowers,
174+
"CI_higher" = CI_highers,
175+
"Prior" = priors
176+
)
172177

173178
return(data)
174179
}
175180

176181

177182

178183
#' @keywords internal
179-
.summary_blavaan <- function(fit, CI=90, standardize=FALSE){
180-
181-
solution <- lavaan::parameterEstimates(fit, se = TRUE, ci=TRUE, standardized=FALSE, level = CI/100)
184+
.summary_blavaan <- function(fit, CI = 90, standardize = FALSE) {
185+
solution <- lavaan::parameterEstimates(fit, se = TRUE, ci = TRUE, standardized = FALSE, level = CI / 100)
182186

183187
solution <- solution %>%
184-
rename("From" = "rhs",
185-
"To" = "lhs",
186-
"Operator" = "op",
187-
"Coef" = "est",
188-
"SE" = "se",
189-
"CI_lower" = "ci.lower",
190-
"CI_higher" = "ci.upper") %>%
188+
rename(
189+
"From" = "rhs",
190+
"To" = "lhs",
191+
"Operator" = "op",
192+
"Coef" = "est",
193+
"SE" = "se",
194+
"CI_lower" = "ci.lower",
195+
"CI_higher" = "ci.upper"
196+
) %>%
191197
mutate(Type = dplyr::case_when(
192198
Operator == "=~" ~ "Loading",
193-
Operator == "~" ~ "Regression",
199+
Operator == "~" ~ "Regression",
194200
Operator == "~~" ~ "Correlation",
195-
TRUE ~ NA_character_)) %>%
201+
TRUE ~ NA_character_
202+
)) %>%
196203
select(one_of(c("To", "Operator", "From", "Type"))) %>%
197204
mutate_("Effect" = "as.character(paste0(To, Operator, From))") %>%
198-
full_join(.process_blavaan(fit, CI=CI, standardize=standardize) %>%
199-
mutate_("Effect" = "as.character(Effect)"), by="Effect") %>%
205+
full_join(.process_blavaan(fit, CI = CI, standardize = standardize) %>%
206+
mutate_("Effect" = "as.character(Effect)"), by = "Effect") %>%
200207
select_("-Effect") %>%
201-
mutate_("Median" = "replace_na(Median, 1)",
202-
"MAD" = "replace_na(MAD, 0)",
203-
"MPE" = "replace_na(MPE, 100)") %>%
208+
mutate_(
209+
"Median" = "replace_na(Median, 1)",
210+
"MAD" = "replace_na(MAD, 0)",
211+
"MPE" = "replace_na(MPE, 100)"
212+
) %>%
204213
select(one_of(c("From", "Operator", "To", "Median", "MAD", "CI_lower", "CI_higher", "MPE", "Prior", "Type"))) %>%
205214
dplyr::filter_("Operator != '~1'")
206215

R/analyze.fa.R

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@
1212
#' @examples
1313
#' library(psycho)
1414
#' library(psych)
15-
#'
15+
#'
1616
#' x <- psych::fa(psych::Thurstone.33, 2)
17-
#'
17+
#'
1818
#' results <- analyze(x)
1919
#' print(results)
2020
#' summary(results)
2121
#' plot(results)
22-
#'
23-
#'
2422
#' @author \href{https://dominiquemakowski.github.io/}{Dominique Makowski}
2523
#'
2624
#' @export
@@ -109,11 +107,11 @@ analyze.fa <- function(x, labels = NULL, treshold = "max", ...) {
109107
#' @examples
110108
#' \dontrun{
111109
#' library(psycho)
112-
#'
110+
#'
113111
#' x <- psych::fa(psych::Thurstone.33, 2)
114112
#' format_loadings(x)
115113
#' }
116-
#'
114+
#'
117115
#' @import dplyr
118116
#' @export
119117
format_loadings <- function(x, labels = NULL) {
@@ -163,12 +161,11 @@ format_loadings <- function(x, labels = NULL) {
163161
#' @examples
164162
#' \dontrun{
165163
#' library(psycho)
166-
#'
164+
#'
167165
#' x <- psych::fa(psych::Thurstone.33, 2)
168166
#' get_loadings_max(format_loadings(x)$loadings)
169167
#' }
170-
#'
171-
#'
168+
#'
172169
#' @import dplyr
173170
#' @export
174171
get_loadings_max <- function(loadings) {
@@ -194,14 +191,13 @@ get_loadings_max <- function(loadings) {
194191
#' @examples
195192
#' \dontrun{
196193
#' library(psycho)
197-
#'
194+
#'
198195
#' x <- psych::fa(psych::Thurstone.33, 2)
199196
#' loadings <- format_loadings(x)$loadings
200-
#' get_cfa_model(loadings, treshold="max")
201-
#' get_cfa_model(loadings, treshold=0.1)
197+
#' get_cfa_model(loadings, treshold = "max")
198+
#' get_cfa_model(loadings, treshold = 0.1)
202199
#' }
203-
#'
204-
#'
200+
#'
205201
#' @import dplyr
206202
#' @export
207203
get_cfa_model <- function(loadings, treshold = "max") {
@@ -240,12 +236,11 @@ get_cfa_model <- function(loadings, treshold = "max") {
240236
#' @examples
241237
#' \dontrun{
242238
#' library(psycho)
243-
#'
239+
#'
244240
#' x <- psych::fa(psych::Thurstone.33, 2)
245241
#' plot_loadings(format_loadings(x)$loadings)
246242
#' }
247-
#'
248-
#'
243+
#'
249244
#' @import dplyr
250245
#' @export
251246
plot_loadings <- function(loadings) {
@@ -265,8 +260,3 @@ plot_loadings <- function(loadings) {
265260

266261
return(p)
267262
}
268-
269-
270-
271-
272-

0 commit comments

Comments
 (0)