Announcement

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

  • graph with range in x axis

    Dear All, Suppose that I have this code
    Code:
    sysuse auto, clear
    twoway (scatter mpg weight if weight < 2500) (scatter mpg weight if weight > 2500) ///
    (lfit mpg weight if weight < 2500) (lfit mpg weight if weight > 2500), ///
    xline(2500) legend(off)
    which generates
    Click image for larger version

Name:	auto.png
Views:	1
Size:	72.8 KB
ID:	1591302


    My question is that, how can I obtain a graph with weight < 3,500 only (x axis)? Any suggestions are appreciated.
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

  • #2
    Is this want you want?
    Code:
    sysuse auto, clear
    twoway (scatter mpg weight if weight < 2500) (scatter mpg weight if inrange(weight,2500,3500)) ///
    (lfit mpg weight if weight < 2500) (lfit mpg weight,range(2500 3500)), ///
    xline(2500) xlabel(2000(500)3500) legend(off)
    Attached Files
    Best regards.

    Raymond Zhang
    Stata 17.0,MP

    Comment


    • #3
      This leaves the question open of whether you want:

      1. The linear fit for values above 2500, but only drawn up to 3500.

      2. The linear fit for value above 2500 and below 3500.

      which in general are not the same.

      Comment


      • #4
        Dear Raymond, Thanks for your suggestion. However, as indicated by Nick, this is not what I wanted.
        Ho-Chuan (River) Huang
        Stata 17.0, MP(4)

        Comment


        • #5
          Dear Nick, My purpose is, as indicated by you, 1. The linear fit for values above 2500, but only drawn up to 3500. That is, I have to use the "whole" data set to draw the graph, but only want to show the graph up to 3,500. Any suggestions?
          Ho-Chuan (River) Huang
          Stata 17.0, MP(4)

          Comment


          • #6
            I think this is what you want:

            Code:
            clear *
            sysuse auto
            reg mpg weight if weight>2500
            predict yhat
            twoway (scatter mpg weight if weight < 2500) (scatter mpg weight if weight > 2500 & weight<3500) ///
            (lfit mpg weight if weight < 2500) (line yhat weight if weight > 2500 & weight<3500), ///
            xline(2500) legend(off) yla(10(10)40)
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	47.1 KB
ID:	1591473

            Last edited by Ali Atia; 26 Jan 2021, 17:06.

            Comment


            • #7
              Dear Ali, Thanks for the suggestion. Yes, the figure is what I had in mind. However, the problem is that the graph above is generated by some other (net install rdrobust, from(https://raw.githubusercontent.com/rd...t/master/stata) replace
              ) commands (According to your suggestion, I guess I have to modify the rdplot.ado file) to mofigy). I wonder if there is a straightforward way to do what I wanted. Since l am leaving my office, if needed, I can post an example with code/data/example/graph later.
              Ho-Chuan (River) Huang
              Stata 17.0, MP(4)

              Comment


              • #8
                Yes
                Last edited by Raymond Zhang; 27 Jan 2021, 00:44.
                Best regards.

                Raymond Zhang
                Stata 17.0,MP

                Comment


                • #9
                  @River Huang
                  I want to give another solution.Maybe it is better-looking than the graph in #6(the orange line seems odd) .
                  Code:
                  sysuse auto, clear
                  twoway (scatter mpg weight if weight < 2500) (scatter mpg weight if inrange(weight,2500,3500)) ///
                  (lfit mpg weight if weight < 2500) , ///
                  xline(2500) legend(off)
                  keep if weight > 2500
                  addplot:lfit mpg weight,range(2520,3500)
                  Attached Files
                  Last edited by Raymond Zhang; 27 Jan 2021, 01:21.
                  Best regards.

                  Raymond Zhang
                  Stata 17.0,MP

                  Comment


                  • #10
                    Here is another solution.
                    Code:
                    sysuse auto, clear
                    twoway (scatter mpg weight if weight < 2500) (scatter mpg weight if inrange(weight,2500,3500)) ///
                    (lfit mpg weight if weight < 2500) , ///
                    xline(2500) legend(off)
                    keep if inrange(weight,2500,3500)
                    addplot:lfit mpg weight
                    Attached Files
                    Best regards.

                    Raymond Zhang
                    Stata 17.0,MP

                    Comment


                    • #11
                      Dear Raymond, Thanks for this helpful suggestion. Could you have a look at this related link (https://www.statalist.org/forums/for...robust-problem).

                      Ho-Chuan (River) Huang
                      Stata 17.0, MP(4)

                      Comment

                      Working...
                      X