我試圖從邏輯迴歸中得分客戶。在計算它們的概率後,我創建了一個包含這些變量的數據框: CUSTID,depvar,prob 接下來,我可以得到概率的十進制數。R中的If-then-else語句
> quantile(prob, p=seq(0, 1, length=11), type=5)
0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%
0.0373546 0.1990744 0.2961668 0.3748728 0.4393759 0.4970248 0.5554679 0.6162423 0.6905081 0.8007684 0.9999996
最後,我想將十分位數附加到數據幀的末尾。這裏是我的代碼:
> #Chained if-then-else
> if (prob <=.1990744) {decile<-10} else if (prob >.1990744) {decile<-9} else if (prob >.2961668){decile<-8} else {if (prob >.3748728) {decile<-7} else if(prob >.4393759) {decile<-6} else if (prob >.4970248){decile<-5} else {if (prob >.5554679) {decile<-4} else if(prob >.6162423) {decile<-3} else if (prob >.6905081){decile<-2} else {if (prob >.8007684) {decile<-1} else {decile=0}
+
正如你看到的,我留下了一個+號,如同R的期待我輸入別的東西。我應該如何構造這個if-then-else語句?
謝謝。
「+」是括號不匹配的結果。無論如何,你不能在這裏使用'if'和'else'。你可以使用向量化的嵌套'ifelse',但你應該使用'cut'或'findInterval'。 – Roland
要完成@Roland評論,在對代碼進行重新格式化之後,您有兩個位於'else'和'if'之間的地方,搜索'else {if'。嘗試使用像這樣的長語句的腳本並縮進內部語句,它確實有助於確定何時不能正確關閉。 – Tensibai