{"id":427,"date":"2023-04-12T03:08:34","date_gmt":"2023-04-12T03:08:34","guid":{"rendered":"https:\/\/yunshangtulv.com.cn\/?p=427"},"modified":"2023-04-12T03:08:34","modified_gmt":"2023-04-12T03:08:34","slug":"r%e8%af%ad%e8%a8%80%e5%9f%ba%e7%a1%80%e7%bb%9f%e8%ae%a1%e7%a4%ba%e4%be%8b%ef%bc%9a%e9%80%bb%e8%be%91%e5%9b%9e%e5%bd%92","status":"publish","type":"post","link":"https:\/\/yunshangtulv.com.cn\/?p=427","title":{"rendered":"R\u8bed\u8a00\u57fa\u7840\u7edf\u8ba1\u793a\u4f8b\uff1a\u903b\u8f91\u56de\u5f52"},"content":{"rendered":"<p>\u903b\u8f91\u56de\u5f52\u662f\u4e00\u79cd\u5e7f\u6cdb\u5e94\u7528\u7684\u7edf\u8ba1\u5b66\u65b9\u6cd5\uff0c\u4e3b\u8981\u7528\u4e8e\u9884\u6d4b\u5206\u7c7b\u53d8\u91cf\uff08\u5982\u4e8c\u5206\u7c7b\u6216\u591a\u5206\u7c7b\uff09\u3002\u4ee5\u4e0b\u662f\u5173\u4e8eR\u8bed\u8a00\u4e2d\u903b\u8f91\u56de\u5f52\u7684\u8be6\u7ec6\u89e3\u91ca\u53ca\u5e94\u7528\u5b9e\u4f8b\u3002<\/p>\n<ol>\n<li>\n<h2>\u903b\u8f91\u56de\u5f52\u7684\u539f\u7406<\/h2>\n<\/li>\n<\/ol>\n<p>\u903b\u8f91\u56de\u5f52\u662f\u57fa\u4e8e\u5bf9\u6570\u51e0\u7387\uff08log odds\uff09\u7684\u7ebf\u6027\u56de\u5f52\u3002\u5bf9\u4e8e\u4e8c\u5206\u7c7b\u95ee\u9898\uff0c\u903b\u8f91\u56de\u5f52\u6a21\u578b\u9884\u6d4b\u4e8b\u4ef6\u53d1\u751f\u7684\u6982\u7387\uff08P\uff09\uff0c\u901a\u8fc7Sigmoid\u51fd\u6570\u5c06\u7ebf\u6027\u56de\u5f52\u7ed3\u679c\u6620\u5c04\u52300\u52301\u4e4b\u95f4\u3002<\/p>\n<ol>\n<li>\n<h2>R\u8bed\u8a00\u5b9e\u73b0\u903b\u8f91\u56de\u5f52<\/h2>\n<\/li>\n<\/ol>\n<p>R\u8bed\u8a00\u4e2d\u7684<code>glm()<\/code>\u51fd\u6570\u53ef\u7528\u4e8e\u62df\u5408\u903b\u8f91\u56de\u5f52\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>glm()<\/code>\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\">glm<span class=\"hljs-punctuation\">(<\/span>formula<span class=\"hljs-punctuation\">,<\/span> data<span class=\"hljs-punctuation\">,<\/span> family <span class=\"hljs-operator\">=<\/span> binomial<span class=\"hljs-punctuation\">(<\/span>link <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"logit\"<\/span><span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>\u5176\u4e2d\uff0c<code>formula<\/code>\u8868\u793a\u6a21\u578b\u516c\u5f0f\uff0c<code>data<\/code>\u8868\u793a\u6570\u636e\u96c6\uff0c<code>family<\/code>\u6307\u5b9a\u4f7f\u7528\u4e8c\u9879\u903b\u8f91\u56de\u5f52\u3002<\/p>\n<ol start=\"3\">\n<li>\n<h2>\u5e94\u7528\u5b9e\u4f8b<\/h2>\n<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u903b\u8f91\u56de\u5f52\u5e94\u7528\u5b9e\u4f8b\uff0c\u4f7f\u7528R\u8bed\u8a00\u5185\u7f6e\u7684mtcars\u6570\u636e\u96c6\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\"><span class=\"hljs-comment\"># \u52a0\u8f7d\u6570\u636e\u96c6<\/span><br \/>\ndata<span class=\"hljs-punctuation\">(<\/span>mtcars<span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u5c06\u6570\u636e\u96c6\u5206\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6<\/span><br \/>\nset.seed<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-number\">123<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\ntrain_index <span class=\"hljs-operator\">&lt;-<\/span> sample<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-number\">1<\/span><span class=\"hljs-operator\">:<\/span>nrow<span class=\"hljs-punctuation\">(<\/span>mtcars<span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">0.8<\/span> <span class=\"hljs-operator\">*<\/span> nrow<span class=\"hljs-punctuation\">(<\/span>mtcars<span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\ntrain_data <span class=\"hljs-operator\">&lt;-<\/span> mtcars<span class=\"hljs-punctuation\">[<\/span>train_index<span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-punctuation\">]<\/span><br \/>\ntest_data <span class=\"hljs-operator\">&lt;-<\/span> mtcars<span class=\"hljs-punctuation\">[<\/span><span class=\"hljs-operator\">-<\/span>train_index<span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-punctuation\">]<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u4f7f\u7528glm()\u51fd\u6570\u62df\u5408\u903b\u8f91\u56de\u5f52\u6a21\u578b<\/span><br \/>\n<span class=\"hljs-comment\"># \u9884\u6d4b\u6c7d\u8f66\u662f\u5426\u4e3a\u81ea\u52a8\u6321\uff08am\uff09\u57fa\u4e8e\u9a6c\u529b\uff08hp\uff09\u548c\u91cd\u91cf\uff08wt\uff09<\/span><br \/>\nlogit_model <span class=\"hljs-operator\">&lt;-<\/span> glm<span class=\"hljs-punctuation\">(<\/span>am <span class=\"hljs-operator\">~<\/span> hp <span class=\"hljs-operator\">+<\/span> wt<span class=\"hljs-punctuation\">,<\/span> data <span class=\"hljs-operator\">=<\/span> train_data<span class=\"hljs-punctuation\">,<\/span> family <span class=\"hljs-operator\">=<\/span> binomial<span class=\"hljs-punctuation\">(<\/span>link <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"logit\"<\/span><span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u67e5\u770b\u6a21\u578b\u6458\u8981<\/span><br \/>\nsummary<span class=\"hljs-punctuation\">(<\/span>logit_model<span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u5bf9\u6d4b\u8bd5\u96c6\u8fdb\u884c\u9884\u6d4b<\/span><br \/>\ntest_data<span class=\"hljs-operator\">$<\/span>predicted_prob <span class=\"hljs-operator\">&lt;-<\/span> predict<span class=\"hljs-punctuation\">(<\/span>logit_model<span class=\"hljs-punctuation\">,<\/span> newdata <span class=\"hljs-operator\">=<\/span> test_data<span class=\"hljs-punctuation\">,<\/span> type <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"response\"<\/span><span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u5c06\u9884\u6d4b\u6982\u7387\u8f6c\u6362\u4e3a\u5206\u7c7b\u6807\u7b7e<\/span><br \/>\ntest_data<span class=\"hljs-operator\">$<\/span>predicted_label <span class=\"hljs-operator\">&lt;-<\/span> ifelse<span class=\"hljs-punctuation\">(<\/span>test_data<span class=\"hljs-operator\">$<\/span>predicted_prob <span class=\"hljs-operator\">&gt;<\/span> <span class=\"hljs-number\">0.5<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">1<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-number\">0<\/span><span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u8ba1\u7b97\u51c6\u786e\u7387<\/span><br \/>\naccuracy <span class=\"hljs-operator\">&lt;-<\/span> mean<span class=\"hljs-punctuation\">(<\/span>test_data<span class=\"hljs-operator\">$<\/span>predicted_label <span class=\"hljs-operator\">==<\/span> test_data<span class=\"hljs-operator\">$<\/span>am<span class=\"hljs-punctuation\">)<\/span><br \/>\ncat<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"Accuracy:\"<\/span><span class=\"hljs-punctuation\">,<\/span> accuracy<span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>glm()<\/code>\u51fd\u6570\u62df\u5408\u4e86\u4e00\u4e2a\u903b\u8f91\u56de\u5f52\u6a21\u578b\uff0c\u4ee5\u9884\u6d4b\u6c7d\u8f66\u662f\u5426\u4e3a\u81ea\u52a8\u6321\uff08am\uff09\uff0c\u57fa\u4e8e\u9a6c\u529b\uff08hp\uff09\u548c\u91cd\u91cf\uff08wt\uff09\u3002\u6211\u4eec\u5c06\u9884\u6d4b\u7684\u6982\u7387\u8f6c\u6362\u4e3a\u5206\u7c7b\u6807\u7b7e\uff0c\u5e76\u8ba1\u7b97\u4e86\u6a21\u578b\u5728\u6d4b\u8bd5\u96c6\u4e0a\u7684\u51c6\u786e\u7387\u3002<\/p>\n<p>\u903b\u8f91\u56de\u5f52\u5728R\u8bed\u8a00\u4e2d\u7684\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\uff0c\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u5206\u7c7b\u95ee\u9898\u3002\u8981\u6df1\u5165\u4e86\u89e3\u903b\u8f91\u56de\u5f52\u5728R\u8bed\u8a00\u4e2d\u7684\u5e94\u7528\uff0c\u53ef\u4ee5\u9605\u8bfb\u6709\u5173\u903b\u8f91\u56de\u5f52\u7684\u4e13\u4e1a\u4e66\u7c4d\u3001\u7814\u7a76\u8bba\u6587\u548c\u6559\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u903b\u8f91\u56de\u5f52\u662f\u4e00\u79cd\u5e7f\u6cdb\u5e94\u7528\u7684\u7edf\u8ba1\u5b66\u65b9\u6cd5\uff0c\u4e3b\u8981\u7528\u4e8e\u9884\u6d4b\u5206\u7c7b\u53d8\u91cf\uff08\u5982\u4e8c\u5206\u7c7b\u6216\u591a\u5206\u7c7b\uff09\u3002\u4ee5\u4e0b\u662f\u5173\u4e8eR\u8bed\u8a00\u4e2d\u903b\u8f91\u56de\u5f52\u7684\u8be6\u7ec6 [&hellip;]<\/p>\n","protected":false},"author":111,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-427","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=427"}],"version-history":[{"count":0,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/427\/revisions"}],"wp:attachment":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}