Announcement

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

  • pystata - getting coefficient by name

    Hello,

    I am using pystata on Jupyter notebook and I want to get regression coefficient by name (using stata API is preferred)

    I know that in pystata, we can use
    Code:
    pystata.stata.sfi.Matrix.get('e(b)')
    which returns the columns of coefficients in array. However, this is inconvenient for me to index by integer. I am looking for a way to get coefficient, p-values by name as in stata we can use command
    Code:
    _b[_cons]
    Thank you
    Phan

  • #2
    I figure the solution to work around. I post here in case someone might need

    Code:
    from pystata import stata
    from sfi import Scalar, Matrix
    
    # Run sample regression
    stata.run('sysuse auto, clear')
    stata.run('regress mpg weight foreign')
    
    # Show r(table):
    stata.run('mat list r(table)')
    
    # Accessing the r(table) by:
    df = Matrix.get('r(table)')
    rows = Matrix.getRowNames('r(table)')
    cols = Matrix.getColNames('r(table)')
    
    # Export to result with Dataframe format
    result = pd.DataFrame(data=df, index=rows, columns=cols)
    Hope this helps
    Phan

    Comment

    Working...
    X