Content
* ITSP 14.7 -
mata: mata clear
clear
type nneighbor.ado
/*
program nneighbor
version 10.1
syntax varlist(numeric) [if] [in], Y(string) MATCHOBS(string) MATCHVAL(string)
marksample touse
if r(N) == 0 {
error 2000
}
// validate new variable names
confirm numeric variable `y'
confirm new variable `matchobs'
confirm new variable `matchval'
qui generate long `matchobs' = .
qui generate `matchval' = .
mata: mf_nneighbor("`varlist'", "`matchobs'", "`y'", "`matchval'", "`touse'")
su `y' if `touse', meanonly
di _n "Nearest neighbors for `r(N)' observations of `y'"
di "Based on 2-norm of vars: `varlist'"
di "Matched observation numbers: `matchobs'"
di "Matched values: `matchval'"
qui correlate `y' `matchval' if `touse'
di "Correlation[ `y', `matchval' ] = " %5.4f `r(rho)'
end
*/
do mf_nneighbor.mata
type mf_nneighbor.mata
/*
version 10.1
mata:
void function mf_nneighbor(string scalar matchvars,
string scalar closest,
string scalar response,
string scalar match,
string scalar touse)
{
real matrix X, Z, mc
real colvector ind
real colvector w
real colvector d
string rowvector vars
vars = tokens(matchvars)
v = vars[|1, .|]
st_view(X, ., v, touse)
// standardize matchvars with mm_meancolvar from moremata
mc = mm_meancolvar(X)
Z = (X:- mc[1, .]):/ sqrt(mc[2, .])
n = rows(X)
k = cols(X)
st_view(C, ., closest, touse)
st_view(y, ., response, touse)
st_view(ystar, ., match, touse)
// loop over observations
for(i = 1; i <= n; i++) {
// loop over matchvars
d = J(n, 1, 0)
for(j = 1; j <= k; j++) {
d = d + (Z[., j]:- Z[i, j]) :^2
}
d = sqrt(d)
minindex(d, 2, ind, w)
C[i] = ind[2]
ystar[i] = y[ind[2]]
}
}
end
*/
do mf_nneighbor.mata
use usairquality, clear
sort pop
nneighbor pop, y(so2) matchobs(mo1) matchval(mv1)
list pop mo1 so2 mv1, sep(0)
nneighbor pop temp wind, y(so2) matchobs(mo3) matchval(mv3)
nneighbor pop temp wind precip days, y(so2) matchobs(mo5) matchval(mv5)