Announcement

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

  • Last estimates not found r(301) error code when using local linear regression.

    I am not sure why all of sudden I am not getting the predicted estimates as I was getting it a while back. All I did was drop all units that ever report zeros across all variables of interest. I recheked the estimation again with removing the newly added code chunk and it is still giving the same error.

    lpoly Violent year2, bwidth(2)
    predict Vio_Pred, new
    Graph.png

    Last edited by Anupam Ghosh; 08 Dec 2024, 00:43.

  • #2
    My guess is that for some reason you have squared years 1990 to 2019 and are trying to smooth a response as a function of year squared to see a trend. There may be a problem with collinearity or perhaps the command is trying to work with powers or other functions of the x variable and running into numerical difficulties.

    The exercise is puzzling on other grounds:

    1. Over that range year squared is essentially collinear with year any way. It might be prudent to work with (year - 2000)^2 rather than year squared if you're fitting a quadratic, One way to appreciate this is to see that your x variable is approximately equally spaced. Graph below.

    2. The outcome is positively skewed with outlier(s): it may be worse than it seems even, if there is over-plotting. That might well imply that local polynomial smoothing is not the tool of choice.

    Perhaps you meant to work with year all along and forgot that year2 was year squared.

    Code:
    twoway function year_squared=x^2, range(1990 2019) xtitle(year)
    Click image for larger version

Name:	year_squared.png
Views:	1
Size:	39.7 KB
ID:	1769023

    Comment


    • #3
      I tried with the code that you suggested but I am getting the same error. Also, I am trying to get a local linear regression with a quadratic time variable.

      use 6AgencyYear.dta, clear
      drop ori
      egen ori=group(ORI)
      xtset ori year
      sort ori year
      //
      bys ori (year): gen year2 = (year-2000)^2

      local vars "Male Female Hispanic Non_Hispanic White Black Native Asian_PI Age_1624"
      foreach var in `vars' {
      bys ori (year): gen `var'_Perc = `var'/Tot_Pop
      }

      *reg Violent year2 Tot_Officers Unemp_Rate Age_1624_Perc Black_Perc Male_Perc Hispanic_Perc
      lpoly Violent year2, bwidth(2)
      estimates store Vio_Pred
      predict Vio_Pred, xb
      Last edited by Anupam Ghosh; 08 Dec 2024, 11:23.

      Comment


      • #4
        I have three problems here of different kind.

        1. I can't explore your results as you don't give a data example -- or name a standard Stata dataset -- that could be used to reproduce the problem. .

        2. You seem to be confirming that you want to smooth an outcome against year squared. As in #2 I can't see any sense to that.

        3. I didn't explain myself well enough, but (year - 2000)^2 as a single predictor makes even less sense as a predictor than year squared because values either side of 2000 map to the same positive square, e.g. 1999 and 2001 map to 1, 1998 and 2002 map to 4, and so forth. What I had in mind was that IF you want to model a quadratic fit THEN you need to use e.g. year - 2000 and (year - 2000)^2 as predictors which don't show the massive problem that year squared is to a very good approximation linear in year over the range of your data.

        So sorry, but I am now even more puzzled than in #2 on what you are trying to do.

        In short, there are some questions here that I can't answer. You should be able to answer the first; if not, you need to talk this through with a teacher, supervisor, or mentor, depending on your level.

        Why are you trying to do this?

        Why do you get that error message in Stata? I can't add to earlier speculations.

        Detail:

        Code:
        bys ori (year): gen year2 = (year-2000)^2
        is nothing but

        Code:
        gen year2 = (year-2000)^2
        The by: prefix makes no difference to the calculation.

        That suggests a further misconception in what you think you are doing.

        Comment


        • #5
          Thanks, Nick for responding. I am trying to detect outliers, by regressing index crime on a quadratic time trend and then getting the absolute value of the percentage difference between actual and predicted. This comes quite standard in the crime literature; hence, I am trying to replicate that. However, interestingly on running the full specification regression, as given with the "regress" command I get the same distribution of the difference term. Thus, even I am a bit confused here. Thinking if the local linear regression has much sense to it anyway?

          Comment


          • #6
            The graph in #2 alone shows that you won't a quadratic fit out of year squared by itself.

            Comment


            • #7
              I don't know what's standard in the crime literature -- it is all too common across statistical science that dubious practices become embedded in some small literatures -- but the least problematic way of fitting a quadratic trend might be

              1. Constructing variables year MINUS 2000 and (year MINUS 2000)*2 as predictors. There is nothing magic or mystical about the choice of 2000; it's merely a convenient choice in the middle of your data, and so one that avoids the collinearity of year and year squared and an intercept for year 0 that would be at best awkward and at worst meaningless.

              2. A Poisson regression with robust standard errors that treats variability on a logarithmic scale as seems needed on various grounds, including the need to ensure positive predictions and to match the idea that it's percentage difference that is a reasonable scale.

              I wouldn't trust that as a way of identifying outliers. A glance at your other predictors

              Code:
              Tot_Officers Unemp_Rate Age_1624_Perc Black_Perc Male_Perc Hispanic_Perc
              suggests that you should be thinking about outliers relative to those predictors as well.

              Comment


              • #8
                Naturally the suggestion in #7 are not fitting a plain quadratic in year, but a Poisson analogue.

                Comment

                Working...
                X