Announcement

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

  • How to recall beta coefficient and standard error parameters of a stored model to make calculations using the scalar function?

    Hello all,
    I am trying to calculate test statistics for the TOST procedure and am trying to calculate the upper and lower bounds of the acceptance region by calling the beta coefficient and standard errors from two models that I previously did store in my code using the est function.

    Here is my code:

    // Step 3: Fit logistic regression models
    logit burn_post2 ib0.Active1_Sham0 if Session==1 & PreviousStimulationYes1_No0==0

    // Step 4: Estimate the difference in means
    est store model1

    // Step 5: Fit the reference model
    logit burn_post2 if Session==1 & PreviousStimulationYes1_No0==0

    // Step 6: Estimate the reference model
    est store model2

    // Step 7: Calculate the test statistics for the TOST procedure
    estimates table model1 model2, b se

    // Step 8: Calculate the lower and upper bounds of the acceptance region
    local alpha = 0.05 // Set the significance level
    scalar critical_value = invnormal(1 - `alpha'/2)

    scalar lower_bound = $ML_b[model1] - critical_value * sqrt($ML_se[model1]^2 + $ML_se[model2]^2)
    scalar upper_bound = $ML_b[model1] + critical_value * sqrt($ML_se[model1]^2 + $ML_se[model2]^2)


    ^the code works perfectly until I get to the scalar lower_bound line. I get an error that says "model1" not found. When I format my line of code to say $ML_b[model1], $ML_se[model1], etc. in the bolded scalar code, I am trying to draw from the beta coefficients and standard error in models 1 and 2. I did run the command "estimate dir" and got this output below that confirms that the Stata session did store my model1, so I am assuming there is an error in my syntax in the last two statements of my code.

    This is the output of "estimate dir" to confirm that I do indeed have models 1 and 2 stored:
    ------------------------------------------------------------------
    | Dependent Number of
    Name | Command variable param. Title
    -------------+----------------------------------------------------
    model1 | logit burn_post2 3 Logistic regression
    model2 | logit burn_post2 1 Logistic regression
    ------------------------------------------------------------------



    Any thoughts would be greatly appreciated, thank you.

  • #2
    Are you trying to do this for a coefficient? There are none but the constant in model2.

    Pardon my ignorance, but what is $ML_b?

    Comment


    • #3
      Model 2 is supposed to be only a reference model with no predictor. $ML_b is my educated guess on what the beta coefficient is named for model 1.
      Last edited by Lara Kh; 17 Jul 2023, 14:13.

      Comment


      • #4
        Where did those last two lines of code come from? ChatGPT? They are not valid in any case.

        Stata has special "system variables" that can be used to reference beta and SE values from the most recent estimation command. These are _b and _se, respectively. You will need to get whatever scalar values you need from each model one variable at a time, one model at a time first. Then you can combine them for your confidence interval calculation. (You can also work with matrices, but that seems an unnecessary complication for your described needs.)

        Here is an example that you can work with, but it is not tested as there is no reproducible example.

        Code:
        estimates restore model1
        scalar mod1_b = _b[myvar]
        scalar mod1_b_se = _se[myvar]
        
        estimates restore model2
        scalar mod2_b = _b[myvar]
        scalar mod2_b_se = _se[myvar]
        
        scalar lower_bound = mod2_b - critical_value * sqrt(mod1_b_se^2 + mod2_b_se^2)
        scalar lower_bound = mod2_b + critical_value * sqrt(mod1_b_se^2 + mod2_b_se^2)

        Comment


        • #5
          Yes, I had gotten the last two lines from ChatGPT. I didn't realize I would need to make scalar values from each model one variable at a time initially, but thank you so much, your code worked and I learned something new!

          Comment


          • #6
            Leonardo's approach will work, but there's no myvar in model2 but the constant. But maybe that's what you want. _b[_cons] , _se[_cons]

            Comment


            • #7
              have you tried tostregress?

              search tostregress

              Comment


              • #8
                I have not, but I am currently reading the .txt file documentation for it now and installed it. Thank you!

                Comment


                • #9
                  Originally posted by George Ford View Post
                  Leonardo's approach will work, but there's no myvar in model2 but the constant. But maybe that's what you want. _b[_cons] , _se[_cons]
                  Yes, myvar was just a stand-in for whatever coefficient name was wanted. If it's _cons then that bit needs to change.

                  Comment

                  Working...
                  X