Announcement

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

  • Question about using margins command after SEM

    Hello,

    I have a question about using margins command after sem command (structural equation model) with categorical variables.
    For example, let's assume that I have two variables, sex and group. Sex has two values, male and female. Group has three values, 1,2 and 3. Since sem does not allow factor-variable notation, I need to create several new variables (e.g. one variable for sex, two for group, and 6 for the interaction between sex and group) in order to use sem.

    After the regression, I want to obtain the margins of the two variables and all possible combination of the two. If factor-variable notation is allowed, it would be "margins sex group sex#group." Since the notation is NOT allowed (and I have 9 new variables instead), I am wondering if anyone knows how to obtain the margins.

    Thank you very much!

  • #2
    You need to define how you are using the categorical variables before anyone can provide any helpful input. For example, if sex and group are manifest variables of a construct that you are modeling, you could use the appropriate link and family functions with gsem. If you are using them as covariates, you'd probably have an easier time fitting the model as a multi group model and testing the different measurement invariance constraints to see if the parameters of interest vary across the groups.

    Comment


    • #3
      Thank you for your suggestion wbuchanan! Let me restate my problem.

      I used sem to address missing data issue in my regression. Since my data needs to be weighted using survey weights, I did not use gsem. My focal independent variables are gender (female/male) and group (grp1/grp2/grp3). Without sem, my regression would be pretty straightforward: reg i.gender##i.group cv1 cv2 cv3 cv4. However since sem does not allow factor-variable notation, I made dummies for the group variable, and also created interaction variables before putting into the regression. So after using sem, my regression looks like (with base categories omitted): svy: sem (dv <- gender_female grp2 grp3 female_grp2 female_grp3 cv1 cv2 cv3 cv4), method(mlmv). After sem, I found significant interaction results so I tried to plot the interactions. Usually margins command can directly estimate the margins before I use marginsplot, by margins i.gender##i.group, however since I used sem for regression, I still could not use factor notation. Thus I tried to estimate margins at each level of the interactions. To be specific, I did margins, subpop(if gender == 1 & group == 1), margins, subpop(if gender == 1 & group == 2), margins, subpop(if gender == 1 & group == 3), margins, subpop(if gender == 2 & group == 1), margins, subpop(if gender == 1 & group == 2), margins, subpop(if gender == 1 & group == 3). My plan was to keep record of each margins estimation, and draw the interaction plots manually.

      While 5 out of 6 of these margin estimations went well, one of them margins, subpop(if gender == 1 & group == 2) gave me this error message:
      "missing predicted values encountered within the estimation sample"

      I was wondering whether anyone can help me with this? Thank you!
      Last edited by Chris Yin; 14 Feb 2015, 09:53.

      Comment


      • #4
        Chris, I am curious as to whether you worked this out? I am in a similar situation. Thank you.

        Comment


        • #5
          I could not find an example for this problem on the internet. With some trial and error I found a solution, perhaps it is useful for someone.
          Here is an example with a categorical variable "partv" with 5 categories (from 0 to 4).
          The reference category is set to 1 (I once found the adofile refgroup to do this, I think this is not available any more).
          As you can see the margins for the reference group (category 1) can be estimated when all dummies get the value zero.

          * Dummies in sem and marginsplot
          refgroup partv 1
          xi: sem (timepress <- i.partv ) [pweight=myweight] , nocapslatent method(mlmv)
          margins, at(_Ipartv_0=1 _Ipartv_2=0 _Ipartv_3=0 _Ipartv_4=0) ///
          at(_Ipartv_0=0 _Ipartv_2=0 _Ipartv_3=0 _Ipartv_4=0) ///
          at(_Ipartv_0=0 _Ipartv_2=1 _Ipartv_3=0 _Ipartv_4=0) ///
          at(_Ipartv_0=0 _Ipartv_2=0 _Ipartv_3=1 _Ipartv_4=0) ///
          at(_Ipartv_0=0 _Ipartv_2=0 _Ipartv_3=0 _Ipartv_4=1)
          marginsplot, name(sem, replace) xlab(1 "0 p" 2 "1 p" 3 "2 p" 4 "3 p" 5 "4 p")

          Comment


          • #6
            My example above contained some building blocks to an answer, but did not yet show the complete syntax for a sem with an interaction effect between two dummified categorical variables.
            In the example below, a dataset accessible via the internet is used. First, a weighted linear regression is performed using factor-variable notation leading to a marginsplot that is only shown at the end.
            Next, a weighted sem similar to the regression is performed with the same main effects and interaction effect but now specified with the prefix xi. Syntax is shown for the margins and the marginsplot. I can't get the lines for females and males properly in one graph, so two graphs are made. Finally, the three pictures are presented next to each other. The resemblance is clear.

            Code:
            . webuse nhanes2, clear
            
            . svyset [pweight=finalwgt]
            
            Sampling weights: finalwgt
                         VCE: linearized
                 Single unit: missing
                    Strata 1: <one>
             Sampling unit 1: <observations>
                       FPC 1: <zero>
            
            . recode hlthstat (5=1 Poor) (4=2 Fair) (3=3 Good) (2=4 "Very Good") (1=5 Excellent) (8=.), gen(health)
            (7411 differences between hlthstat and health)
            
            . label var health "Health status"
            
            . codebook health female agegrp, compact
            
            Variable     Obs Unique      Mean  Min  Max  Label
            ---------------------------------------------------------------------------------------------------------------------------------------------
            health     10335      5  3,413836    1    5  Health status
            female     10351      2  ,5251667    0    1  Female
            agegrp     10351      6   3,35813    1    6  Age group
            ---------------------------------------------------------------------------------------------------------------------------------------------
            
            . quietly: svy: regress health agegrp##female
            
            . quietly: margins agegrp#female
            
            . marginsplot, name(model1, replace) xlab(,alternate) legend(pos(2) ring(0)) nodraw
            
            Variables that uniquely identify margins: agegrp female
            
            . quietly: xi: svy: sem (health <- i.agegrp*i.female)
            
            . margins, ///
            > at(_Ifemale_1=0 _Iagegrp_2=0 _Iagegrp_3=0 _Iagegrp_4=0 _Iagegrp_5=0 _Iagegrp_6=0) /// male & agegrp 1 20–29
            > at(_Ifemale_1=0 _Iagegrp_2=1 _Iagegrp_3=0 _Iagegrp_4=0 _Iagegrp_5=0 _Iagegrp_6=0) /// male & agegrp 2 30-39
            > at(_Ifemale_1=0 _Iagegrp_2=0 _Iagegrp_3=1 _Iagegrp_4=0 _Iagegrp_5=0 _Iagegrp_6=0) /// etc.
            > at(_Ifemale_1=0 _Iagegrp_2=0 _Iagegrp_3=0 _Iagegrp_4=1 _Iagegrp_5=0 _Iagegrp_6=0) ///
            > at(_Ifemale_1=0 _Iagegrp_2=0 _Iagegrp_3=0 _Iagegrp_4=0 _Iagegrp_5=1 _Iagegrp_6=0) ///
            > at(_Ifemale_1=0 _Iagegrp_2=0 _Iagegrp_3=0 _Iagegrp_4=0 _Iagegrp_5=0 _Iagegrp_6=1)
            
            Predictive margins
            
            Number of strata =      1                        Number of obs   =      10.335
            Number of PSUs   = 10.335                        Population size = 116.997.257
            Model VCE: Linearized                            Design df       =      10.334
            
            Expression: Linear prediction (Health status), predict(xb(health))
            1._at: _Iagegrp_2 = 0
                   _Iagegrp_3 = 0
                   _Iagegrp_4 = 0
                   _Iagegrp_5 = 0
                   _Iagegrp_6 = 0
                   _Ifemale_1 = 0
            2._at: _Iagegrp_2 = 1
                   _Iagegrp_3 = 0
                   _Iagegrp_4 = 0
                   _Iagegrp_5 = 0
                   _Iagegrp_6 = 0
                   _Ifemale_1 = 0
            3._at: _Iagegrp_2 = 0
                   _Iagegrp_3 = 1
                   _Iagegrp_4 = 0
                   _Iagegrp_5 = 0
                   _Iagegrp_6 = 0
                   _Ifemale_1 = 0
            4._at: _Iagegrp_2 = 0
                   _Iagegrp_3 = 0
                   _Iagegrp_4 = 1
                   _Iagegrp_5 = 0
                   _Iagegrp_6 = 0
                   _Ifemale_1 = 0
            5._at: _Iagegrp_2 = 0
                   _Iagegrp_3 = 0
                   _Iagegrp_4 = 0
                   _Iagegrp_5 = 1
                   _Iagegrp_6 = 0
                   _Ifemale_1 = 0
            6._at: _Iagegrp_2 = 0
                   _Iagegrp_3 = 0
                   _Iagegrp_4 = 0
                   _Iagegrp_5 = 0
                   _Iagegrp_6 = 1
                   _Ifemale_1 = 0
            
            ------------------------------------------------------------------------------
                         |            Delta-method
                         |     Margin   std. err.      t    P>|t|     [95% conf. interval]
            -------------+----------------------------------------------------------------
                     _at |
                      1  |   4,097491   ,0453547    90,34   0,000     4,008587    4,186395
                      2  |   4,002443   ,0394277   101,51   0,000     3,925157    4,079729
                      3  |   3,680211   ,0492593    74,71   0,000     3,583653    3,776769
                      4  |   3,315885   ,0501174    66,16   0,000     3,217645    3,414125
                      5  |   3,032181   ,0400314    75,75   0,000     2,953712     3,11065
                      6  |   2,979195   ,0708554    42,05   0,000     2,840305    3,118086
            ------------------------------------------------------------------------------
            
            . marginsplot, name(model2, replace) xlab(1 "20-29" 2 "30-39" 3 "40-49" 4 "50-59" 5 "60-69" 6 "70+", alternate) xtit("Age group") tit(Male)  
            > nodraw
            
            Variables that uniquely identify margins: _atopt
            Multiple at() options specified:
                  _atoption=1: _Ifemale_1=0 _Iagegrp_2=0 _Iagegrp_3=0 _Iagegrp_4=0 _Iagegrp_5=0 _Iagegrp_6=0
                  _atoption=2: _Ifemale_1=0 _Iagegrp_2=1 _Iagegrp_3=0 _Iagegrp_4=0 _Iagegrp_5=0 _Iagegrp_6=0
                  _atoption=3: _Ifemale_1=0 _Iagegrp_2=0 _Iagegrp_3=1 _Iagegrp_4=0 _Iagegrp_5=0 _Iagegrp_6=0
                  _atoption=4: _Ifemale_1=0 _Iagegrp_2=0 _Iagegrp_3=0 _Iagegrp_4=1 _Iagegrp_5=0 _Iagegrp_6=0
                  _atoption=5: _Ifemale_1=0 _Iagegrp_2=0 _Iagegrp_3=0 _Iagegrp_4=0 _Iagegrp_5=1 _Iagegrp_6=0
                  _atoption=6: _Ifemale_1=0 _Iagegrp_2=0 _Iagegrp_3=0 _Iagegrp_4=0 _Iagegrp_5=0 _Iagegrp_6=1
            
            . margins, ///
            > at(_Ifemale_1=1 _Iagegrp_2=0 _Iagegrp_3=0 _Iagegrp_4=0 _Iagegrp_5=0 _Iagegrp_6=0 /// female & agegrp 1 20–29
            > _IageXfem_2_1=0 _IageXfem_3_1=0 _IageXfem_4_1=0 _IageXfem_5_1=0 _IageXfem_6_1=0) /// 
            > at(_Ifemale_1=1 _Iagegrp_2=1 _Iagegrp_3=0 _Iagegrp_4=0 _Iagegrp_5=0 _Iagegrp_6=0 /// female & agegrp 2 30-39
            > _IageXfem_2_1=1 _IageXfem_3_1=0 _IageXfem_4_1=0 _IageXfem_5_1=0 _IageXfem_6_1=0) ///
            > at(_Ifemale_1=1 _Iagegrp_2=0 _Iagegrp_3=1 _Iagegrp_4=0 _Iagegrp_5=0 _Iagegrp_6=0 /// etc.
            > _IageXfem_2_1=0 _IageXfem_3_1=1 _IageXfem_4_1=0 _IageXfem_5_1=0 _IageXfem_6_1=0) ///
            > at(_Ifemale_1=1 _Iagegrp_2=0 _Iagegrp_3=0 _Iagegrp_4=1 _Iagegrp_5=0 _Iagegrp_6=0 ///
            > _IageXfem_2_1=0 _IageXfem_3_1=0 _IageXfem_4_1=1 _IageXfem_5_1=0 _IageXfem_6_1=0) ///
            > at(_Ifemale_1=1 _Iagegrp_2=0 _Iagegrp_3=0 _Iagegrp_4=0 _Iagegrp_5=1 _Iagegrp_6=0 ///
            > _IageXfem_2_1=0 _IageXfem_3_1=0 _IageXfem_4_1=0 _IageXfem_5_1=1 _IageXfem_6_1=0) ///
            > at(_Ifemale_1=1 _Iagegrp_2=0 _Iagegrp_3=0 _Iagegrp_4=0 _Iagegrp_5=0 _Iagegrp_6=1 ///
            > _IageXfem_2_1=0 _IageXfem_3_1=0 _IageXfem_4_1=0 _IageXfem_5_1=0 _IageXfem_6_1=1)
            
            Adjusted predictions
            
            Number of strata =      1                        Number of obs   =      10.335
            Number of PSUs   = 10.335                        Population size = 116.997.257
            Model VCE: Linearized                            Design df       =      10.334
            
            Expression: Linear prediction (Health status), predict(xb(health))
            1._at: _Iagegrp_2    = 0
                   _Iagegrp_3    = 0
                   _Iagegrp_4    = 0
                   _Iagegrp_5    = 0
                   _Iagegrp_6    = 0
                   _Ifemale_1    = 1
                   _IageXfem_2_1 = 0
                   _IageXfem_3_1 = 0
                   _IageXfem_4_1 = 0
                   _IageXfem_5_1 = 0
                   _IageXfem_6_1 = 0
            2._at: _Iagegrp_2    = 1
                   _Iagegrp_3    = 0
                   _Iagegrp_4    = 0
                   _Iagegrp_5    = 0
                   _Iagegrp_6    = 0
                   _Ifemale_1    = 1
                   _IageXfem_2_1 = 1
                   _IageXfem_3_1 = 0
                   _IageXfem_4_1 = 0
                   _IageXfem_5_1 = 0
                   _IageXfem_6_1 = 0
            3._at: _Iagegrp_2    = 0
                   _Iagegrp_3    = 1
                   _Iagegrp_4    = 0
                   _Iagegrp_5    = 0
                   _Iagegrp_6    = 0
                   _Ifemale_1    = 1
                   _IageXfem_2_1 = 0
                   _IageXfem_3_1 = 1
                   _IageXfem_4_1 = 0
                   _IageXfem_5_1 = 0
                   _IageXfem_6_1 = 0
            4._at: _Iagegrp_2    = 0
                   _Iagegrp_3    = 0
                   _Iagegrp_4    = 1
                   _Iagegrp_5    = 0
                   _Iagegrp_6    = 0
                   _Ifemale_1    = 1
                   _IageXfem_2_1 = 0
                   _IageXfem_3_1 = 0
                   _IageXfem_4_1 = 1
                   _IageXfem_5_1 = 0
                   _IageXfem_6_1 = 0
            5._at: _Iagegrp_2    = 0
                   _Iagegrp_3    = 0
                   _Iagegrp_4    = 0
                   _Iagegrp_5    = 1
                   _Iagegrp_6    = 0
                   _Ifemale_1    = 1
                   _IageXfem_2_1 = 0
                   _IageXfem_3_1 = 0
                   _IageXfem_4_1 = 0
                   _IageXfem_5_1 = 1
                   _IageXfem_6_1 = 0
            6._at: _Iagegrp_2    = 0
                   _Iagegrp_3    = 0
                   _Iagegrp_4    = 0
                   _Iagegrp_5    = 0
                   _Iagegrp_6    = 1
                   _Ifemale_1    = 1
                   _IageXfem_2_1 = 0
                   _IageXfem_3_1 = 0
                   _IageXfem_4_1 = 0
                   _IageXfem_5_1 = 0
                   _IageXfem_6_1 = 1
            
            ------------------------------------------------------------------------------
                         |            Delta-method
                         |     Margin   std. err.      t    P>|t|     [95% conf. interval]
            -------------+----------------------------------------------------------------
                     _at |
                      1  |   3,924531   ,0308484   127,22   0,000     3,864062       3,985
                      2  |   3,795212   ,0379889    99,90   0,000     3,720746    3,869677
                      3  |   3,587782   ,0503661    71,23   0,000     3,489055    3,686509
                      4  |   3,261401   ,0472408    69,04   0,000       3,1688    3,354002
                      5  |   3,017406   ,0313813    96,15   0,000     2,955893     3,07892
                      6  |    2,87854   ,0562886    51,14   0,000     2,768204    2,988877
            ------------------------------------------------------------------------------
            
            . marginsplot, name(model3, replace) xlab(1 "20-29" 2 "30-39" 3 "40-49" 4 "50-59" 5 "60-69" 6 "70+", alternate) xtit("Age group") plot1opts(m
            > s(O) color(maroon)) ciopt(color(maroon)) tit(Female) nodraw
            
            Variables that uniquely identify margins: _atopt
            Multiple at() options specified:
                  _atoption=1: _Ifemale_1=1 _Iagegrp_2=0 _Iagegrp_3=0 _Iagegrp_4=0 _Iagegrp_5=0 _Iagegrp_6=0 _IageXfem_2_1=0 _IageXfem_3_1=0
                      _IageXfem_4_1=0 _IageXfem_5_1=0 _IageXfem_6_1=0
                  _atoption=2: _Ifemale_1=1 _Iagegrp_2=1 _Iagegrp_3=0 _Iagegrp_4=0 _Iagegrp_5=0 _Iagegrp_6=0 _IageXfem_2_1=1 _IageXfem_3_1=0
                      _IageXfem_4_1=0 _IageXfem_5_1=0 _IageXfem_6_1=0
                  _atoption=3: _Ifemale_1=1 _Iagegrp_2=0 _Iagegrp_3=1 _Iagegrp_4=0 _Iagegrp_5=0 _Iagegrp_6=0 _IageXfem_2_1=0 _IageXfem_3_1=1
                      _IageXfem_4_1=0 _IageXfem_5_1=0 _IageXfem_6_1=0
                  _atoption=4: _Ifemale_1=1 _Iagegrp_2=0 _Iagegrp_3=0 _Iagegrp_4=1 _Iagegrp_5=0 _Iagegrp_6=0 _IageXfem_2_1=0 _IageXfem_3_1=0
                      _IageXfem_4_1=1 _IageXfem_5_1=0 _IageXfem_6_1=0
                  _atoption=5: _Ifemale_1=1 _Iagegrp_2=0 _Iagegrp_3=0 _Iagegrp_4=0 _Iagegrp_5=1 _Iagegrp_6=0 _IageXfem_2_1=0 _IageXfem_3_1=0
                      _IageXfem_4_1=0 _IageXfem_5_1=1 _IageXfem_6_1=0
                  _atoption=6: _Ifemale_1=1 _Iagegrp_2=0 _Iagegrp_3=0 _Iagegrp_4=0 _Iagegrp_5=0 _Iagegrp_6=1 _IageXfem_2_1=0 _IageXfem_3_1=0
                      _IageXfem_4_1=0 _IageXfem_5_1=0 _IageXfem_6_1=1
            
            . graph combine model1 model2 model3, ycommon row(1)
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	49.6 KB
ID:	1705381

            Comment

            Working...
            X