Announcement

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

  • Problem Using collect with spregress

    I would like to export the results of a spatial error lag model using collect in Stata 19. Example 1 below illustrates that when I restrict the number of coefficients shown in the collect table that I lose the estimated error term even though the term (e.mpg) is included in the table layout and is also a level of the colname dimension. Example 2 shows that e.mpg is included in the table layout when I make no attempt to restrict the variables displayed.

    I'm not sure if this is a bug or feature of collect. I would welcome any advice on how to get the error term to display when restricting the variables shown in the table layout.

    EDIT: I added an Example 3 below with a workaround using autolevels. However, it's still not clear to me why Example 1 fails. Genuinely curious if there's a bug or something I'm missing about the syntax.

    Code:
    clear all
    spset, clear
    
    * Import Auto Dataset
    clear all
    spset, clear
    
    * Import Auto Dataset
    sysuse auto, clear
    
    * Generate ID
    gen id = _n
    
    * Generate Longitude and Latitude Coordinates
    gen _CX =  40 + rnormal()
    gen _CY = -70 + rnormal()
    
    * Set the Data for Spatial Analysis
    spset id, coord(_CX _CY) coordsys(latlong, miles)
    
    * Create a Spatial Matrix Based on Inverse Distance
    spmatrix create idistance W
    
    * Estimate Regression Model
    collect _r_b _r_se, tag(model[1]): spregress mpg price weight, gs2sls errorlag(W)
    collect _r_b _r_se, tag(model[2]): spregress mpg price weight, ml vce(robust) errorlag(W)
    
    * Example 1: Select Model Variables (e.mpg not displayed)
    collect layout (colname[price _cons e.mpg var(e.mpg)]#result[_r_b _r_se]) (model)
    
    * Show that e.mpg is in the colname dimension
    collect levelsof colname
    
    * Example 2: All Model Variables
    collect layout (colname#result[_r_b _r_se]) (model)
    
    * Example 3: Workaround using autolevels
    collect style autolevels colname price _cons e.mpg var(e.mpg)
    collect layout (colname#result[_r_b _r_se]) (model)
    Last edited by Michael Evangelist; 17 Feb 2026, 12:38.

  • #2
    Michael found a bug in collect layout and a work-around.

    We hope to have this fixed in a future Stata 19 update.

    Comment


    • #3
      This is now fixed in Stata 19 and StataNow 19, update on 15apr2026.

      In help whatsnew on 15apr2026, see item 6.
      Code:
      6. collect layout, when working with results from
         spregress, failed to correctly parse levels of colname
         with operator e..  This has been fixed.
      
         For example, suppose you collected the results from
      
          . spregress y ..., ... errorlag(W)
      
         where e.y is a coefficient in the second equation, and
         arranged the collected results into a table with
      
          . collect layout (colname[_cons e.y]) (result[_r_b])
      
         Before this fix, collect layout would report
      
          (level e.y of dimension colname not found)
      
         and fail to show the requested coefficient.

      Comment

      Working...
      X