Announcement

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

  • Saving beta coefficients, constant and residuals from linear regression models? Is this code correct?

    Hi, I am trying to save beta coefficients, constant and residuals and need advice if the following procedure returns the correct values.

    Problem and cohort description: I am studying a disease outcome (lsf), over time (measure_disease_exam_duration) among a cohort of individuals (personal identification=pnr). I would like to make a regression model for each patient (pnr) and save the beta coefficient, constant and residual for each of the resulting model/patient. I have searched and tried some code examples, but have not been able to get them to work. I did manage to get the code below to work, but I am uncertain if the code returns the correct values.


    *********************
    *Stata 13.1

    *Saving residual

    sort pnr examdate

    set more off

    levelsof pnr, local(pnr)

    gen residual_lsf = .

    tempvar temp

    foreach y of local pnr {

    regress lsf measure_disease_exam_duration if pnr == `"`y'"'

    predict `temp', resid

    replace residual_lsf = `temp' if pnr == `"`y'"'

    drop `temp'

    }




    *Saving coefficients and constants

    sort pnr examdate

    set more off

    levelsof pnr, local(pnr)

    gen lsf_beta = .

    gen lsf_constant = .

    tempvar beta1

    foreach y of local pnr {

    regress lsf measure_disease_exam_duration if pnr == `"`y'"'

    replace lsf_constant = _b[_cons] if pnr == `"`y'"'

    replace lsf_beta = _b[measure_disease_exam_duration] if pnr == `"`y'"'

    }


  • #2
    Code:
    ssc install rangestat
    
    local x  measure_disease_exam_duration
    rangestat (reg) lsf `x',  int(examdate . .) by(pnr) 
    
    gen residual = lsf - b_cons - b_`x' * `x'

    Comment

    Working...
    X