sanyas2
January 30, 2021, 12:43am
1
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 = " + ")
```

FJCC
January 30, 2021, 2:21am
3
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.

sanyas2
February 1, 2021, 10:32pm
5
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?

sanyas2
February 2, 2021, 6:53pm
11
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).

FJCC:

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

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 = '+'))
```

system
Closed
February 26, 2021, 11:04am
14
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.