Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • An error in feeding polychoric correlation to sem

    Hi Friends,

    I want to feed polychoric correlation to sem as it provides the goodness of fit test. I keep running into a syntax error. I found the code from an old post in the stata forum.

    Here is my code after a little adaptation
    local thevars BYS22*
    polychoric `thevars'
    mat polychR = r(R)
    forvalues i=1/`: word count `thevars' ' {
    forvalues j=1/`i' {
    local setcor `setcor' `=polychR[`i',`j']'
    }
    if `i' < `: word count `thevars' ' local setcor `setcor' \
    }
    local N = r(N)
    clear
    ssd init `thevars'
    ssd set obs `N'
    ssd set cor `setcor'
    sem(envdis2->BYS22*)///
    ,latent(endis2) nocapslatent stand

    stata indicates "syntax error
    Syntax is ssd init varnames
    You must specify two or more new variables names" after the command ssd init `thevars'.
    Can anyone tell me what goes wrong with my code and how to fix it?

  • #2
    I expect that you expect that the result of
    Code:
    local thevars BYS22*
    to be a list of variables names whose names begin with BYS22. That is not the case, as the following example demonstrates.
    Code:
    . gen x1 = .
    
    . gen x2 = .
    
    . local foo x*
    
    . macro list _foo
    _foo:           x*
    
    . display `: word count `foo''
    1
    
    . unab bar : x*
    
    . macro list _bar
    _bar:           x1 x2
    
    . display `: word count `bar''
    2

    Comment


    • #3
      Hi Will,

      Thank you for the response. The local list in my code returns the result I wanted. For instance, I can get the polychoric correlation if I typed the following codes
      local thevars BYS22*
      polychoric `thevars'.

      I got the correlation matrix, from the responses to an old post, there seems to be no problem for other users as well. So I am not sure of whether this is is the problem.
      Click image for larger version

Name:	polychoric correlation.JPG
Views:	1
Size:	65.9 KB
ID:	1449942


      Comment


      • #4
        And what precisely was the result when you replaced
        Code:
        local thevars BYS22*
        with
        Code:
        unab thevars : BYS22*
        as my example would suggest doing? Did it make any improvement? Or perhaps you dismissed the idea without trying it?

        I would expect the loops building the setcor macro to work better going through them more than 1 time. And after the clear command, the varlist BYS22* cannot be expanded because there are no longer any variables in active memory to match.

        The code you are copying from will not work with a varlist containing wild cards. It worked for other people because they explicitly listed the variable names.

        Comment


        • #5
          Thanks for the clarification. I ran the code, it worked.

          Comment

          Working...
          X