{"id":833,"date":"2017-07-10T15:28:20","date_gmt":"2017-07-10T07:28:20","guid":{"rendered":"https:\/\/www.beneplot.com\/?p=833"},"modified":"2021-05-28T16:24:27","modified_gmt":"2021-05-28T08:24:27","slug":"%e7%a7%92%e6%87%82logistic%e5%9b%9e%e5%bd%92-r%e8%af%ad%e8%a8%80%e5%ae%9e%e6%93%8d%e8%ae%b2%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.beneplot.com\/?p=833","title":{"rendered":"logistic\u56de\u5f52\u2014R\u8bed\u8a00\u5b9e\u64cd"},"content":{"rendered":"<p>\u56de\u5f52\u6a21\u578b\u662f\u7edf\u8ba1\u4e2d\u6700\u5e38\u7528\u7684\u6a21\u578b\u4e4b\u4e00\uff0c\u5b83\u4e3b\u8981\u7528\u4e8e<strong>\u89e3\u91ca\u548c\u9884\u6d4b<\/strong>\u3002\u672c\u6587\u8981\u8bb2\u7684<strong>Logistic\u56de\u5f52<\/strong>\u53c8\u662f\u4e00\u79cd\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\u7684\u56de\u5f52\u6a21\u578b\u3002<\/p>\n<p>\u7ebf\u6027\u56de\u5f52\u662f\u4eba\u4eec\u6700\u719f\u6089\u7684\u56de\u5f52\u6a21\u578b\uff0c\u7b80\u8981\u8868\u8fbe\u4e3a\uff1aY = aX + b\uff0c\u5176\u4e2dX\u548cY\u90fd\u662f\u8fde\u7eed\u6570\u636e\uff0c\u6bd4\u5982\u60f3\u77e5\u9053\u4eba\u7684\u667a\u529b(X)\u80fd\u4e0d\u80fd\u9884\u6d4b\u5176\u672a\u6765\u7684\u6570\u5b66\u6210\u7ee9(Y)\u3002\u7136\u800c\u6709\u65f6\u5019\uff0c<strong>Y\u4e0d\u662f\u8fde\u7eed\u6570\u636e<\/strong>\uff0c\u6bd4\u5982\u4e00\u4e2a\u4eba\u4f1a\u4e0d\u4f1a\u56e0\u4e3a\u4f60\u7684\u5de5\u8d44\u6570\u989d(X)\u800c\u559c\u6b22\u4f60(Y)\uff0c\u8fd9\u91cc\u7684Y\u53ea\u6709\u559c\u6b22\u548c\u4e0d\u559c\u6b22\u4e24\u7c7b\uff1b\u518d\u6bd4\u5982\u4eca\u5929\u80a1\u5e02\u7684\u6536\u76ca\u7387(X)\u80fd\u4e0d\u80fd\u9884\u6d4b\u660e\u5929\u80a1\u5e02\u7684\u6da8\u8dcc(Y)\uff0c\u8fd9\u91cc\u7684Y\u53ea\u6709\u6da8\u548c\u8dcc\u4e24\u7c7b\u3002\u8fdb\u884c\u7ebf\u6027\u56de\u5f52\u9700\u8981\u6ee1\u8db3\u5f88\u591a\u524d\u63d0\u6761\u4ef6\uff0c\u6bd4\u5982balabala\u2026\uff0cLogistic\u56de\u5f52\u5219\u6ca1\u90a3\u4e48\u591a\u6761\u6761\u6846\u6846\u3002<\/p>\n<p>\u4e0e\u7ebf\u6027\u56de\u5f52\u4e00\u6837\uff0cLogistic\u56de\u5f52\u7684\u7b80\u8981\u8868\u8fbe\u4e5f\u662f\uff1aY = aX + b\u3002\u4e0d\u540c\u70b9\u5728\u4e8eY\u3002\u7ebf\u6027\u56de\u5f52\u4e2d\u4e00\u822cY\u5c31\u662fY\uff0c\u800c\u5728Logistic\u56de\u5f52\u4e2d\uff0c\u7531\u4e8e<strong>Y\u53ea\u6709\u4e24\u79cd\u9009\u62e9<\/strong>\uff08\u4e8b\u5b9e\u4e0a\u4e5f\u53ef\u4ee5\u67093\u30014\u30015\u2026\u7b49\u7b49\u591a\u7c7b\uff0c\u4f46\u4e0d\u5728\u672c\u6587\u8ba8\u8bba\u8303\u56f4\uff09\uff0c\u559c\u6b22\u6216\u4e0d\u559c\u6b22\u3001\u6da8\u6216\u8dcc\u3001\u6210\u529f\u6216\u5931\u8d25\u3001\u53d1\u751f\u6216\u672a\u53d1\u751f\u3001\u6b63\u9762\u6216\u53cd\u9762\u3001\u7537\u6216\u5973\u2026\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0cY\u8981\u4e48\u662f1\uff0c\u8981\u4e48\u662f0\u3002<strong>\u5047\u8bbeY\u662f1\u7684\u6982\u7387\u662fP1\uff0c\u662f0\u7684\u6982\u7387\u662fP0\uff0cP1+P0 = 1<\/strong>\uff0c<strong>Logistic\u56de\u5f52\u7684Y\u662fP1\/P0\u7684\u81ea\u7136\u5bf9\u6570<\/strong>\uff0c\u5373\uff1a<\/p>\n<p><strong>Y = LN(P1\/P0) = LN(P1\/(1-P1)) = aX + b<\/strong><\/p>\n<p><strong>\u4f8b\u5b501<\/strong>\uff1a\u5047\u5982TA\u559c\u6b22\u4f60\u7684\u6982\u7387\u662fP1 = 50%\uff0c\u4e0d\u559c\u6b22\u4f60\u7684\u6982\u7387\u662fP0 = 50%\uff0c\u5219\uff1aY = LN(50%\/50%) = LN(0.5\/(1-0.5)) = LN(0.5\/0.5) = 0 = aX + b\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u518d\u5047\u8bbe\u659c\u7387a = 2\uff0c\u622a\u8dddb = 2\uff0c\u5f97\uff1a0 = 2X + 2\uff1b\u6c42\u5f97X = 1\uff0c<strong>\u4e4b\u6240\u4ee5\u4e0d\u662f-1\uff0c\u662f\u56e0\u4e3a\u5de5\u8d44\u6570\u989d\u4e0d\u53ef\u80fd\u662f\u8d1f\u6570\u3002<\/strong><\/p>\n<p>\u4f8b\u5b501\u8bf4\u660e\uff0c<strong>\u5047\u5982\u4f60\u7684\u5de5\u8d44\u662f1\uff0c\u90a3\u4e48TA\u670950%\u7684\u53ef\u80fd\u6027\u559c\u6b22\u4f60\uff0c\u670950%\u7684\u53ef\u80fd\u6027\u4e0d\u559c\u6b22\u4f60\u3002<\/strong><\/p>\n<p>\u90a3\u5982\u679c\u4f60\u7684\u5de5\u8d44\u662f2\u5462\uff1f\u5373\u9700\u8981<strong>\u6c42Y<\/strong>\uff0c\u6709\uff1a<\/p>\n<p>LN(P1\/(1-P1)) = 2X + 2 = 2*2 + 2 = 6 \uff0c<\/p>\n<p>P1\/(1-P1) = EXP(6) = 403<\/p>\n<p>P1 = 403 \u2013 403*P1<\/p>\n<p>\u6c42\u5f97P1 \u2248 0.998<\/p>\n<p>\u6ca1\u9519\uff0c<strong>\u5982\u679c\u4f60\u7684\u5de5\u8d44\u7ffb\u4e86\u4e00\u500d\uff0cTA\u670999.8%\u7684\u53ef\u80fd\u6027\u559c\u6b22\u4f60<\/strong>\u3002<\/p>\n<p>\u8fd9\u5c31\u662f<strong>Logistic\u56de\u5f52\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u4f5c\u7528\uff1a\u57fa\u4e8e\u6982\u7387\u6765\u5224\u5b9a\u7c7b\u522b\uff01<\/strong><\/p>\n<hr \/>\n<p>\u63a5\u4e0b\u6765\u7ed3\u5408<strong>R\u8bed\u8a00<\/strong>\u8fdb\u4e00\u6b65\u8bb2\u89e3Logistic\u56de\u5f52\u3002<\/p>\n<p>\u5982\u679c\u4f60\u89c1\u5230\u7684\u6570\u636e\u662f\u4e0b\u9762\u8fd9\u6837\u7684\uff1a<strong>\u4e00\u4e2a\u53d8\u91cf(Y)\u4e0d\u662f0\u5c31\u662f1\uff0c\u53e6\u4e00\u4e2a\u53d8\u91cf(X)\u662f\u8fde\u7eed\u7684\uff0c\u4f60\u53ef\u80fd\u5c31\u8981\u8003\u8651\u4f7f\u7528\u903b\u8f91\u56de\u5f52\u6765\u8fdb\u884c\u5206\u6790\u4e86\u3002<\/strong><\/p>\n<p><img src=\" https:\/\/www.beneplot.com\/?attachment_id=835\" alt=\"\" \/><\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0bR\u4ee3\u7801\u53ef\u8c03\u53d6\u4e0a\u8ff0\u6570\u636e\uff1a<\/p>\n<p><code><strong>library(DAAG)<\/strong><\/code><\/p>\n<p><code><strong>head(anesthetic)<\/strong><\/code><\/p>\n<p>\u201c<strong>anesthetic<\/strong>\u201d\u6570\u636e\u4e2d\uff0c<strong>conc<\/strong>\u8868\u793a\u836f\u7269\u4f7f\u7528\u5242\u91cf\uff0c<strong>move\/nomove<\/strong>\u8868\u793a\u7528\u836f\u540e\u60a3\u8005\u662f\u5426\u53ef\u4ee5\u6b63\u5e38\u79fb\u52a8\u3002<\/p>\n<p>\u7528\u4ee5\u4e0bR\u4ee3\u7801\u5bf9\u6570\u636e\u201c<strong>anesthetic<\/strong>\u201d\u8fdb\u884c\u52a0\u5de5\uff1a<\/p>\n<p><code><strong>anestot=aggregate(anesthetic[,c('move','nomove')],by=list(conc=anesthetic$conc),FUN=sum)<\/strong><\/code><\/p>\n<p>\u5f97\u5230<strong>anestot<\/strong>\u6570\u636e\uff1a<\/p>\n<p><img src=\" https:\/\/www.beneplot.com\/?attachment_id=836\" alt=\"\" \/><\/p>\n<p>\u5982\u524d\u6240\u8ff0\uff0c\u5f53Y\u662f\u4e24\u7c7b\u65f6\uff0c\u5728\u672c\u4f8b\u4e2d\uff0cmove\u7684\u53cd\u9762\u662fnomove\uff0c\u4e5f\u5c31\u662fmove=0\u65f6\uff0cnomove=1\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u800c\u4e0eY\u5bf9\u5e94\u7684X\u5b9e\u9645\u4e0a\u4e5f\u53ef\u4ee5\u5206\u7c7b\u6574\u7406\uff0c\u6bd4\u5982\u5728\u672c\u4f8b\u4e2d\uff0c\u6ce8\u5c04\u5242\u91cfconc=0.8\u7684\u67097\u4eba\uff0c\u5176\u4e2d6\u4eba\u53ef\u4ee5\u79fb\u52a8\uff0c1\u4eba\u65e0\u6cd5\u79fb\u52a8\u3002<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0bR\u4ee3\u7801\u5bf9\u6570\u636e\u201c<strong>anestot<\/strong>\u201d\u8fdb\u884c\u52a0\u5de5\uff1a<\/p>\n<p><code><strong>anestot$total = apply(anestot[,c('move','nomove')],1,sum)<\/strong><\/code><\/p>\n<p><code><strong>anestot$prop = round(anestot$nomove\/anestot$total,3)<\/strong><\/code><\/p>\n<p><code><strong>anestot$logit = round(log(anestot$prop\/(1-anestot$prop)),3)<\/strong><\/code><\/p>\n<p>\u201c<strong>anestot<\/strong>\u201d\u53d8\u4e3a\uff1a<\/p>\n<p><img src=\" https:\/\/www.beneplot.com\/?attachment_id=837\" alt=\"\" \/><\/p>\n<p>\u4e0a\u8868\u4e2d\u7684<strong>prop<\/strong>\u4e3a\u67d0\u5242\u91cf\u4e0b\uff0c\u4e0d\u80fd\u79fb\u52a8\u7684\u4eba\u6570\u5360\u603b\u4eba\u6570\u7684\u6bd4\u4f8b\uff0c\u4e5f\u5c31\u662f<strong>Y\u662f1\u7684\u6982\u7387\uff1aP1\u3002<\/strong><strong>logit<\/strong> = LN(P1\/(1-P1))\u3002\u81f3\u6b64\uff0clogistic\u56de\u5f52\u53d8\u6210\u4e86\uff1a<strong>logit = a * conc + b<\/strong><\/p>\n<p><strong>\u4ee5\u4e0a\u662flogistic\u56de\u5f52\u7684\u524d\u671f\u6570\u636e\u6574\u7406\u8fc7\u7a0b\u3002<\/strong><\/p>\n<p>\u9488\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u5f62\u5f0f\uff0cR\u8bed\u8a00\u6709\u76f8\u5e94\u7684\u4ee3\u7801\u53ef\u4ee5\u8fdb\u884c\u5206\u6790\uff0c\u5176\u7ed3\u679c\u76f8\u540c\uff1a<\/p>\n<p><code><strong>anes1<\/strong>=glm(nomove~conc,family=binomial(link='logit'),data=anesthetic)<\/code><\/p>\n<p><code><strong>anes2<\/strong>=glm(cbind(nomove,move)~conc,family=binomial(link='logit'),data=anestot)<\/code><\/p>\n<p><code><strong>anes3<\/strong>=glm(prop~conc,family=binomial(link='logit'),weights=total,data=anestot)<\/code><\/p>\n<p>\u82e5\u662f\u672a\u7ecf\u6574\u7406\u7684\u539f\u59cb\u6570\u636e\uff0c\u5982anesthetic\u6570\u636e\u8868\uff0c\u4f7f\u7528<strong>anes1<\/strong>\u8bed\u53e5\u5206\u6790\uff1b\u82e5\u662f\u6570\u636e\u5df2\u7ecf\u8fc7\u6574\u7406\uff0c\u5982anestot\u6570\u636e\u8868\uff0c\u4f7f\u7528<strong>anes2\u3001anes3<\/strong>\u8bed\u53e5\u3002<\/p>\n<p>\u5206\u6790\u53ef\u6c42\u5f97\uff1a<strong>a = 5.57\uff0cb = -6.47\u3002<\/strong><\/p>\n<p>\u5728logistic\u56de\u5f52\u4e2d\uff0c\u5982\u679c\u5047\u5b9a<strong>50%\u662f\u4e34\u754c\u503c<\/strong>(\u5373\u4e0b\u6587\u4f1a\u8be6\u7ec6\u8bf4\u5230\u7684<strong>\u9608\u503c<\/strong>)\uff0c<strong>P1&gt;50%\u610f\u5473\u7740Y=1\uff0cP1&lt;50%\u5219\u610f\u5473\u7740Y=0<\/strong>\u3002\u56e0\u6b64\uff0c<strong>\u65e0\u8bba\u662f\u8d85\u8fc7\u4e34\u754c\u503c\u8fd8\u662f\u4f4e\u4e8e\u4e34\u754c\u503c\uff0c\u90fd\u610f\u5473\u7740\u9009\u62e9\u7684\u6539\u53d8<\/strong>\u3002\u82e5\u5047\u5b9a<strong>50%\u662f\u4e34\u754c\u503c\uff0c\u5982\u679ca\u3001b\u5df2\u77e5\uff0c\u5219\u53ef\u6c42\u53d6\u4e34\u754c\u7684X\u503c<\/strong>\uff08<strong>\u4e34\u754cP\u503c=50%\u65f6\uff0cY = 0 <\/strong>,\u81f3\u4e8eY\u4e3a\u4ec0\u4e48\u7b49\u4e8e0\uff0c\u4ee5\u53ca\u5982\u4f55\u6c42\u4e34\u754cX\u503c\uff0c\u8be6\u89c1\u4e0a\u8ff0<strong>\u4f8b\u5b501<\/strong>)\u3002<\/p>\n<p>\u6c42\u4e34\u754cX\u503c\u7684R\u4ee3\u7801\uff1a<\/p>\n<p><code>X = abs(anes1$coefficients[1]\/anes1$coefficients[2])<\/code><\/p>\n<p>\u6c42\u5f97\uff1aX = 1.16<\/p>\n<p>\u7ed3\u679c\u610f\u5473\u7740\uff0c\u5f53\u7528\u836f\u5242\u91cf\u8d85\u8fc71.16\u65f6\uff0c\u75c5\u4eba\u5f88\u53ef\u80fd\u5c31\u65e0\u6cd5\u79fb\u52a8\u4e86\uff01<strong>\u5982\u679c\u8fd9\u662f\u4e00\u9879\u6b63\u5f0f\u7684\u7814\u7a76\uff0c\u7ed3\u679c\u7684\u51c6\u786e\u6027\u662f\u5f88\u91cd\u8981\u7684\uff01<\/strong><\/p>\n<p><code>cdplot(factor(nomove)~conc,data=anesthetic,main='\u6761\u4ef6\u5bc6\u5ea6\u56fe',ylab='\u75c5\u4eba\u79fb\u52a8',xlab='\u9ebb\u9189\u5242\u91cf') ##\u901a\u8fc7\u7ed8\u56fe\u4e5f\u5927\u81f4\u53ef\u4ee5\u770b\u51fa\u4e34\u754cX\u503c\u57281.2\u9644\u8fd1\u3002<\/code><\/p>\n<p>\u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c\u4e00\u822c\u6765\u8bf4\uff0c<strong>\u8fdb\u884clogistic\u56de\u5f52\u5206\u6790\u7684\u6837\u672c\u91cf\u4e0d\u5e94\u8be5\u4f4e\u4e8e100\uff0c\u5e76\u4e14\u968f\u7740\u53d8\u91cf\u7684\u589e\u52a0\uff0c\u6837\u672c\u91cf\u4e5f\u9700\u8981\u76f8\u5e94\u589e\u52a0\u3002<\/strong><\/p>\n<p>\u81f3\u6b64\uff0c\u672c\u6587\u7528\u7b80\u5355\u7684\u4f8b\u5b50\u8bb2\u8ff0\u4e86\u4ec0\u4e48\u662flogistic\u56de\u5f52\u3002\u4f46\u8fd9\u6279\u6570\u636e\u8fc7\u5c11\uff0c\u53d8\u91cf\u4e5f\u53ea\u67091\u4e2a\uff0c<strong>\u63a5\u4e0b\u6765\uff0c\u672c\u6587\u5c06\u7528\u4e00\u4e2a\u66f4\u4e3a\u590d\u6742\u7684\u5b9e\u9645\u4f8b\u5b50\uff0c\u8be6\u7ec6\u8bb2\u8ff0\u5982\u4f55\u4f7f\u7528logistic\u56de\u5f52\u8fdb\u884c\u5efa\u6a21\u5206\u6790\u3002<\/strong><\/p>\n<hr \/>\n<p>\u672c\u4f8b\u5c06<strong>\u4f7f\u7528logistic\u56de\u5f52\u9884\u6d4b\u80a1\u5e02\u7684\u6da8\u8dcc\uff0c<\/strong>\u90e8\u5206\u5185\u5bb9\u6765\u81ea\u4e66\u7c4d\u300aAn Introduction to Statistical Learning with Applications in R\u300b<strong>\u3002<\/strong><\/p>\n<p>R\u4ee3\u7801\u8c03\u53d6\u6570\u636e\uff1a<\/p>\n<p><code>library (ISLR)<\/code><\/p>\n<p><code>head(Smarket)<\/code><\/p>\n<p><img src=\" https:\/\/www.beneplot.com\/?attachment_id=838\" alt=\"\" \/><img src=\" https:\/\/www.beneplot.com\/?attachment_id=840 \" alt=\"\" \/><img src=\" https:\/\/www.beneplot.com\/?attachment_id=838\" alt=\"\" \/><\/p>\n<p>\u6b64\u6570\u636e\u96c6\u5305\u62ec\u4ece<strong>2001~2005\u5e74<\/strong>\u6807\u51c6\u666e\u5c14500\u6307\u6570\u7684\u6295\u8d44\u56de\u62a5\u7387\u3002<strong>Lag1~Lag5<\/strong>\u4e3a\u8fc7\u53bb5\u5929\u6bcf\u4e2a\u4ea4\u6613\u65e5\u7684\u6295\u8d44\u56de\u62a5\u7387\uff0c<strong>Today<\/strong>\u4e3a\u5f53\u65e5\u7684\u6295\u8d44\u56de\u62a5\u7387\uff0c<strong>Direction<\/strong>\u8868\u793a\u5f53\u65e5\u6295\u8d44\u56de\u62a5\u7387\u662f\u6b63\u6570Up (\u6da8)\uff0c\u8fd8\u662f\u8d1f\u6570Down (\u8dcc)\u3002<\/p>\n<p>\u672c\u6587<strong>\u5c06\u4f7f\u75282001~2004\u5e74\u7684\u6570\u636e\u8fdb\u884c\u5efa\u6a21\uff0c\u7136\u540e\u9884\u6d4b2005\u5e74\u7684\u6da8\u8dcc<\/strong><strong style=\"font-size: 1rem;\">\u3002<\/strong><\/p>\n<div>\u00a0<\/div>\n<div><code>train =(Smarket$Year &lt;2005) ##\u8bbe\u5b9a\u6570\u636e\u5206\u5272\u6807\u51c6<\/code><\/div>\n<div><code>Smarket.2005 = Smarket[! train ,]##\u63d0\u53d62005\u5e74\u7684\u6570\u636e<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>glm.fits1 = glm(Direction ~ Lag1+Lag2+Lag3+Lag4+Lag5+Volume, data=Smarket,<\/code><\/div>\n<div><code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 family =binomial ,subset =train)##\u4f7f\u75282001-2004\u7684\u6570\u636e\u5efa\u6a21<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>glm.probs1 = predict(glm.fits1, Smarket.2005, type=\"response\")##\u901a\u8fc72001-2004\u7684\u6570\u636e\u6a21\u578b,\u9884\u6d4b2005\u5e74\u7684\u80a1\u5e02\u6da8\u8dcc<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>Direction.2005 = Smarket$Direction [! train]##2005\u5e74\u6bcf\u5929\u7684\u5b9e\u9645\u6da8\u8dcc\u60c5\u51b5<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>glm.pred1=rep(\"Down\" ,252)<\/code><\/div>\n<div><code>glm.pred1[glm.probs1&gt;0.5]=\"Up\"##\u6da8\u8dcc\u9884\u6d4b\u7ed3\u679c<\/code><\/div>\n<div><code>glm.pred1 = as.factor(glm.pred1)<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>table(glm.pred1, Direction.2005)###\u5b9e\u9645\u60c5\u51b5\u4e0e\u9884\u6d4b\u60c5\u51b5\u5bf9\u6bd4<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>mean(glm.pred1 == Direction.2005)###\u9884\u6d4b\u6b63\u786e\u7684\u6982\u7387<\/code><\/div>\n<div><code>mean(glm.pred1 != Direction.2005)###\u9884\u6d4b\u9519\u8bef\u7684\u6982\u7387<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\n<p>\u901a\u8fc7\u8ba1\u7b97\u7ed3\u679c\u53ef\u77e5\uff0c<strong>\u9884\u6d4b\u6b63\u786e\u7387\u4e3a48%\uff01\u8fd9\u662f\u4e00\u4e2a\u7cdf\u7cd5\u7684\u7ed3\u679c\uff0c\u56e0\u4e3a\u53ea\u6709up&amp;down\u4e24\u7c7b\uff0c\u5373\u4f7f\u662f\u778e\u731c\uff0c\u6b63\u786e\u7387\u4e5f\u5e94\u8be5\u670950%\uff01<\/strong><\/p>\n<p><code>summary(glm.fits1)$coef ##\u56de\u5230\u6700\u5f00\u59cb\u7684\u6a21\u578b\u6570\u636e,\u89c2\u5bdfPr(&gt;|z|)\u503c,P\u503c\u8d8a\u5c0f,\u8bf4\u660e\u56e0\u7d20\u7684\u5f71\u54cd\u529b\u8d8a\u5927,\u8d8a\u5173\u952e\u3002<\/code><br \/>\n<code>cor(Smarket[,-9]) ##\u540c\u6837\uff0c\u53ef\u4ee5\u5728\u5efa\u6a21\u4e4b\u521d\u901a\u8fc7\u76f8\u5173\u5206\u6790\uff0c\u8fc5\u901f\u9501\u5b9a\u54ea\u4e9b\u56e0\u7d20\u4e0e\u8981\u9884\u6d4b\u7684\u53d8\u91cf\u76f8\u5173\u9ad8\u3002<\/code><\/p>\n<p>\u901a\u8fc7\u4e0d\u65ad\u5c1d\u8bd5\uff0c\u6700\u7ec8\u786e\u5b9a\uff0c<strong>\u7528\u8fc7\u53bb\u4e24\u5929\u7684\u6295\u8d44\u56de\u62a5\u7387\u8fdb\u884c\u5efa\u6a21\u6765\u9884\u6d4b\u4eca\u5929\u7684\u56de\u62a5\u7387\u6700\u4e3a\u51c6\u786e\uff08\u8fd9\u8bf4\u660e\u5728\u5efa\u6a21\u8fc7\u7a0b\u4e2d\uff0c\u5e76\u975e\u53d8\u91cf\u8d8a\u591a\u8d8a\u597d\uff09\u3002\u51c6\u786e\u7387\u4e3a56%\uff0c\u6bd4\u778e\u731c\u597d\u4e00\u4e9b\u4e86\uff01<\/strong>\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<div><code>glm.fits2 = glm(Direction ~ Lag1+Lag2, data=Smarket,<\/code><\/div>\n<div><code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 family =binomial ,subset =train)##\u4f7f\u75282001-2004\u7684\u6570\u636e\u5efa\u6a21<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>glm.probs2 = predict(glm.fits2, Smarket.2005, type=\"response\")##\u901a\u8fc72001-2004\u7684\u6570\u636e\u6a21\u578b,\u9884\u6d4b2005\u5e74\u7684\u80a1\u5e02\u6da8\u8dcc<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>Direction.2005 = Smarket$Direction [! train]##2005\u5e74\u6bcf\u5929\u7684\u5b9e\u9645\u6da8\u8dcc\u60c5\u51b5<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>glm.pred2 = rep(\"Down\" ,252)<\/code><\/div>\n<div><code>glm.pred2[glm.probs2&gt;0.5] = \"Up\"##\u6da8\u8dcc\u9884\u6d4b\u7ed3\u679c<\/code><\/div>\n<div><code>glm.pred2 = as.factor(glm.pred2)<\/code><\/div>\n<div><code>glm.pred2 = factor(glm.pred2, levels=rev(levels(glm.pred2)))<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>mean(glm.pred2 == Direction.2005)## \u9884\u6d4b\u6b63\u786e\u7684\u6982\u7387<\/code><\/div>\n<div><code>mean(glm.pred2 != Direction.2005)## \u9884\u6d4b\u9519\u8bef\u7684\u6982\u7387<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div><code>table(glm.pred2, Direction.2005)###\u8fd9\u53e5\u4ee3\u7801\u7684\u8f93\u51fa\u88ab\u79f0\u4e3a<strong>\u5206\u7c7b\u77e9\u9635<\/strong>\uff0c\u5982\u4e0b\u3002<\/code><\/div>\n<div>\u00a0<\/div>\n<\/div>\n<div>\n<div><img src=\" https:\/\/www.beneplot.com\/?attachment_id=841\" alt=\"\" \/><img src=\" https:\/\/www.beneplot.com\/?attachment_id=842 \" alt=\"\" \/><\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div>\n<div><code>AP = 106+35 ##\u5b9e\u9645\u4e3aup\u7684\u5929\u6570<\/code><\/div>\n<div><code>AN = 76+35 ##\u5b9e\u9645\u4e3adown\u7684\u5929\u6570<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>TP = 106 ##\u6b63\u786e\u5730\u9884\u6d4bup\u4e3aup\u7684\u5929\u6570<\/code><\/div>\n<div><code>TN = 35 ##\u6b63\u786e\u5730\u9884\u6d4bdown\u4e3adown\u7684\u5929\u6570<\/code><\/div>\n<div><code>FP = 76 ##\u9519\u8bef\u5730\u9884\u6d4bdown\u4e3aup\u7684\u5929\u6570<\/code><\/div>\n<div><code>FN = 35 ##\u9519\u8bef\u5730\u9884\u6d4bup\u4e3adown\u7684\u5929\u6570<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>TPTN = 35+106 #\u9884\u6d4b\u6b63\u786e\u7684\u5929\u6570\uff0c\u540c\u662fdown\u548c\u540c\u662fup<\/code><\/div>\n<div><code>FPFN = 35+76 #\u9884\u6d4b\u9519\u8bef\u7684\u5929\u6570\uff0c\u4e00\u4e2a\u4e3aup\u53e6\u4e00\u4e2a\u4e3adown<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>total = 35+106+35+76<\/code><\/div>\n<div><code>rightR = (TP+TN)\/total ##\u9884\u6d4b\u6b63\u786e\u7684\u6982\u7387<\/code><\/div>\n<div><code>wrongR = (FP+FN)\/total ##\u9884\u6d4b\u9519\u8bef\u7684\u6982\u7387<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>TPR = TP\/AP\u00a0 ###TPR\u5168\u79f0\u4e3aTrue Positive Rate\uff0c\u8868\u660e\u4e86\u5c06\u5b9e\u9645\u7684up\u6b63\u786e\u5730\u9884\u6d4b\u4e3aup\u7684\u6982\u7387<\/code><\/div>\n<div><code>FPR = FP\/AN ###FPR\u5168\u79f0\u4e3aFalse Positive Rate\uff0c\u8868\u660e\u4e86\u5c06\u5b9e\u9645\u7684down\u9519\u8bef\u5730\u9884\u6d4b\u4e3aup\u7684\u6982\u7387<\/code><\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\n<p>\u4e0b\u8868\u4e3a\u82f1\u6587\u5bf9\u7167~~~<\/p>\n<\/div>\n<div><img src=\" https:\/\/www.beneplot.com\/?attachment_id=843\" alt=\"\" \/><img src=\" https:\/\/www.beneplot.com\/?attachment_id=844\" alt=\"\" \/><\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\n<p>\u5728\u5efa\u6a21\u8fc7\u7a0b\u4e2d\uff0c\u76ee\u6807\u662f\u5e0c\u671bTPR\u5c3d\u91cf\u7684\u5927\uff0c\u800cFPR\u5c3d\u91cf\u7684\u5c0f\uff0c<strong>\u6700\u4f73\u72b6\u51b5\u662fTPR=1\uff0cFPR=0<\/strong>\u3002\u4f46\u662f\uff0c<strong>TPR\u548cFPR\u4f1a\u540c\u65f6\u589e\u52a0\u6216\u540c\u65f6\u51cf\u5c0f<\/strong>\u3002\u56e0\u6b64\uff0c\u9700\u8981\u627e\u5230\u4e00\u4e2a\u4e34\u754c\u70b9\uff0c<strong>\u5728\u8fd9\u4e2a\u4e34\u754c\u70b9\u4e0a\uff0cTPR\u5c3d\u53ef\u80fd\u7684\u5927\uff0cFPR\u5c3d\u53ef\u80fd\u7684\u5c0f\uff01<\/strong><\/p>\n<p>\u4e3a\u4e86\u627e\u5230\u8fd9\u4e2a\u4e34\u754c\u70b9\uff0c\u9700\u8981\u753b\u4e00\u6761\u66f2\u7ebf\uff0c\u8fd9\u6761\u66f2\u7ebf\u88ab\u79f0\u4e3a<strong>ROC\uff08receiver operating characteristic\uff09\u66f2\u7ebf\uff0c<\/strong>\u4e2d\u6587\u8bd1\u4e3a\uff1a<strong>\u63a5\u53d7\u8005\u64cd\u4f5c\u7279\u6027\u66f2\u7ebf<\/strong>\u3002<\/p>\n<\/div>\n<div>\n<p><strong>\u6765\u753b\u4e00\u4e0b\uff1a<\/strong><\/p>\n<p><code>library(pROC)<\/code><br \/>\n<code>roc1=roc(Direction.2005, glm.probs1)<\/code><br \/>\n<code>roc2=roc(Direction.2005, glm.probs2)<\/code><br \/>\n<code>plot(roc2,print.auc=TRUE,auc.polygon=TRUE,<\/code><br \/>\n<code>grid=c(0.1,0.2),grid.col=c(\"green\",\"red\"),max.auc.polygon=TRUE,<\/code><br \/>\n<code>auc.polygon.col=\"lightblue\",print.thres=\"best\")<\/code><br \/>\n<code>plot(roc1, add=TRUE, col=\"red\")<\/code><br \/>\n<code>plot(smooth(roc2), add=TRUE, col=\"blue\")<\/code><\/p>\n<\/div>\n<p><img src=\" https:\/\/www.beneplot.com\/?attachment_id=845\" alt=\"\" \/><img src=\" https:\/\/www.beneplot.com\/?attachment_id=846\" alt=\"\" \/><\/p>\n<p>\u4e0a\u56fe\u4e2d\u6709\u5f88\u591a\u8d77\u4f0f\u7684\u9ed1\u7ebf\u5373\u662f\u6700\u4f73\u6a21\u578b\u7684ROC\u66f2\u7ebf\uff08\u9884\u6d4b\u51c6\u786e\u738756%\uff09\uff0c\u84dd\u8272\u7684\u7ebf\u4e3a\u5b83\u7684\u5e73\u6ed1\u66f2\u7ebf\u3002<strong>ROC\u66f2\u7ebf\u79bb\u5bf9\u89d2\u7ebf\uff08\u90a3\u6761\u4e0d\u592a\u770b\u5f97\u6e05\u7684\u7070\u7ebf\uff09\u8d8a\u8fdc\uff0c\u8868\u660e\u6a21\u578b\u7684\u9884\u6d4b\u6548\u679c\u8d8a\u597d<\/strong>\u300256%\u7684\u51c6\u786e\u7387\u5b9e\u5728\u4e0d\u600e\u4e48\u597d\uff0c\u6240\u4ee5\u5b83\u4eec\u79bb\u5f97\u5f88\u8fd1\u3002\u7ea2\u8272\u7684\u662f\u9884\u6d4b\u51c6\u786e\u7387\u53ea\u670948%\u6a21\u578b\u7684ROC\u66f2\u7ebf\uff0c<strong>\u901a\u5e38\u4f1a\u5c06\u591a\u4e2a\u9884\u6d4b\u6a21\u578b\u7684ROC\u66f2\u7ebf\u7ed8\u5236\u5728\u4e00\u8d77\uff0c\u76f4\u89c2\u5730\u9274\u522b\u6a21\u578b\u4f18\u52a3\u3002<\/strong><\/p>\n<p><strong>AUC<\/strong><strong>\uff08Area Under Curve\uff09<\/strong>\u662f\u53e6\u4e00\u4e2a\u6a21\u578b\u8bc4\u4f30\u6307\u6807\uff0c\u5373ROC\u66f2\u7ebf\u4e0b\u7684\u9762\u79ef\uff08\u84dd\u8272\u533a\u57df\u7684\u9762\u79ef\uff09\u3002\u56fe\u4e2d\u663e\u793aAUC=0.558\uff0c\u610f\u601d\u662f\u5728\u8fd9\u5757\u6b63\u65b9\u5f62\u7684\u533a\u57df\u4e2d\uff0c\u84dd\u8272\u7684\u9762\u79ef\u536055.8%\u3002<strong>AUC\u6700\u5927\u4e3a1\uff0c\u5f53AUC\u22640.5\u65f6\uff0c\u8868\u660e\u9884\u6d4b\u6a21\u578b\u6beb\u65e0\u4ef7\u503c\uff01\u4e00\u822c\u60c5\u51b5\uff0c\u5f53AUC\u5927\u4e8e0.8\u65f6\uff0c\u624d\u8bf4\u660e\u8be5\u9884\u6d4b\u6a21\u578b\u6709\u8f83\u597d\u7684\u4ef7\u503c\u3002AUC\u8868\u793a\u4e86\u6a21\u578b\u7684\u89e3\u91ca\u80fd\u529b\u6216\u9884\u6d4b\u80fd\u529b!<\/strong><\/p>\n<div>\n<p>\u5728ROC2\u66f2\u7ebf\u4e0a\uff0c\u67091\u4e2a\u5c0f\u9ed1\u70b9\uff0c\u88ab\u79f0\u4e3a<strong>\u9608\u503c\u70b9<\/strong>\u3002<strong>ROC\u66f2\u7ebf\u8d8a\u9760\u8fd1\u5de6\u4e0a\u89d2\uff0c\u8bf4\u660e\u6a21\u578b\u9884\u6d4b\u80fd\u529b\u8d8a\u597d<\/strong>\u3002<strong>\u9608\u503c\u70b9\u662fROC\u66f2\u7ebf\u4e0a\u79bb\u5de6\u4e0a\u89d2\u6700\u8fd1\u7684\u4e00\u70b9<\/strong>\uff0c\u5176\u503c\u4e3a<strong>0.502\uff080.387,0.738\uff09\u3002<\/strong><\/p>\n<p><strong>\u4e0a\u56fe\u662f\u600e\u4e48\u753b\u51fa\u6765\u7684\uff1f\u6a2a\u5750\u6807\u548c\u7eb5\u5750\u6807\u5206\u522b\u662f\u4ec0\u4e48\uff1f\u9608\u503c\u70b9\u662f\u600e\u4e48\u7b97\u51fa\u6765\u7684\uff1f\u5b83\u7684\u610f\u4e49\u662f\u4ec0\u4e48\uff1f<\/strong>\u7528\u4ee3\u7801\u6765\u89e3\u91ca\u4f1a\u5f88\u6e05\u695a\u3002<\/p>\n<p><strong>ROC<\/strong><strong>\u66f2\u7ebf\u7ed8\u5236\u8fc7\u7a0b\u4ee3\u7801\u5982\u4e0b\uff1a<\/strong><\/p>\n<div><code>data=data.frame(prob=glm.probs2,obs=Direction.2005)##\u5c06\u9884\u6d4b\u6982\u7387\u548c\u5b9e\u9645\u7ed3\u679c\u653e\u5728\u4e00\u4e2a\u6570\u636e\u6846\u4e2d<\/code><\/div>\n<div><code>data=data[order(data$prob),]##\u5c06\u9884\u6d4b\u6982\u7387\u6309\u7167\u4ece\u4f4e\u5230\u9ad8\u6392\u5e8f<\/code><\/div>\n<div><code>n=nrow(data)<\/code><\/div>\n<div><code>tpr=fpr=rep(0,n)<\/code><\/div>\n<div><code>head(data)<\/code><\/div>\n<div><code>tpr = c()<\/code><\/div>\n<div><code>fpr = c()<\/code><\/div>\n<div><code>tnr = c()<\/code><\/div>\n<\/div>\n<div>\n<div><code>for (i in 1:n){<\/code><\/div>\n<div><code>\u00a0 threshold=data$prob[i]<\/code><\/div>\n<div><code>\u00a0 tp=sum(data$prob&gt;threshold&amp;data$obs==\"Up\")##Up\u9884\u6d4b\u5224\u5b9a\u4e3aUp<\/code><\/div>\n<div><code>\u00a0 fp=sum(data$prob&gt;threshold&amp;data$obs==\"Down\")##Down\u9884\u6d4b\u5224\u5b9a\u4e3aUp<\/code><\/div>\n<div><code>\u00a0 tn=sum(data$prob&lt;threshold&amp;data$obs==\"Down\")##Down\u9884\u6d4b\u5224\u5b9a\u4e3aDown<\/code><\/div>\n<div><code>\u00a0 fn=sum(data$prob&lt;threshold&amp;data$obs==\"Up\")##Up\u9884\u6d4b\u5224\u5b9a\u4e3aDown<\/code><\/div>\n<div><code>\u00a0 tpr[i]=tp\/(tp+fn)\u00a0 ##\u5373\u4e0a\u56fe\u548c\u672c\u56fe\u7684\u7eb5\u5750\u6807<\/code><\/div>\n<div><code>\u00a0 fpr[i]=fp\/(tn+fp)\u00a0 ##\u5373\u672c\u56fe\u7684\u6a2a\u5750\u6807<\/code><\/div>\n<div><code>\u00a0 tnr[i]=1-fpr[i]\u00a0 ##\u5373\u4e0a\u56fe\u7684\u6a2a\u5750\u6807\u3002\u6ce8\u610f\uff01\u4e0a\u56fe\u6a2a\u5750\u6807\u662f\u4ece1\u52300\uff0c\u6545\u6a2a\u5750\u6807\u662f1- fpr<\/code><\/div>\n<div><code>}<\/code><\/div>\n<div><code>plot(fpr,tpr,type='l')<\/code><\/div>\n<div><code>abline(a=0,b=1) ###\u81f3\u6b64\u5b8c\u6210\u7b80\u6613\u7248ROC\u66f2\u7ebf\u7684\u7ed8\u5236<\/code><\/div>\n<div><code>point = tpr - fpr##\u6c42tpr\u4e0efpr\u7684\u5dee<\/code><br \/>\n<code>options(digits = 3)<\/code><br \/>\n<code>tdata = cbind(data,tpr,fpr,tnr,point) ##\u5c06\u9884\u6d4b\u6982\u7387\u3001\u5b9e\u9645\u7ed3\u679c\u3001tpr\u3001fpr\u3001tnr\u3001point\u653e\u5728\u4e00\u4e2a\u6570\u636e\u6846<\/code><br \/>\n<code>tdata = tdata[order(tdata$point,decreasing = TRUE),]##\u5c06tpr\u4e0efpr\u7684\u5dee\u6309\u7167\u4ece\u9ad8\u5230\u4f4e\u6392\u5e8f<\/code><br \/>\n<code>head(tdata)##\u7ed3\u679c\u5982\u4e0b<\/code><\/div>\n<div>\u00a0<\/div>\n<\/div>\n<div><img src=\" https:\/\/www.beneplot.com\/?attachment_id=848\" alt=\"\" \/><img src=\" https:\/\/www.beneplot.com\/?attachment_id=849\" alt=\"\" \/><\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\n<p><strong>\u4e0a\u8868\u7b2c\u4e00\u884c\u5373\u662f\u9608\u503c\u70b90.502\uff080.387,0.738\uff09\u4e2d\u5404\u503c\u7684\u6765\u5904\u53ca\u610f\u4e49\uff01<\/strong><\/p>\n<p><strong>\u6700\u4f73\u6a21\u578b\u5e94\u6ee1\u8db3TPR\u5c3d\u91cf\u7684\u5927\uff0cFPR\u5c3d\u91cf\u7684\u5c0f\uff0c\u6545\u53ef\u901a\u8fc7\u6c42\u4e24\u8005\u4e4b\u5dee\uff0c\u627e\u5230\u6700\u5927\u7684\u5dee\u503c\uff0c\u5373\u4e3a\u9608\u503c\uff0c\u6b64\u65f6\u6a21\u578b\u8868\u73b0\u6700\u4f73\u3002<\/strong><\/p>\n<p><strong>\u9608\u503c0.502<\/strong>\u662f\u4e00\u4e2a\u9884\u6d4b\u6982\u7387(\u5373<strong>tdata$prob<\/strong>\u4e2d\u7684\u4e00\u4e2a\u503c)\uff0c\u5b83\u7684\u610f\u601d\u662f\u5f53\u6982\u7387\u8d85\u8fc750.2%\u65f6\uff0c\u9884\u6d4b\u7ed3\u679c\u4e3aUp(<strong>\u5bf9\u5e94\u4ee3\u7801\u4e3a1<\/strong>)\uff0c\u53cd\u4e4b\u4e3aDown(<strong>\u5bf9\u5e94\u4ee3\u7801\u4e3a0<\/strong>)\u3002\u5bf9\u5e94\u7684<strong>TPR=0.736\uff0cFPR=0.613\uff0cTNR=0.387<\/strong>(\u81f3\u4e8e\u5b83\u4e3a\u4ec0\u4e48\u8981\u7528\u7b2c3\u884c\u7684TPR=0.738\uff0c\u6211\u4e5f\u4e0d\u6e05\u695a:-)\u3002\u7531\u6b64\u53ef\u77e5\uff0c\u4e0a\u56fe\u4e2d\u7684<strong>\u6a2a\u5750\u6807\u662fTNR\uff0cTNR = 1-FPR\uff0c\u53c8\u79f0\u7279\u5f02\u6027\uff1b\u7eb5\u5750\u6807\u5373TPR\uff0c\u53c8\u79f0\u654f\u611f\u6027<\/strong>\u3002<\/p>\n<p><strong>\u5f53\u9608\u503c\u4e3a0.502\u65f6\uff0c\u8be5\u6a21\u578b\u6709\u6700\u4f73\u8868\u73b0\uff0c\u6ee1\u8db3TPR\u548cTNR\u5c3d\u91cf\u7684\u5927\uff0cFPR\u548cFNR\u5c3d\u91cf\u7684\u5c0f<\/strong>\u3002<\/p>\n<\/div>\n<div>\u00a0<\/div>\n<div>\n<p>\u81f3\u6b64\uff0c\u672c\u6587\u901a\u8fc7\u4e24\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u8fd0\u7528R\u8bed\u8a00\u4ee3\u7801\uff0cStep by step\u7684\u8bb2\u8ff0\u4e86Logistic\u56de\u5f52\u662f\u4ec0\u4e48\uff0c\u4ee5\u53ca\u5e94\u8be5\u600e\u4e48\u7528\u3002\u8981\u8bf4\u660e\u7684\u662f\uff0cLogistic\u56de\u5f52\u5728\u771f\u5b9e\u573a\u666f\u88ab\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5b9e\u9645\u4e2d\u9700\u8981\u8003\u8651\u66f4\u591a\u7684\u56e0\u7d20\uff0c\u5e94\u7528\u66f4\u52a0\u590d\u6742\u3002<\/p>\n<p><span style=\"font-size: 10px;\"><em>\u6ce8*\u672c\u6587\u90e8\u5206\u5185\u5bb9\u6765\u81ea\u7f51\u7edc\u6216\u4e66\u7c4d\u3002<\/em><\/span><\/p>\n<\/div>\n<div>###\u4ee3\u7801\u6c47\u603b###<\/div>\n<div>\u00a0<\/div>\n<div>\n<div><code>library(DAAG)<\/code><\/div>\n<div><code>head(anesthetic)<\/code><\/div>\n<div><code>anestot=aggregate(anesthetic[,c('move','nomove')],by=list(conc=anesthetic$conc),FUN=sum)<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>anestot$total = apply(anestot[,c('move','nomove')],1,sum)<\/code><\/div>\n<div><code>anestot$prop = round(anestot$nomove\/anestot$total,3)<\/code><\/div>\n<div><code>anestot$logit = round(log(anestot$prop\/(1-anestot$prop)),3)<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>anes1=glm(nomove~conc,family=binomial(link='logit'),data=anesthetic)<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>anes2=glm(cbind(nomove,move)~conc,family=binomial(link='logit'),data=anestot)<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>anes3=glm(prop~conc,family=binomial(link='logit'),weights=total,data=anestot)<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>X = abs(anes1$coefficients[1]\/anes1$coefficients[2])<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>cdplot(factor(nomove)~conc,data=anesthetic,<\/code><\/div>\n<div><code>\u00a0 \u00a0 \u00a0 \u00a0main='\u6761\u4ef6\u5bc6\u5ea6\u56fe',ylab='\u75c5\u4eba\u79fb\u52a8',xlab='\u9ebb\u9189\u5242\u91cf') ##\u901a\u8fc7\u7ed8\u56fe\u4e5f\u5927\u81f4\u53ef\u4ee5\u770b\u51fa\u4e34\u754cX\u503c\u57281.2\u9644\u8fd1\u3002<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>library (ISLR)<\/code><\/div>\n<div><code>head(Smarket)<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>train = (Smarket$Year &lt;2005) ##\u8bbe\u5b9a\u6570\u636e\u5206\u5272\u6807\u51c6<\/code><\/div>\n<div><code>Smarket.2005 = Smarket[! train ,]##\u63d0\u53d62005\u5e74\u7684\u6570\u636e<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>glm.fits1 = glm(Direction ~ Lag1+Lag2+Lag3+Lag4+Lag5+Volume, data=Smarket,<\/code><\/div>\n<div><code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 family =binomial ,subset =train)##\u4f7f\u75282001-2004\u7684\u6570\u636e\u5efa\u6a21<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>glm.probs1 = predict(glm.fits1, Smarket.2005, type=\"response\")##\u901a\u8fc72001-2004\u7684\u6570\u636e\u6a21\u578b,\u9884\u6d4b2005\u5e74\u7684\u80a1\u5e02\u6da8\u8dcc<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>Direction.2005 = Smarket$Direction [! train]##2005\u5e74\u6bcf\u5929\u7684\u5b9e\u9645\u6da8\u8dcc\u60c5\u51b5<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>glm.pred1=rep(\"Down\" ,252)<\/code><\/div>\n<div><code>glm.pred1[glm.probs1&gt;0.5]=\"Up\"##\u6da8\u8dcc\u9884\u6d4b\u7ed3\u679c<\/code><\/div>\n<div><code>glm.pred1 = as.factor(glm.pred1)<\/code><\/div>\n<div><code>#glm.pred1 = factor(glm.pred1, levels=rev(levels(glm.pred1)))<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>table(glm.pred1, Direction.2005)###\u5b9e\u9645\u60c5\u51b5\u4e0e\u9884\u6d4b\u60c5\u51b5\u5bf9\u6bd4<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>mean(glm.pred1 == Direction.2005)###\u9884\u6d4b\u6b63\u786e\u7684\u6982\u7387<\/code><\/div>\n<div><code>mean(glm.pred1 != Direction.2005)###\u9884\u6d4b\u9519\u8bef\u7684\u6982\u7387<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>summary(glm.fits1)$coef ##\u56de\u5230\u6700\u5f00\u59cb\u7684\u6a21\u578b\u6570\u636e,\u89c2\u5bdfPr(&gt;|z|)\u503c,P\u503c\u8d8a\u5c0f,\u8bf4\u660e\u56e0\u7d20\u7684\u5f71\u54cd\u529b\u8d8a\u5927,\u8d8a\u5173\u952e\u3002<\/code><\/div>\n<div><code>cor(Smarket[,-9]) ##\u540c\u6837\uff0c\u53ef\u4ee5\u5728\u5efa\u6a21\u4e4b\u521d\u901a\u8fc7\u76f8\u5173\u5206\u6790\uff0c\u8fc5\u901f\u9501\u5b9a\u54ea\u4e9b\u56e0\u7d20\u4e0e\u8981\u9884\u6d4b\u7684\u53d8\u91cf\u76f8\u5173\u9ad8\u3002<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div><code>glm.fits2 = glm(Direction ~ Lag1+Lag2, data=Smarket,<\/code><\/div>\n<div><code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 family =binomial ,subset =train)##\u4f7f\u75282001-2004\u7684\u6570\u636e\u5efa\u6a21<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>glm.probs2 = predict(glm.fits2, Smarket.2005, type=\"response\")##\u901a\u8fc72001-2004\u7684\u6570\u636e\u6a21\u578b,\u9884\u6d4b2005\u5e74\u7684\u80a1\u5e02\u6da8\u8dcc<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>Direction.2005 = Smarket$Direction [! train]##2005\u5e74\u6bcf\u5929\u7684\u5b9e\u9645\u6da8\u8dcc\u60c5\u51b5<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>glm.pred2 = rep(\"Down\" ,252)<\/code><\/div>\n<div><code>glm.pred2[glm.probs2&gt;0.5] = \"Up\"##\u6da8\u8dcc\u9884\u6d4b\u7ed3\u679c<\/code><\/div>\n<div><code>glm.pred2 = as.factor(glm.pred2)<\/code><\/div>\n<div><code>glm.pred2 = factor(glm.pred2, levels=rev(levels(glm.pred2)))<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>mean(glm.pred2 == Direction.2005)## \u9884\u6d4b\u6b63\u786e\u7684\u6982\u7387<\/code><\/div>\n<div><code>mean(glm.pred2 != Direction.2005)## \u9884\u6d4b\u9519\u8bef\u7684\u6982\u7387<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div><code>table(glm.pred2, Direction.2005)###\u8fd9\u53e5\u4ee3\u7801\u7684\u8f93\u51fa\u88ab\u79f0\u4e3a\u5206\u7c7b\u77e9\u9635\u3002<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div><code>AP = 106+35 ##\u5b9e\u9645\u4e3aup\u7684\u5929\u6570<\/code><\/div>\n<div><code>AN = 76+35 ##\u5b9e\u9645\u4e3adown\u7684\u5929\u6570<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>TP = 106 ##\u6b63\u786e\u5730\u9884\u6d4bup\u4e3aup\u7684\u5929\u6570<\/code><\/div>\n<div><code>TN = 35 ##\u6b63\u786e\u5730\u9884\u6d4bdown\u4e3adown\u7684\u5929\u6570<\/code><\/div>\n<div><code>FP = 76 ##\u9519\u8bef\u5730\u9884\u6d4bdown\u4e3aup\u7684\u5929\u6570<\/code><\/div>\n<div><code>FN = 35 ##\u9519\u8bef\u5730\u9884\u6d4bup\u4e3adown\u7684\u5929\u6570<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>TPTN = 35+106 #\u9884\u6d4b\u6b63\u786e\u7684\u5929\u6570\uff0c\u540c\u662fdown\u548c\u540c\u662fup<\/code><\/div>\n<div><code>FPFN = 35+76 #\u9884\u6d4b\u9519\u8bef\u7684\u5929\u6570\uff0c\u4e00\u4e2a\u4e3aup\u53e6\u4e00\u4e2a\u4e3adown<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>total = 35+106+35+76<\/code><\/div>\n<div><code>rightR = (TP+TN)\/total ##\u9884\u6d4b\u6b63\u786e\u7684\u6982\u7387<\/code><\/div>\n<div><code>wrongR = (FP+FN)\/total ##\u9884\u6d4b\u9519\u8bef\u7684\u6982\u7387<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>TPR = TP\/AP\u00a0 ###TPR\u5168\u79f0\u4e3aTrue Positive Rate\uff0c\u8868\u660e\u4e86\u5c06\u5b9e\u9645\u7684up\u6b63\u786e\u5730\u9884\u6d4b\u4e3aup\u7684\u6982\u7387<\/code><\/div>\n<div><code>FPR = FP\/AN ###FPR\u5168\u79f0\u4e3aFalse Positive Rate\uff0c\u8868\u660e\u4e86\u5c06\u5b9e\u9645\u7684down\u9519\u8bef\u5730\u9884\u6d4b\u4e3aup\u7684\u6982\u7387<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>library(pROC)<\/code><\/div>\n<div><code>roc1=roc(Direction.2005, glm.probs1)<\/code><\/div>\n<div><code>roc2=roc(Direction.2005, glm.probs2)<\/code><\/div>\n<div><code>plot(roc2,print.auc=TRUE,auc.polygon=TRUE,<\/code><\/div>\n<div><code>\u00a0 \u00a0 \u00a0grid=c(0.1,0.2),grid.col=c(\"green\",\"red\"),max.auc.polygon=TRUE,<\/code><\/div>\n<div><code>\u00a0 \u00a0 \u00a0auc.polygon.col=\"lightblue\",print.thres=\"best\")<\/code><\/div>\n<div><code>plot(roc1, add=TRUE, col=\"red\")<\/code><\/div>\n<div><code>plot(smooth(roc2), add=TRUE, col=\"blue\")<\/code><\/div>\n<div>\u00a0<\/div>\n<div>\u00a0<\/div>\n<div><code>data=data.frame(prob=glm.probs2,obs=Direction.2005)##\u5c06\u9884\u6d4b\u6982\u7387\u548c\u5b9e\u9645\u7ed3\u679c\u653e\u5728\u4e00\u4e2a\u6570\u636e\u6846\u4e2d<\/code><\/div>\n<div><code>data=data[order(data$prob),]##\u5c06\u9884\u6d4b\u6982\u7387\u6309\u7167\u4ece\u4f4e\u5230\u9ad8\u6392\u5e8f<\/code><\/div>\n<div><code>n=nrow(data)<\/code><\/div>\n<div><code>tpr=fpr=rep(0,n)<\/code><\/div>\n<div><code>head(data)<\/code><\/div>\n<div><code>tpr = c()<\/code><\/div>\n<div><code>fpr = c()<\/code><\/div>\n<div><code>tnr = c()<\/code><\/div>\n<div><code>for (i in 1:n){<\/code><\/div>\n<div><code>\u00a0 threshold=data$prob[i]<\/code><\/div>\n<div><code>\u00a0 tp=sum(data$prob&gt;threshold&amp;data$obs==\"Up\")##Up\u9884\u6d4b\u5224\u5b9a\u4e3aUp<\/code><\/div>\n<div><code>\u00a0 fp=sum(data$prob&gt;threshold&amp;data$obs==\"Down\")##Down\u9884\u6d4b\u5224\u5b9a\u4e3aUp<\/code><\/div>\n<div><code>\u00a0 tn=sum(data$prob&lt;threshold&amp;data$obs==\"Down\")##Down\u9884\u6d4b\u5224\u5b9a\u4e3aDown<\/code><\/div>\n<div><code>\u00a0 fn=sum(data$prob&lt;threshold&amp;data$obs==\"Up\")##Up\u9884\u6d4b\u5224\u5b9a\u4e3aDown<\/code><\/div>\n<div><code>\u00a0 tpr[i]=tp\/(tp+fn)\u00a0 ##\u5373\u4e0a\u56fe\u548c\u672c\u56fe\u7684\u7eb5\u5750\u6807<\/code><\/div>\n<div><code>\u00a0 fpr[i]=fp\/(tn+fp)\u00a0 ##\u5373\u672c\u56fe\u7684\u6a2a\u5750\u6807<\/code><\/div>\n<div><code>\u00a0 tnr[i]=1-fpr[i]\u00a0 ##\u5373\u4e0a\u56fe\u7684\u6a2a\u5750\u6807\u3002\u6ce8\u610f\uff01\u4e0a\u56fe\u6a2a\u5750\u6807\u662f\u4ece1\u52300\uff0c\u6545\u6a2a\u5750\u6807\u662f1- fpr<\/code><\/div>\n<div><code>}<\/code><\/div>\n<div><code>plot(fpr,tpr,type='l')<\/code><\/div>\n<div><code>abline(a=0,b=1) ###\u81f3\u6b64\u5b8c\u6210\u7b80\u6613\u7248ROC\u66f2\u7ebf\u7684\u7ed8\u5236<\/code><\/div>\n<div>\u00a0<\/div>\n<div><code>point = tpr - fpr##\u6c42tpr\u4e0efpr\u7684\u5dee<\/code><\/div>\n<div><code>options(digits = 3)<\/code><\/div>\n<div><code>tdata = cbind(data,tpr,fpr,tnr,point) ##\u5c06\u9884\u6d4b\u6982\u7387\u3001\u5b9e\u9645\u7ed3\u679c\u3001tpr\u3001fpr\u3001tnr\u3001point\u653e\u5728\u4e00\u4e2a\u6570\u636e\u6846<\/code><\/div>\n<div><code>tdata = tdata[order(tdata$point,decreasing = TRUE),]##\u5c06tpr\u4e0efpr\u7684\u5dee\u6309\u7167\u4ece\u9ad8\u5230\u4f4e\u6392\u5e8f<\/code><\/div>\n<div><code>head(tdata)##\u7ed3\u679c\u5982\u4e0b<\/code><\/div>\n<\/div>\n<div>\n<div><code><\/code><code><\/code><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u56de\u5f52\u6a21\u578b\u662f\u7edf\u8ba1\u4e2d\u6700\u5e38\u7528\u7684\u6a21\u578b\u4e4b\u4e00\uff0c\u5b83\u4e3b\u8981\u7528\u4e8e\u89e3\u91ca\u548c\u9884\u6d4b\u3002\u672c\u6587\u8981\u8bb2\u7684Logistic\u56de\u5f52\u53c8\u662f\u4e00\u79cd\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\u7684\u56de [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.beneplot.com\/?p=833\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"logistic\u56de\u5f52\u2014R\u8bed\u8a00\u5b9e\u64cd - Beneplot\" \/>\n<meta property=\"og:description\" content=\"\u56de\u5f52\u6a21\u578b\u662f\u7edf\u8ba1\u4e2d\u6700\u5e38\u7528\u7684\u6a21\u578b\u4e4b\u4e00\uff0c\u5b83\u4e3b\u8981\u7528\u4e8e\u89e3\u91ca\u548c\u9884\u6d4b\u3002\u672c\u6587\u8981\u8bb2\u7684Logistic\u56de\u5f52\u53c8\u662f\u4e00\u79cd\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\u7684\u56de [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.beneplot.com\/?p=833\" \/>\n<meta property=\"og:site_name\" content=\"Beneplot\" \/>\n<meta property=\"article:published_time\" content=\"2017-07-10T07:28:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-28T08:24:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.beneplot.com\/?attachment_id=835\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.beneplot.com\/#website\",\"url\":\"https:\/\/www.beneplot.com\/\",\"name\":\"Beneplot\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.beneplot.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-CN\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.beneplot.com\/?p=833#primaryimage\",\"inLanguage\":\"zh-CN\",\"url\":\" https:\/\/www.beneplot.com\/?attachment_id=835\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.beneplot.com\/?p=833#webpage\",\"url\":\"https:\/\/www.beneplot.com\/?p=833\",\"name\":\"logistic\\u56de\\u5f52\\u2014R\\u8bed\\u8a00\\u5b9e\\u64cd - Beneplot\",\"isPartOf\":{\"@id\":\"https:\/\/www.beneplot.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.beneplot.com\/?p=833#primaryimage\"},\"datePublished\":\"2017-07-10T07:28:20+00:00\",\"dateModified\":\"2021-05-28T08:24:27+00:00\",\"author\":{\"@id\":\"https:\/\/www.beneplot.com\/#\/schema\/person\/ea14f85ae789ceaaa712ceee1dd1f95b\"},\"inLanguage\":\"zh-CN\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.beneplot.com\/?p=833\"]}]},{\"@type\":[\"Person\"],\"@id\":\"https:\/\/www.beneplot.com\/#\/schema\/person\/ea14f85ae789ceaaa712ceee1dd1f95b\",\"name\":\"beneplot\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.beneplot.com\/#personlogo\",\"inLanguage\":\"zh-CN\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/15d96ce801cfddbd59ef2b0d986cd9b1?s=96&r=g\",\"caption\":\"beneplot\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/posts\/833"}],"collection":[{"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=833"}],"version-history":[{"count":20,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/posts\/833\/revisions"}],"predecessor-version":[{"id":936,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=\/wp\/v2\/posts\/833\/revisions\/936"}],"wp:attachment":[{"href":"https:\/\/www.beneplot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.beneplot.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}