Announcement

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

  • Obtaining BLUPs from a random intercept and slope model

    Hi,

    I would like to know how the BLUPs are calculated in a random intercept and slope mixed model

    Please see the following code:

    //########################
    use https://stats.idre.ucla.edu/stat/sta...a/tolerance_pp, clear

    mixed tolerance time exposure male || id: time, reml estmetric
    mixed tolerance time exposure male || id: time, reml
    predict double pred, xb

    predict double rslope rintercept, reffects

    gen double fitted_v1=(_b[_cons]+rintercept) + _b[male]*male + _b[exposure]*exposure + (_b[time]*time + rslope*time)

    predict fitted_v2, fitted

    //########################


    Please can anyone tell me how the variables rslope rintercept are calculated? I would like to know how these variables are calculated.

    I have been able to obtain successfully the rintercept variable in the context of a random intercept model, but I don't know how to do it in the context of random intercept and slope model:

    //########################

    use https://stats.idre.ucla.edu/stat/sta...a/tolerance_pp, clear

    mixed tolerance time exposure male || id:, reml
    predict double pred, xb
    generate double res = tolerance - pred

    egen double ml = mean(res), by(id)

    mixed tolerance time exposure male || id:, reml estmetric

    bysort id: gen N=_N

    gen double shrinkage=exp([lns1_1_1]_cons)^2/(exp([lns1_1_1]_cons)^2 + exp([lnsig_e]_cons)^2/N)

    generate double rintercept1 = shrinkage*ml
    predict double rintercept2, reffects // rintercept1 and rintercept2 are the same

    gen double fitted2=pred+rintercept1

    //########################


    Thank you in advance for your help,


    Andrew

  • #2
    Originally posted by Andrew Xavier View Post
    Please can anyone tell me how the variables rslope rintercept are calculated? I would like to know how these variables are calculated.
    It's shown under the Methods and formulas heading of the entry mixed postestimation in the user's manual, if that's what you're asking for.

    You can get there the most quickly by typing
    Code:
    help mixed_postestimation
    at the command line in Stata, then clicking on the hyperlink (View complete PDF manual entry) at the top of the help page that pops up, and finally clicking on the header shown in the table of contents at the beginning of the user's manual entry.

    I have been able to obtain successfully the rintercept variable in the context of a random intercept model, but I don't know how to do it in the context of random intercept and slope model:
    Again, it's not clear what you're asking inasmuch as you've just shown how to do it in the first example that you quoted in your post: first fit the regression model and then follow the instructions in the help file that I referred to above to predict the best linear unbiased predictions for the model's two random effects. The dataset that your quoted example uses apparently isn't any longer available at UCLA's website, but I show a homebrew example below (begin at the "Begin here" comment).

    .ÿ
    .ÿversionÿ16.1

    .ÿ
    .ÿclearÿ*

    .ÿ
    .ÿsetÿseedÿ`=strreverse("1589799")'

    .ÿ
    .ÿquietlyÿdrawnormÿicpÿslo,ÿdoubleÿcorr(1ÿ-0.5ÿ\ÿ-0.5ÿ1)ÿn(50)

    .ÿgenerateÿbyteÿpidÿ=ÿ_n

    .ÿgenerateÿbyteÿsexÿ=ÿmod(_n,ÿ2)

    .ÿ
    .ÿquietlyÿexpandÿ3

    .ÿbysortÿpid:ÿgenerateÿbyteÿtimÿ=ÿ_nÿ-ÿ1

    .ÿ
    .ÿgenerateÿdoubleÿoutÿ=ÿ///
    >ÿÿÿÿÿÿÿÿÿ0ÿ+ÿicpÿ+ÿ///
    >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ0ÿ*ÿtimÿ+ÿ(timÿ-ÿ1)ÿ*ÿsloÿ*ÿsqrt(5)ÿ+ÿ///
    >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ0ÿ*ÿsexÿ+ÿ///
    >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿrnormal()

    .ÿ
    .ÿ*
    .ÿ*ÿBeginÿhere
    .ÿ*
    .ÿ//ÿFirstÿfitÿtheÿmodel
    .ÿmixedÿoutÿi.sexÿc.timÿ||ÿpid:ÿtim,ÿremlÿdfmethod(kroger)ÿnolrtestÿnolog

    Mixed-effectsÿREMLÿregressionÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿNumberÿofÿobsÿÿÿÿÿ=ÿÿÿÿÿÿÿÿ150
    Groupÿvariable:ÿpidÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿNumberÿofÿgroupsÿÿ=ÿÿÿÿÿÿÿÿÿ50

    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿObsÿperÿgroup:
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿminÿ=ÿÿÿÿÿÿÿÿÿÿ3
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿavgÿ=ÿÿÿÿÿÿÿÿ3.0
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿmaxÿ=ÿÿÿÿÿÿÿÿÿÿ3
    DFÿmethod:ÿKenward-RogerÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿDF:ÿÿÿÿÿÿÿÿÿÿÿminÿ=ÿÿÿÿÿÿ48.00
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿavgÿ=ÿÿÿÿÿÿ48.64
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿmaxÿ=ÿÿÿÿÿÿ49.60

    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿF(2,ÿÿÿÿ64.35)ÿÿÿÿ=ÿÿÿÿÿÿÿ1.05
    Logÿrestricted-likelihoodÿ=ÿ-347.30434ÿÿÿÿÿÿÿÿÿÿProbÿ>ÿFÿÿÿÿÿÿÿÿÿÿ=ÿÿÿÿÿ0.3562

    ------------------------------------------------------------------------------
    ÿÿÿÿÿÿÿÿÿoutÿ|ÿÿÿÿÿÿCoef.ÿÿÿStd.ÿErr.ÿÿÿÿÿÿtÿÿÿÿP>|t|ÿÿÿÿÿ[95%ÿConf.ÿInterval]
    -------------+----------------------------------------------------------------
    ÿÿÿÿÿÿÿ1.sexÿ|ÿÿ-1.131694ÿÿÿ.7820515ÿÿÿÿ-1.45ÿÿÿ0.154ÿÿÿÿ-2.704114ÿÿÿÿ.4407259
    ÿÿÿÿÿÿÿÿÿtimÿ|ÿÿ-.0509498ÿÿÿ.3199954ÿÿÿÿ-0.16ÿÿÿ0.874ÿÿÿÿ-.6938066ÿÿÿÿ.5919071
    ÿÿÿÿÿÿÿ_consÿ|ÿÿÿ.6571373ÿÿÿÿ.553734ÿÿÿÿÿ1.19ÿÿÿ0.241ÿÿÿÿ-.4560387ÿÿÿÿ1.770313
    ------------------------------------------------------------------------------

    ------------------------------------------------------------------------------
    ÿÿRandom-effectsÿParametersÿÿ|ÿÿÿEstimateÿÿÿStd.ÿErr.ÿÿÿÿÿ[95%ÿConf.ÿInterval]
    -----------------------------+------------------------------------------------
    pid:ÿIndependentÿÿÿÿÿÿÿÿÿÿÿÿÿ|
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿvar(tim)ÿ|ÿÿÿ4.620919ÿÿÿÿ1.09878ÿÿÿÿÿÿ2.899522ÿÿÿÿÿ7.36428
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿvar(_cons)ÿ|ÿÿÿ6.854455ÿÿÿ1.673391ÿÿÿÿÿÿ4.247833ÿÿÿÿ11.06059
    -----------------------------+------------------------------------------------
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿvar(Residual)ÿ|ÿÿÿ.9978658ÿÿÿ.2497436ÿÿÿÿÿÿ.6109918ÿÿÿÿ1.629705
    ------------------------------------------------------------------------------

    .ÿ
    .ÿ//ÿThenÿfollowÿtheÿhelpÿfileÿforÿtheÿpostestimationÿcommand
    .ÿpredictÿdoubleÿblup*,ÿreffects

    .ÿ
    .ÿlocalÿline_sizeÿ`c(linesize)'

    .ÿsetÿlinesizeÿ72

    .ÿdescribeÿblup*

    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿstorageÿÿÿdisplayÿÿÿÿvalue
    variableÿnameÿÿÿtypeÿÿÿÿformatÿÿÿÿÿlabelÿÿÿÿÿÿvariableÿlabel
    ------------------------------------------------------------------------
    blup1ÿÿÿÿÿÿÿÿÿÿÿdoubleÿÿ%10.0gÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿBLUPÿr.e.ÿforÿpid:ÿtim
    blup2ÿÿÿÿÿÿÿÿÿÿÿdoubleÿÿ%10.0gÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿBLUPÿr.e.ÿforÿpid:ÿ_cons

    .ÿsetÿlinesizeÿ`line_size'

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .


    Am I misunderstanding what you're asking?

    Comment


    • #3
      Hi, thank you for your reply

      I don't think I was very clear in my explanation

      What I want to know is how the following command is calculated

      predict double rslope rintercept, reffects
      This command calculates automatically rslope rintercept but I want to understand the formula used to calculate it, ie, I would like to derive it myself.

      I looked at the formulas and methods in the help file but I cannot workout how to derive rslope rintercept using the information contained there.

      Please could you help me to figure out that?

      Thanks

      Andrew

      Comment


      • #4
        You can try going through these slides for an example of a step-by-step illustration of the thinking behind it.

        Comment


        • #5
          Thanks for sending the link, I came across this example before but I still cannot figure out how to obtain blups for the random intercept and slope model. Another problem is I cannot understand the mathematical notation of it...

          Comment


          • #6
            Please could you write a few lines of code in stata and derive this for me in stata with the example you generated?

            Thanks

            Comment

            Working...
            X