library("metrics")
randomize(66666)
n = 200 ; sample size
ss1 = #(1,0.9)~#(0.9,1) ; covariance matrix of error terms
g = #(1) ; true coefficient of decision equation
b = #(-9, 1) ; true intercept and slope of outcome equation
u = gennorm(n, #(0,0), ss1) ; generate realizations of joint distribution of error terms
ss2 = #(1,0.4)~#(0.4,1) ; covariance matrix of regressors
xz = gennorm(n, #(0,0), ss2) ; generate realizations of joint distribution of regressors
z = xz[,2] ; regressor of decision equation
q =(z*g+u[,1].>=0) ; generate binary dependent variable of decision equation
hd = 0.1*(max(z) - min(z)) ; bandwidth for dwade procedure
d = dwade(z,q,hd)*(2*sqrt(3)*pi) ; dwade estimate * scaling factor
id = z*d ; estimated first-step index
h =(quantile(id, 0.7))|(0.2*(max(id) - min(id))) ; bandwidth for select procedure
x = matrix(n)~xz[,1] ; regressors for outcome equation
y = x*b+u[,2] ; dependent variable for outcome equation
zz = paf(y~x~id, q) ; impose censored sampling
y = zz[,1]
x = zz[,3:(cols(zz)-1)]
id = zz[,cols(zz)]
{a,b} = select(x,y,id,h)
d~a~b ; first-step estimate ~ intercept estimate ~ slope estimate