Announcement

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

  • Convert matrix to var in stata: problematic svmat

    After using svmat command to save the beta coefficients of my regression as variables, I could not find a way to use them, for instance to generate new variables since they are only saved in one observation (see below). How can I make the beta coefficients saved for every observation? Thanks in advance.


    stcox lnAGE lnBMI if Male==0,nolog nohr

    failure _d: GeneralCVDA
    analysis time _t: FTGeneralCVDA
    id: ID_Cmod

    Cox regression -- Breslow method for ties

    No. of subjects = 6,355 Number of obs = 6,355
    No. of failures = 568
    Time at risk = 26344753
    LR chi2(2) = 155.23
    Log likelihood = -4856.5839 Prob > chi2 = 0.0000

    ------------------------------------------------------------------------------
    _t | Coef. Std. Err. z P>|z| [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    lnAGE | 4.040776 .4100609 9.85 0.000 3.237071 4.844481
    lnBMI | 1.508066 .1998089 7.55 0.000 1.116448 1.899685
    ------------------------------------------------------------------------------

    . mat beta=e(b)

    .
    . svmat double beta, names(coef)

    .
    . tab coef1,m

    coef1 | Freq. Percent Cum.
    ------------+-----------------------------------
    4.040776 | 1 0.01 0.01
    . | 11,600 99.99 100.00
    ------------+-----------------------------------
    Total | 11,601 100.00



    . gen AgeF=coef1 if Male==0
    (11,600 missing values generated)

    . tab AgeF,m


    AgeF | Freq. Percent Cum.
    ------------+-----------------------------------
    4.040776 | 1 0.01 0.01
    . | 11,600 99.99 100.00
    ------------+-----------------------------------
    Total | 11,601 100.00
    Last edited by Kigo Kariuki; 27 Dec 2015, 18:56.

  • #2
    I'm not sure I understand what you are trying to do. You want each coefficient to be a separate "variable" with the same value in each observation in the data set? Why would you want that? Whatever it is you plan to do with it, there is almost certainly a better way to do that with the coefficients in a matrix or as scalars. If you explain what your ultimate goal is, we can probably find a good way for you to achieve it.

    Comment


    • #3
      Thank you Clyde for reaching out to help. Am trying to compare the beta coefficients of a cox proportional hazard model I generated with those of a model published earlier by a team of experts in my field. I am hoping to do that using the z-score formula: z =(b1 – b2)/√(seb1^2 + seb2^2). The plan is to impute the beta coefficients of the published model in the dataset, but for the model I generated, I thinking that tying the beta coefficients to every observation will enable me to run the formula command in stata. Your guidance on this is highly appreciated.

      Jacob

      Comment


      • #4
        Unless I am missing something, there is no need to have the value of the coefficient repeated in every observation. In fact, you don't even need the -matrix beta = e(b)- command. You can calculate z as follows:

        Code:
        scalar b2 = VALUE OF PUBLISHED lnAGE COEFFICIENT GOES HERE
        scalar se2 = VALUE OF PUBLISHED lnAGE STANDARD ERROR GOES HERE
        scalar z = (_b[lnAGE] - b2) / (_se[lnAGE] - se2)
        display z
        The only problem you may encounter here is that names of scalars and variables can clash. So if you have a variable named b, se2, or z, you will run into trouble here. The solution to that is to use a different name in the above code for any scalar in that situation.

        Comment


        • #5
          By the way, the norm in this community is to use our real first and last names for our Forum username; it helps maintain a professional atmosphere. So are you Kigo or are you Jacob? If Kigo Kariuki is not your real name, please click on CONTACT US and ask the administrator to change your user name to your real first and last name. Thank you.

          Comment


          • #6
            Originally posted by Clyde Schechter View Post
            Unless I am missing something, there is no need to have the value of the coefficient repeated in every observation. In fact, you don't even need the -matrix beta = e(b)- command. You can calculate z as follows:

            Code:
            scalar b2 = VALUE OF PUBLISHED lnAGE COEFFICIENT GOES HERE
            scalar se2 = VALUE OF PUBLISHED lnAGE STANDARD ERROR GOES HERE
            scalar z = (_b[lnAGE] - b2) / (_se[lnAGE] - se2)
            display z
            The only problem you may encounter here is that names of scalars and variables can clash. So if you have a variable named b, se2, or z, you will run into trouble here. The solution to that is to use a different name in the above code for any scalar in that situation.

            Thank you so much Clyde. The code you have provided works very well and has significantly reduced the commands I have to use to compare the 7 variables in my analysis. In your example you seem to generate scalar z using a different formula i.e. scalar z = (_b[lnAGE] - b2) / (_se[lnAGE] - se2). I am using scalar z = (_b[lnAGE] - b2) / sqrt (_se[lnAGE]^2 + se2^2) based on the attached article by Paternoster and Brame. Do you think this approach could be problematic? When I used the two formulas to compare my model vs published model's beta coefficient for age, I got different results (see below). Once again, thank you very much for taking your time to help me with this.

            . scalar z = (_b[lnAGE] - Pubage) / sqrt(_se[lnAGE]^2 + PubageSE^2)

            . display z
            .78793916

            scalar z = (_b[lnAGE] -Pubage) /(_se[lnAGE]-PubageSE)

            . display z
            2.9742741
            Attached Files

            Comment


            • #7
              Originally posted by Clyde Schechter View Post
              By the way, the norm in this community is to use our real first and last names for our Forum username; it helps maintain a professional atmosphere. So are you Kigo or are you Jacob? If Kigo Kariuki is not your real name, please click on CONTACT US and ask the administrator to change your user name to your real first and last name. Thank you.

              Thank you for bringing this to my attention. Both Jacob and Kigo are my official names, My colleagues in the US use Jacob, while the people I work with back home use Kigo. I will contact the admin to get some guidance on how to avoid this confusion.

              Comment


              • #8
                In response to #6, it seems I've been making a lot of errors this morning. The formula should be your original formula:

                Code:
                scalar z = (_b[lnAGE] - Pubage) / sqrt(_se[lnAGE]^2 + PubageSE^2)
                I don't know what I was thinking with that (_se[lnAge]-se2) denominator! I'm a bit off my game today. Sorry for any confusion I caused.

                Comment


                • #9
                  Originally posted by Clyde Schechter View Post
                  In response to #6, it seems I've been making a lot of errors this morning. The formula should be your original formula:

                  Code:
                  scalar z = (_b[lnAGE] - Pubage) / sqrt(_se[lnAGE]^2 + PubageSE^2)
                  I don't know what I was thinking with that (_se[lnAge]-se2) denominator! I'm a bit off my game today. Sorry for any confusion I caused.
                  That human error pales compared to the help you have given me and many others today....I spent a lot of time yesterday trying to figure out how to do this using the svmat command with no much progress, and then you came in and simplified everything in a brilliant way. Again, thank you so much for your time and the great help. Have a great evening!

                  Comment

                  Working...
                  X