Docsity

Using Parametric Models with Parametric and Non-Parametric Regression, Slides of Advanced Data Analysis

Toggle sidebar
bg1
Testing Parametric Regression Models with
Nonparametric Smoothers
36-402, Advanced Data Analysis
10 February 2011
Contents
1 Testing Functional Forms 1
1.1 Examples of Testing a Parametric Mo del . . . . . . . . . . . . . . 3
2 Why Use Parametric Models At All? 13
3 Why We Sometimes Want Mis-Specified Parametric Models 14
1 Testing Functional Forms
One important, but under-appreciated, use of nonparametric regression is in
testing whether parametric regressions are well-specified.
The typical parametric regression model is something like
Y=f(X;θ) + (1)
where fis some function which is completely specified except for the adjustable
parameters θ, and , as usual, is uncorrelated noise. Usually, but not necessarily,
people use a function fthat is linear in the variables in X, or perhaps includes
some interactions between them.
How can we tell if the specification is right? If, for example, it’s a linear
model, how can we check whether there might not be some nonlinearity? One
common approach is to modify the specification by adding in specific departures
from the modeling assumptions — say, adding a quadratic term — and seeing
whether the coefficients that go with those terms are significantly non-zero, or
whether the improvement in fit is significant.1For example, one might compare
the model
Y=θ1x1+θ2x2+(2)
to the model
Y=θ1x1+θ2x2+θ3x2
1+(3)
1In my experience, this is second in popularity only to ignoring the issue.
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Using Parametric Models with Parametric and Non-Parametric Regression and more Slides Advanced Data Analysis in PDF only on Docsity!

Testing Parametric Regression Models with

Nonparametric Smoothers

36-402, Advanced Data Analysis

10 February 2011

Contents

1 Testing Functional Forms 1 1.1 Examples of Testing a Parametric Model.............. 3

2 Why Use Parametric Models At All? 13

3 Why We Sometimes Want Mis-Specified Parametric Models 14

1 Testing Functional Forms

One important, but under-appreciated, use of nonparametric regression is in testing whether parametric regressions are well-specified. The typical parametric regression model is something like

Y = f (X; θ) +  (1)

where f is some function which is completely specified except for the adjustable parameters θ, and , as usual, is uncorrelated noise. Usually, but not necessarily, people use a function f that is linear in the variables in X, or perhaps includes some interactions between them. How can we tell if the specification is right? If, for example, it’s a linear model, how can we check whether there might not be some nonlinearity? One common approach is to modify the specification by adding in specific departures from the modeling assumptions — say, adding a quadratic term — and seeing whether the coefficients that go with those terms are significantly non-zero, or whether the improvement in fit is significant.^1 For example, one might compare the model Y = θ 1 x 1 + θ 2 x 2 +  (2)

to the model Y = θ 1 x 1 + θ 2 x 2 + θ 3 x^21 +  (3) (^1) In my experience, this is second in popularity only to ignoring the issue.

by checking whether the estimated θ 3 is significantly different from 0, or whether the residuals from the second model are significantly smaller than the residuals from the first. This can work, if you have chosen the right nonlinearity to test. It has the power to detect certain mis-specifications, if they exist, but not others. (What if the departure from linearity is not quadratic but cubic?) If you have good reasons to think that when the model is wrong, it can only be wrong in certain ways, fine; if not, though, why only check for those errors? Nonparametric regression effectively lets you check for all kinds of system- atic errors, rather than singling out a particular one. There are three basic approaches, which I give in order of increasing sophistication.

  • If the parametric model is right, it should predict as well as, or even better than, the non-parametric one, and we can check whether M SEp(̂θ) − M SEnp(̂r) is sufficiently small.
  • If the parametric model is right, the non-parametric estimated regression curve should be very close to the parametric one. So we can check whether f (x; ̂θ) − ̂r(x) is approximately zero everywhere.
  • If the parametric model is right, then its residuals should be patternless and independent of input features, because

E [Y − f (x; θ)|X] = E [f (x; θ) +  − f (x; θ)|X] = E [|X] = 0

So we can apply non-parametric smoothing to the parametric residuals, y − f (x; ̂θ), and see if their expectation is approximately zero everywhere.

We’ll stick with the first procedure, because it’s simpler for us to implement computationally. However, it turns out to be easier to develop theory for the other two, and especially for the third — see Li and Racine (2007, ch. 12), or Hart (1997). Here is the basic procedure.

  1. Get data (x 1 , y 1 ), (x 2 , y 2 ),... (xn, yn).
  2. Fit the parametric model, getting an estimate ̂θ, and in-sample mean- squared error M SEp(θ̂).
  3. Fit your favorite nonparametric regression (using cross-validation to pick control settings as necessary), getting curve r̂ and in-sample mean-squared error M SEnp(̂r).
  4. Calculate ̂t = M SEp(θ̂) − M SEnp(̂ r).
  5. Simulate from the parametric model θ̂ to get faked data (x′ 1 , y′ 1 ),... (x′ n, y n′).
  6. Fit the parametric model to the simulated data, getting estimate ˜θ and M SEp(θ˜).

IID Gaussian noise with mean 0 and standard deviation 0.15. (This is one of the two examples from the notes to Lecture 4.) Figure 1 shows the regression function and the data. The nonlinearity is clear with the curve to “guide the eye”, but fairly subtle. A simple linear regression looks pretty good:

> glinfit = lm(y~x,data=gframe) > print(summary(glinfit),signif.stars=FALSE,digits=2)

Call: lm(formula = y ~ x, data = gframe)

Residuals: Min 1Q Median 3Q Max -0.416 -0.115 0.004 0.118 0.

Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 0.208 0.019 11 <2e- x 0.434 0.011 41 <2e-

Residual standard error: 0.16 on 298 degrees of freedom Multiple R-squared: 0.85,Adjusted R-squared: 0. F-statistic: 1.7e+03 on 1 and 298 DF, p-value: <2e-

R^2 is ridiculously high — the regression line preserves 85% of the variance in the data. The p-value reported by R is also very, very low, which seems good, but remember all this really means is “you’d have to be crazy to think a flat line fit better than one with a slope” (Figure 2) The in-sample MSE of the linear fit^4

> mean(residuals(glinfit)^2) [1] 0.

The nonparametric regression has a somewhat smaller MSE^5

> gnpr <- npreg(y~x,data=gframe)

> gnpr$MSE [1] 0.

So ˆt = 0.0045: (^4) If we ask R for the MSE, by doing summary(glinfit)$sigma^ 2 , we get 0.02635298. These differ by a factor of n/(n − 2) = 300/298 = 1.0067, because R is trying to estimate the out-of-sample error by scaling up the in-sample error, the same way the estimated population variance scales up the sample variance. We want to compare in-sample fits. (^5) npreg does not apply the kind of correction mentioned in the previous footnote.

0.0 0.5 1.0 1.5 2.0 2.5 3.

x

y

x <- runif(300,0,3) yg <- log(x+1)+rnorm(length(x),0,0.15) gframe <- data.frame(x=x,y=yg) plot(x,yg,xlab="x",ylab="y") curve(log(1+x),col="grey",add=TRUE)

Figure 1: True regression curve (grey) and data points (circles). The curve g(x) = log x + 1.

One surrogate data set for simple linear regression

Inputs: linear model (linfit), x values to simulate at (test.x)

Outputs: Data frame with columns x and y, latter simulated

sim.lm <- function(linfit, test.x) { n <- length(test.x) sim.frame <- data.frame(x=test.x) # Add the y column later sigma <- summary(linfit)$sigma*(n-2)/n # MLE value y.sim <- predict(linfit,newdata=sim.frame) y.sim <- y.sim + rnorm(n,0,sigma) # Add noise sim.frame <- data.frame(sim.frame,y=y.sim) # Add a column return(sim.frame) }

Code Example 1: Simulate a new data set from a linear model, assuming homoskedastic Gaussian noise. It also assumes that there is one input variable, x, and that the response variable is called y. Could you modify it to work with multiple regression?

> t.hat = mean(glinfit$residual^2) - gnpr$MSE > t.hat [1] 0.

Now we need to simulate from the fitted parametric model, using its esti- mated coefficients and noise level. We have seen several times now how to do this. The function sim.lm in Example 1 does this, along the same lines as the examples in Lecture 8; it assumes homoskedastic Gaussian noise. Again, as before, we need a function which will calculate the difference in MSEs between a linear model and a kernel smoother fit to the same data set — which will do automatically what we did by hand above. This is calc.T in Example 2. Note that the kernel bandwidth has to be re-tuned to each new data set. If we call calc.T on the output of sim.lm, we get one value of the test statistic under the null distribution:

> calc.T(sim.lm(glinfit,x)) [1] 0.

Now we just repeat this a lot to get a good approximation to the sampling distribution of T under the null hypothesis:

null.samples.T <- replicate(200,calc.T(sim.lm(glinfit,x)))

This takes some time, because each replication involves not just generating a new simulation sample, but also cross-validation to pick a bandwidth. This adds up to about a second per replicate on my laptop, and so a couple of minutes for 200 replicates. (While the computer is thinking, look at the command a little more closely. It leaves the x values alone, and only uses simulation to generate new y values.

Calculate the difference-in-MSEs test statistic

Inputs: A data frame (my.data)

Presumes: data has columns "x" and "y", which are input and

response

Calls: np::npreg

Output: Difference in MSEs between linear model and kernel

smoother

calc.T <- function(data) {

Fit the linear model, extract residuals, calculate MSE

MSE.p <- mean((lm(y~x, data=data)$residuals)^2)

npreg gets mysteriously unhappy when called with a "data"

argument that is defined inside this function; npregbw

doesn’t complain

MSE.np.bw <- npregbw(y~x,data=data) MSE.np <- npreg(MSE.np.bw)$MSE return(MSE.p - MSE.np) }

Code Example 2: Calculate the difference-in-MSEs test statistic.

This is appropriate here because our model doesn’t really say where the x values came from; it’s just about the conditional distribution of Y given X. If the model we were testing specified a distribution for x, we should generate x each time we invoke calc.T. If the specification is vague, like “x is IID” but with no particular distribution, then use the nonparametric bootstrap. The command would be something like

replicate(200,calc.T(sim.lm(glinfit,resample(x)))

using the resample function from lecture 8, to draw a different bootstrap sample of x each time.) When it’s done, we can plot the distribution and see that the observed value ˆt is pretty far out along the right tail (Figure 3). This tells us that it’s very

unlikely that npreg would improve so much on the linear model if the latter were true. In fact, none of the bootstrap replicates were that big:

> sum(null.samples.T > t.hat) [1] 0

so our estimated p-value is 2011. We can thus reject the linear model pretty confidently.^6 As a second example, let’s suppose that the linear model is right — then the test should give us a high p-value. So let us stipulate that in reality

Y = 0.2 + 0. 5 x + η (4)

0.0 0.5 1.0 1.5 2.0 2.5 3.

x

y

y2 = 0.2+0.5*x + rnorm(length(x),0,0.15) y2.frame <- data.frame(x=x,y=y2) plot(x,y2,xlab="x",ylab="y") abline(0.2,0.5,col="grey")

Figure 4: Data from the linear model (true regression line in grey).

with η ∼ N (0, 0. 152 ). Figure 4 shows data from this, of the same size as before. Repeating the same exercise as before, we get that ˆt = 6. 8 × 10 −^4 , together with a slightly different null distribution (Figure 5). Now the p-value is 32%, which one would be quite rash to reject.

(^6) If we wanted a more precise estimate of the p-value, we’d need to use more bootstrap samples.

2 Why Use Parametric Models At All?

It might seem by this point that there is little point to using parametric models at all. Either our favorite parametric model is right, or it isn’t. If it is right, then a consistent nonparametric estimate will eventually approximate it arbitrarily closely. If the parametric model is wrong, it will not self-correct, but the non- parametric estimate will eventually show us that the parametric model doesn’t work. Either way, the parametric model seems superfluous. There are two things wrong with this line of reasoning — two good reasons to use parametric models.

  1. One use of statistical models, like regression models, is to connect scien- tific theories to data. The theories are about the mechanisms generating the data. Sometimes these hypotheses are “tight” enough to tell us what the functional form of the regression should be, or even what the distri- bution of noise terms should be, but still contain unknown parameters. In this case, the parameters themselves are substantively meaningful and interesting — we don’t just care about prediction. It can be very hard to relate non-parametric smoothing curves to aspects of scientific theories in the same way.^7
  2. Even if all we care about is prediction accuracy, there is still the bias- variance trade-off to consider. Non-parametric smoothers will have larger variance in their predictions, at the same sample size, than correctly- specified parametric models, simply because the former are more flexible. Both models are converging on the true regression function, but the para- metric model converges faster, because it searches over a more confined space. In terms of total prediction error, the parametric model’s low variance plus vanishing bias beats the non-parametric smoother’s larger variance plus vanishing bias. (Remember that this is part of the logic of testing parametric models in the previous section.) In the next section, we will see that this argument can actually be pushed further, to work with not-quite-correctly specified models.

Of course, both of these advantages of parametric models only obtain if they are well-specified. If we want to claim those advantages, we need to check the specification.

(^7) On the other hand, it is not uncommon for scientists to write down theories positing linear relationships between variables, not because they actually believe that, but because that’s the only thing they know how to estimate statistically.

3 Why We Sometimes Want Mis-Specified Para-

metric Models

Low-dimensional parametric models have potentially high bias (if the real re- gression curve is very different from what the model posits), but low variance (because there isn’t that much to estimate). Non-parametric regression mod- els have low bias (they’re flexible) but high variance (they’re flexible). If the parametric model is true, it can converge faster than the non-parametric one. Even if the parametric model isn’t quite true, a small bias plus low variance can sometimes still beat a non-parametric smoother’s smaller bias and substantial variance. With enough data the non-parametric smoother will eventually over- take the mis-specified parametric model, but with small samples we might be better off embracing bias. To illustrate, suppose that the true regression function is

E [Y |X = x] = 0.2 +

sin x 10

x (5)

This is very nearly linear over small ranges — say x ∈ [0, 3] (Figure 6). I will use the fact that I know the true model here to calculate the actual expected generalization error, by averaging over many samples (Example 3). Figure 7 shows that, out to a fairly substantial sample size (≈ 500), the lower bias of the non-parametric regression is systematically beaten by the lower variance of the linear model — though admittedly not by much.

nearly.linear.out.of.sample = function(n) {

Combines simulating the true model with fitting parametric

model and smoother, calculating MSEs

x=seq(from=0,to=3,length.out=n) y = h(x) + rnorm(n,0,0.15) data <- data.frame(x=x,y=y) y.new = h(x) + rnorm(n,0,0.15) sim.lm <- lm(y~x,data=data) lm.mse = mean(( fitted(sim.lm) - y.new )^2) sim.np.bw <- npregbw(y~x,data=data) sim.np <- npreg(sim.np.bw) np.mse = mean((sim.np$mean - y.new)^2) mses <- c(lm.mse,np.mse) return(mses) }

nearly.linear.generalization = function(n,m=100) { raw = replicate(m,nearly.linear.out.of.sample(n)) reduced = rowMeans(raw) return(reduced) }

Code Example 3: Evaluating the out-of-sample error for the nearly-linear problem as a function of n, and evaluting the generalization error by averaging over many samples.

0 100 200 300 400 500

n

RMS generalization error

sizes = c(5,10,15,20,25,30,50,100,200,500) generalizations = sapply(sizes,nearly.linear.generalization) plot(sizes,sqrt(generalizations[1,]),ylim=c(0.12,0.22),type="l", xlab="n",ylab="RMS generalization error") lines(sizes,sqrt(generalizations[2,]),lty=2) abline(h=0.15,col="grey")

Figure 7: Root-mean-square generalization error for linear model (solid line) and kernel smoother (dashed line), fit to the same sample of the indicated size. The true regression curve is as in 6, and observations are corrupted by IID Gaussian noise with σ = 0.15 (grey horizontal line). The cross-over after which the nonparametric regressor has better generalization performance happens shortly before n = 500.