Announcement

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

  • Running regression over age +/-3 without loop

    I need to run a regression that produces outputs for specific ages (0 to 105) in my dataset, but does so in a way that includes ages +/-3 for each age to give a degree of smoothing.

    My current code is:

    Code:
    forvalues v=1/22 {
    forvalues x=0/105 {
    capture qui poisson actrate postpandemic i.sex#(i.age c.fyear) if age>=`x'-3 & age<=`x'+3 & act_num==`v'  [pw=round(population/(abs(`x'-age)+1))],  base iter(50)
    capture qui estout using "H:\myfolder\myoutput.txt" ,append del(",") cells("b se") keep(*.sex#c.fyear) labcol2("`v'|`x'" "`v'|`x'") collabels(none) mlabels(none)
    }
    }
    The produces the desired results (it's the coefficient and se on fyear I'm interested in) but in a fairly badly formatted text file that has "actrate,,," between each regression and doesn't include the labels for act_num. E.g.

    Code:
    actrate,,,
    1.sex#c.fyear,1|0,0,.
    2.sex#c.fyear,1|0,.0003093,.0035586
    actrate,,,
    1.sex#c.fyear,1|1,0,.
    2.sex#c.fyear,1|1,.0010353,.0039513
    actrate,,,
    1.sex#c.fyear,1|2,0,.
    2.sex#c.fyear,1|2,.0018247,.0036703
    etc
    Can anyone advise if is possible to

    (a) replace the inner loop over ages with a single regression; and/or
    (b) not include the "actrate,,," but include the value label for act_num in the output and/or
    (c) more generally suggest better way to run this?

    Many thanks

  • #2
    I think most of your dissatisfaction arises from using -estout- to manage the output. I think if I were in your shoes, I would instead set up a -frame- to capture the desired results, and -frame post- them inside the loop after each regression. You didn't provide any example data, so I've coded it for an analogous problem using the nhanes2 data set. Here the variable height plays the role of fyear in your problem, and location plays the role of act_num. I've also made the code a little safer by handling the possibility that some combinations of `x' and `v' may produce a non-empty estimation sample but doesn't happen to contain both sexes. Also, since you used the -iterate(50)- option, you apparently anticipate that some of these regressions may fail to converge in a reasonable time, I modified the code so that results from non-convergent regressions will be discarded. I removed the -qui-s from the code because -capture- implies -quietly- unless you explicitly override that. And, finally, I restricted the ranges on the -forvalues- loops so that it wouldn't take so long to run; evidently you will want to set those back to your original parameters

    Code:
    clear*
    webuse nhanes2
    
    frame create results byte location int central_age  ///
        float(b_male_height se_male_height b_female_height se_female_height)
        
    forvalues v = 10/20 {
        forvalues x = 70/80 {
            capture poisson bpsystol i.sex#(i.age c.height) if location == `v' ///
                & abs(age - `x') <= 3, iterate(50)
            if c(rc) == 0 & e(converged) {
                capture frame post results (`v') (`x') (_b[1.sex#height]) (_se[1.sex#height]) ///
                    (_b[2.sex#height]) (_se[2.sex#height])
                if c(rc) != 0 {
                    frame post results (`v') (`x') (.) (.) (.) (.)
                }
            }
            
        }
    }
    
    frame change results
    The dataset in frame results at the end of this contains all the information you were trying to export using -estout-. If the ultimate goal is to have this as a text file, you can use -export delimited- and get a nicer result than -estout- would give you. But given that you are going to generate over 2,000 results, I suspect that what you really need is a data set in the first place so that you can do some additional analyses on these results. So probably this part of your task is covered without further ado.

    Comment

    Working...
    X