2017-08-26 11 views
-1

いくつかの計算を行い、オブジェクトf.conを計算するデータフレームがあります。問題は、aux1を印刷すると、22行目以降の行番号が正しくないことです.23行目ではなく221行になります。それ以上の計算を行い、その行は無視されます。これは基本的に制約です。データフレームレコードの行数に誤差があり、計算でレコードが無視される

library(utils); library(xlsx) 
library(lpSolve) # load lpSolve package previously installed 
library(lpSolveAPI) 
datadea<- structure(list(DMUS = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 21, 22), Input1Cash = c(5, 6, 
4, 8, 5, 8, 4.4, 2.6, 3.4, 3.6, 2, 3, 3, 2.6, 4, 5, 6, 4, 7, 
6, 8, 9), Input2LEV = c(4, 5, 5, 5, 6, 3, 4.4, 8, 8, 4.4, 7, 
7, 5.6, 5, 4, 3.2, 4, 3.5, 3, 2.5, 2, 2), Output1EPS = c(1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 
members = c(1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("DMUS", 
"Input1Cash", "Input2LEV", "Output1EPS", "members" 
), row.names = c(NA, 22L), class = "data.frame") 

私はいくつかの計算を実行し、次のコードがあります。その結果得られたデータフレームAUX1で

N <- 22 # number of DMU 
s = 2 # number of inputs 
m = 1 # number of outputs 
inputs = datadea[,c(2,3)] 
outputs = datadea[,4] 
library(lpSolve) # load lpSolve package previously installed 
library(lpSolveAPI) 
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(rep(0,s),t(datadea[i,4])) # objective function coefficients 
f.con <- f.con <- rbind(aux, c(unlist(datadea[i,c(2,3)]), rep(0, m))) 

results <- lp("max",f.obj,f.con,f.dir,f.rhs,scale=1,compute.sens=TRUE) 
    multipliers <- results$solution # input and output weights 
efficiency <- results$objval # efficiency score 
duals <- results$duals # shadow prices 
if (i==1) { 
weights = c(multipliers[seq(1,s+m)]) 
effcrs <- efficiency 
lambdas = duals [seq(1,N)] 
} else { 
weights <- rbind(weights,c(multipliers[seq(1,s+m)])) 
effcrs <- rbind(effcrs , efficiency) 
lambdas <- rbind(lambdas,duals[seq(1,N)]) 
} 
} 
matrix_results <- cbind(effcrs,weights,lambdas) 
rownames(matrix_results) <- rownames(datadea) 
colnames(matrix_results) <- c("efficiency",colnames(datadea)[1:(s+m)], 
rownames(datadea)) 
rownames(matrix_results) <- rownames(datadea) 

crosseffmin = matrix(0,nrow=N,ncol=N) # initialize cross efficiency matrix 
i=18 
totaloutputs <- sum(outputs) ; 
totaloutputs = totaloutputs-as.numeric(outputs[i]) 
totalinputs <- colSums(inputs) ; 
totalinputs = totalinputs-as.numeric(unlist(inputs[i,])) 
f.obj <- c(totaloutputs,as.numeric(-totalinputs)) 
aux1 <- cbind(outputs,-1*inputs); aux11 = aux1[which(row(aux1)[,1]!=i),] ; 
aux1<-aux11[1:(N-1),] 
aux1<- rbind(aux1,c(0*rep(1,m),as.numeric(inputs[i,]))) 
aux1<- rbind(aux1,c(as.numeric(outputs[i]), 
effcrs[i]*as.numeric(-inputs[i,]))) 
f.con <- aux1 
print(aux1) 

を、あなたは、以下の出力で確認することができる行22の後曖昧行番号を見ることができます。

aux1 
    outputs Input1Cash Input2LEV 
1   1  -5.0  -4.0 
2   1  -6.0  -5.0 
3   1  -4.0  -5.0 
4   1  -8.0  -5.0 
5   1  -5.0  -6.0 
6   1  -8.0  -3.0 
7   1  -4.4  -4.4 
8   1  -2.6  -8.0 
9   1  -3.4  -8.0 
10  1  -3.6  -4.4 
11  1  -2.0  -7.0 
12  1  -3.0  -7.0 
13  1  -3.0  -5.6 
14  1  -2.6  -5.0 
15  1  -4.0  -4.0 
16  1  -5.0  -3.2 
17  1  -6.0  -4.0 
19  1  -7.0  -3.0 
20  1  -6.0  -2.5 
21  1  -8.0  -2.0 
22  1  -9.0  -2.0 
221  0  4.0  3.5 
23  1  -4.0  -3.5 

この点ですべてのヘルプは大

+0

@ycwコード内の最後の2行を読んだように動作しませんでした。基本的にオブジェクトaux1をf.conに渡しています。 aux1をチェックする必要があります。 – Hibu

+0

どういう意味ですか?私はあなたの投稿を変更しませんでした。私がしたのは、 'dataframe'タグを追加して他の人があなたの投稿を簡単に見つけることができるようにすることでした。 – www

+0

@ycw oh okありがとう – Hibu

答えて

1

を理解されるであろう私はあなたの分析に精通していませんが、rbindを使用してデータを結合しているようです。行名が期待通りでない場合は、aux1<- rbind(aux1,c(0*rep(1,m),as.numeric(inputs[i,])))行の後に次の操作を実行できます。

rownames(aux1) <- 1:nrow(aux1) 

これは、行名が行番号と同じであることを確認します。

+1

つまり、NULLに 'rownames(aux1)< - NULL'を割り当てます – akrun

+1

@akrun共有してくれてありがとう! – www

0

問題は18行目を省略し、次に22行目を追加することにあります。 data.frameの18行目を省略すると、row.namesは新しく作成されませんが、同じです。だから、18は欠けていますが、22はまだそこにあります。

# .... your code here ... 
f.obj <- c(totaloutputs,as.numeric(-totalinputs)) 
aux1 <- cbind(outputs,-1*inputs); aux11 = aux1[which(row(aux1)[,1]!=i),] ; 
aux1<-aux11[1:(N-1),] 
row.names(aux1) <- 1:nrow(aux1) # this is the new line 
aux1<- rbind(aux1,c(0*rep(1,m),as.numeric(inputs[i,]))) 
aux1<- rbind(aux1,c(as.numeric(outputs[i]), 
        effcrs[i]*as.numeric(-inputs[i,]))) 
f.con <- aux1 
print(aux1) 

row.namesで始まる行は新しいものです。これをあなたの現在のコードに埋め込み、evreythingは正常に動作します!これは、row.namesを書き換えて、22行目を安全に追加できるようにします。

関連する問題