With the goal of searching-for-specifications in mind, its useful to have a method of non-parametric regression that's fast since the researcher will have to try many things. Locally weighted polynomial regressions are fairly slow, so the faster and simpler Nadaraya-Watson estimator (Nadaraya, 1964 and Watson, 1964) comes in handy.

My code for the NW estimator in Matlab is here.

(If your data is in Stata and you want to move it to Matlab, see my code for that here).

It contains 4 .m files. The file test.m will generate some random data and estimate the conditional mean with the NW estimator. The code uses bootstrap-resampling to estimate confidence intervals. All models have flexible but fixed bandwidth.

With one dependent variable, NWbootstrap.m (with a normal kernel) and NWbootstrap_epkv.m (with an Epinechnikov kernel) are appropriate.

With two dependent variables, NWbootstrap_epkv_3D.m is useful. This should be used, for example, if you are looking for "interaction terms" in a multiple regression model (the slope of a function changes as a function of another variable).

*UPDATE: A problem with dropping missing variables has been fixed. The code drop_missing_Y is now in the NW toolbox to solve the problem.*

All files have helpfiles with syntax. Below is a demonstration of the code.

This is the example in the file test.m.

If you estimate the conditional mean non-parametrically, you see there is some curvature.

Perhaps you believe that a second variable (X2) is also important for explaining the slope of Y on X1. You can plot all three in a scatter:

but it's a little bit overwhelming. The NWbootstrap_epkv_3D code will fit a surface that is the mean of Y on conditional on a location (X1, X2).

Once you remove the overwhelming scatter, it becomes much easier to see what's going on:

(The whiskers are showing the bootstrapped confidence interval at each point.)

Thank you for this post. Finally somebody who speaks about the possibility of non-linearity. It's possible to go through the whole first year of this program without ever hearing the possibility that data may be non-linear and consequently not multivariate normal. From studying natural science we know that most processes are in fact non-linear. No reason to think the same should not apply in social systems.

ReplyDelete