Announcement

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

  • Using response weights (svy) on a linear regression (regress) but want beta values

    I am using a large dataset (5000+) survey data with weights accounting for demographics that were underrepresented (e.g. accounting for a lack of younger participants).

    I am using linear regression to find associates of my dependent variable. I have employed a stepwise regression model, including only those that remain significant within each model. My command currently is the following:

    svy: regress depvar1 indvar1 indvar2 ...indvar16


    I have a model that includes variables measured in different ways (continuous, categorical, dummy etc.), meaning coefficients are tricky to interpret. So I would like to report on the beta.

    Unfortunately the beta option does not work when using the svy command, though I did find the following work around:
    https://www.statalist.org/forums/for...-weighted-data

    When I employ the above workaround (see do file below), I get an error at: st_matrix (“betas”, sx:/sy):*bx) “<istmt>: 3200 conformability error”, which means “A matrix, vector, or scalar has the wrong number of rows and/or columns for what is required. Adding a 2 x 3 matrix to a 1 x 4 would result in this error.”

    I can't seem to find the source of the error in the code, and it appears to have worked for other people, so I'm at a bit of a loss.

    I use stata 15, can download add ons if needed. Any additional info needed, let me know!

    Any help would be greatly appreciated.

    my do file:

    local var1 /* outcome */

    local var2 var3 var4 var5 var6 var7 var8 var9 var10 var11 var 12 var13 var14 var15 var16 length weight /* predictors */



    svyset [pw = weight]



    * Get coefficients *

    svy: regress var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 var11 var 12 var13 var14 var15 var16

    matrix b = e(b)



    * Get SDs of y and predictors *

    svy: mean var1

    estat sd

    matrix sy = r(sd)



    svy: mean var2 var3 var4 var5 var6 var7 var8 var9 var10 var11 var 12 var13 var14 var15 var16

    estat sd

    matrix sx = r(sd)



    *Compute standardized coefficients *

    mata:

    sy = st_matrix("sy")

    sx = st_matrix("sx")'

    b = st_matrix("b")

    bx = b[1 ,1..(cols(b)-1)]'

    st_matrix("betas",(sx:/sy):*bx)

    end


  • #2
    Welcome to Statalist.

    Despite the fact that you are using mata, your question will see a much larger audience if you post it in Statalist's General Forum. In particular, your workaround from a 2015 post may now have alternatives that do not involve mata. Essentially, your question may reflect a problem in your approach rather than in your mata code, and you will find a larger body of knowledgeable users in the General Forum.

    Also, if you have not already done so, take a look at the Statalist FAQ linked to at the top of this page for posting guidelines and suggestions. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], rather than what you did in the post above.
    Last edited by William Lisowski; 16 Jan 2019, 12:43.

    Comment


    • #3
      I will add that the code you copied from the 2015 post works for me, so there is something wrong with your setup.

      And copying your code into an editor to make it more readable, I find
      Code:
      svy: mean var2 var3 var4 var5 var6 var7 var8 var9 var10 var11 var 12 var13 var14 var15 var16
      which should resulted in an error at that point.

      So I'm assuming that what you show us is not necessarily the code you ran. That's a mistake. Section 12.1 of the Statalist FAQ is particularly pertinent

      12.1 What to say about your commands and your problem

      Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
      Added in edit: Add the command
      Code:
      mata describe
      before the command that fails. It will report the dimensions of each of the objects. b should be a vector of length 16, bx and sx length 15, and sy should be a scalar,
      Last edited by William Lisowski; 16 Jan 2019, 13:20.

      Comment

      Working...
      X