有没有更优雅的方法来测试预测模型是否正确?

时间:2016-12-22 17:16:22

标签: r

我有一个建模/预测的更改和实际更改。预测的更改位于名为forecastHPIChange的列中,实际更改名为HPIChange。它采用以下形式:

$"ALTER DATABASE [{databaseName}] SET READ_WRITE"

我想测试143个实例,预测的符号对齐是否正确。所以实际上有四种情况:

  1. 预测为正,实际为正 - >正确阳性
  2. 预测为负,实际为负 - >正确阴性
  3. 预测为正,实际为负 - >不正确阳性
  4. 预测为负,实际为正 - >不正确阴性
  5. 为了检查这一点,我已经将以下代码整合在一起,我可以将它们输入到数据框中,但我想查看是否有更优雅的方法来进行此检查?

            HPIChange forecastHPIChange
    1              NA      1.547368e-02
    2   -0.0026155187      1.485668e-02
    3    0.0002906977      1.251108e-02
    4   -0.0077877127      1.718729e-02
    5    0.0200058841      2.143551e-02
    

2 个答案:

答案 0 :(得分:2)

在插入符号包中尝试confusionMatrix

library(caret)

make_factor <- function(x) factor(sign(x), levels = c(-1, 1))
signs <- as.data.frame(lapply(data1, make_factor))
with(signs, confusionMatrix(forecastHPIChange, reference = HPIChange))

或使用管道:

library(purrr)

data1 %>%
      map_df(make_factor) %>%
      { confusionMatrix(.$forecastHPIChange, reference = .$HPIChange) }

要么:

Confusion Matrix and Statistics

          Reference
Prediction -1 1
        -1  0 0
        1   2 2

               Accuracy : 0.5             
                 95% CI : (0.0676, 0.9324)
    No Information Rate : 0.5             
    P-Value [Acc > NIR] : 0.6875          

                  Kappa : 0               
 Mcnemar's Test P-Value : 0.4795          

            Sensitivity : 0.0             
            Specificity : 1.0             
         Pos Pred Value : NaN             
         Neg Pred Value : 0.5             
             Prevalence : 0.5             
         Detection Rate : 0.0             
   Detection Prevalence : 0.0             
      Balanced Accuracy : 0.5        

对于显示的输入并非显示所有因子级别,但如果实际输入确实具有所有因子级别,那么我们可以消除make_factor并仅使用sign

注意:上面使用的可重现形式的输入data1是:

data1 <- structure(list(HPIChange = c(NA, -0.0026155187, 0.0002906977, 
-0.0077877127, 0.0200058841), forecastHPIChange = c(0.01547368, 
0.01485668, 0.01251108, 0.01718729, 0.02143551)), .Names = c("HPIChange", 
"forecastHPIChange"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5"))

答案 1 :(得分:0)

从以下数据开始(稍微更改了您的示例数据,以便为所有类TP,FP,TN,FN提供数据点):

 data1
      HPIChange forecastHPIChange
1            NA        0.01547368
2 -0.0026155187        0.01485668
3  0.0002906977        0.01251108
4 -0.0077877127       -0.01718729
5  0.0200058841       -0.02143551

# transform the data1 to dataset data2 where we have only + and - labels (represented by +1 and -1)
data2 <- as.data.frame(sapply(data1, function(x) ifelse(x > 0, 1, -1)))

table(data2)       

    forecastHPIChange
HPIChange  -1 1
       -1   1 1   #  1,  1 = TP   1, -1 = FN
        1   1 1   # -1. -1 = TN  -1,  1 = FP

# using the package caret
library(caret)
confusionMatrix(data2$forecastHPIChange, data2$HPIChange)