Announcement

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

  • Plotting error

    Hello,

    I am trying to draw a line graph with the outcome of my regressions.

    I am estimating separate 'oaxaca' regressions for each quantile, then trying to save the coefficients for then be able to plot the values of one specific coefficient across the different percentiles.

    My code is the following:

    forvalues qt=5(5)95{
    replace rifatr=rif03_`qt'_r if year2==0
    replace rifatr=rif12_`qt'_r if year2==1
    svy brr: oaxaca rifatr $correl, by(year23) weight(1) detail
    matrix B=e(b)
    svmat double B, name(coef)
    gen quant=`q'
    keep quant coef*
    keep if _n==1
    }

    sort quant
    label var quant "Quantile"
    graph twoway (connected coef1 quant if quant>0.0 & quant<1.0 ) /*


    I get the following error when running the first loop:
    "new variables cannot be uniquely named or already defined"

    Can anyone help? Thanks.

  • #2
    Charlotte,

    you generate the variable quant in your loop. This works the first time Stata parses through. However, when Stata gets there the second time, the variable quant is already defined, so you receive an error.
    You should either save each run as a separate dataset and append them afterwards, or you need to generate a quant variable with a unique name in each iteration of the loop.
    E.g.:

    Code:
    forvalues qt=5(5)95{
    replace rifatr=rif03_`qt'_r if year2==0
    replace rifatr=rif12_`qt'_r if year2==1
    svy brr: oaxaca rifatr $correl, by(year23) weight(1) detail
    matrix B=e(b)
    svmat double B, name(coef)
    gen quant`q'=`q'              //yields a unique name
    keep quant`q' coef*
    keep if _n==1
    }
    Or something like this, which I suppose works better for what you want:
    Code:
    gen quant = .
    forvalues qt=5(5)95{
    replace rifatr=rif03_`qt'_r if year2==0
    replace rifatr=rif12_`qt'_r if year2==1
    svy brr: oaxaca rifatr $correl, by(year23) weight(1) detail
    matrix B=e(b)
    svmat double B, name(coef)
    replace quant =`q'              //replaces quant with the desired value
    keep quant coef*
    keep if _n==1
    capture append using quant.dta //you need capture here to suppress the error in the first iteration that the dataset does not exist yet
    save quant.dta, replace
    }
    Hope this helps.

    Best,
    Martin

    Comment

    Working...
    X