{"id":180219,"date":"2024-05-08T20:30:52","date_gmt":"2024-05-08T12:30:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/180219.html"},"modified":"2024-05-08T20:30:57","modified_gmt":"2024-05-08T12:30:57","slug":"r%e8%af%ad%e8%a8%80%e4%b8%bb%e6%88%90%e5%88%86%e5%88%86%e6%9e%90%e5%a6%82%e4%bd%95%e9%80%89%e5%8f%96%e4%b8%bb%e6%88%90%e5%88%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/180219.html","title":{"rendered":"R\u8bed\u8a00\u4e3b\u6210\u5206\u5206\u6790\u5982\u4f55\u9009\u53d6\u4e3b\u6210\u5206"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27065347\/7a24faed-fd9b-49f0-a18e-b3dbc38c01ec.webp\" alt=\"R\u8bed\u8a00\u4e3b\u6210\u5206\u5206\u6790\u5982\u4f55\u9009\u53d6\u4e3b\u6210\u5206\" \/><\/p>\n<p><p><strong>R\u8bed\u8a00\u4e2d\u8fdb\u884c\u4e3b\u6210\u5206\u5206\u6790\uff08PCA\uff09\u65f6\u9009\u62e9\u4e3b\u6210\u5206\u7684\u4f9d\u636e\u4e3b\u8981\u5305\u62ec\uff1a\u7d2f\u79ef\u8d21\u732e\u7387\u3001\u7279\u5f81\u503c\u5927\u5c0f\u3001Scree Plot\u68c0\u9a8c\u3001\u89e3\u91ca\u529b\u5ea6\u548c\u5b9e\u9645\u5e94\u7528\u9700\u6c42<\/strong>\u3002\u4e00\u822c\u800c\u8a00\uff0c\u7d2f\u79ef\u8d21\u732e\u7387\u8fbe\u523070%-90%\u65f6\uff0c\u53ef\u4ee5\u8ba4\u4e3a\u9009\u53d6\u7684\u4e3b\u6210\u5206\u5df2\u8db3\u591f\u8868\u5f81\u6570\u636e\u7684\u5927\u90e8\u5206\u4fe1\u606f\u3002\u5177\u4f53\u5230\u5b9e\u8df5\u4e2d\uff0c\u7d2f\u79ef\u8d21\u732e\u7387\u901a\u5e38\u662f\u9009\u62e9\u4e3b\u6210\u5206\u7684\u7b2c\u4e00\u6807\u51c6\uff0c<strong>\u7d2f\u79ef\u8d21\u732e\u7387<\/strong> \u53cd\u6620\u4e86\u6240\u9009\u4e3b\u6210\u5206\u5bf9\u539f\u59cb\u6570\u636e\u96c6\u53d8\u5f02\u6027\u7684\u89e3\u91ca\u7a0b\u5ea6\u3002\u7d2f\u79ef\u8d21\u732e\u7387\u8d8a\u9ad8\uff0c\u9009\u62e9\u7684\u4e3b\u6210\u5206\u5bf9\u6570\u636e\u7684\u89e3\u91ca\u5ea6\u8d8a\u9ad8\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7d2f\u79ef\u8d21\u732e\u7387\u7684\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u7d2f\u79ef\u8d21\u732e\u7387\u662f\u8bc4\u4f30\u4e3b\u6210\u5206\u4e2a\u6570\u7684\u5173\u952e\u6307\u6807\uff0c\u53ef\u4ee5\u901a\u8fc7R\u8bed\u8a00\u4e2d\u7684<code>prcomp<\/code>\u6216<code>princomp<\/code>\u7b49\u51fd\u6570\u8fdb\u884cPCA\u5206\u6790\u540e\uff0c\u8ba1\u7b97\u5404\u4e3b\u6210\u5206\u7684\u65b9\u5dee\u8d21\u732e\u7387\uff0c\u5e76\u7d2f\u52a0\u4ee5\u8bc4\u4f30\u6574\u4f53\u7684\u89e3\u91ca\u80fd\u529b\u3002\u901a\u5e38\uff0c\u9009\u62e9\u7d2f\u79ef\u8d21\u732e\u7387\u8fbe\u523070%-90%\u7684\u4e3b\u6210\u5206\u4e2a\u6570\u4e3a\u5b9c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7279\u5f81\u503c\u7684\u5927\u5c0f<\/h3>\n<\/p>\n<p><p>\u7279\u5f81\u503c\u4ee3\u8868\u4e86\u8be5\u4e3b\u6210\u5206\u8868\u5f81\u7684\u6570\u636e\u53d8\u5f02\u91cf\u3002\u7279\u5f81\u503c\u5927\u4e8e1\u7684\u4e3b\u6210\u5206\u901a\u5e38\u8ba4\u4e3a\u662f\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5b83\u89e3\u91ca\u7684\u53d8\u5f02\u6027\u8d85\u8fc7\u4e86\u4e00\u4e2a\u6807\u51c6\u53d8\u91cf\u3002\u5bf9\u4e8e\u8f83\u5c0f\u7684\u6570\u636e\u96c6\uff0c\u7279\u5f81\u503c\u5927\u5c0f\u4e5f\u662f\u9009\u62e9\u4e3b\u6210\u5206\u7684\u4e00\u4e2a\u91cd\u8981\u6807\u51c6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001Scree Plot\u68c0\u9a8c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Scree Plot\u53ef\u4ee5\u5e2e\u52a9\u76f4\u89c2\u5224\u65ad\u54ea\u4e9b\u4e3b\u6210\u5206\u8f83\u4e3a\u91cd\u8981\u3002Scree Plot\u662f\u4e00\u4e2a\u5c06\u7279\u5f81\u503c\u4ece\u5927\u5230\u5c0f\u5448\u73b0\u7684\u6298\u7ebf\u56fe\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6298\u7ebf\u7684\u524d\u4e00\u90e8\u5206\u4e0b\u964d\u8f83\u5feb\uff0c\u800c\u540e\u90e8\u5206\u5e73\u7f13\uff0c\u5448\u73b0\u4e3a\u4e00\u4e2a\u8098\u90e8\u7684\u5f62\u72b6\u3002\u8098\u90e8\u4e4b\u524d\u7684\u4e3b\u6210\u5206\u88ab\u8ba4\u4e3a\u662f\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u89e3\u91ca\u529b\u5ea6\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u4e3b\u6210\u5206\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5bf9\u5176\u8f7d\u8377\u8fdb\u884c\u5206\u6790\uff0c\u786e\u5b9a\u54ea\u4e9b\u53d8\u91cf\u5bf9\u6b64\u4e3b\u6210\u5206\u5177\u6709\u8f83\u5927\u7684\u8d21\u732e\uff0c\u5e76\u57fa\u4e8e\u8fd9\u4e9b\u4fe1\u606f\u51b3\u5b9a\u662f\u5426\u4fdd\u7559\u76f8\u5e94\u7684\u4e3b\u6210\u5206\u3002\u8fd9\u6709\u52a9\u4e8e\u63d0\u5347\u6a21\u578b\u7684\u53ef\u89e3\u91ca\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u9700\u6c42<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u8fd8\u9700\u8003\u8651\u4e1a\u52a1\u76ee\u6807\u548c\u5b9e\u9645\u60c5\u5883\u3002\u6709\u65f6\u6a21\u578b\u7684\u53ef\u89e3\u91ca\u6027\u548c\u64cd\u4f5c\u6027\u53ef\u80fd\u6bd4\u7eaf\u7cb9\u7684\u7edf\u8ba1\u6307\u6807\u66f4\u4e3a\u91cd\u8981\uff0c\u56e0\u6b64\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\u8981\u7ed3\u5408\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u76ee\u7684\u6765\u9009\u62e9\u4e3b\u6210\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u64cd\u6f14\u793a<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u6f14\u793a\u5982\u4f55\u4f7f\u7528R\u8bed\u8a00\u7684<code>prcomp<\/code>\u51fd\u6570\u8fdb\u884cPCA\uff0c\u5e76\u57fa\u4e8e\u4e0a\u8ff0\u51c6\u5219\u5982\u4f55\u9009\u62e9\u4e3b\u6210\u5206\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\u5b89\u88c5\u5e76\u52a0\u8f7d\u5fc5\u8981\u7684\u5305\uff0c\u5e76\u51c6\u5907\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">install.packages(&quot;ggplot2&quot;)<\/p>\n<p>library(ggplot2)<\/p>\n<p>data &lt;- mtcars  # \u4f7f\u7528mtcars\u6570\u636e\u96c6\u6765\u8fdb\u884c\u6f14\u793a<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6267\u884cPCA\u5206\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">pca_result &lt;- prcomp(data[, -1],  # \u7701\u7565\u7b2c\u4e00\u5217\uff08\u6c7d\u8f66\u578b\u53f7\u540d\uff09<\/p>\n<p>                     scale. = TRUE)  # \u662f\u5426\u6807\u51c6\u5316\u6570\u636e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8ba1\u7b97\u4e3b\u6210\u5206\u7684\u65b9\u5dee\u8d21\u732e\u7387\u5e76\u6253\u5370\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">contrib_rate &lt;- pca_result$sdev^2 \/ sum(pca_result$sdev^2)<\/p>\n<p>cum_contrib_rate &lt;- cumsum(contrib_rate)<\/p>\n<p>print(cum_contrib_rate)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7ed8\u5236Scree Plot\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">plot(pca_result, type = &quot;lines&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7ed3\u5408Scree Plot\u548c\u7d2f\u79ef\u8d21\u732e\u7387\u6765\u5224\u65ad\u4fdd\u7559\u7684\u4e3b\u6210\u5206\u4e2a\u6570\u3002\u4ee5\u5b9e\u9645\u6570\u636e\u548c\u5206\u6790\u9700\u6c42\u6765\u7efc\u5408\u5224\u65ad\u3002\u6839\u636e\u7ed3\u679c\u9009\u62e9\u4fdd\u7559\u524d\u51e0\u4e2a\u4e3b\u6210\u5206\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u964d\u7ef4\uff0c\u5e76\u636e\u6b64\u8fdb\u884c\u540e\u7eed\u5206\u6790\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5206\u6790\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5408\u7406\u9009\u62e9\u5728PCA\u4e2d\u9009\u53d6\u7684\u4e3b\u6210\u5206\u4e2a\u6570\u3002\u5728\u5b9e\u9645\u7684\u6570\u636e\u5206\u6790\u9879\u76ee\u4e2d\uff0c\u9009\u62e9\u4e3b\u6210\u5206\u662f\u4e00\u4e2a\u9700\u8981\u7efc\u5408\u8003\u91cf\u591a\u4e2a\u6807\u51c6\u548c\u5b9e\u9645\u5e94\u7528\u573a\u666f\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u4ec0\u4e48\u662fR\u8bed\u8a00\u4e3b\u6210\u5206\u5206\u6790\u4ee5\u53ca\u5982\u4f55\u9009\u62e9\u4e3b\u6210\u5206\uff1f<\/strong><\/p>\n<p>\u4e3b\u6210\u5206\u5206\u6790\u662f\u4e00\u79cd\u7528\u4e8e\u964d\u4f4e\u6570\u636e\u7ef4\u5ea6\u548c\u63a2\u7d22\u6570\u636e\u4e4b\u95f4\u76f8\u5173\u6027\u7684\u7edf\u8ba1\u5206\u6790\u65b9\u6cd5\u3002\u5728R\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>prcomp()<\/code>\u51fd\u6570\u6267\u884c\u4e3b\u6210\u5206\u5206\u6790\uff0c\u5e76\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\u9009\u62e9\u4e3b\u6210\u5206\u3002<\/p>\n<p><strong>1. \u7d2f\u8ba1\u65b9\u5dee\u8d21\u732e\u7387\uff1a<\/strong> \u4e3b\u6210\u5206\u7684\u7d2f\u8ba1\u65b9\u5dee\u8d21\u732e\u7387\u53ef\u4ee5\u544a\u8bc9\u6211\u4eec\u4e3b\u6210\u5206\u89e3\u91ca\u4e86\u591a\u5c11\u539f\u59cb\u6570\u636e\u4e2d\u7684\u65b9\u5dee\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ed8\u5236\u7d2f\u8ba1\u65b9\u5dee\u8d21\u732e\u7387\u7684\u6298\u7ebf\u56fe\u6765\u5224\u65ad\u4fdd\u7559\u591a\u5c11\u4e3b\u6210\u5206\u3002\u901a\u5e38\uff0c\u6211\u4eec\u9009\u62e9\u7d2f\u8ba1\u65b9\u5dee\u8d21\u732e\u7387\u4e3a85-95%\u7684\u4e3b\u6210\u5206\u3002<\/p>\n<p><strong>2. \u7279\u5f81\u503c\uff1a<\/strong> \u7279\u5f81\u503c\u4e5f\u53ef\u4ee5\u7528\u6765\u9009\u62e9\u4e3b\u6210\u5206\u3002\u7279\u5f81\u503c\u8868\u793a\u4e3b\u6210\u5206\u7684\u65b9\u5dee\uff0c\u8f83\u5927\u7684\u7279\u5f81\u503c\u610f\u5473\u7740\u8be5\u4e3b\u6210\u5206\u89e3\u91ca\u4e86\u66f4\u591a\u7684\u65b9\u5dee\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u7279\u5f81\u503c\u7684\u5927\u5c0f\u6765\u51b3\u5b9a\u4fdd\u7559\u591a\u5c11\u4e3b\u6210\u5206\u3002<\/p>\n<p><strong>3. \u6210\u5206\u8f7d\u8377\uff1a<\/strong> \u6210\u5206\u8f7d\u8377\u662f\u4e3b\u6210\u5206\u4e0e\u539f\u59cb\u53d8\u91cf\u4e4b\u95f4\u7684\u76f8\u5173\u6027\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u6210\u5206\u8f7d\u8377\u77e9\u9635\u6765\u786e\u5b9a\u54ea\u4e9b\u53d8\u91cf\u5bf9\u4e8e\u89e3\u91ca\u4e3b\u6210\u5206\u6700\u91cd\u8981\u3002\u5982\u679c\u67d0\u4e2a\u53d8\u91cf\u7684\u8f7d\u8377\u503c\u7edd\u5bf9\u503c\u8f83\u5927\uff0c\u5219\u8bf4\u660e\u8be5\u53d8\u91cf\u5bf9\u5e94\u7684\u4e3b\u6210\u5206\u91cd\u8981\u3002<\/p>\n<p>\u603b\u4e4b\uff0c\u5728\u9009\u62e9\u4e3b\u6210\u5206\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u7efc\u5408\u8003\u8651\u7d2f\u8ba1\u65b9\u5dee\u8d21\u732e\u7387\u3001\u7279\u5f81\u503c\u548c\u6210\u5206\u8f7d\u8377\u7b49\u56e0\u7d20\uff0c\u6765\u786e\u5b9a\u4fdd\u7559\u7684\u4e3b\u6210\u5206\u6570\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"R\u8bed\u8a00\u4e2d\u8fdb\u884c\u4e3b\u6210\u5206\u5206\u6790\uff08PCA\uff09\u65f6\u9009\u62e9\u4e3b\u6210\u5206\u7684\u4f9d\u636e\u4e3b\u8981\u5305\u62ec\uff1a\u7d2f\u79ef\u8d21\u732e\u7387\u3001\u7279\u5f81\u503c\u5927\u5c0f\u3001Scree Plot\u68c0\u9a8c\u3001 [&hellip;]","protected":false},"author":3,"featured_media":180227,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/180219"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=180219"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/180219\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/180227"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=180219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=180219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=180219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}