capture program drop regpt
capture program drop ldataw
capture program drop ldatax
capture program drop lplotw
capture program drop lplotx
capture program drop reshow
capture program drop xplus
capture program drop xmin
capture program drop yplus
capture program drop ymin
capture program drop resetxy
capture program drop xlvr2pl
capture program drop hlpregpt

program define regpt
  version 6.0

  * preserve

  qui capture ldataw
  qui capture ldatax
  qui ldatax $DB_r $DB_n
  global DB_x 0
  global DB_y 0
  global DB_plot OLS Scatterplot with Regression Line
  qui lplotx $DB_x $DB_y
  capture lplotw

  * restore
end

program define ldataw
  version 6.0

  global temp1 "Make scatterplot from R="
  window control static temp1  10 10 100 10
  global DB_rL = ".9 .8 .7 .6 .5 .4 .3 .2 .1 0 -.1 -.2 -.3 -.4 -.5 -.6 -.7 -.8 -.9"
  global DB_r = .3
  window control scombo DB_rL 90 10 25 100 DB_r

  global temp2 "With N="
  window control static temp2 60 30 100 10
  global DB_nL = "10 20 30 40 50 60 70 80 90 100"
  global DB_n = 30
  window control scombo DB_nL 90 30 25 100 DB_n

  global DB_done "quietly exit 3000"
  window control button "Continue" 60 55 40 20 DB_done
  window dialog "Make Data for Scatterplot" . . 150 100

end

program define ldatax
  version 6.0
  args corr n

  clear
  qui set obs `n'

  generate ty = invnormal(uniform())
  egen y = std(ty)

  generate z = invnormal(uniform())
  generate tx = sqrt(1 - (`corr')^2)*z + `corr'*y
  egen x = std(tx)

  qui regress y x in 2/l
  global orig_b0 = _b[_cons]
  global orig_b1 = _b[x]

  predict origyhat
  replace origyhat = . in 1

  keep x y origyhat
  label var x ""
  label var y ""
end

program define lplotx
  version 6

  local atx `1'
  local incy `2'

  replace x = `atx' in 1
  replace y = $orig_b0 + $orig_b1*x + `incy' in 1

  *****************************

  if "$DB_plot" == "rreg Scatterplot with Regression Line" {
    capture drop genwt
    rreg y x, tune(7) genwt(genwt)
  }
  else if "$DB_plot" == "qreg Scatterplot with Regression Line" {
    qreg y x
  }
  else {
    regress y x
  }

  capture drop yhat
  predict yhat

  global b0r = string(_b[_cons],"%6.2f")
  global b1r = string(_b[x],"%6.2f")

  regress y x

  capture drop res
  predict res , resid
  global DB_res = string(res[1],"%6.2f")

  capture drop cook
  predict cook , cooksd
  global DB_cook = string(cook[1],"%6.2f")

  capture drop lev
  predict lev , leverage
  global DB_lev = string(lev[1],"%6.2f")


  global ob0r = string($orig_b0,"%6.2f")
  global ob1r = string($orig_b1,"%6.2f")

  global eq1 "Regression Equations w/o and with MP"
  global eq2 "W/O (green line) : Y = $ob0r + X * $ob1r"
  global eq3 "with (red line)     : Y = $b0r + X * $b1r"

  /*
  window control radbegin "Scatterplot & Reg Line" 10  80 70 10 DB_plot
  window control radio    "Res vs. Fit"       10  90 70 10 DB_plot
  window control radio    "Res vs. Lev"          10 100 70 10 DB_plot
  window control radend   "Res vs. Lev w/Cook"     10 110 70 10 DB_plot
  */

  if "$DB_plot" == "OLS Scatterplot with Regression Line" | "$DB_plot" == "rreg Scatterplot with Regression Line" | "$DB_plot" == "qreg Scatterplot with Regression Line" {
    capture drop y2
    gen y2 = y in 1
    replace y = . in 1
    label var y2 "Moving Point"
    label var y "Rest of Sample"
    graph y2 y yhat origyhat x, symbol(Ooii) c(..ll) /*
      */ /* t1title(`"`t1tit'"') t2title(`"`t2tit'"')*/ /*
      */ pen(2334)    /*
      */ ylabel(-3 -2 to 6) xlabel(-3 -2 to 6) /*
      */ l2title("Value of Y") b2title("Value of X") sort `options'
  }

  capture drop rstan
  predict rstan , rstan
  capture drop rstan2
  predict rstan2 in 1, rstan
  replace rstan = . in 1
  label var rstan "Rest of Sample"
  label var rstan2 "Moving Point"

  if "$DB_plot" == "Residual vs. Fitted Plot" {
    graph rstan2 rstan yhat, xlabel(-1.5 -1 to 1.5) ylabel(-3 -2 to 3) symbol(Oo)
  }
  if "$DB_plot" == "Leverage vs. Residual Squared" {
    xlvr2pl , symbol(Oo)
  }
  if "$DB_plot" == "Lev vs. Res Squared with Cooks D" {
    xlvr2pl , symbol(Oo) cook
  }

  * drop y2 yhat
end

program define lplotw
  version 6.0
  window manage forward dialog

  global DB_yplus "yplus"
  window control button "Y+.5" 10 10 20 10 DB_yplus

  global DB_ymin "ymin"
  window control button "Y-.5" 10 25 20 10 DB_ymin

  global DB_xmin "xmin"
  window control button "X-.5" 35 35 20 10 DB_xmin

  global DB_xplus "xplus"
  window control button "X+.5" 65 35 20 10 DB_xplus

  global mvtemp "Shift Moving Point (MP)"
  window control static mvtemp 30 1 100 10

  global temp2 "MP at Y-Yhat="
  window control static temp2 40 15 50 10
  window control static DB_y  90 15 15 10

  global temp1 "     MP at X="
  window control static temp1 40 25 50 10
  window control static DB_x  90 25 15 10


  window control static eq1   1 50 170 10
  window control static eq2   6 57 170 10
  window control static eq3   6 64 170 10

  global DB_xxx = "OLS Regression Diagnostic Statistics w/MP"
  window control static DB_xxx 1 74 130 10

  global temp3 "Residual"
  window control static temp3   6 81 30 10
  window control static DB_res  6 88 30 10

  global temp4 "Leverage"
  window control static temp4   40 81 30 10
  window control static DB_lev  40 88 30 10

  global temp5 "Cook's D"
  window control static temp5    80 81 30 10
  window control static DB_cook  80 88 35 10

  global DB_show "reshow"
  window control button "Reshow Now" 50 98 50 10 DB_show

  /*
  global DB_yyy = "Show Plot as"
  window control static DB_yyy 5 100 45 10
  global DB_plot = 1
  window control radbegin "Scatterplot with Regression Line" 10 110 120 10 DB_plot
  window control radio    "Residual vs. Fitted Plot        " 10 118 120 10 DB_plot
  window control radio    "Leverage vs. Residual Squared   " 10 126 120 10 DB_plot
  window control radend   "Lev vs. Res Squared with Cooks D" 10 134 120 10 DB_plot
  */

  global temp9 "Show Plot As"
  window control static temp9  5 100 45 10
  global DB_plotL OLS Scatterplot with Regression Line,rreg Scatterplot with Regression Line,qreg Scatterplot with Regression Line,Residual vs. Fitted Plot,Leverage vs. Residual Squared,Lev vs. Res Squared with Cooks D
  window control scombo DB_plotL 5 110 130 60 DB_plot parse(,)

  global DB_done "quietly exit 3000"
  window control button "Done" 10 150 30 10 DB_done escape

  global DB_reset "resetxy"
  window control button "Reset" 50 150 30 10 DB_reset

  global DB_help "hlpregpt"
  window control button "Help" 90 150 30 10 DB_help

  window dialog "Regression and a Single Point" . .  140 180
  window dialog update

end

program define reshow
  lplotx $DB_x $DB_y
  window manage forward dialog
end

program define hlpregpt
  whelp regpt
end

program define xplus
  global DB_x = $DB_x + .5
  lplotx $DB_x $DB_y
  window manage forward dialog
end

program define xmin
  global DB_x = $DB_x -.5
  lplotx $DB_x $DB_y
  window manage forward dialog
end

program define yplus
  global DB_y = $DB_y + .5
  lplotx $DB_x $DB_y
  window manage forward dialog
end

program define ymin
  global DB_y = $DB_y -.5
  lplotx $DB_x $DB_y
  window manage forward dialog
end

program define resetxy
  global DB_y = 0
  global DB_x = 0
  lplotx $DB_x $DB_y
  window manage forward dialog
end

program define xlvr2pl
/* leverage vs. residual squared */
	version 6
	_isfit cons
	syntax [, XLIne(string) YLIne(string) cook *]

      * mnm add
      if "`cook'" != "" {
        local w [w=cook]
      }

	if "`e(vcetype)'"=="Robust" {
		di in red "leverage plot not available after robust estimation"
		exit 198
	}

	tempvar h h2 r
	quietly {
		_predict `h' if e(sample), hat
		_predict `r' if e(sample), resid
		replace `r'=`r'*`r'
		sum `r'
		replace `r'=`r'/(r(mean)*r(N))
		local x=1/r(N)
		local y=(e(df_m)+1)*`x'
	}
	if "`xline'"!="" {
		if "`xline'"!="." { local xline "xline(`xline')" }
		else local xline
	}
	else	local xline "xline(`x')"
	if "`yline'"!="" {
		if "`yline'"!="." { local yline "yline(`yline')" }
		else local yline
	}
	else	local yline "yline(`y')"

      gen `h2' = `h' in 1
      replace `h' = . in 1

	label var `h' "Rest of Sample"
      label var `h2' "Moving Point"
	label var `r' "Normalized residual squared"

	gr `h2' `h' `r' `w', `options' `xline' `yline' l1title("Leverage")
end