How to calculate Mallows's Cp

1

I find myself in a disadvantage, I am trying to calculate Mallows' Cp for different linear regressions (each regression has a different number of independent variables), but when I try to use the% package leaps() sends me an error

  

Error in colnames<- ( *tmp* , value = c (as.character (1: 9),   LETTERS) [1: NCOL (x)]): attempt to set 'colnames' on an object with   less than two dimensions

I do not know how to correct it. Also, I do not know how to define variable variables X so that software R can calculate the coefficient Cp. I do not have an advanced knowledge of the software, I'm just learning to use the program.

data = read.table('clipboard')
names(data)

y=datos$V1
x1=datos$V2
x2=datos$V3
x3=datos$V4
x4=datos$V5
x5=x1*x2
x6=x1*x3
x7=x1^2
x8=x2^2
x9=x3^2
x10=x4^2

m.general=lm(y~x1+x2+x3+x3+x4+x5+x6+x7+x8+x9+x10) 
summary(m.general) 
best=regsubsets(y~x1+x2+x3+x3+x4+x5+x6+x7+x8+x9+x10,datos,nb‌​est=3) 
summary(best)
m.r1=lm(y~x5,datos)
summary(m.r1)
AIC(m.r1)
press(m.r1)
x=data$V6
y=data$V1
m.r1Cp=leaps(x,y,method=c("Cp","adjr2","rsq"))
    
asked by Ruben Casalins Mendoza 18.11.2017 в 04:47
source

2 answers

0

You should give a reproducible example of your data, so we can help you more easily. In principle it seems that you are trying to assign a vector to colnames (that is, the name of the variables that go through columns) longer than the number of columns that that data frame has.

I hope it helps you! Greetings!

    
answered by 18.11.2017 в 14:31
0

I will answer you promptly for the error, since not having the data it is difficult to understand what you are looking for. According to the package documentation leaps , the function leaps() expects the parameter x is: A matrix of predictors and clearly in your example it seems that it is not, since you are indicating data$V6 which although it is a column, the way you are indicating it is "promoted" to a vector.

Some possible solutions:

  • You are wrongly defining the x parameter, it is assumed that to evaluate models this value should have at least two predictor variables, it does not make sense to evaluate a model with a single predictor variable, I think the problem goes through this, to add variables, you simply do something like this: x <- data$[c("V5","V6")] .
  • You could still solve the error by forcing some things in your code in the following way:

    x=data$[c("V6")] <- Con esto al menos mantenemos el tipo de dato original
    m.r1Cp=leaps(x=x,y=y,method=c("Cp","adjr2","rsq"),strictly.compatible=FALSE)
    
answered by 21.11.2017 в 16:51