Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
R in Action, Second Edition.pdf
Скачиваний:
540
Добавлен:
26.03.2016
Размер:
20.33 Mб
Скачать

Selecting the “best” regression model

201

8.5.3Adding or deleting variables

Changing the variables in a model will impact the fit of the model. Sometimes, adding an important variable will correct many of the problems that we’ve discussed. Deleting a troublesome variable can do the same thing.

Deleting variables is a particularly important approach for dealing with multicollinearity. If your only goal is to make predictions, then multicollinearity isn’t a problem. But if you want to make interpretations about individual predictor variables, then

you must deal with it. The most common approach is to delete one of the variables involved in the multicollinearity (that is, one of the variables with a vif > 2). An alternative is to use ridge regression, a variant of multiple regression designed to deal with multicollinearity situations.

8.5.4Trying a different approach

As you’ve just seen, one approach to dealing with multicollinearity is to fit a different type of model (ridge regression in this case). If there are outliers and/or influential observations, you can fit a robust regression model rather than an OLS regression. If you’ve violated the normality assumption, you can fit a nonparametric regression model. If there’s significant nonlinearity, you can try a nonlinear regression model. If you’ve violated the assumptions of independence of errors, you can fit a model that specifically takes the error structure into account, such as time-series models or multilevel regression models. Finally, you can turn to generalized linear models to fit a wide range of models in situations where the assumptions of OLS regression don’t hold.

We’ll discuss some of these alternative approaches in chapter 13. The decision regarding when to try to improve the fit of an OLS regression model and when to try a different approach is a complex one. It’s typically based on knowledge of the subject matter and an assessment of which approach will provide the best result.

Speaking of best results, let’s turn now to the problem of deciding which predictor variables to include in a regression model.

8.6Selecting the “best” regression model

When developing a regression equation, you’re implicitly faced with a selection of many possible models. Should you include all the variables under study, or drop ones that don’t make a significant contribution to prediction? Should you add polynomial and/or interaction terms to improve the fit? The selection of a final regression model always involves a compromise between predictive accuracy (a model that fits the data as well as possible) and parsimony (a simple and replicable model). All things being equal, if you have two models with approximately equal predictive accuracy, you favor the simpler one. This section describes methods for choosing among competing models. The word “best” is in quotation marks because there’s no single criterion you can use to make the decision. The final decision requires judgment on the part of the investigator. (Think of it as job security.)

202

CHAPTER 8 Regression

8.6.1Comparing models

You can compare the fit of two nested models using the anova() function in the base installation. A nested model is one whose terms are completely included in the other model. In the states multiple-regression model, you found that the regression coefficients for Income and Frost were nonsignificant. You can test whether a model without these two variables predicts as well as one that includes them (see the following listing).

Listing 8.11 Comparing nested models using the anova() function

> states <- as.data.frame(state.x77[,c("Murder", "Population", "Illiteracy", "Income", "Frost")])

> fit1 <- lm(Murder ~ Population + Illiteracy + Income + Frost, data=states)

>fit2 <- lm(Murder ~ Population + Illiteracy, data=states)

>anova(fit2, fit1)

Analysis of Variance Table

Model

1:

Murder

~ Population

+

Illiteracy

 

Model

2:

Murder

~

Population +

Illiteracy + Income + Frost

 

Res.Df

RSS

Df

Sum

of Sq

F

Pr(>F)

1

 

47

289.246

 

 

 

 

 

 

2

 

45

289.167

2

0.079

 

0.0061

 

0.994

Here, model 1 is nested within model 2. The anova() function provides a simultaneous test that Income and Frost add to linear prediction above and beyond Population and Illiteracy. Because the test is nonsignificant (p = .994), you conclude that they don’t add to the linear prediction and you’re justified in dropping them from your model.

The Akaike Information Criterion (AIC) provides another method for comparing models. The index takes into account a model’s statistical fit and the number of parameters needed to achieve this fit. Models with smaller AIC values—indicating adequate fit with fewer parameters—are preferred. The criterion is provided by the AIC() function (see the following listing).

Listing 8.12 Comparing models with the AIC

> fit1 <- lm(Murder ~ Population + Illiteracy + Income + Frost, data=states)

>fit2 <- lm(Murder ~ Population + Illiteracy, data=states)

>AIC(fit1,fit2)

 

df

AIC

fit1

6

241.6429

fit2

4

237.6565

The AIC values suggest that the model without Income and Frost is the better model. Note that although the ANOVA approach requires nested models, the AIC approach doesn’t.

Comparing two models is relatively straightforward, but what do you do when there are 4, or 10, or 100 possible models to consider? That’s the topic of the next section.

Selecting the “best” regression model

203

8.6.2Variable selection

Two popular approaches to selecting a final set of predictor variables from a larger pool of candidate variables are stepwise methods and all-subsets regression.

STEPWISE REGRESSION

In stepwise selection, variables are added to or deleted from a model one at a time, until some stopping criterion is reached. For example, in forward stepwise regression, you add predictor variables to the model one at a time, stopping when the addition of variables would no longer improve the model. In backward stepwise regression, you start with a model that includes all predictor variables, and then you delete them one at a time until removing variables would degrade the quality of the model. In stepwise stepwise regression (usually called stepwise to avoid sounding silly), you combine the forward and backward stepwise approaches. Variables are entered one at a time, but at each step, the variables in the model are reevaluated, and those that don’t contribute to the model are deleted. A predictor variable may be added to, and deleted from, a model several times before a final solution is reached.

The implementation of stepwise regression methods varies by the criteria used to enter or remove variables. The stepAIC() function in the MASS package performs stepwise model selection (forward, backward, or stepwise) using an exact AIC criterion. The next listing applies backward stepwise regression to the multiple regression problem.

Listing 8.13 Backward stepwise selection

>library(MASS)

>states <- as.data.frame(state.x77[,c("Murder", "Population",

"Illiteracy", "Income", "Frost")])

> fit <- lm(Murder ~ Population + Illiteracy + Income + Frost, data=states)

> stepAIC(fit, direction="backward")

Start:

AIC=97.75

 

 

 

Murder ~ Population + Illiteracy

+ Income + Frost

 

 

Df Sum of Sq

RSS

AIC

- Frost

 

1

0.02

289.19

95.75

- Income

1

0.06

289.22

95.76

<none>

 

 

 

289.17

97.75

- Population

1

39.24

328.41

102.11

- Illiteracy

1

144.26

433.43

115.99

Step:

AIC=95.75

 

 

 

Murder ~ Population + Illiteracy

+ Income

 

 

Df Sum of Sq

RSS

AIC

- Income

1

0.06

289.25

93.76

<none>

 

 

 

289.19

95.75

- Population

1

43.66

332.85

100.78

- Illiteracy

1

236.20

525.38

123.61

204

 

 

CHAPTER 8 Regression

Step: AIC=93.76

 

 

 

Murder ~ Population

+ Illiteracy

 

 

Df Sum

of Sq

RSS

AIC

<none>

 

 

289.25

93.76

- Population

1

48.52

337.76

99.52

- Illiteracy

1

299.65

588.89

127.31

Call:

lm(formula=Murder ~ Population + Illiteracy, data=states)

Coefficients:

(Intercept) Population Illiteracy

1.6515497 0.0002242 4.0807366

You start with all four predictors in the model. For each step, the AIC column provides the model AIC resulting from the deletion of the variable listed in that row. The AIC value for <none> is the model AIC if no variables are removed. In the first step, Frost is removed, decreasing the AIC from 97.75 to 95.75. In the second step, Income is removed, decreasing the AIC to 93.76. Deleting any more variables would increase the AIC, so the process stops.

Stepwise regression is controversial. Although it may find a good model, there’s no guarantee that it will find the “best” model. This is because not every possible model is evaluated. An approach that attempts to overcome this limitation is all subsets regression.

ALL SUBSETS REGRESSION

In all subsets regression, every possible model is inspected. The analyst can choose to have all possible results displayed or ask for the nbest models of each subset size (one predictor, two predictors, and so on). For example, if nbest=2, the two best onepredictor models are displayed, followed by the two best two-predictor models, followed by the two best three-predictor models, up to a model with all predictors.

All subsets regression is performed using the regsubsets() function from the leaps package. You can choose the R-squared, Adjusted R-squared, or Mallows Cp statistic as your criterion for reporting “best” models.

As you’ve seen, R-squared is the amount of variance accounted for in the response variable by the predictors variables. Adjusted R-squared is similar but takes into account the number of parameters in the model. R-squared always increases with the addition of predictors. When the number of predictors is large compared to the sample size, this can lead to significant overfitting. The Adjusted R-squared is an attempt to provide a more honest estimate of the population R-squared—one that’s less likely to take advantage of chance variation in the data. The Mallows Cp statistic is also used as a stopping rule in stepwise regression. It has been widely suggested that a good model is one in which the Cp statistic is close to the number of model parameters (including the intercept).

In listing 8.14, we’ll apply all subsets regression to the states data. The results can be plotted with either the plot() function in the leaps package or the subsets() function in the car package. An example of the former is provided in figure 8.17, and an example of the latter is given in figure 8.18.

Selecting the “best” regression model

205

adjr2

0.55

0.54

0.54

0.53

0.48

0.48

0.48

0.48

0.31

0.29

0.28

0.1

0.033

(Intercept)

Population

Illiteracy

Income

Frost

Figure 8.17 Best four models for each subset size based on Adjusted R-square

Listing 8.14 All subsets regression

library(leaps)

states <- as.data.frame(state.x77[,c("Murder", "Population", "Illiteracy", "Income", "Frost")])

leaps <-regsubsets(Murder ~ Population + Illiteracy + Income + Frost, data=states, nbest=4)

plot(leaps, scale="adjr2")

library(car)

subsets(leaps, statistic="cp",

main="Cp Plot for All Subsets Regression") abline(1,1,lty=2,col="red")

Figure 8.17 can be confusing to read. Looking at the first row (starting at the bottom), you can see that a model with the intercept and Income has an adjusted R-square of 0.33. A model with the intercept and Population has an adjusted R-square of 0.1. Jumping to the 12th row, a model with the intercept, Population, Illiteracy, and Income has an adjusted R-square of 0.54, whereas one with the intercept, Population, and Illiteracy alone has an adjusted R-square of 0.55. Here you see that a model with fewer predictors has a larger adjusted R-square (something that can’t happen with an unadjusted R-square). The graph suggests that the two-predictor model (Population and Illiteracy) is the best.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]