【发布时间】:2021-03-18 03:20:20
【问题描述】:
创建了一个 Logistic 模型:
Banks_Logit<- glm(Banks$Financial.Condition ~ .,data = Banks, family="binomial")
options(scipen=999)
summary(Banks_Logit)
然后:
pred <-predict(Banks_Logit,Banks)
gain <-gains(Banks$Financial.Condition,pred,groups=20)
plot(c(0,gain$cume.pct.of.total*sum(Banks$Financial.Condition))~
c(0,gain$cume.obs),
xlab = "Observations", ylab = "Cumulative", main="Model Performance", type="l")
lines(c(0,sum(Banks$Financial.Condition))~c(0,dim(Banks)[1]),lty=2)
library(caret)
confusionMatrix(ifelse(pred >0.5, 1,0), Banks$Financial.Condition)
错误 -
错误:data 和 reference 应该是具有相同水平的因子。
这是预测数据
1 2 3 4 5 6 7 8 9 10
0.9999999999999997779554 0.9999999999999997779554 0.9999999999999997779554 0.9999999999999997779554 0.9999999999999997779554 0.9999999999999997779554 0.9999999999138624584560 0.9999999999891036051025 0.9999999999995110577800 0.9999999999999997779554
11 12 13 14 15 16 17 18 19 20
0.0000000000176082421301 0.0000000000352379135751 0.0000000000431425778626 0.0000000000000002220446 0.0000000000002227450487 0.0000000000000002220446 0.0000000000000002220446 0.0000000000000002220446 0.0000000000000002220446 0.0000000000000002220446
str(pred)
Named num [1:20] 1 1 1 1 1 ...
- attr(*, "names")= chr [1:20] "1" "2" "3" "4" ...
this is the dataset (Str(Banks):
'data.frame': 20 obs. of 5 variables:
$ Obs : int 1 2 3 4 5 6 7 8 9 10 ...
$ Financial.Condition: int 1 1 1 1 1 1 1 1 1 1 ...
$ TotCap.Assets : num 9.7 1 6.9 5.8 4.3 9.1 11.9 8.1 9.3 1.1 ...
$ TotExp.Assets : num 0.12 0.11 0.09 0.1 0.11 0.13 0.1 0.13 0.16 0.16 ...
$ TotLns.Lses.Assets : num 0.65 0.62 1.02 0.67 0.69 0.74 0.79 0.63 0.72 0.57 ...
【问题讨论】:
-
我想我只是误解了级别的事情。
-
pred和Banks$Financial.Condition都必须是因子(请参阅?confusionMatrix,如果您对因子感到困惑,请参阅 ?factor)。如果你只需要一张桌子,table(ifelse(pred >0.5, 1,0), Banks$Financial.Condition)就可以了。
标签: r plot logistic-regression r-caret confusion-matrix