Announcement

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

  • the use of -marginsplot- after running margins

    Hi all,

    I am running a model using -xtmixed- and then I use -margins- to get the predicted values I need. Below is the command of my model using xtmixed.

    Code:
    xtmixed READING age race i.race##c.age || schoolid:, || childid: age, mle cov(un) var pweight(C1_7FC0)
    then I used margins
    Code:
    margins race, at(age=(-0.69 -0.51 -0.35 -0.36 -0.22 -0.11 0 0.41 0.69 0.92 1.10 1.25 1.39 1.50 1.61 1.70 1.79 1.87 1.95 2.01 2.08 2.14 2.19 2.25 2.30)) vsquish predict() post
    As you may notice, the age variable is a continuous variable and I have to get the predicted value of reading performance at the specified 25 age values like the above. My question is how or whether can I still use marginsplot to make graph out of the predicted values that stata gives me? I tried the marginsplot command right after margins command but stata spit out an error message like below:

    PHP Code:
    invalid at() dimension information;
    using variable race as a factor variable and a regular variable is not supported 
    I guess it's the problem of my age time points, right? But if I have to specify the age to the 25 values like above, can marginsplot be used afterwards?

    Thanks!
    Last edited by Man Yang; 09 Feb 2017, 19:40.

  • #2
    A couple of things:

    Unless you have an older version of State, you should probably use mixed instead of xtmixed.

    It would help if you showed all your commands and output, or at least the exact error message.

    You have both race and i.race in the xtmixed command. To be safe, I think the command should be

    xtmixed READING age i.race i.race##c.age || schoolid:, || childid: age, mle cov(un) var pweight(C1_7FC0)

    As it is, I think you are treating race as both a continuous and categorical var.

    In your at you have

    -0.69 -0.51 -0.35 -0.36 -0.22

    I suspect the -.36 is an error and maybe not going having values in a consistent sequence of low to high causes problems. That might even be my first guess as to what the problem is.

    I am a big fan of Patrick Royston's mcp command (available from SSC). For example,

    Code:
    webuse nlswork,clear
    mixed ln_w i.race grade age c.age#c.age ttl_exp tenure c.tenure#c.tenure || id:
    mcp age race
    For a highlights discussion of mcp see

    http://www3.nd.edu/~rwilliam/xsoc73994/Margins03.pdf

    The whole paper is at

    http://www.stata-journal.com/article...article=gr0056

    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    StataNow Version: 19.5 MP (2 processor)

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

    Comment


    • #3
      Here is the image mcp generated in my last example.
      Click image for larger version

Name:	mcp.png
Views:	1
Size:	77.7 KB
ID:	1373673
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      StataNow Version: 19.5 MP (2 processor)

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

      Comment


      • #4
        Thanks for your reply Richard!! Your suggestion to replace race with i.race after xtmixed helps and now stata no longer gives me error message after marginsplot, which is a good sign. Another question is that I was able to get a graph after marginsplot, but why the plots are straight? They are supposed to be curve lines like the one you presented in your example. Is it because when using -margins-, I added predict() at the end? Or is it because of the x axis? Maybe I should extend x axis to include more age points? One thing I need to clarify is that the age variable I put in the model is the log-transferred format of my original age variable, which is a categorical variable (e.g., 5,6,7,8,9,...). Ideally I would love to cover age 5 to 16 on x axis, do you know whether that's doable if I am using the log-transferred form of age in my model? If yes, how? Thank you!
        Click image for larger version

Name:	Graph.jpg
Views:	1
Size:	82.1 KB
ID:	1373676


        Last edited by Man Yang; 09 Feb 2017, 20:55.

        Comment


        • #5
          Why should the lines be curved? You have an interaction with race and age, but you don't have an age^2 term.

          As for the other Qs, in your at statement you can plug in whatever values you want, so you could cover the range that corresponds to ages 5 to 16. (But if you took logs of 5 to 16, how did you get negative numbers for age?)

          Also, mcp shows how to do this cool trick where you use log age in the model, but then the graph can show non-logged age.
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          StataNow Version: 19.5 MP (2 processor)

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

          Comment


          • #6
            Hi Richard, thanks again for your reply. The reason why I have negative value is because when transferring the original age variable, I firstly subtract 5 from each of them and then take the log of the difference. The reason why I say it should be curve lines is because if I graph the predicted value using the original age variable (in x axis) using EXCEL, it shows as curve lines. Each log-transferred age has its matching original age value and that original age value is what I take into the graph when using EXCEL, not the age variable I throw in the model (LNAGE5). Does it make sense?

            So can I take your explanation as if I use the log-transferred age variable to plot using stata, there is no way I can get curve lines?

            I did see that mcp has function to graph using log-transformed value. Below is the command I use

            Code:
            xtmixed READ LNAGE5 i.race i.race##c.LNAGE5 || SCHID:, || CHILDID:LNAGE5, mle cov(un) var pweight(C1_7FC0)
            estimates store results
            
            margins race, at(LNAGE5=(-0.69 -0.51 -0.36 -0.22 -0.11 0 0.41 0.69 0.92 1.10 1.25 1.39 1.50 1.61 1.70 1.79 1.87 1.95 2.01 2.08 2.14 2.19 2.25 2.30)) vsquish predict() post
            
            sum AGEYRS_5
            range w1 r(min) r(max) 20
            gen logw1=log(w1)
            mcp AGEYRS_5 (LNAGE5), var1(w1 (logw1)) show
            where AGEYRS_5 is the original age value-5. But stata gives me an error message

            PHP Code:
            mcp AGEYRS_5(LNAGE5), var1(w1 (logw1)) show
            LNAGE5 not in model
            r
            (111); 
            Any clues for me please?

            Thanks!

            Comment


            • #7
              Why subtract 5??? I don't think you should do that if you are going to then log the variable.

              On your margins command you used the -post- option. That means the results from the margins command replaced the estimates from xtmixed. Get rid of the post option (or get rid of the margins command altogether) and see if it works then.
              -------------------------------------------
              Richard Williams, Notre Dame Dept of Sociology
              StataNow Version: 19.5 MP (2 processor)

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

              Comment


              • #8
                Hi Richard, thanks! I followed your suggestions but stata gives me another error

                PHP Code:
                mcp AGEYRS_5 (LNAGE5), var1(w1 (logw1)) show
                margins 
                atLNAGE5=() ) 
                There was a problem executing -margins-.
                The command issued was essentially as follows:
                margins atLNAGE5=() ) 
                r(122); 
                All I did is to drop the post option...

                Comment


                • #9
                  Do frequencies on w1 and logw1. I am guessing something got screwed up when you computed them. If still having problems reshow all your commands so we can see exactly what you are doing now.
                  -------------------------------------------
                  Richard Williams, Notre Dame Dept of Sociology
                  StataNow Version: 19.5 MP (2 processor)

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

                  Comment

                  Working...
                  X