我尝试运行面向输出的CCR DEA模型,并得到向量长度和向量类型误差

wqsoz72f  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(117)

我试图运行一个面向输出的DEA模型,并得到以下错误:
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'

我分享我的完整代码如下:

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'
ncecgwcz

ncecgwcz1#

首先,lpcompute.sens参数不接受TRUE/FALSE,它默认为“no”,所以我相信您可以安全地删除该参数,请参见下面的?lp

预解数值:预解-默认为0(否);任何非零值表示“是”。当前已忽略。

第二,最后一条错误消息来自if/else逻辑块的第五行。weightslambdas没有在您的环境或if/else块的else部分中定义,所以R认为你在尝试rbind函数stats::weights(一个闭包)。您可以在将来通过单独运行每一行来查找错误发生的位置,从而排除类似这样的if/else块。

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) }

如何修复错误取决于您的总体编程目标以及您希望如何定义weights。您可以将定义weightslambdas的部分复制粘贴到您的else部分,但这取决于您的目标。

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)

您最近评论的代码输出:

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

相关问题