��Ū ���ǡ����ޤ����ٿ�ʬ��ɽ�ǡ����˴�Ť��ơ�ROC �������������ޤ���ROC ���������Ѥ�����롣 ����ˡ������ ���·����������ǡ����ˤĤ��� ROC0(disease, normal, lowest=NULL, width=NULL) ����disease ���·���¬���ͥ٥��ȥ� ����normal �Է���¬���ͥ٥��ȥ� ����lowest �Ǥ⾮�����ͤΥ���Τ褤���� ����width �������ʷ����١ˤΥ���Τ褤���� ���� lowest �� width ���ǥե�����ͤʤ顤�ǡ�������Ŭ���˷��� �ٿ�ʬ��ɽ�η��ǤޤȤ���Ƥ���ǡ����ˤĤ��� ROC(x, disease, normal) ����x ʬ��ɽ�β����ͤΥ٥��ȥ�����껲�ȡ� ����disease ���·����ٿ�ʬ�ۥ٥��ȥ� ����normal �Է����ٿ�ʬ�ۥ٥��ȥ� ������ ���ȡ���ϡ��ʲ��� 1 �Ԥԡ�����R ������˥ڡ����Ȥ��� source("http://aoki2.si.gunma-u.ac.jp/R/src/ROC.R", encoding="euc-jp") # ���ǡ����ޤ����ٿ�ʬ��ɽ�ǡ����˴�Ť��ơ�ROC �������������ޤ���ROC ���������Ѥ������ # ���ǡ���������Ȥ��ʸ�Ҥ��� ROC �ؿ���ɬ�פʤΤ����ա� ROC0 <- function( disease, # ���·���¬���ͥ٥��ȥ� normal, # �Է���¬���ͥ٥��ȥ� lowest=NULL, # �ǡ����κǾ��ͤ�꾮��������Τ褤���� width=NULL) # �ٿ�ʬ�ۤ�������볬�����Υ���Τ褤���� { my.hist <- function(x, brks) # R �� hist �ؿ��ϡ���³��ΰ���������饷���ΤǼ����δؿ���� { k <- length(brks) freq <- numeric(k) for (i in 1:(k-1)) { freq[i] <- sum(brks[i] <= x & x < brks[i+1]) } freq[k] <- sum(x >= brks[k]) freq } x <- c(disease, normal) # �ǡ�����ס��뤹�� min.x <- min(x) # �Ǿ��� max.x <- max(x) # �Ǿ��� cat("�Ǿ��� x = ", min.x, "\n") cat("������ x = ", max.x, "\n\n") if (is.null(lowest) || is.null(width)) { temp <- pretty(c(disease, normal), n=min(length(disease)+length(normal), 50)) lowest <- temp[1] width <- diff(temp)[1] cat("�Ǿ��ͤ�꾮��������Τ褤���� = ", lowest, "\n") cat("�ٿ�ʬ�ۤ�������볬�������ڤ�Τ褤���� = ", width, "\n\n") } brks <- seq(lowest, max.x+width, by=width) ROC(brks, my.hist(disease, brks), my.hist(normal, brks)) } # �ٿ�ʬ��ɽ�����ʤ��Ȥ������ǡ�������������Ȥ��ˤ⡤�������Ȥ��ƻȤ��� ROC <- function( x, # ʬ��ɽ�β����ͤΥ٥��ȥ� disease, # ���·����ٿ�ʬ�ۥ٥��ȥ� normal) # �Է����ٿ�ʬ�ۥ٥��ȥ� { k <- length(x) # �ٿ�ʬ��ɽ��Ĺ�� stopifnot(k == length(disease) && k == length(normal)) # �ǡ����Υ����å� Sensitivity <- c(rev(cumsum(rev(disease)))/sum(disease), 0) False.Positive.Rate <- c(rev(cumsum(rev(normal)))/sum(normal), 0) Specificity <- 1-False.Positive.Rate plot(False.Positive.Rate, Sensitivity, type="b") abline(h=c(0, 1), v=c(0, 1)) c.index <- sum(sapply(1:k, function(i) (False.Positive.Rate[i]-False.Positive.Rate[i+1])*(Sensitivity[i+1]+Sensitivity[i])/2)) # area under ROC curve result <- cbind(x, disease, normal, Sensitivity[-k-1], Specificity[-k-1], False.Positive.Rate[-k-1]) rownames(result) <- as.character(1:k) colnames(result) <- c("Value", "Disease", "Normal", "Sensitivity", "Specificity", "F.P. rate") list(result=result, c.index=c.index) } ������ ���ͤˤ����ڡ����ˤ����㡣 FRA <- c(100, 220, 230, 240, 250, 260, 270, 280, 290, 300, 320, 340, 360, 400) FRA.disease <- c(3, 2, 1, 4, 7, 4, 16, 5, 3, 9, 10, 5, 10, 21) FRA.normal <- c(25, 7, 19, 17, 7, 8, 7, 6, 2, 2, 0, 0, 0, 0) ROC(FRA, FRA.disease, FRA.normal) ���ǡ���������Ȥ����㡣 disease.x <- as.integer(rnorm(200, mean=110, sd=10)) # ʿ����=110��ɸ���к�=10 �Υƥ��ȥǡ�����200�������� normal.x <- as.integer(rnorm(300, mean=100, sd=10)) # ʿ����=100��ɸ���к�=10 �Υƥ��ȥǡ�����300�������� ROC0(disease.x, normal.x) ���Ϸ���� > FRA <- c(100, 220, 230, 240, 250, 260, 270, 280, 290, 300, 320, 340, 360, 400) > FRA.disease <- c(3, 2, 1, 4, 7, 4, 16, 5, 3, 9, 10, 5, 10, 21) > FRA.normal <- c(25, 7, 19, 17, 7, 8, 7, 6, 2, 2, 0, 0, 0, 0) > ROC(FRA, FRA.disease, FRA.normal) $result Value Disease Normal Sensitivity Specificity F.P. rate # Value �ϳ���β����͡ʶ�֤˴ޤޤ��� 1 100 3 25 1.00 0.00 1.00 # �Ĥޤꡤ�ǽ�γ���ϡ�100 �ʾ塤220 ̤���ȹͤ��� 2 220 2 7 0.97 0.25 0.75 # F.P. rate �ϵ�����Ψ 3 230 1 19 0.95 0.32 0.68 4 240 4 17 0.94 0.51 0.49 5 250 7 7 0.90 0.68 0.32 6 260 4 8 0.83 0.75 0.25 7 270 16 7 0.79 0.83 0.17 8 280 5 6 0.63 0.90 0.10 9 290 3 2 0.58 0.96 0.04 10 300 9 2 0.55 0.98 0.02 11 320 10 0 0.46 1.00 0.00 12 340 5 0 0.36 1.00 0.00 13 360 10 0 0.31 1.00 0.00 14 400 21 0 0.21 1.00 0.00 $c.index # ROC ���������� [1] 0.88215 �ʲ��Τ褦�ʿޤ��������> disease.x <- as.integer(rnorm(200, mean=110, sd=10)) > normal.x <- as.integer(rnorm(300, mean=100, sd=10)) > ROC0(disease.x, normal.x) �Ǿ��� x = 68 ������ x = 132 �Ǿ��ͤ�꾮��������Τ褤���� = 68 �ٿ�ʬ�ۤ�������볬�������ڤ�褤���� = 1 $result Value Disease Normal Sensitivity Specificity F.P. rate 1 68 0 1 1.000 0.000000000 1.000000000 2 69 0 0 1.000 0.003333333 0.996666667 3 70 0 0 1.000 0.003333333 0.996666667 4 71 0 0 1.000 0.003333333 0.996666667 5 72 0 0 1.000 0.003333333 0.996666667 6 73 0 1 1.000 0.003333333 0.996666667 7 74 0 0 1.000 0.006666667 0.993333333 8 75 0 2 1.000 0.006666667 0.993333333 9 76 0 1 1.000 0.013333333 0.986666667 ���������ά 59 126 2 0 0.035 1.000000000 0.000000000 60 127 1 0 0.025 1.000000000 0.000000000 61 128 1 0 0.020 1.000000000 0.000000000 62 129 1 0 0.015 1.000000000 0.000000000 63 130 0 0 0.010 1.000000000 0.000000000 64 131 0 0 0.010 1.000000000 0.000000000 65 132 2 0 0.010 1.000000000 0.000000000 66 133 0 0 0.000 1.000000000 0.000000000 $c.index [1] 0.7734917
���ͤˤ����ڡ���