如何处理R降价中的错误?

问题描述 投票:0回答:1

我正在为一个学校项目做一份R Markdown报告,当我在R上执行我的代码块时,一切都很好,但是当我尝试将其编织为html时,我得到了警告,并且执行被暂停。

这里是收到警告的图片:

enter image description here

您可以看到,当我执行块时,它可以正常工作,但是当我尝试编织时,出现此错误:

Quitting from lines 300-301 (rapport.Rmd) 
Error in Math.factor(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,  : 
  'round' not meaningful for factors
Calls: <Anonymous> ... eval -> plot_confusion_matrix -> data.frame -> Math.factor
De plus : Warning messages:
1: Removed 26508 rows containing non-finite values (stat_density). 
2: Removed 10 rows containing missing values (geom_vline). 
Execution haltered

有关信息,我正在使用的功能如下:

plot_confusion_matrix <- function(verset, sSubtitle) {
    tst <- data.frame(round(verset$predicted,0), verset$Class)
    opts <-  c("Predicted", "True")
    names(tst) <- opts
    cf <- plyr::count(tst)
    cf[opts][cf[opts]==0] <- "Not Fraud"
    cf[opts][cf[opts]==1] <- "Fraud"

    ggplot(data =  cf, mapping = aes(x = True, y = Predicted)) +
      labs(title = "Confusion matrix", subtitle = sSubtitle) +
      geom_tile(aes(fill = freq), colour = "grey") +
      geom_text(aes(label = sprintf("%1.0f", freq)), vjust = 1) +
      scale_fill_gradient(low = "blue", high = "red") +
      theme_bw() + theme(legend.position = "none")

}

如果有人在想,这里是test_svm的结构:

> str(test_svm)
'data.frame':   85191 obs. of  32 variables:
 $ Time     : num  7 9 10 12 12 13 15 17 23 26 ...
 $ V1       : num  -0.894 -0.338 0.385 -0.752 1.103 ...
 $ V2       : num  0.2862 1.1196 0.6161 0.3455 -0.0403 ...
 $ V3       : num  -0.113 1.044 -0.874 2.057 1.267 ...
 $ V4       : num  -0.272 -0.222 -0.094 -1.469 1.289 ...
 $ V5       : num  2.67 0.499 2.925 -1.158 -0.736 ...
 $ V6       : num  3.7218 -0.2468 3.317 -0.0778 0.2881 ...
 $ V7       : num  0.37 0.652 0.47 -0.609 -0.586 ...
 $ V8       : num  0.8511 0.0695 0.5382 0.0036 0.1894 ...
 $ V9       : num  -0.392 -0.737 -0.559 -0.436 0.782 ...
 $ V10      : num  -0.41 -0.367 0.31 0.748 -0.268 ...
 $ V11      : num  -0.705 1.018 -0.259 -0.794 -0.45 ...
 $ V12      : num  -0.11 0.836 -0.326 -0.77 0.937 ...
 $ V13      : num  -0.286 1.007 -0.09 1.048 0.708 ...
 $ V14      : num  0.0744 -0.4435 0.3628 -1.0666 -0.4686 ...
 $ V15      : num  -0.329 0.15 0.929 1.107 0.355 ...
 $ V16      : num  -0.21 0.739 -0.129 1.66 -0.247 ...
 $ V17      : num  -0.49977 -0.54098 -0.80998 -0.27927 -0.00921 ...
 $ V18      : num  0.119 0.477 0.36 -0.42 -0.596 ...
 $ V19      : num  0.57 0.452 0.708 0.433 -0.576 ...
 $ V20      : num  0.0527 0.2037 0.126 0.2635 -0.1139 ...
 $ V21      : num  -0.0734 -0.2469 0.0499 0.4996 -0.0246 ...
 $ V22      : num  -0.268 -0.634 0.238 1.354 0.196 ...
 $ V23      : num  -0.20423 -0.12079 0.00913 -0.25657 0.0138 ...
 $ V24      : num  1.0116 -0.385 0.9967 -0.0651 0.1038 ...
 $ V25      : num  0.3732 -0.0697 -0.7673 -0.0391 0.3643 ...
 $ V26      : num  -0.3842 0.0942 -0.4922 -0.0871 -0.3823 ...
 $ V27      : num  0.0117 0.2462 0.0425 -0.181 0.0928 ...
 $ V28      : num  0.1424 0.0831 -0.0543 0.1294 0.0371 ...
 $ Amount   : num  93.2 3.68 9.99 15.99 12.99 ...
 $ Class    : int  0 0 0 0 0 0 0 0 0 0 ...
 $ predicted: num  0.0148 0.0169 0.0337 0.0176 0.0364 ...

[请注意,当我尝试在R上运行代码时,我不会收到任何警告。

r markdown r-markdown
1个回答
0
投票

首先,您得到的是错误,而不是警告。其次,重新编写函数并添加类似的内容,将因子转换为数字(discussion):

verset$predicted <- as.numeric(as.character(verset$predicted))

在不检查数据的情况下,我可以说的太多。

© www.soinside.com 2019 - 2024. All rights reserved.