# AIC & Cp Mallows method regression (32 variables) [closed]

I am trying to make a variable selection using AIC & Cp Mallows methods.

For AIC:

require(leaps)
rs<-summary(b)
rs$which n<-nrow(datasetX[,3:35]) p<-ncol(datasetX[,3:35]) (AIC<-n*log(rs$rss/n)+(2:p))
plot(AIC~I(1:(p-1)),ylab="AIC",xlab="Number of Predictors")


The plot I get is:

The plot says that the minimum AIC is for 8 predictors that, looking at the which table, are perimeter_mean, area_mean, smoothness_mean, concave_points_mean, perimeter_se, area_se, smoothness_se and concave_points_worst.

Everything seems ok. But when I try to do the same for Cp Mallows criteria, I get:

plot(1:(p-1),rs$cp,xlab="Number of predictors",ylab="Cp Mallows") Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ  That's true. The model has 32 variables and rs$cp is just 8. But I cannot understand why. Could you help me? Maybe am I doing something wrong?

The problem here is that regsubsets checks by default models with just up to a few predictors - something about 8 predictors. Therefore, rs\$rss and rs\$cp length is 8, but 1:(p-1) is longer. That prompts the error message and triggers vector recycling, that causes the pattern we can see in the graph.
You can check in-program help for regsubsets to adjust the maximum number of predictors (but beware that with large numbers of predictors it may take a long time or just crash), or you can adjust the plot order just to plot values from the models actually adjusted by regsubsets. Whatever action that makes the two vectors be of the same length may avoid that error.