regsubsets: functions for model selection

Description Usage Arguments Details Value Note See Also Examples

View source: R/leaps.R

Description

Model selection by exhaustive search, forward or backward stepwise, or sequential replacement

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
regsubsets(x=, ...)

## S3 method for class 'formula'
regsubsets(x=, data=, weights=NULL, nbest=1, nvmax=8,
 force.in=NULL, force.out=NULL, intercept=TRUE,
 method=c("exhaustive", "backward", "forward", "seqrep"),
 really.big=FALSE,
 nested=(nbest==1),...)

## Default S3 method:
regsubsets(x=, y=, weights=rep(1, length(y)), nbest=1, nvmax=8,
force.in=NULL, force.out=NULL, intercept=TRUE,
 method=c("exhaustive","backward", "forward", "seqrep"),
really.big=FALSE,nested=(nbest==1),...)

## S3 method for class 'biglm'
regsubsets(x,nbest=1,nvmax=8,force.in=NULL,
method=c("exhaustive","backward", "forward", "seqrep"),
really.big=FALSE,nested=(nbest==1),...)

## S3 method for class 'regsubsets'
summary(object,all.best=TRUE,matrix=TRUE,matrix.logical=FALSE,df=NULL,...)

## S3 method for class 'regsubsets'
coef(object,id,vcov=FALSE,...)
## S3 method for class 'regsubsets'
vcov(object,id,...)

Arguments

x

design matrix or model formula for full model, or biglm object

data

Optional data frame

y

response vector

weights

weight vector

nbest

number of subsets of each size to record

nvmax

maximum size of subsets to examine

force.in

index to columns of design matrix that should be in all models

force.out

index to columns of design matrix that should be in no models

intercept

Add an intercept?

method

Use exhaustive search, forward selection, backward selection or sequential replacement to search.

really.big

Must be TRUE to perform exhaustive search on more than 50 variables.

nested

See the Note below: if nested=FALSE, models with columns 1, 1 and 2, 1-3, and so on, will also be considered

object

regsubsets object

all.best

Show all the best subsets or just one of each size

matrix

Show a matrix of the variables in each model or just summary statistics

matrix.logical

With matrix=TRUE, the matrix is logical TRUE/FALSE or string "*"/" "

df

Specify a number of degrees of freedom for the summary statistics. The default is n-1

id

Which model or models (ordered as in the summary output) to return coefficients and variance matrix for

vcov

If TRUE, return the variance-covariance matrix as an attribute

...

Other arguments for future methods

Details

Since this function returns separate best models of all sizes up to nvmax and since different model selection criteria such as AIC, BIC, CIC, DIC, ... differ only in how models of different sizes are compared, the results do not depend on the choice of cost-complexity tradeoff.

When x is a biglm object it is assumed to be the full model, so force.out is not relevant. If there is an intercept it is forced in by default; specify a force.in as a logical vector with FALSE as the first element to allow the intercept to be dropped.

The model search does not actually fit each model, so the returned object does not contain coefficients or standard errors. Coefficients and the variance-covariance matrix for one or model models can be obtained with the coef and vcov methods.

Value

regsubsets returns an object of class "regsubsets" containing no user-serviceable parts. It is designed to be processed by summary.regsubsets.

summary.regsubsets returns an object with elements

which

A logical matrix indicating which elements are in each model

rsq

The r-squared for each model

rss

Residual sum of squares for each model

adjr2

Adjusted r-squared

cp

Mallows' Cp

bic

Schwartz's information criterion, BIC

outmat

A version of the which component that is formatted for printing

obj

A copy of the regsubsets object

The coef method returns a coefficient vector or list of vectors, the vcov method returns a matrix or list of matrices.

Note

As part of the setup process, the code initially fits models with the first variable in x, the first two, the first three, and so on. For forward and backward selection it is possible that the model with the k first variables will be better than the model with k variables from the selection algorithm. If it is, the model with the first k variables will be returned, with a warning. This can happen for forward and backward selection. It (obviously) can't for exhaustive search.

With nbest=1 you can avoid these extra models with nested=TRUE, which is the default.

See Also

leaps

Examples

1
2
3
4
5
6
7
8
data(swiss)
a<-regsubsets(as.matrix(swiss[,-1]),swiss[,1])
summary(a)
b<-regsubsets(Fertility~.,data=swiss,nbest=2)
summary(b)

coef(a, 1:3)
vcov(a, 3)

Example output

Subset selection object
5 Variables  (and intercept)
                 Forced in Forced out
Agriculture          FALSE      FALSE
Examination          FALSE      FALSE
Education            FALSE      FALSE
Catholic             FALSE      FALSE
Infant.Mortality     FALSE      FALSE
1 subsets of each size up to 5
Selection Algorithm: exhaustive
         Agriculture Examination Education Catholic Infant.Mortality
1  ( 1 ) " "         " "         "*"       " "      " "             
2  ( 1 ) " "         " "         "*"       "*"      " "             
3  ( 1 ) " "         " "         "*"       "*"      "*"             
4  ( 1 ) "*"         " "         "*"       "*"      "*"             
5  ( 1 ) "*"         "*"         "*"       "*"      "*"             
Subset selection object
Call: regsubsets.formula(Fertility ~ ., data = swiss, nbest = 2)
5 Variables  (and intercept)
                 Forced in Forced out
Agriculture          FALSE      FALSE
Examination          FALSE      FALSE
Education            FALSE      FALSE
Catholic             FALSE      FALSE
Infant.Mortality     FALSE      FALSE
2 subsets of each size up to 5
Selection Algorithm: exhaustive
         Agriculture Examination Education Catholic Infant.Mortality
1  ( 1 ) " "         " "         "*"       " "      " "             
1  ( 2 ) " "         "*"         " "       " "      " "             
2  ( 1 ) " "         " "         "*"       "*"      " "             
2  ( 2 ) " "         " "         "*"       " "      "*"             
3  ( 1 ) " "         " "         "*"       "*"      "*"             
3  ( 2 ) "*"         " "         "*"       "*"      " "             
4  ( 1 ) "*"         " "         "*"       "*"      "*"             
4  ( 2 ) " "         "*"         "*"       "*"      "*"             
5  ( 1 ) "*"         "*"         "*"       "*"      "*"             
[[1]]
(Intercept)   Education 
 79.6100585  -0.8623503 

[[2]]
(Intercept)   Education    Catholic 
 74.2336892  -0.7883293   0.1109210 

[[3]]
     (Intercept)        Education         Catholic Infant.Mortality 
     48.67707330      -0.75924577       0.09606607       1.29614813 

                  (Intercept)     Education      Catholic Infant.Mortality
(Intercept)      62.711883147 -0.2349982009 -0.0011120059     -2.952862263
Education        -0.234998201  0.0136416868  0.0004427309      0.003360365
Catholic         -0.001112006  0.0004427309  0.0007408169     -0.001716363
Infant.Mortality -2.952862263  0.0033603646 -0.0017163629      0.149759535

leaps documentation built on Jan. 17, 2020, 1:09 a.m.

Related to regsubsets in leaps...