Announcement

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

  • How to run a structural equation modeling in Stata with a panel data set using fixed effects?

    Dear Stata users,

    I'm trying to run a SEM model in Stata 18 for a panel data set (that you can download here). I would like to use this information to understand the indirect and direct effects in this model, by using M1 and M2 variables as mediation variables, the IV, CV1, CV2, and CV3 as independent variables, and the "DV" as dependent variable. Since it is a panel data, I would like to account for unit (id_dum) and time (year_dum) fixed effects. I have tried so many things without success.


    Code:
    use "...\PanelData.dta", clear
    xtset id timevar, delta(1)
    qui tabulate id, generate(id_dum)
    qui tabulate timevar, generate(year_dum)
    My 1st try was with the sem command, which works perfect. But it doesn't include the unit and time fixed effects:
    Code:
    sem (DV <- M1 M2 IV CV*, ) (M1 <- IV CV*, ) (M2 <- IV CV*, ), vce(robust) nocapslatent
    Therefore, my 2nd try was including unit and time fixed effects. But I got an error message related to the fitting model:
    Code:
    sem (DV <- MV1 MV2 IV CV* id_dum* year_dum*, ) ///
    (MV1 <- IV CV* id_dum* year_dum*, )  ///
    (MV2 <- IV CV* id_dum* year_dum*, ), vce(robust) nocapslatent
    Fitting target model:
            __sem_d2L_dth2():  3900  unable to allocate real <tmp>[111155,111155]
         _sem_eval_ml_d2_g():     -  function returned error
            __sem_d2L_dth2():  3900  out of memory
         _sem_eval_ml_d2_g():     -  function returned error
           _sem_eval_ml_d2():     -  function returned error
          opt__calluser1_d():     -  function returned error
           opt__eval_nr_d2():     -  function returned error
                 opt__eval():     -  function returned error
    opt__looputil_iter0_common():     -  function returned error
    opt__looputil_iter0_nr():     -  function returned error
              opt__loop_nr():     -  function returned error
                 opt__loop():     -  function returned error
                 _optimize():     -  function returned error
                  _sem_fit():     -  function returned error
                st_sem_fit():     -  function returned error
                     <istmt>:     -  function returned error
    My 3rd try was using the xtdpdml command, but I got an error saying there is an invalid name:
    Code:
    xtdpdml (MV1 <- IV CV* id_dum* year_dum*) ///
    (MV2 <- IV CV* id_dum* year_dum*) ///
    (DV <- MV1 MV2 IV CV* id_dum* year_dum*), ///
    lags(0) iv(IV CV1 CV2 CV3) fe twostep robust
        
    < invalid name
    r(198);
    Does anyone know how to properly code a SEM model (that considers a mediation variable) with unit and time fixed effects in Stata? Any help is appreciated!
    Last edited by Adriana Castillo Castillo; 24 Mar 2025, 08:55.

  • #2
    The problem with your second solution is that you have to generate a covariance matrix with rows and columns for every year and id, so you try to generate a 111155 by 111155 matrix (so 12,355,434,025 cells) and your computer can't allocate that much RAM. I like the advice from Andrew Musau in #5 of this thread where he suggests using the xtdata command and gsem.

    As for your third solution, I could be wrong, but I don't think you can use the SEM syntax in xtdpdml. From what I can see it seems to use a different syntax to express the models.

    Comment


    • #3
      Yes, that isn't correct syntax for xtdpdml. See the help file. Or, view the article at

      https://journals.sagepub.com/doi/pdf...867X1801800201
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      StataNow Version: 19.5 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://www3.nd.edu/~rwilliam

      Comment


      • #4
        Thank you Daniel Schaefer and Richard Williams for your answers. If I understand correctly, then the code will be as follows?:

        Code:
        use "...\PanelData.dta", clear
        xtset id timevar
        xtreg DV IV MV* CV*, fe
        xtdata, fe clear
        gsem (DV <- MV1 MV2 IV CV* , ) (MV1 <- IV CV* , ) (MV2 <- IV CV* , ), vce(robust) nocapslatent
        Sorry, but do you mind please telling me if there is a similar command to "gsem" for this particular context that I can use to calculate the direct/indirect/total effects? When I try to do it after using "gsem", I get the following error message:
        Code:
        estat teffects
        estat teffects not valid
        r(321);
        Many thanks in advance!
        Last edited by Adriana Castillo Castillo; 28 Mar 2025, 06:25.

        Comment


        • #5
          Daniel's point in #2 is that you can include fixed effects in linear models either by using indicators or the within transformation (which is what xtdata, fe does). Therefore, there is no need to switch from sem to gsem. Use the within transformation for your unit fixed effects, and then introduce the time effects using indicators. Below, you’ll notice that the coefficients and standard errors of our variables of interest (highlighted) remain the same whether I include the FEs using indicators or apply xtdata, fe.

          Code:
          webuse grunfeld, clear
          xi: sem (invest <- mvalue kstock i.company i.year,) (mvalue <- kstock i.company i.year,), vce(robust)
          
          webuse grunfeld, clear
          xtset company year
          xtdata, fe
          xi: sem (invest <- mvalue kstock i.year, ) (mvalue <- kstock i.year, ), vce(robust)
          Res.:

          Code:
          . xi: sem (invest <- mvalue kstock i.company i.year,) (mvalue <- kstock i.company i.year,), vce(robust)
          i.company         _Icompany_1-10      (naturally coded; _Icompany_1 omitted)
          i.year            _Iyear_1935-1954    (naturally coded; _Iyear_1935 omitted)
          
          Endogenous variables
            Observed: invest mvalue
          
          Exogenous variables
            Observed: kstock _Icompany_2 _Icompany_3 _Icompany_4 _Icompany_5 _Icompany_6 _Icompany_7 _Icompany_8 _Icompany_9 _Icompany_10
                      _Iyear_1936 _Iyear_1937 _Iyear_1938 _Iyear_1939 _Iyear_1940 _Iyear_1941 _Iyear_1942 _Iyear_1943 _Iyear_1944
                      _Iyear_1945 _Iyear_1946 _Iyear_1947 _Iyear_1948 _Iyear_1949 _Iyear_1950 _Iyear_1951 _Iyear_1952 _Iyear_1953
                      _Iyear_1954
          
          Fitting target model:
          Iteration 0:  Log pseudolikelihood = -3417.2766  
          Iteration 1:  Log pseudolikelihood = -3417.2766  
          
          Structural equation model                                  Number of obs = 200
          Estimation method: ml
          
          Log pseudolikelihood = -3417.2766
          
          --------------------------------------------------------------------------------
                         |               Robust
                         | Coefficient  std. err.      z    P>|z|     [95% conf. interval]
          ---------------+----------------------------------------------------------------
          Structural     |
            invest       |
                  mvalue |   .1177158   .0176752     6.66   0.000     .0830731    .1523585
                  kstock |   .3579163   .0501345     7.14   0.000     .2596544    .4561781
             _Icompany_2 |   207.0542   42.27979     4.90   0.000     124.1873    289.9211
             _Icompany_3 |  -135.2308    40.2181    -3.36   0.001    -214.0568    -56.4048
             _Icompany_4 |    95.3538   62.08779     1.54   0.125    -26.33603    217.0436
             _Icompany_5 |  -5.438636   65.75705    -0.08   0.934    -134.3201    123.4428
             _Icompany_6 |   102.8886   66.71839     1.54   0.123    -27.87703    233.6542
             _Icompany_7 |   51.46657   68.34067     0.75   0.451    -82.47868    185.4118
             _Icompany_8 |   67.49048   63.05639     1.07   0.284    -56.09777    191.0787
             _Icompany_9 |   30.21752   65.31009     0.46   0.644     -97.7879    158.2229
            _Icompany_10 |   126.8371    74.4061     1.70   0.088    -18.99619    272.6704
             _Iyear_1936 |  -19.19741   18.81824    -1.02   0.308    -56.08049    17.68567
             _Iyear_1937 |  -40.69001   23.24907    -1.75   0.080    -86.25734    4.877328
             _Iyear_1938 |   -39.2264   21.10693    -1.86   0.063    -80.59523    2.142419
             _Iyear_1939 |  -69.47029   27.07226    -2.57   0.010    -122.5309   -16.40964
             _Iyear_1940 |  -44.23507    18.8221    -2.35   0.019     -81.1257   -7.344445
             _Iyear_1941 |  -18.80446   18.48915    -1.02   0.309    -55.04252     17.4336
             _Iyear_1942 |  -21.13979   19.16364    -1.10   0.270    -58.69984    16.42026
             _Iyear_1943 |  -42.97762    19.3607    -2.22   0.026     -80.9239   -5.031338
             _Iyear_1944 |  -43.09876   22.45687    -1.92   0.055    -87.11342    .9158871
             _Iyear_1945 |  -55.68303   21.39705    -2.60   0.009    -97.62048   -13.74559
             _Iyear_1946 |  -31.16928   21.94335    -1.42   0.155    -74.17747     11.8389
             _Iyear_1947 |  -39.39223    21.0543    -1.87   0.061     -80.6579    1.873432
             _Iyear_1948 |  -43.71651   25.20204    -1.73   0.083    -93.11161    5.678582
             _Iyear_1949 |   -73.4951   24.02181    -3.06   0.002     -120.577   -26.41321
             _Iyear_1950 |  -75.89611    24.6479    -3.08   0.002    -124.2051    -27.5871
             _Iyear_1951 |   -62.4809   30.30456    -2.06   0.039    -121.8767   -3.085059
             _Iyear_1952 |  -64.63233   34.24109    -1.89   0.059    -131.7436    2.478976
             _Iyear_1953 |  -67.71796   35.92463    -1.89   0.059    -138.1289    2.693013
             _Iyear_1954 |  -93.52622   27.01574    -3.46   0.001    -146.4761   -40.57635
                   _cons |  -86.90019   69.51771    -1.25   0.211    -223.1524    49.35202
            -------------+----------------------------------------------------------------
            mvalue       |
                  kstock |   .4747497   .2310142     2.06   0.040     .0219702    .9275292
             _Icompany_2 |  -2194.158   168.2658   -13.04   0.000    -2523.953   -1864.363
             _Icompany_3 |  -2274.652   163.2499   -13.93   0.000    -2594.616   -1954.688
             _Icompany_4 |  -3390.352    184.642   -18.36   0.000    -3752.243    -3028.46
             _Icompany_5 |  -4025.622   150.1327   -26.81   0.000    -4319.877   -3731.368
             _Icompany_6 |  -3655.645   191.7502   -19.06   0.000    -4031.468   -3279.822
             _Icompany_7 |  -4025.731   163.0334   -24.69   0.000     -4345.27   -3706.191
             _Icompany_8 |  -3395.748   190.8277   -17.79   0.000    -3769.764   -3021.733
             _Icompany_9 |  -3833.779   163.7844   -23.41   0.000     -4154.79   -3512.767
            _Icompany_10 |    -3957.9   204.1307   -19.39   0.000    -4357.989   -3557.812
             _Iyear_1936 |   366.4578   123.5205     2.97   0.003     124.3621    608.5535
             _Iyear_1937 |   625.5599   180.5886     3.46   0.001     271.6126    979.5071
             _Iyear_1938 |   103.0697   168.6405     0.61   0.541    -227.4595    433.5989
             _Iyear_1939 |   328.5231   113.9313     2.88   0.004     105.2217    551.8244
             _Iyear_1940 |   380.7274   118.6411     3.21   0.001     148.1951    613.2597
             _Iyear_1941 |   326.1077   119.8332     2.72   0.007     91.23893    560.9765
             _Iyear_1942 |   106.1918   141.0124     0.75   0.451    -170.1874    382.5709
             _Iyear_1943 |   215.8596   106.6296     2.02   0.043     6.869337    424.8498
             _Iyear_1944 |     249.24   112.5446     2.21   0.027     28.65659    469.8233
             _Iyear_1945 |    358.131   128.3853     2.79   0.005     106.5005    609.7616
             _Iyear_1946 |   415.1337    126.436     3.28   0.001     167.3237    662.9437
             _Iyear_1947 |   103.2275   134.7287     0.77   0.444    -160.8359    367.2908
             _Iyear_1948 |   53.40396   155.7235     0.34   0.732    -251.8085    358.6165
             _Iyear_1949 |   57.19508   142.5177     0.40   0.688    -222.1344    336.5246
             _Iyear_1950 |   107.1275   143.9059     0.74   0.457    -174.9228    389.1778
             _Iyear_1951 |   327.6631   132.6963     2.47   0.014     67.58324     587.743
             _Iyear_1952 |   345.9053   135.9387     2.54   0.011     79.47039    612.3402
             _Iyear_1953 |    531.716   140.6274     3.78   0.000     256.0913    807.3407
             _Iyear_1954 |   453.8139   174.4327     2.60   0.009     111.9322    795.6956
                   _cons |   3753.248   206.9347    18.14   0.000     3347.663    4158.833
          ---------------+----------------------------------------------------------------
            var(e.invest)|   2260.735   383.4235                       1621.38    3152.206
            var(e.mvalue)|   70741.88   12433.48                      50126.89    99834.91
          --------------------------------------------------------------------------------
          
          .
          
          .
          . xtdata, fe
          
          .
          . xi: sem (invest <- mvalue kstock i.year, ) (mvalue <- kstock i.year, ), vce(robust)
          i.year            _Iyear_1935-1954    (naturally coded; _Iyear_1935 omitted)
          
          Endogenous variables
            Observed: invest mvalue
          
          Exogenous variables
            Observed: kstock _Iyear_1936 _Iyear_1937 _Iyear_1938 _Iyear_1939 _Iyear_1940 _Iyear_1941 _Iyear_1942 _Iyear_1943 _Iyear_1944
                      _Iyear_1945 _Iyear_1946 _Iyear_1947 _Iyear_1948 _Iyear_1949 _Iyear_1950 _Iyear_1951 _Iyear_1952 _Iyear_1953
                      _Iyear_1954
          
          Fitting target model:
          Iteration 0:  Log pseudolikelihood = -3165.7724  
          Iteration 1:  Log pseudolikelihood = -3165.7724  (backed up)
          
          Structural equation model                                  Number of obs = 200
          Estimation method: ml
          
          Log pseudolikelihood = -3165.7724
          
          -------------------------------------------------------------------------------
                        |               Robust
                        | Coefficient  std. err.      z    P>|z|     [95% conf. interval]
          --------------+----------------------------------------------------------------
          Structural    |
            invest      |
                 mvalue |   .1177158   .0176752     6.66   0.000     .0830731    .1523585
                 kstock |   .3579163   .0501345     7.14   0.000     .2596544    .4561781
            _Iyear_1936 |  -19.19741   18.81824    -1.02   0.308    -56.08049    17.68567
            _Iyear_1937 |  -40.69001   23.24907    -1.75   0.080    -86.25734    4.877328
            _Iyear_1938 |   -39.2264   21.10693    -1.86   0.063    -80.59523    2.142419
            _Iyear_1939 |  -69.47029   27.07226    -2.57   0.010    -122.5309   -16.40964
            _Iyear_1940 |  -44.23507    18.8221    -2.35   0.019     -81.1257   -7.344445
            _Iyear_1941 |  -18.80446   18.48915    -1.02   0.309    -55.04252     17.4336
            _Iyear_1942 |  -21.13979   19.16364    -1.10   0.270    -58.69984    16.42026
            _Iyear_1943 |  -42.97762    19.3607    -2.22   0.026     -80.9239   -5.031338
            _Iyear_1944 |  -43.09876   22.45687    -1.92   0.055    -87.11342    .9158871
            _Iyear_1945 |  -55.68303   21.39705    -2.60   0.009    -97.62048   -13.74559
            _Iyear_1946 |  -31.16928   21.94335    -1.42   0.155    -74.17747     11.8389
            _Iyear_1947 |  -39.39223    21.0543    -1.87   0.061     -80.6579    1.873432
            _Iyear_1948 |  -43.71651   25.20204    -1.73   0.083    -93.11161    5.678582
            _Iyear_1949 |   -73.4951   24.02181    -3.06   0.002     -120.577   -26.41321
            _Iyear_1950 |  -75.89611    24.6479    -3.08   0.002    -124.2051    -27.5871
            _Iyear_1951 |   -62.4809   30.30456    -2.06   0.039    -121.8767   -3.085059
            _Iyear_1952 |  -64.63233   34.24109    -1.89   0.059    -131.7436    2.478976
            _Iyear_1953 |  -67.71796   35.92463    -1.89   0.059    -138.1289    2.693013
            _Iyear_1954 |  -93.52622   27.01574    -3.46   0.001    -146.4761   -40.57635
                  _cons |  -32.83631    20.8187    -1.58   0.115     -73.6402    7.967586
            ------------+----------------------------------------------------------------
            mvalue      |
                 kstock |   .4747497   .2310142     2.06   0.040     .0219702    .9275292
            _Iyear_1936 |   366.4578   123.5205     2.97   0.003     124.3621    608.5535
            _Iyear_1937 |   625.5599   180.5886     3.46   0.001     271.6126    979.5071
            _Iyear_1938 |   103.0697   168.6405     0.61   0.541    -227.4595    433.5989
            _Iyear_1939 |   328.5231   113.9313     2.88   0.004     105.2217    551.8244
            _Iyear_1940 |   380.7274   118.6411     3.21   0.001     148.1951    613.2597
            _Iyear_1941 |   326.1077   119.8332     2.72   0.007     91.23893    560.9765
            _Iyear_1942 |   106.1918   141.0124     0.75   0.451    -170.1874    382.5709
            _Iyear_1943 |   215.8596   106.6296     2.02   0.043     6.869337    424.8498
            _Iyear_1944 |     249.24   112.5446     2.21   0.027     28.65659    469.8233
            _Iyear_1945 |    358.131   128.3853     2.79   0.005     106.5005    609.7616
            _Iyear_1946 |   415.1337    126.436     3.28   0.001     167.3237    662.9437
            _Iyear_1947 |   103.2275   134.7287     0.77   0.444    -160.8359    367.2908
            _Iyear_1948 |   53.40396   155.7235     0.34   0.732    -251.8085    358.6165
            _Iyear_1949 |   57.19508   142.5177     0.40   0.688    -222.1344    336.5246
            _Iyear_1950 |   107.1275   143.9059     0.74   0.457    -174.9228    389.1778
            _Iyear_1951 |   327.6631   132.6963     2.47   0.014     67.58324     587.743
            _Iyear_1952 |   345.9053   135.9387     2.54   0.011     79.47039    612.3402
            _Iyear_1953 |    531.716   140.6274     3.78   0.000     256.0913    807.3407
            _Iyear_1954 |   453.8139   174.4327     2.60   0.009     111.9322    795.6956
                  _cons |   677.8893   107.3022     6.32   0.000     467.5809    888.1978
          --------------+----------------------------------------------------------------
           var(e.invest)|   2260.735   383.4235                       1621.38    3152.206
           var(e.mvalue)|   70741.88   12433.48                      50126.89    99834.91
          -------------------------------------------------------------------------------
          Last edited by Andrew Musau; 28 Mar 2025, 07:19.

          Comment


          • #6
            An additional point: vce(robust) in sem is not equivalent to vce(cluster panelvar) as is the case with panel data estimators. With panel data, you need the latter.

            Comment


            • #7
              Many many thanks, Andrew Musau ! If you don't mind, I would like to ask 2 additional and last questions.

              Based on your answer, here is the line I'm running:


              Code:
              xi: sem (DV <- MV1 MV2 IV CV* c.treat_cont##c.treat_time i.timevar) ///
              (MV1 <- IV CV* c.treat_cont##c.treat_time i.timevar) ///
              (MV2 <- IV CV* c.treat_cont##c.treat_time i.timevar) [iw=weights_T], vce(cluster id) nocapslatent
              I got an error message related to the use of interactions:
              Code:
              interaction operators not allowed
              r(101);
              I tried to solve this error by creating the interaction of both variables: gen treat_interaction = treat_cont * time_treat, and then use this information into the code. Is this solution valid?:
              Code:
              xi: sem (DV <- MV1 MV2 IV CV*  treat_cont treat_time treat_interaction i.timevar) ///
              (MV1 <- IV CV* treat_cont treat_time treat_interaction i.timevar) ///
              (MV2 <- IV CV* treat_cont treat_time treat_interaction i.timevar) [iw=weights_T], vce(cluster id) nocapslatent
              Last edited by Adriana Castillo Castillo; 28 Mar 2025, 13:13.

              Comment


              • #8
                It seems that the "sem" command doesn't allow to include weights into the estimations?
                That is not correct. What Stata is objecting to in your code is placing -[aw=weights_T]- in the options part of the command. In all Stata estimation commands, the weights have to go before the comma. That said, the one kind of weight that -sem- does not support is -aweight-s. Are you sure that -aweight-ing is appropriate for your data? From -help weight-:
                3. aweights, or analytic weights, are weights that are inversely proportional to the variance of an observation; that is, the variance of the jth observation is assumed to be sigma^2/w_j, where w_j are the weights. Typically, the observations represent averages and the weights are the number of elements that gave rise to the average.

                Comment


                • #9
                  You're absolutely right, Clyde Schechter. Indeed, the weights were before the comma, and it worked perfect now. And also, you made me realize "aweights" was not appropriate for my data. Many thanks! I will edit my post so only the error about the interaction is seen. Btw, do you have any idea if my approach for the interaction is correct?

                  Comment


                  • #10
                    I will edit my post so only the error about the interaction is seen.
                    It would be better not to do that. If you do that, people following the thread will not understand why #8 was even written as you will have removed what it responds to.

                    do you have any idea if my approach for the interaction is correct?
                    If by this you mean, is the code you showed for creating and using the interaction in -xi: sem-, the answer is, yes, it looks right to me. If you are asking whether I think modeling this interaction is appropriate to your situation, I can only say that I have only perused the thread lightly and don't feel I know the situation well enough to comment on that either way.

                    Comment


                    • #11
                      Many thanks, Clyde Schechter . I'm sorry. I already edited the post. I'll later try to leave it as it was originally.

                      Comment


                      • #12
                        It seems that I can't edit a previous post, so I'm posting here the error I got initially in #7 with respect to the weights:

                        Code:
                        xi: sem (DV <- MV1 MV2 IV CV* treat_cont treat_time treat_interaction i.timevar) ///
                        (MV1 <- IV CV* treat_cont treat_time treat_interaction i.timevar) ///
                        (MV2 <- IV CV* treat_cont treat_time treat_interaction i.timevar), ///
                        vce(cluster id) nocapslatent [aw=weights_T]
                        option [ not allowed
                        r(198);
                        As Clyde Schechter kindly made me noticed, the weights option should go after the comma. And also, the "aweights" is not supported and is not appropriate for my case.

                        Comment

                        Working...
                        X