Announcement

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

  • marginsplot mlabel format values

    Dear Statalister,
    my first post, so forgive me if I get something wrong. My problem is this. By using e.g.

    xtreg life_sat i.cat_min_age_at_child, vce(robust)
    margins i.cat_min_age_at_child, atmeans cformat(%3.2f)

    I can test how satisfied people are depending on the age at which they had their first child. Margins appear in a nice format, e.g. 7. 40 as the life satisfaction that people have when their first child was born when they 32 etc... In my marginsplot, it is exactly these values that I would use as marker labels. I am able to do so with the following command:

    marginsplot, plotopts(connect(direct) mlabel(_margin)) recastci(rarea)

    also possible:
    marginsplot, plotopts(connect(direct) mlabel(y)) recastci(rarea)

    However, the marker label for 32 then reads 7.399988 (the margins are not in a nice format).

    The obvious solutions, e.g.
    marginsplot, plotopts(connect(direct) mlabel(y, format(%3.2f)) recastci(rarea)
    or
    marginsplot, plotopts(connect(direct) mlabel(y) mlabstyle(%3.2f)) recastci(rarea)

    have no effect. I cannot even change the format or the marker label in the graph editor. It looks like this:



    Click image for larger version

Name:	Graph.png
Views:	1
Size:	47.4 KB
ID:	1474244


    Is there any way to have the marker label displayed in a more "readable" format, such as %3.2f?

    Thanks for you help!

    Martin

  • #2
    Welcome to Statalist.

    Some experimentation convinces me that numeric marker labels are displayed using the format associated with the variable in the dataset. (I find it telling that the documentation for mlabel suggests that marker labels are usually string variables.)

    Of course, the problem you have is that marginsplot is generating your prediction as a temporary variable, perhaps even in a temporary dataset, so there is no mechanism for you to adjust its formatting from the default.

    That you cannot change the formatting in the Graph Editor suggests to me that there is no option you've overlooked for specifying a format for the numeric prediction.

    I do not see a way to accomplish what you want using mlabel.

    Comment


    • #3

      You can see the data set that marginsplot is using by calling serset use:

      Code:
      clear*
      webuse grunfeld,clear
      reg invest i.com kst
      margins i.com, atmeans
      marginsplot, plotopts(connect(direct) mlabel(_margin) ) recastci(rarea)  
      // graph save Graph "C:\Users\Scott\Desktop\Graph.gph", replace
      // graph use "C:\Users\Scott\Desktop\Graph.gph"
      serset use
      rename __00 company
      format _margin %5.1fc
      l
      twoway connected _margin com  , mlabel(_margin, ) mlabp(12)  /// 
          || rarea _ci_u _ci_l com , color(gs6%50) /// 
          xtitle(company) ytitle(Linear Prediction)  /// 
          legend(off) xlabel(1(1)10) /// 
          title(Adjusted Predictions of company with 95% CIs) /// 
          name(gr1, replace)

      Comment


      • #4
        Dear Scott,
        this totally worked. Opening the (implicit) file that marginsplot creates, changing the format of the variable and then plotting it as a twoway graph together with the confidence interval seems a bit cumbersome, but it works (and as far as I can tell is the only way to plot the data in a desired format). So thank you very much indeed! Many thanks also to William, glad we found a workaround!

        Comment

        Working...
        X