Announcement

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

  • Stata Syntax Error When I Created Scatter Plot

    I want to plot a simple scatter plot with the Stata classical dataset auto,dta.
    Before regression, for the purpose of interpretation, I centered the two continuous variables length and weight and created two continuous variables c_weight, and c_length,respectively.
    However, the Stata syntax is wrong when I try to do the scatter plot.

    webuse auto,clear
    egen l=mean(length)
    egen w=mean(weight)
    gen c_weight=weight-w
    gen c_length=length-l
    reg price c_weight c_length i.foreign

    scatter price mpg if foreign==1 & weight==c_weight & length==c_length
    There is no any point in the scatter plot.

    Can someone help me to solve this problem?
    Thank you!
    Last edited by smith Jason; 21 May 2022, 14:24.

  • #2
    when you do equality checks you need two equals signs (as you have in your first condition); in addition, there is no variable "l_length" in your code; finally, it is hard to believe that the 2 final conditions in your command will leave you any data at all

    Comment


    • #3
      Originally posted by Rich Goldstein View Post
      when you do equality checks you need two equals signs (as you have in your first condition); in addition, there is no variable "l_length" in your code; finally, it is hard to believe that the 2 final conditions in your command will leave you any data at all
      Can you help me to use any other variables in the dataset to create a scatter plot like what I want?
      Thank you!

      Comment


      • #4
        since I have no idea what you want, or, more important, why, no, I can't

        Comment


        • #5
          My question is when I centered some continuous variables in the regression model, if I want to graph scatter plot, how could I correctly write the Stata code after controlling for other variables' effects.

          Comment


          • #6
            Originally posted by Rich Goldstein View Post
            since I have no idea what you want, or, more important, why, no, I can't
            I want to correct the syntax errors and graph the scatter plot. As I said, when I centered some continuous variables in the regression model, if I want to graph scatter plot, how could I correctly write the Stata code after controlling for other variables' effects?

            Comment


            • #7
              sorry but what you want is not clear to me - maybe someone else can help you

              Comment


              • #8
                Starting with the obvious: the thread title is wrong or no longer appropriate. There is no syntax error here.

                The problem is that the syntax that you are using is not producing the results that you want. Like Rich, I do not know what it is that you want. I can tell you that

                Originally posted by smith Jason View Post
                scatter price mpg if foreign==1 & weight==c_weight & length==c_length
                is restricting the observations to foreign cars, which is fine. It also restricts the data to those observations for which weight equals exactly the mean weight and length equals exactly the mean length. There is not a single observation for which either condition is true

                Code:
                . count if weight == c_weight
                  0
                
                . count if length == c_length
                  0
                Therefore, there is nothing to plot.


                Perhaps, you could tell us what you were hoping to get from the if expression? Which observations do you want to have in your scatter plot?

                Also, tell us what you were hoping to achieve by centering those variables? I do not see what shifting the mean value has to do with anything you do here.

                Last, I do not understand why you want to look at mpg. This variable is neither centered nor entered in the regression model. How is it related to what you want?
                Last edited by daniel klein; 22 May 2022, 00:45.

                Comment


                • #9
                  the following is a wild guess: what you want is a scatter plot of, not price, but of the predicted value of price from your regression (v. mpg though I certainly don't understand why); if that is close, I, or someone else, can provide some code

                  Comment


                  • #10
                    Yes. it is the case after controlling for other variables' impact.

                    Comment


                    • #11
                      Code:
                      regress price ...
                      predict price_hat
                      scatter price_hat ...

                      Comment

                      Working...
                      X