我试图从逻辑回归中得分客户。在计算它们的概率后,我创建了一个包含这些变量的数据框: 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