Maybe related with Paste()

Hi Community,

I am trying to make a model with the code below.
Model1 <- ' f1 =~ aaq1 + aaq2 + aaq3 + aaq4 + aaq5 + aaq6 + aaq7+ aaq8'

It seems possible to make it easy to use a function because there is a pattern in the code. Could you let me know how I can do it?

I tried this but it didn't work.
Model1 <- ' f1 =~ paste0(aaq, [1:8], sep = '+')'

Best,

Perhaps

paste0("aaq", 1:8, collapse = " + ")

Another possible path would use as.formula()

as.formula(paste("~",paste0("aaq", 1:8, collapse = '+')))
~aaq1 + aaq2 + aaq3 + aaq4 + aaq5 + aaq6 + aaq7 + aaq8
1 Like

Ah, much better than my poor effort.

Thank you so much for both of you!
I am trying to run Structural Equation Modeling with Lavaan. Thus, I tried this code below. But when I used "=~" symbol, it doesn't work. Could you let me know how to handle this?

practice <- as.formula(paste("=~", paste0("aaqii", 1:8, collapse = "+")))
#> Error in str2lang(x): <text>:1:1: unexpected '='
#> 1: =
#>     ^

You have *"=~" when FJCC said "~"

Does this help?

Yes, but while "~" symbol is often used to indicate regression, I want to use "=~" symbol, which is used in Structural Equation Modeling.

Remove the "~" and insert the "="?

Thanks for the suggestion, but unfortunately it doesn't work either.

Can you point us to an example in Structural Equation Modeling?

When we say x1 and x2 are predicted by A, we could use the code below.

A =~ x1 + x2

In this case, A is latent variable and x1/x2 are observed variable (i.e., indicator).

Closest I can come, adapting FJCC's example is

(paste("=~",paste0("aaq", 1:8, collapse = '+')))

I might be wrong, but from what I remember lavaan needs a string not a formula. The string can contain many formulae... so @jrkrideau is probably right. You can then add f1 before the =~

paste("f1 =~",paste0("aaq", 1:8, collapse = '+'))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.