0

I am trying to run an output oriented DEA model and am getting following errors:

1.

Warning message:
In rbind(const.mat, const.dir.num, const.rhs) :
  number of columns of result is not a multiple of vector length (arg 2)
Error in rbind(weights, results$solution[1]) : 
  cannot coerce type 'closure' to vector of type 'list'

I am sharing my complete code below:

library(readxl)
library(lpSolve)
library (rJava)
library(WriteXLS)
library(xlsxjars)
#defining dataset
df=data.frame(read_excel(path = "Data1.xlsx", sheet= "1"))
inputs=data.frame(df[1:2])
outputs=data.frame(df[3:4])
m=2
s=ncol(df)-m
N= nrow(df)
f.con=matrix(ncol=N+1,nrow=m+s)
for (j in 1:N)
   f.rhs = c(unlist(unname(df[j,(1):(m),1])),rep(0,s), 1)
f.dir = c(rep("<=",m),rep(">=",s), "=")  
f.obj = c(1, rep(0,N))
for(i in 1:m){}
f.con[i,1:(N+1)]=c(0,df[,i])
for(i in 1:m){f.con[i,1:(N+1)]=c(0,df[,i])}
for(r in (m+1):(s+m)) {f.con[r,1:(N+1)]=c(as.numeric(-df[j,r]),as.numeric(df[,r]))}
#solving the model
results =lp ("max", as.numeric(f.obj), f.con, f.dir, f.rhs, scale=0, compute.sens=F)
> Warning message:
> In rbind(const.mat, const.dir.num, const.rhs) :
  number of columns of result is not a multiple of vector length (arg 2)
if (j==1) {weights = results$solution[1]
lambdas = results$solution[seq(2,(N+1))]
xbench =lambdas%*% as.matrix(inputs)
ybench =lambdas%*% as.matrix(outputs)
} else{
  weights = rbind(weights, results$solution[1])
  lambdas = rbind(lambdas, results$solution[seq(2,(N+1))])
  xbench = lambdas %*% as.matrix(inputs)
  ybench = lambdas %*% as.matrix(outputs) }
> Error in rbind(weights, results$solution[1]) : 
  cannot coerce type 'closure' to vector of type 'list'
jrcalabrese
  • 2,184
  • 3
  • 10
  • 30
  • Please make your post [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by providing your data. We can't currently access `Data1.xlsx`. – jrcalabrese Dec 06 '22 at 18:24
  • structure(list(I1 = c(20, 11, 32, 21, 20, 12, 7, 31, 19, 32), I2 = c(11, 40, 30, 30, 11, 43, 45, 45, 22, 11), O1 = c(8, 21, 34, 18, 6, 23, 28, 40, 27, 38), O2 = c(30, 20, 40, 50, 17, 58, 30, 20, 23, 45)), class = "data.frame", row.names = c(NA, -10L)) This is my dataset, – Atul Anand Dec 09 '22 at 14:53

1 Answers1

0

First, the compute.sens argument for lp does not accept TRUE/FALSE. It defaults to "no", so I believe you can safely remove that argument. See ?lp below:

presolve Numeric: presolve? Default 0 (no); any non-zero value means "yes." Currently ignored.

Second, your final error message is coming from the fifth line in your if/else logic chunk. weights and lambdas are not defined in your Environment or in the else part of your if/else chunk, so R thinks that you're trying to rbind the function stats::weights (a closure) with results$solution[1]. You can troubleshoot an if/else chunk like this in the future by running each line individually to see where the error occurs.

if (j == 1) { 
  weights = results$solution[1]
  lambdas = results$solution[seq(2,(N+1))]
  xbench = lambdas%*% as.matrix(inputs)
  ybench = lambdas%*% as.matrix(outputs) 
} else {
  weights = rbind(weights, results$solution[1]) # problem 
  lambdas = rbind(lambdas, results$solution[seq(2,(N+1))]) # also a problem
  xbench = lambdas %*% as.matrix(inputs)
  ybench = lambdas %*% as.matrix(outputs) }

How to fix the error depends on your overall programming goal and how you want to define weights. You can copy-paste the parts where you define weights and lambdas if the if part into your else part, but it just depends on what your goal is.

df <- structure(list(I1 = c(20, 11, 32, 21, 20, 12, 7, 31, 19, 32), I2 = c(11, 40, 30, 30, 11, 43, 45, 45, 22, 11), O1 = c(8, 21, 34, 18, 6, 23, 28, 40, 27, 38), O2 = c(30, 20, 40, 50, 17, 58, 30, 20, 23, 45)), class = "data.frame", row.names = c(NA, -10L)) 
library(lpSolve)

inputs = data.frame(df[1:2])
outputs = data.frame(df[3:4])
m = 2
s = ncol(df)-m
N = nrow(df)
f.con=matrix(ncol=N+1,nrow=m+s)
for (j in 1:N) { f.rhs = c(unlist(unname(df[j,(1):(m),1])),rep(0,s), 1) }
f.dir = c(rep("<=",m),rep(">=",s), "=")  
f.obj = c(1, rep(0,N))
for(i in 1:m){}
f.con[i,1:(N+1)]=c(0,df[,i])
for(i in 1:m){f.con[i,1:(N+1)]=c(0,df[,i])}
for(r in (m+1):(s+m)) {f.con[r,1:(N+1)]=c(as.numeric(-df[j,r]),as.numeric(df[,r]))}

#solving the model
results <- lp("max", f.obj, f.con, f.dir, f.rhs, scale=0, compute.sens=F)

if (j == 1) { 
  weights = results$solution[1]
  lambdas = results$solution[seq(2,(N+1))]
  xbench = lambdas %*% as.matrix(inputs)
  ybench = lambdas %*% as.matrix(outputs) 
} else {
  weights = results$solution[1] # NEW
  weights = rbind(weights, results$solution[1]) 
  lambdas = results$solution[seq(2,(N+1))] # NEW
  lambdas = rbind(lambdas, results$solution[seq(2,(N+1))])
  xbench = lambdas %*% as.matrix(inputs)
  ybench = lambdas %*% as.matrix(outputs) }

Es = data.frame(weights,lambdas,xbench,ybench)

Output from code from your most recent comment:

head(Es)
        weights X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 I1 I2 O1 O2
weights       1  0  0  0  0  0  0  0  0  0   1 32 11 38 45
              1  0  0  0  0  0  0  0  0  0   1 32 11 38 45
jrcalabrese
  • 2,184
  • 3
  • 10
  • 30
  • As suggested I copied the modification made by you, but after running the code I am getting the following error: Es = data.frame(weights,lambdas,xbench,ybench) Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ‘"function"’ to a data.frame – Atul Anand Dec 13 '22 at 08:08
  • I updated my reprex to include the entire code at once, including your new code that computes `Es`; I don't get a similar error message. – jrcalabrese Dec 13 '22 at 19:00