Alex Bădoi
Alex Bădoi

Reputation: 830

Error in match.names(clabs, nmi) - Linear programming model - R

I am applying the CCR Data Envelopment Analysis model to benchmark between stock data. To do that I am running R code from a DEA paper published here. This document comes with step by step instructions on how to implement the model below in R.

The mathematical formulation looks like this:

enter image description here

Finding the model I needed already made for me seemed too good to be true. I am getting this error when I run it:

Error in match.names(clabs, nmi) : names do not match previous names

Traceback:

4 stop("names do not match previous names") 
3 match.names(clabs, nmi) 
2 rbind(deparse.level, ...) 
1 rbind(aux, c(inputs[i, ], rep(0, m))) 

My test data looks as follows:

> dput(testdfst)
structure(list(Name = structure(1:10, .Label = c("Stock1", "Stock2", 
"Stock3", "Stock4", "Stock5", "Stock6", "Stock7", "Stock8", "Stock9", 
"Stock10"), class = "factor"), Date = structure(c(14917, 14917, 
14917, 14917, 14917, 14917, 14917, 14917, 14917, 14917), class = "Date"), 
    `(Intercept)` = c(0.454991569278089, 1, 0, 0.459437188169979, 
    0.520523252955415, 0.827294243132907, 0.642696631099892, 
    0.166219881886161, 0.086341470900152, 0.882092217743293), 
    rmrf = c(0.373075150411683, 0.0349067218712968, 0.895550280607866, 
    1, 0.180151549474574, 0.28669170468735, 0.0939821798173586, 
    0, 0.269645291515763, 0.0900619760898984), smb = c(0.764987877309785, 
    0.509094491489323, 0.933653313048327, 0.355340700554647, 
    0.654000372286503, 1, 0, 0.221454091364611, 0.660571586102851, 
    0.545086931342479), hml = c(0.100608151187926, 0.155064872867367, 
    1, 0.464298576152336, 0.110803875258027, 0.0720803195598597, 
    0, 0.132407005239869, 0.059742053684015, 0.0661623383303703
    ), rmw = c(0.544512524466665, 0.0761995312858816, 1, 0, 0.507699534880555, 
    0.590607506295898, 0.460148690870041, 0.451871218073951, 
    0.801698199214685, 0.429094840372901), cma = c(0.671162426988512, 
    0.658898571758625, 0, 0.695830176886926, 0.567814542084284, 
    0.942862571603074, 1, 0.37571611336359, 0.72565234813082, 
    0.636762557753099), Returns = c(0.601347600017365, 0.806071701848376, 
    0.187500487065719, 0.602971876359073, 0.470386289298666, 
    0.655773224143057, 0.414258177255333, 0, 0.266112191477882, 
    1)), .Names = c("Name", "Date", "(Intercept)", "rmrf", "smb", 
"hml", "rmw", "cma", "Returns"), row.names = c("Stock1.2010-11-04", 
"Stock2.2010-11-04", "Stock3.2010-11-04", "Stock4.2010-11-04", 
"Stock5.2010-11-04", "Stock6.2010-11-04", "Stock7.2010-11-04", 
"Stock8.2010-11-04", "Stock9.2010-11-04", "Stock10.2010-11-04"
), class = "data.frame")

And the linear model program is this:

namesDMU <- testdfst[1]
inputs <- testdfst[c(4,5,6,7,8)]
outputs <- testdfst[9]

N <- dim(testdfst)[1] # number of DMU
s <- dim(inputs)[2] # number of inputs
m <- dim(outputs)[2] # number of outputs

f.rhs <- c(rep(0,N),1) # RHS constraints
f.dir <- c(rep("<=",N),"=") # directions of the constraints
aux <- cbind(-1*inputs,outputs) # matrix of constraint coefficients in (6)
for (i in 1:N) {
  f.obj <- c(0*rep(1,s),outputs[i,]) # objective function coefficients
  f.con <- rbind(aux ,c(inputs[i,], rep(0,m))) # add LHS of bTz=1
  results <-lp("max",f.obj,f.con,f.dir,f.rhs,scale=1,compute.sens=TRUE) # solve LPP
  multipliers <- results$solution # input and output weights
  efficiency <- results$objval # efficiency score
  duals <- results$duals # shadow prices
  if (i==1) {
    weights <- multipliers
    effcrs <- efficiency
    lambdas <- duals [seq(1,N)]
  } else {
    weights <- rbind(weights,multipliers)
    effcrs <- rbind(effcrs , efficiency)
    lambdas <- rbind(lambdas,duals[seq(1,N)])
  }
}

Spotting the problem..

A quick search reveals that the rbind function might be at fault. This is located on this line:

f.con <- rbind(aux ,c(inputs[i,], rep(0,m))) 

I tried to isolate the data from the loops to see what the problem is:

aux <- cbind(-1*inputs,outputs)
a <- c(inputs[1,])
b <- rep(0,m)

> aux
          rmrf        smb         hml         rmw        cma   Returns
1  -0.37307515 -0.7649879 -0.10060815 -0.54451252 -0.6711624 0.6013476
2  -0.03490672 -0.5090945 -0.15506487 -0.07619953 -0.6588986 0.8060717
3  -0.89555028 -0.9336533 -1.00000000 -1.00000000  0.0000000 0.1875005
4  -1.00000000 -0.3553407 -0.46429858  0.00000000 -0.6958302 0.6029719
5  -0.18015155 -0.6540004 -0.11080388 -0.50769953 -0.5678145 0.4703863
6  -0.28669170 -1.0000000 -0.07208032 -0.59060751 -0.9428626 0.6557732
7  -0.09398218  0.0000000  0.00000000 -0.46014869 -1.0000000 0.4142582
8   0.00000000 -0.2214541 -0.13240701 -0.45187122 -0.3757161 0.0000000
9  -0.26964529 -0.6605716 -0.05974205 -0.80169820 -0.7256523 0.2661122
10 -0.09006198 -0.5450869 -0.06616234 -0.42909484 -0.6367626 1.0000000

> a
$rmrf
[1] 0.3730752

$smb
[1] 0.7649879

$hml
[1] 0.1006082

$rmw
[1] 0.5445125

$cma
[1] 0.6711624

I also looked at this:

> identical(names(aux[1]), names(a[1]))
[1] TRUE

Column and row names are unimportant to me as long as the problem is calculated so I decided to try remove them. This one works but doesn't solve the problem.

rownames(testdfst) <- NULL

Looking at the contents of a and aux, maybe the problem lies with the column names.

colnames(testdfst) <- NULL does not work. It deletes everything in my data-frame. It could maybe... provide a solution to the problem if I can figure out how to remove the column names.

Upvotes: 2

Views: 414

Answers (1)

josliber
josliber

Reputation: 44330

As you correctly identified, the following line is giving you the trouble:

i <- 1
f.con <- rbind(aux ,c(inputs[i,], rep(0,m))) # add LHS of bTz=1
# Error in match.names(clabs, nmi) : names do not match previous names

You can use the str function to see the structure of each element of this expression:

str(aux)
# 'data.frame': 10 obs. of  6 variables:
#  $ rmrf   : num  -0.3731 -0.0349 -0.8956 -1 -0.1802 ...
#  $ smb    : num  -0.765 -0.509 -0.934 -0.355 -0.654 ...
#  $ hml    : num  -0.101 -0.155 -1 -0.464 -0.111 ...
#  $ rmw    : num  -0.5445 -0.0762 -1 0 -0.5077 ...
#  $ cma    : num  -0.671 -0.659 0 -0.696 -0.568 ...
#  $ Returns: num  0.601 0.806 0.188 0.603 0.47 ...

str(inputs[i,])
# 'data.frame': 1 obs. of  5 variables:
#  $ rmrf: num 0.373
#  $ smb : num 0.765
#  $ hml : num 0.101
#  $ rmw : num 0.545
#  $ cma : num 0.671

str(c(inputs[i,], rep(0, m)))
# List of 6
#  $ rmrf: num 0.373
#  $ smb : num 0.765
#  $ hml : num 0.101
#  $ rmw : num 0.545
#  $ cma : num 0.671
#  $     : num 0

Now you can see that the list you are trying to combine with rbind has different names from the data frame it's being combined with. Probably the simplest way to proceed would be to pass a vector as the new row instead of a list, which you can accomplish by converting inputs[i,] to a matrix with as.matrix:

str(c(as.matrix(inputs[i,]), rep(0, m)))
#  num [1:6] 0.373 0.765 0.101 0.545 0.671 ...

This will cause the code to work without an error:

f.con <- rbind(aux, c(as.matrix(inputs[i,]), rep(0, m)))

A few unsolicited R coding tips -- instead of dim(x)[1] and dim(x)[2] to get the number of rows and columns, most would find it more readable to do nrow(x) and ncol(x). Also, building objects in a for loop by rbinding one row at a time can be very inefficient -- you can read more about that in the second circle of the R Inferno.

Upvotes: 2

Related Questions