ROC 曲线单独绘制而不进入绘图网格,R 中的 plot_grid() 函数不起作用
ROC curves plotting individually and not going into plot grids, plot_grid() function in R not working
我只是想在 Rstudio 的绘图网格中绘制 ROC 曲线,神经网络模型都在工作,但 ROC 曲线不会进入网格布局,它们单独绘制但有很多不止于此,markdown 文件将有 20 页长。我已将所有模型减少为两个神经元作为虚拟数据以使其快速 运行,只需要调整 setwd() 和读取的数据行。
这是空的绘图网格:
但在这里您可以看到它们有效并且将单独绘制:
library(Rcpp)
library(RSNNS)
library(ggplot2)
library(plotROC)
library(tidyr)
library(cowplot)
setwd("**set working directory or copy paste sample**")
data <- read.csv("WDBC.csv", header=T)
cancerdata <- data[,2:11] #numeric data
cancerdata <- scale(cancerdata) # normalizes the cancerdata
numHneurons2 = 2
numHneurons3 = 3
numHneurons10 = 10
numHneurons20 = 20
numHneurons50 = 50
DecTargets = decodeClassLabels(data[,12]) #non-numeric decision data
head(DecTargets) #decoding based on Diagnosis, M or B for malignant or benign
train.test3 <- splitForTrainingAndTest(cancerdata, DecTargets,ratio = 0.50) # split
train.test10 <- splitForTrainingAndTest(cancerdata, DecTargets,ratio = 0.50) # split
train.test20 <- splitForTrainingAndTest(cancerdata, DecTargets,ratio = 0.50) # split
train.test50 <- splitForTrainingAndTest(cancerdata, DecTargets,ratio = 0.50) # split
model3_02 <- mlp(train.test3$inputsTrain, train.test3$targetsTrain, # build model3
size = numHneurons2, learnFuncParams = c(0.02),maxit = 10000,
inputsTest = train.test3$inputsTest,
targetsTest = train.test3$targetsTest)
model10_02 <- mlp(train.test10$inputsTrain, train.test10$targetsTrain, # build model10
size = numHneurons2, learnFuncParams = c(0.02),maxit = 10000,
inputsTest = train.test10$inputsTest,
targetsTest = train.test10$targetsTest)
model20_02 <- mlp(train.test20$inputsTrain, train.test20$targetsTrain, # build model20
size = numHneurons2, learnFuncParams = c(0.02),maxit = 10000,
inputsTest = train.test20$inputsTest,
targetsTest = train.test20$targetsTest)
model50_02 <- mlp(train.test50$inputsTrain, train.test50$targetsTrain, # build model50
size = numHneurons2, learnFuncParams = c(0.02),maxit = 10000,
inputsTest = train.test50$inputsTest,
targetsTest = train.test50$targetsTest)
#--------------------------------------
# ROC curves:
#--------------------------------------
roc3_02 <- plotROC(fitted.values(model3_02), train.test3$targetsTrain)
roc10_02 <- plotROC(fitted.values(model10_02), train.test10$targetsTrain)
roc20_02 <- plotROC(fitted.values(model20_02), train.test10$targetsTrain)
roc50_02 <- plotROC(fitted.values(model50_02), train.test10$targetsTrain)
plotsROC02 <- plot_grid(roc3_02, roc10_02, roc20_02, roc50_02, labels = c("N3", "N10", "N20", "N50"), label_size = 16)
title <- ggplot() +
labs(title = "ANN Models with 0.02 Learning Rate",
subtitle = "Models with 3, 10, 20, and 50 Neurons") +
theme_minimal()
plot_grid(title, plotsROC02, ncol = 1, rel_heights = c(0.1, 0.9))
这是癌症数据的示例:
PatientID radius texture perimeter area smoothness compactness concavity concavePoints symmetry fractalDimension Diagnosis
1 842302 17.990 10.38 122.80 1001.0 0.11840 0.27760 0.30010 0.14710 0.2419 0.07871 M
2 842517 20.570 17.77 132.90 1326.0 0.08474 0.07864 0.08690 0.07017 0.1812 0.05667 M
3 84300903 19.690 21.25 130.00 1203.0 0.10960 0.15990 0.19740 0.12790 0.2069 0.05999 M
4 84348301 11.420 20.38 77.58 386.1 0.14250 0.28390 0.24140 0.10520 0.2597 0.09744 M
5 84358402 20.290 14.34 135.10 1297.0 0.10030 0.13280 0.19800 0.10430 0.1809 0.05883 M
6 843786 12.450 15.70 82.57 477.1 0.12780 0.17000 0.15780 0.08089 0.2087 0.07613 M
7 844359 18.250 19.98 119.60 1040.0 0.09463 0.10900 0.11270 0.07400 0.1794 0.05742 M
8 84458202 13.710 20.83 90.20 577.9 0.11890 0.16450 0.09366 0.05985 0.2196 0.07451 M
9 844981 13.000 21.82 87.50 519.8 0.12730 0.19320 0.18590 0.09353 0.2350 0.07389 M
10 84501001 12.460 24.04 83.97 475.9 0.11860 0.23960 0.22730 0.08543 0.2030 0.08243 M
11 845636 16.020 23.24 102.70 797.8 0.08206 0.06669 0.03299 0.03323 0.1528 0.05697 M
12 84610002 15.780 17.89 103.60 781.0 0.09710 0.12920 0.09954 0.06606 0.1842 0.06082 M
13 846226 19.170 24.80 132.40 1123.0 0.09740 0.24580 0.20650 0.11180 0.2397 0.07800 M
14 846381 15.850 23.95 103.70 782.7 0.08401 0.10020 0.09938 0.05364 0.1847 0.05338 M
15 84667401 13.730 22.61 93.60 578.3 0.11310 0.22930 0.21280 0.08025 0.2069 0.07682 M
16 84799002 14.540 27.54 96.73 658.8 0.11390 0.15950 0.16390 0.07364 0.2303 0.07077 M
17 848406 14.680 20.13 94.74 684.5 0.09867 0.07200 0.07395 0.05259 0.1586 0.05922 M
18 84862001 16.130 20.68 108.10 798.8 0.11700 0.20220 0.17220 0.10280 0.2164 0.07356 M
19 849014 19.810 22.15 130.00 1260.0 0.09831 0.10270 0.14790 0.09498 0.1582 0.05395 M
20 8510426 13.540 14.36 87.46 566.3 0.09779 0.08129 0.06664 0.04781 0.1885 0.05766 B
21 8510653 13.080 15.71 85.63 520.0 0.10750 0.12700 0.04568 0.03110 0.1967 0.06811 B
22 8510824 9.504 12.44 60.34 273.9 0.10240 0.06492 0.02956 0.02076 0.1815 0.06905 B
23 8511133 15.340 14.26 102.50 704.4 0.10730 0.21350 0.20770 0.09756 0.2521 0.07032 M
24 851509 21.160 23.04 137.20 1404.0 0.09428 0.10220 0.10970 0.08632 0.1769 0.05278 M
25 852552 16.650 21.38 110.00 904.6 0.11210 0.14570 0.15250 0.09170 0.1995 0.06330 M
26 852631 17.140 16.40 116.00 912.7 0.11860 0.22760 0.22290 0.14010 0.3040 0.07413 M
27 852763 14.580 21.53 97.41 644.8 0.10540 0.18680 0.14250 0.08783 0.2252 0.06924 M
28 852781 18.610 20.25 122.10 1094.0 0.09440 0.10660 0.14900 0.07731 0.1697 0.05699 M
29 852973 15.300 25.27 102.40 732.4 0.10820 0.16970 0.16830 0.08751 0.1926 0.06540 M
30 853201 17.570 15.05 115.00 955.1 0.09847 0.11570 0.09875 0.07953 0.1739 0.06149 M
我会根据 pROC
包(我创建了一个维护包)给你一个答案。
首先,我创建了一个 ROC 曲线列表:
library(pROC)
roc3_02 <- roc(as.vector(train.test3$targetsTrain), as.vector(fitted.values(model3_02)))
roc10_02 <- roc(as.vector(train.test3$targetsTrain), as.vector(fitted.values(model10_02)))
roc20_02 <- roc(as.vector(train.test3$targetsTrain), as.vector(fitted.values(model20_02)))
roc50_02 <- roc(as.vector(train.test3$targetsTrain), as.vector(fitted.values(model50_02)))
roc_list <- list(
roc3_02=roc3_02,
roc10_02=roc10_02,
roc20_02=roc20_02,
roc50_02=roc50_02)
然后我可以结合facet_wrap
使用pROC的ggroc
函数。
library(ggplot)
g.group <- ggroc(roc_list, aes="group")
g.group
g.group + facet_wrap(.~name)
然后您可以像自定义任何 ggplot2 绘图一样自定义绘图。
我只是想在 Rstudio 的绘图网格中绘制 ROC 曲线,神经网络模型都在工作,但 ROC 曲线不会进入网格布局,它们单独绘制但有很多不止于此,markdown 文件将有 20 页长。我已将所有模型减少为两个神经元作为虚拟数据以使其快速 运行,只需要调整 setwd() 和读取的数据行。
这是空的绘图网格:
但在这里您可以看到它们有效并且将单独绘制:
library(Rcpp)
library(RSNNS)
library(ggplot2)
library(plotROC)
library(tidyr)
library(cowplot)
setwd("**set working directory or copy paste sample**")
data <- read.csv("WDBC.csv", header=T)
cancerdata <- data[,2:11] #numeric data
cancerdata <- scale(cancerdata) # normalizes the cancerdata
numHneurons2 = 2
numHneurons3 = 3
numHneurons10 = 10
numHneurons20 = 20
numHneurons50 = 50
DecTargets = decodeClassLabels(data[,12]) #non-numeric decision data
head(DecTargets) #decoding based on Diagnosis, M or B for malignant or benign
train.test3 <- splitForTrainingAndTest(cancerdata, DecTargets,ratio = 0.50) # split
train.test10 <- splitForTrainingAndTest(cancerdata, DecTargets,ratio = 0.50) # split
train.test20 <- splitForTrainingAndTest(cancerdata, DecTargets,ratio = 0.50) # split
train.test50 <- splitForTrainingAndTest(cancerdata, DecTargets,ratio = 0.50) # split
model3_02 <- mlp(train.test3$inputsTrain, train.test3$targetsTrain, # build model3
size = numHneurons2, learnFuncParams = c(0.02),maxit = 10000,
inputsTest = train.test3$inputsTest,
targetsTest = train.test3$targetsTest)
model10_02 <- mlp(train.test10$inputsTrain, train.test10$targetsTrain, # build model10
size = numHneurons2, learnFuncParams = c(0.02),maxit = 10000,
inputsTest = train.test10$inputsTest,
targetsTest = train.test10$targetsTest)
model20_02 <- mlp(train.test20$inputsTrain, train.test20$targetsTrain, # build model20
size = numHneurons2, learnFuncParams = c(0.02),maxit = 10000,
inputsTest = train.test20$inputsTest,
targetsTest = train.test20$targetsTest)
model50_02 <- mlp(train.test50$inputsTrain, train.test50$targetsTrain, # build model50
size = numHneurons2, learnFuncParams = c(0.02),maxit = 10000,
inputsTest = train.test50$inputsTest,
targetsTest = train.test50$targetsTest)
#--------------------------------------
# ROC curves:
#--------------------------------------
roc3_02 <- plotROC(fitted.values(model3_02), train.test3$targetsTrain)
roc10_02 <- plotROC(fitted.values(model10_02), train.test10$targetsTrain)
roc20_02 <- plotROC(fitted.values(model20_02), train.test10$targetsTrain)
roc50_02 <- plotROC(fitted.values(model50_02), train.test10$targetsTrain)
plotsROC02 <- plot_grid(roc3_02, roc10_02, roc20_02, roc50_02, labels = c("N3", "N10", "N20", "N50"), label_size = 16)
title <- ggplot() +
labs(title = "ANN Models with 0.02 Learning Rate",
subtitle = "Models with 3, 10, 20, and 50 Neurons") +
theme_minimal()
plot_grid(title, plotsROC02, ncol = 1, rel_heights = c(0.1, 0.9))
这是癌症数据的示例:
PatientID radius texture perimeter area smoothness compactness concavity concavePoints symmetry fractalDimension Diagnosis
1 842302 17.990 10.38 122.80 1001.0 0.11840 0.27760 0.30010 0.14710 0.2419 0.07871 M
2 842517 20.570 17.77 132.90 1326.0 0.08474 0.07864 0.08690 0.07017 0.1812 0.05667 M
3 84300903 19.690 21.25 130.00 1203.0 0.10960 0.15990 0.19740 0.12790 0.2069 0.05999 M
4 84348301 11.420 20.38 77.58 386.1 0.14250 0.28390 0.24140 0.10520 0.2597 0.09744 M
5 84358402 20.290 14.34 135.10 1297.0 0.10030 0.13280 0.19800 0.10430 0.1809 0.05883 M
6 843786 12.450 15.70 82.57 477.1 0.12780 0.17000 0.15780 0.08089 0.2087 0.07613 M
7 844359 18.250 19.98 119.60 1040.0 0.09463 0.10900 0.11270 0.07400 0.1794 0.05742 M
8 84458202 13.710 20.83 90.20 577.9 0.11890 0.16450 0.09366 0.05985 0.2196 0.07451 M
9 844981 13.000 21.82 87.50 519.8 0.12730 0.19320 0.18590 0.09353 0.2350 0.07389 M
10 84501001 12.460 24.04 83.97 475.9 0.11860 0.23960 0.22730 0.08543 0.2030 0.08243 M
11 845636 16.020 23.24 102.70 797.8 0.08206 0.06669 0.03299 0.03323 0.1528 0.05697 M
12 84610002 15.780 17.89 103.60 781.0 0.09710 0.12920 0.09954 0.06606 0.1842 0.06082 M
13 846226 19.170 24.80 132.40 1123.0 0.09740 0.24580 0.20650 0.11180 0.2397 0.07800 M
14 846381 15.850 23.95 103.70 782.7 0.08401 0.10020 0.09938 0.05364 0.1847 0.05338 M
15 84667401 13.730 22.61 93.60 578.3 0.11310 0.22930 0.21280 0.08025 0.2069 0.07682 M
16 84799002 14.540 27.54 96.73 658.8 0.11390 0.15950 0.16390 0.07364 0.2303 0.07077 M
17 848406 14.680 20.13 94.74 684.5 0.09867 0.07200 0.07395 0.05259 0.1586 0.05922 M
18 84862001 16.130 20.68 108.10 798.8 0.11700 0.20220 0.17220 0.10280 0.2164 0.07356 M
19 849014 19.810 22.15 130.00 1260.0 0.09831 0.10270 0.14790 0.09498 0.1582 0.05395 M
20 8510426 13.540 14.36 87.46 566.3 0.09779 0.08129 0.06664 0.04781 0.1885 0.05766 B
21 8510653 13.080 15.71 85.63 520.0 0.10750 0.12700 0.04568 0.03110 0.1967 0.06811 B
22 8510824 9.504 12.44 60.34 273.9 0.10240 0.06492 0.02956 0.02076 0.1815 0.06905 B
23 8511133 15.340 14.26 102.50 704.4 0.10730 0.21350 0.20770 0.09756 0.2521 0.07032 M
24 851509 21.160 23.04 137.20 1404.0 0.09428 0.10220 0.10970 0.08632 0.1769 0.05278 M
25 852552 16.650 21.38 110.00 904.6 0.11210 0.14570 0.15250 0.09170 0.1995 0.06330 M
26 852631 17.140 16.40 116.00 912.7 0.11860 0.22760 0.22290 0.14010 0.3040 0.07413 M
27 852763 14.580 21.53 97.41 644.8 0.10540 0.18680 0.14250 0.08783 0.2252 0.06924 M
28 852781 18.610 20.25 122.10 1094.0 0.09440 0.10660 0.14900 0.07731 0.1697 0.05699 M
29 852973 15.300 25.27 102.40 732.4 0.10820 0.16970 0.16830 0.08751 0.1926 0.06540 M
30 853201 17.570 15.05 115.00 955.1 0.09847 0.11570 0.09875 0.07953 0.1739 0.06149 M
我会根据 pROC
包(我创建了一个维护包)给你一个答案。
首先,我创建了一个 ROC 曲线列表:
library(pROC)
roc3_02 <- roc(as.vector(train.test3$targetsTrain), as.vector(fitted.values(model3_02)))
roc10_02 <- roc(as.vector(train.test3$targetsTrain), as.vector(fitted.values(model10_02)))
roc20_02 <- roc(as.vector(train.test3$targetsTrain), as.vector(fitted.values(model20_02)))
roc50_02 <- roc(as.vector(train.test3$targetsTrain), as.vector(fitted.values(model50_02)))
roc_list <- list(
roc3_02=roc3_02,
roc10_02=roc10_02,
roc20_02=roc20_02,
roc50_02=roc50_02)
然后我可以结合facet_wrap
使用pROC的ggroc
函数。
library(ggplot)
g.group <- ggroc(roc_list, aes="group")
g.group
g.group + facet_wrap(.~name)
然后您可以像自定义任何 ggplot2 绘图一样自定义绘图。