Announcement

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

  • Temporarily keep a country in a panel

    Starting from a panel data I would like to estimate a reg for each country separately and produce a figure for each country then combining these figures.

    The problem is that the estimation is producing weird results, if I simply run a regression and end it with e.g. if country=="USA". (I am running a non-linear regression by the way.)

    Is there a way to temporarily keep one country similar to

    keep if country=="USA"

    Without Stata erasing the other panel from its memory. So I can jump to the next country and do:

    keep if country=="JPN"

    Sure, I can reload the panel data for each regression, but I would lose the graphs from the memory (I would think) and won't be able to combine them.

    Thank you



  • #2
    Evan:
    wouldn't it be simpler going:
    Code:
    bysort country: regress <depvar> <predictors> <controls>
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      The problem is that the estimation is producing weird results, if I simply run a regression and end it with e.g. if country=="USA". (I am running a non-linear regression by the way.
      Perhaps instead of concluding that you must drop all the other countries before running your commands, you could instead correct your errors that are leading to "weird results" when you run your estimation using the if qualifier. As it stands, instead of asking us about your fundamental problem, you've guessed at a solution and asked us about it. Your solution may be correct, but some of us will be reluctant to point you in the direction you ask if we're not sure that it's the direction you need to go.

      As it stands, though, your problem with the if qualifer isn't clear without more detail, or at a minimum it is too difficult to guess at a good answer from what little you have shared. Please help us help you. Show example data. Show your code. Show us what Stata told you. Tell us what precisely is weird. The Statalist FAQ provides advice on effectively posing your questions, posting data, and sharing Stata output.

      Comment


      • #4
        Carlo,

        Thanks for the answer. The reason I did not try this is because I am following each regression with a "predict" and while perhaps feasible I do not know how to do it.

        My country-level code looks like this

        use "panel.dta", clear
        keep if country=="United Kingdom"
        sort days
        nl (testpc = ({b1=0.5}/(1 +{b2=10}*exp(-1*{b3=0.01}*days)))) if days<366
        predict testpc_fit

        The "testpc_fit" is what I want to graph at the end.

        Now if I want to run it the way you are recommending, it would be:

        use "panel.dta", clear
        bysort country: nl (testpc = ({b1=0.5}/(1 +{b2=10}*exp(-1*{b3=0.01}*days)))) if days<366

        How do I go about predicting in a way I can keep track of the country name?


        William: My question is the way it is, because I was focused on resolving the "keep" issue which I thought was feasible and would help in the future.
        Had my question been about why the regressions are not making sense, I would have included some details about that. But that wasn't my question.
        Thanks for your civil comments nevertheless.


        Comment


        • #5
          As a followup:

          The bysort country: nl (testpc = ({b1=0.5}/(1 +{b2=10}*exp(-1*{b3=0.01}*days)))) if days<366

          Also causes issues, in that the regression does not converge. I tried running a linear regression with no issues. If I do each country alone with the same initial values, I have no issues.

          Comment


          • #6
            Evan:
            if the issue is that the same set of initial values is not "one size fits all" (the countries), probably regressing them one at a time is the only way to go.
            That said, as William wisely pointed out, showing your code is always the way to go because:
            1) it shows interested listers what the matter is with your data (but this was not your question);
            2) can help interested listers to suggest a solution/trick/workaround on the grounds of other threads and/or experienced issues.
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment


            • #7
              Supposing that you have an issue running the command on the full dataset, appending the results will recreate your dataset with the predictions. The following is not tested and may contain typos or errors.

              Code:
              use "panel.dta", clear
              encode country, g(Country)
              save "panel.dta", replace
              tempfile myfile
              qui sum Country
              forval i=1/`r(max)'{
                  use "panel.dta", clear
                  keep if Country==`i'
                  sort days
                  nl (testpc = ({b1=0.5}/(1 +{b2=10}*exp(-1*{b3=0.01}*days)))) if days<366
                  predict testpc_fit
                  local next = cond(`i'==1, "save `myfile', replace",  "append using `myfile'")
                  `next'
                  save `myfile', replace
              }
              use `myfile', clear
              Last edited by Andrew Musau; 18 Aug 2022, 12:44.

              Comment


              • #8
                Thank you both. Andrew, thanks a lot, I will try this solution.

                Comment

                Working...
                X