/*************************************************
 *  Kleinbaum, Chapter 5, Problem #1, pp. 63-64  *
 *************************************************/

/* Read in the data */
capture clear
input age weight
 6 0.029
 7 0.052
 8 0.079
 9 0.125
10 0.181
11 0.261
12 0.425
13 0.738
14 1.130
15 1.882
16 2.812
end


/* Create log base 10 variable */
generate logweight=log10(weight)
format weight %9.3f
format logweight %9.3f
list


/* Create Graphs to replicate the ones in the text */
#delimit ;
graph twoway (scatter weight age),
  xtitle("Age (X)")
  ytitle("Dry Weight (Y)")
  xlabel(5(5)20)
  ylabel(0(.5)3, format(%4.1f))
  name(nolog, replace)
;
#delimit cr


#delimit ;
graph twoway (scatter logweight age),
  xtitle("Age (X)")
  ytitle("Log10 Dry Weight (Z)")
  xlabel(5(5)20)
  ylabel(-2(.5).5, format(%4.1f))
  name(log, replace)
;
#delimit cr


/* This might be more interesting graph */
#delimit ;
graph twoway (scatter weight age,    yaxis(1)
                ylabel(0(.5)3,   nogrid axis(1) format(%5.1f))
                ytitle("Dry Weight (Y)"))
             (scatter logweight age, xaxis(2) yaxis(2)
                ylabel(-2(.5).5, nogrid axis(2) format(%5.1f))
                xlabel(,nolabels noticks axis(2))
                xtitle("", axis(2))
                ytitle("Log10 Dry Weight (Z)", axis(2))),
  xtitle("Age (X)")
  xlabel(5(5)20)
  xtick(5(1)20)
  legend(on ring(0) position(5))
  name(log_nolog, replace)
;
#delimit cr


/* Run the regressions for problems 1b & c, p. 64 */
regress weight age
regress logweight age


/* Create Graphs with regression lines for problem 1d, p. 64*/
#delimit ;
graph twoway (scatter weight age)
             (lfit    weight age),
  xtitle("Age (X)")
  ytitle("Dry Weight (Y)")
  ylabel(-.5(.5)3, format(%4.1f))
  xtick(6(1)16)
  name(nologline, replace)
;
#delimit cr

#delimit ;
graph twoway (scatter logweight age)
             (lfit    logweight age),
  xtitle("Age (X)")
  ytitle("Log10 Dry Weight (Z)")
  ylabel(-1.5(.5).5, format(%4.1f))
  xtick(6(1)16)
  name(logline, replace)
;
#delimit cr


quietly {
  regress logweight age
  predict se, stdp
  predict yhat, xb
  generate lb=yhat-(invttail(11,.05/2)*se)
  generate ub=yhat+(invttail(11,.05/2)*se)
  local ll=round(lb[3], .001)
  local ul=round(ub[3], .001)
  drop se yhat lb ub
}

/* Create Graphs with regression lines for problem 1d, p. 64*/
#delimit ;
graph twoway (lfitci  logweight age, stdf ciplot(rarea))
             (lfitci  logweight age, stdp ciplot(rarea) acolor(yellow) fintensity(inten10))
             (scatter logweight age),
  xtitle("Age (X)")
  ytitle("Log10 Dry Weight (Z)")
  ylabel(-1.5(.5).5, format(%4.1f))
  xtick(6(1)16)
  xline(8, lw(thick) lp(dash))
  text(-1.2  10 "CI95 when X=8:" "   UB=`ul'" "   LB=`ll'", j(left))
  name(ci95, replace)
;
#delimit cr