Announcement

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

  • Combining Coefplot & Scatterplot

    Hi all,

    I have several models, and I use coefplot to plot a single coefficient from these models. Is it possible to do the same with two variables, one of which in the x axis, and the other one is in the y axis?

    I would like to have something like this (from the sepscatter command), but with multiple models (each dot = 1 model) rather than the usual scatterplot setup.

    Click image for larger version

Name:	image_211.png
Views:	1
Size:	30.8 KB
ID:	1545850

    Thanks in advance!

  • #2
    Your question is not clear. In a coefficient plot, one axis shows the value of the coefficient and a second identifies the coefficient. This is different from a scatter plot where you are graphing two continuous variables. If your question is how to make a coefficient plot using the same variable from multiple models, then this can be achieved by appropriately saving the coefficients and their confidence intervals in a matrix. Below I use coefplot from SSC, where I am interested in the coefficient on weight.

    Code:
    sysuse auto, clear
    regress mpg price weight
    mat R= nullmat(R)\((r(table)["b", "weight"])\ (r(table)["ll".."ul", "weight"]))'
    regress mpg price weight disp
    mat R= nullmat(R)\((r(table)["b", "weight"])\ (r(table)["ll".."ul", "weight"]))'
    regress mpg price weight i.rep78
    mat R= nullmat(R)\((r(table)["b", "weight"])\ (r(table)["ll".."ul", "weight"]))'
    *NEED DISTINCT COLUMN NAMES
    clear
    mat R= R'
    svmat R
    rename R* weight*
    mkmat weight*, mat(R)
    *GRAPH
    coefplot mat(R), ci((2 3)) scheme(s1color)
    Res.:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	33.2 KB
ID:	1545874

    Last edited by Andrew Musau; 09 Apr 2020, 17:31.

    Comment


    • #3
      Thanks Andrew!

      Sorry for the ambiguity. Let me reclarify it. I basically want to use two of these variables, let's say weight1 and weight 2, and use weight1 as the x-axis and weight2 as the y-axis. For example:

      Model 1 Coefficients:
      weight1=1.00
      weight2=4.00

      Model 2 Coefficients:
      weight1=3.00
      weight2=5.00

      Then I want to do a graph where there is a dot in the point x=1 y=4 (called Model 1) and another dot in the point x=3 y=5 (called Model 2) and so on and on.

      Comment


      • #4
        Then it's just a simple scatter plot. You need to have your coefficients in a dataset like below and then have it such that you can define an x and y variable.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str7 var float coef str7 model
        "weight1" 1 "Model 1"
        "weight2" 2 "Model 1"
        "weight1" 3 "Model 2"
        "weight2" 5 "Model 2"
        end
        
        reshape wide coef, i(model) j(var, string)
        scatter coefweight1 coefweight2, mlab(model) scheme(s1color) xsc(r(1.8 5.3)) ysc(r(0.9 3.5))
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	12.4 KB
ID:	1545878

        Comment


        • #5
          This is great! Thank you very much, it worked perfectly!

          Comment

          Working...
          X