I have a function that I am using with lapply().
lapply(x, function(x))
The output looks like this:
[[1]]
[1] "att_admire"
[[2]]
[1] "att_hypocrisy"
[[3]]
[1] "att_annoyed"
[[4]]
[1] ""
[[5]]
[1] "att_respectvalues"
When I use sapply(), it looks like this:
att_admire att_hypocrisy att_annoyed att_judge
"att_admire" "att_hypocrisy" "att_annoyed" ""
att_respectvalues
"att_respectvalues"
I would like the output to look neater, like this. Only the variable names should be included, not the "" or indices.
att_admire, att_hypocrisy, att_annoyed, att_respectvalues
What can I do to modify the code I have? I looked at this question, but it focuses on how to get cleaner results in an output file, whereas I would prefer for the output to look cleaner in RStudio itself.
Below, I have included the function I am using and a sample of the data.
Libraries:
install.packages("lavaan")
library(lavaan)
Main command:
iv <- "consistent"
dv <- "behavior_solarize"
mediators <- c("att_admire", "att_hypocrisy", "att_annoyed", "att_judge", "att_respectvalues")
lapply(mediators, med_overall)
Function (using "lavaan" package):
med_overall <- function(mediator) {
mediation.model <-
paste("
# mediator
",mediator," ~ a * ",iv,"
",dv," ~ b * ",mediator,"
# direct effect
",dv," ~ c * ",iv,"
# indirect effect (a*b)
ab := a*b
# total effect
total := c + (a*b)")
fit <- sem(mediation.model, data = df, meanstructure = TRUE,
se = "boot", bootstrap = 500)
val <- parameterEstimates(fit)
ifelse(val[10,8] < 0.05, mediator, "")}
Sample data:
structure(list(behavior_solarize = c(5, 5, 5, 3, 3, 3, 5, 6,
7, 5, 6, 7, 4, 3, 6, 4, 5, 6, 2, 4, 4, 5, 5, 5, 6, 2, 6, 2, 2,
1, 5, 5, 5, 5, 1, 5, 1, 5, 1, 6, 4, 2, 6, 6, 7, 6, 5, 5, 6, 5,
1, 4, 1, 6, 7, 6, 6, 3, 4, 2, 6, 7, 6, 1, 1, 2, 2, 5, 5, 4, 6,
3, 2, 5, 6, 1, 6, 2, 2, 6, 5, 3, 2, 3, 5, 1, 2, 1, 5, 4, 4, 5,
4, 3, 6, 5, 5, 5, 1, 4, 5, 5, 4, 6, 6, 2, 7, 6,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from?) convert toString and cat it.
res <- lapply(mediators, med_overall)
cat(toString(res[res != '']))
# att_admire, att_hypocrisy, att_annoyed, att_respectvalues
I would like to obtain the initial communalities for an exploratory factor analysis in R
(that is, the R squared of each item when predicted by the other items included in the analysis).
Is there a way to do this with either jmv::efa or psych::fa ?
I only see the uniqueness, which informs me of the communalities AFTER factor extraction (1-uniqueness)...
Thank you for your consideration : )
As you note, the initial communalities in a factor analysis are the squared multiple correlations (SMC) of each variable by the remaining variables. Using the built-in attitude dataset as an example they are easily calculated without additional packages via:
1 - 1 / diag(solve(cor(attitude)))
rating complaints privileges learning raises critical advance
0.7326020 0.7700868 0.3831176 0.6194561 0.6770498 0.1881465 0.5186447
The psych package includes the smc() function for convenience:
psych::smc(attitude)
rating complaints privileges learning raises critical advance
0.7326020 0.7700868 0.3831176 0.6194561 0.6770498 0.1881465 0.5186447
Dataset
Here is the dput for the data I am using, hereafter called hwk:
hwk <- structure(list(V1 = structure(c(4, 4, 2, 2, 2, 2, 2, 2, 4, 4,
2, 3, 2, 3, 4, 2, 2, 2, 3, 3, 2, 3, 1, 3, 3, 3, 3, 4, 1, 2, 4,
1, 2, 3, 2, 3, 1, 1, 2, 2, 4, 3, 2, 1, 2, 3, 3, 4, 3, 3, 2, 3,
1, 4, 3, 2, 3, 4, 1, 3, 3, 3, 2, 2, 1, 2, 3, 4, 4, 2, 4, 3, 2,
3, 3, 3, 3, 2, 4, 3, 3, 3, 2, 2, 3, 4, 2, 4, 4, 2, 2, 3, 3), format.spss = "F8.0"),
V2 = structure(c(4, 4, 3, 4, 3, 4, 3, 2, 4, 1, 3, 3, 3, 4,
3, 3, 2, 3, 4, 3, 1, 4, 2, 3, 4, 2, 4, 3, 3, 2, 3, 2, 3,
3, 4, 3, 3, 3, 3, 3, 3, 2, 4, 2, 2, 2, 4, 3, 4, 4, 2, 4,
2, 3, 3, 3, 3, 3, 4, 3, 3, 3, 3, 4, 3, 3, 4, 4, 4, 4, 4,
3, 4, 3, 3, 3, 4, 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.frame"), row.names = c(NA, -93L))
EFA
I did some research after my initial answer and it appears that there is a package for this called EFA Tools. There is a function called EFA that allows you to specify that you want the initial communalities. First, run the library and the EFA itself below:
# Load EFA Tools library:
library(EFAtools)
# Run EFA:
hwkfa <- EFA(hwk,
n_factors = 3,
start_method = "psych",
method = "PAF",
rotation = "promax",
init_comm = "smc", # selected initial communalities
type = "SPSS")
Obtaining initial communalities:
Then from there you can simply select the initial communalities by using the following code:
hwkfa$h2_init
Which gives you the following vector of output:
V1 V2 V3 V4 V5 V6 V7
0.8034001 0.5583605 0.5487691 0.3255253 0.5685402 0.4643686 0.5227481
V8 V9 V10 V11 V12
0.8050573 0.3474202 0.5564858 0.3496354 0.3783390
I ran the same thing in SPSS and got matching values: