pro def regdplot
  version 6

  syntax , [SAVing(str) REPLACE]
  if e(cmd)~="regress" { error 301 }

  if "`saving'"~="" {
    local sav saving(`saving'
    if "`replace'"~="" { local sav `sav' , `replace'}
    local sav `sav')
  }

  preserve

  tempvar res ind
  qui g long `ind'=_n
  qui compress `ind'

  tempfile rvf qnorm
  rvfplot , saving(`rvf') yline(0) s([_n]) title(Residual vs. fitted)

  qui predict `res' if e(sample), res
  qnorm `res', s([`ind']) saving(`qnorm') title(Normal plot for residuals)

  local touse `rvf' `qnorm'

  tempname b
  mat `b'=e(b)
  local vars: coln `b'
  tokenize `vars'
  while "`1'"~="_cons" {
    tempfile `1'
    avplot `1', s([`ind']) saving(``1'') title(Added variable: `1')
    local touse `touse' ``1''
    mac shift
  }
  gr using `touse', `sav' margin(7)
end