Announcement

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

  • Scalars not working in esttab

    For some reason I cannot capture several scalars in this loop:

    eststo clear
    foreach x of local lambdatypesr{
    eststo: xtreg `x' lambda_cumr i.ym if ym<773, fe i(spgrid_id) vce(cluster spgrid_id)
    summarize `x' if lambda_cumr > 0
    estadd scalar ypost = r(mean)
    estadd scalar lightchangemean = `mean_lights'
    estadd scalar adjcoeff = (_b[lambda_cumr] * `mean_lights')/(abs(_b[lambda_cumr])+e(ypost))*100
    estadd scalar adjCIlower = ((_b[lambda_cumr]- 1.96*_se[lambda_cumr]) * `mean_lights') / (abs((_b[lambda_cumr]- 1.96*_se[lambda_cumr])) + e(ypost))*100
    estadd scalar adjCIupper = ((_b[lambda_cumr]+ 1.96*_se[lambda_cumr]) * `mean_lights') / (abs((_b[lambda_cumr]+ 1.96*_se[lambda_cumr])) + e(ypost))*100
    }

    esttab using "paper/kde/tables/regress_kde_`i'`j'.rtf", ///
    keep(lambda_cumr) ///
    depvar se stats(ypost adjcoeff adjCIlower adjCIupper N, labels ("Ave. Post" "% Change" "Lower 5%" "Upper 95%" "N")) ///
    label nonumber ///
    title("Changes in Lights on `i' `j' Crime ") ///
    mtitle("Total" "Gun" "Violent" "Property" "Nuisance" "Fraud") ///
    note(Standard errors in parentheses; Hexagon and year-month fixed effects) ///
    replace

  • #2
    Can you please be more specific? What is the error message? Obviously I can't necessarily reproduce your error because you don't provide example data, but in my toy example it looks like the loop stops at the second iteration because ypost is already defined.

    Code:
    clear
    set obs 1000
    gen x = runiform()
    gen y = runiform()
    gen z = runiform()
    ereturn clear
    foreach var in x y z {
        summarize x
        estadd scalar ypost = r(mean)
    }
    Code:
    . foreach var in x y z {
      2.         summarize x
      3.         estadd scalar ypost = r(mean)
      4. }
    
        Variable |        Obs        Mean    Std. dev.       Min        Max
    -------------+---------------------------------------------------------
               x |      1,000     .492831    .2907891   .0033354   .9995956
    
    added scalar:
                  e(ypost) =  .49283097
    
        Variable |        Obs        Mean    Std. dev.       Min        Max
    -------------+---------------------------------------------------------
               x |      1,000     .492831    .2907891   .0033354   .9995956
    e(ypost) already defined
    r(110);
    
    end of do-file
    After the loop has finished executing, it looks like you want to put the ypost value in a table. You are storing the value in a scalar, meaning that there must be exactly one value (that is the definition of a scalar), but it looks like the value should change on each iteration of the loop because it depends on the `x' local. It seems like you should want to tabulate multiple values here, but you are storing those values in a way that supposes you have exactly one value you are interested in. Something doesn't make sense here.

    Comment


    • #3
      It's not the loop because the ypost works but the other scalars don't work. All the built in scalars work, but these custom created one's don't. I also tried using locals and that doesn't work either. I think it has to do with the xtreg program. When I use a regression and put in the hundred of fixed effects I can capture the scalars.
      Last edited by John MacDonald; 01 Jan 2025, 13:02.

      Comment


      • #4
        You have to associate the added scalar with a specific set of estimates. It is easier to do this while indexing the estimates yourself, rather than relying on esttab to do it automatically. estout is from SSC, as you are asked to explain (FAQ Advice #12).

        Comment


        • #5
          Thanks Andrew!

          Comment

          Working...
          X