{"id":23677,"date":"2024-01-08T17:23:43","date_gmt":"2024-01-08T09:23:43","guid":{"rendered":"http:\/\/www.biocloudservice.com\/wordpress\/?p=23677"},"modified":"2024-01-08T17:23:44","modified_gmt":"2024-01-08T09:23:44","slug":"pca%e7%bb%98%e5%9b%be%ef%bc%8c%e4%bb%8e%e5%8e%9f%e7%90%86%e5%88%b0%e7%bb%98%e5%9b%be","status":"publish","type":"post","link":"http:\/\/www.biocloudservice.com\/wordpress\/?p=23677","title":{"rendered":"PCA\u7ed8\u56fe\uff0c\u4ece\u539f\u7406\u5230\u7ed8\u56fe"},"content":{"rendered":"<p>PCA\u5206\u6790\u662f\u964d\u7eac\u5206\u6790\u4e2d\u5e38\u7528\u7684\u624b\u6bb5\uff0c\u5f80\u5f80\u5927\u91cf\u7684\u6570\u636e\u96c6\u4e0d\u80fd\u76f4\u89c2\u7684\u4f53\u73b0\u51fa\u95ee\u9898\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u964d\u7ef4\u5904\u7406\uff0c\u5206\u6790\u8d77\u5230\u4e3b\u8981\u4f5c\u7528\u7684\u6307\u6570PCA1\u3001PCA2\u3001PCA3&#8230;&#8230;\uff0c\u56e0\u6b64\u79f0\u4e3a\u4e3b\u6210\u5206\u5206\u6790\u3002<\/p>\n<p>\u4f7f\u7528PCA\u5206\u6790\u7684\u8fc7\u7a0b\u4e2d\u6211\u4eec\u9700\u8981\u4e86\u89e3\u5176\u4e2d\u7684\u539f\u7406\uff0c\u624d\u80fd\u66f4\u597d\u7684\u7406\u89e3\u751f\u7269\u5b66\u610f\u4e49\uff0c\u73b0\u5728\u7684R\u5305\u4ee5\u53ca\u4e00\u4e9b\u8f6f\u4ef6\u90fd\u80fd\u505a\u5230\u4e00\u952e\u5206\u6790\u7684\u7a0b\u5ea6\uff0c\u4f46\u662f\u6211\u4eec\u4e5f\u8981\u4e86\u89e3\u80cc\u540e\u7684\u539f\u7406\u3002\u672c\u6587\u63d0\u4f9b\u4e24\u79cd\u505a\u51faPCA\u7684\u65b9\u6cd5\uff0c\u4e00\u79cd\u662f\u7406\u89e3\u539f\u7406\u5f0f\u8fdb\u884c\u4e00\u6b65\u6b65\u5206\u6790\uff0c\u4e00\u79cd\u4e3a\u6253\u5305\u597d\u7684prcomp()\u51fd\u6570\u3001princomp()\u51fd\u6570\u6765\u5206\u6790\u3002<\/p>\n<h2>\u4e00\u6b65\u6b65\u505a\u51faPCA\u56fe<\/h2>\n<p>1\u3001\u6570\u636e\u6807\u51c6\u5316<\/p>\n<p>\u4e3a\u4e86\u7edf\u4e00\u6570\u636e\u7684\u91cf\u7eb2\u5e76\u5bf9\u6570\u636e\u8fdb\u884c\u4e2d\u5fc3\u5316\uff0c\u5728\u4e3b\u6210\u5206\u5206\u6790\u4e4b\u95f4\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u6807\u51c6\u5316\u3002<\/p>\n<p>#\u4f7f\u7528R\u4e2d\u81ea\u5e26\u7684\u9e22\u5c3e\u82b1\u4f5c\u4e3a\u6570\u636e\u8fdb\u884c\u5b9e\u8df5<\/p>\n<p>data&lt;-iris<\/p>\n<p>head(data)<\/p>\n<p>#\u7528\u51fd\u6570scale\uff08\uff09\u8fdb\u884c\u5f52\u4e00\u5316\u7684\u5904\u7406<\/p>\n<p>dt&lt;-as.matrix(scale(data[,1:4]))<\/p>\n<ol>\n<li>\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\u77e9\u9635<\/li>\n<\/ol>\n<p>\u4e3b\u6210\u5206\u5373\u627e\u51fa\u89e3\u91ca\u53d8\u91cf\u65b9\u5dee\u6700\u5927\u7684\u4e3b\u6210\u5206\uff0c\u6240\u4ee5\u9700\u8981\u8ba1\u7b97\u53d8\u91cf\u4e4b\u95f4\u534f\u65b9\u5dee\u3002\u7528cor\uff08\uff09\u8fdb\u884c\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\u77e9\u9635<\/p>\n<p>data_cor&lt;-cor(dt)<\/p>\n<p>data_cor<\/p>\n<ol>\n<li>\u7279\u5f81\u503c\u7279\u5f81\u5411\u91cf<\/li>\n<\/ol>\n<p>data_s&lt;-eigen(data_cor)#\u8ba1\u7b97\u7279\u5f81\u503c\u7279\u5f81\u5411\u91cf<\/p>\n<p>data_s<\/p>\n<p>val &lt;- data_s$values #\u63d0\u53d6\u4e3b\u6210\u5206\u90e8\u5206\uff08\u5373\u7279\u5f81\u5411\u91cf\uff09values<\/p>\n<p>standard_deviation &lt;- sqrt(val) #\u6362\u7b97\u4e3asqrt\uff08\uff09\u6807\u51c6\u5dee\u7684\u5f62\u5f0f<\/p>\n<p>pro_v &lt;- val\/sum(val)#\u8ba1\u7b97\u8d21\u732e\u7387\uff0c\u4f5c\u4e3a\u540e\u8fb9\u7684X\u548cY\u8f74\u7684\u6807\u9898<\/p>\n<ol>\n<li>\u8ba1\u7b97\u4e3b\u6210\u5206\u5f97\u5206<\/li>\n<\/ol>\n<p>#\u63d0\u53d6\u7ed3\u679c\u4e2d\u7684\u7279\u5f81\u5411\u91cf<\/p>\n<p>U&lt;-as.matrix(data_s$vectors)<\/p>\n<p>#\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5\uff0c\u83b7\u5f97\u4e3b\u6210\u5206\u5f97\u5206\uff1b<\/p>\n<p>pc &lt;-dt %*% U<\/p>\n<p>colnames(pc) &lt;- c(&#8220;PC1&#8243;,&#8221;PC2&#8243;,&#8221;PC3&#8243;,&#8221;PC4&#8221;)<\/p>\n<p>head(pc)<\/p>\n<ol>\n<li>\u7ed8\u5236\u56fe<\/li>\n<\/ol>\n<p>#\u5c06iris\u6570\u636e\u96c6\u7684\u7b2c5\u5217\u7269\u79cd\u6570\u636e\u52a0\u4e0a\u5408\u5e76\u8fdb\u6765\uff1b<\/p>\n<p>df&lt;-data.frame(PC,iris$Species)<\/p>\n<p>head(df)<\/p>\n<p>#\u7528ggplot2\u7ed8\u5236\u56fe<\/p>\n<p>#\u8f7d\u5165ggplot2\u5305\uff1b<\/p>\n<p>library(ggplot2)<\/p>\n<p>#\u63d0\u53d6\u4e3b\u6210\u5206\u7684\u65b9\u5dee\u8d21\u732e\u7387\uff0c\u751f\u6210\u5750\u6807\u8f74\u6807\u9898\uff1b<\/p>\n<p>xlab&lt;-paste0(&#8220;PC1(&#8220;,round(pro_v[1]*100,2),&#8221;%)&#8221;)#\u8bbe\u7f6ex\u8f74\u7684\u6807\u9898<\/p>\n<p>ylab&lt;-paste0(&#8220;PC2(&#8220;,round(pro_v[2]*100,2),&#8221;%)&#8221;)#\u8bbe\u7f6ey\u8f74\u7684\u6807\u9898<\/p>\n<p>#\u7ed8\u5236\u6563\u70b9\u56fe\u5e76\u6dfb\u52a0\u7f6e\u4fe1\u692d\u5706\uff1b<\/p>\n<p>p1&lt;-ggplot(data = df,aes(x=PC1,y=PC2,color=iris.Species))+<\/p>\n<p>stat_ellipse(aes(fill=iris.Species),<\/p>\n<p>type =&#8221;norm&#8221;, geom =&#8221;polygon&#8221;,alpha=0.2,color=NA)+<\/p>\n<p>geom_point()+labs(x=xlab,y=ylab,color=&#8221;&#8221;)+<\/p>\n<p>guides(fill=F)<\/p>\n<p>p1<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"640\" height=\"497\" class=\"wp-image-23678\" src=\"https:\/\/i0.wp.com\/www.biocloudservice.com\/wordpress\/wp-content\/uploads\/2024\/01\/2023-06-09-16-38-39.png?resize=640%2C497\" alt=\"\u622a\u5c4f2023-06-09 16.38.39\" srcset=\"https:\/\/i0.wp.com\/www.biocloudservice.com\/wordpress\/wp-content\/uploads\/2024\/01\/2023-06-09-16-38-39.png?w=1026 1026w, https:\/\/i0.wp.com\/www.biocloudservice.com\/wordpress\/wp-content\/uploads\/2024\/01\/2023-06-09-16-38-39.png?resize=300%2C233 300w, https:\/\/i0.wp.com\/www.biocloudservice.com\/wordpress\/wp-content\/uploads\/2024\/01\/2023-06-09-16-38-39.png?resize=1024%2C794 1024w, https:\/\/i0.wp.com\/www.biocloudservice.com\/wordpress\/wp-content\/uploads\/2024\/01\/2023-06-09-16-38-39.png?resize=768%2C596 768w, https:\/\/i0.wp.com\/www.biocloudservice.com\/wordpress\/wp-content\/uploads\/2024\/01\/2023-06-09-16-38-39.png?resize=600%2C465 600w\" sizes=\"(max-width: 640px) 100vw, 640px\" data-recalc-dims=\"1\" \/><\/p>\n<h2>\u4f7f\u7528\u73b0\u5728\u5c01\u88c5\u597d\u7684\u51fd\u6570\u8fdb\u884cPCA\u5206\u6790<\/h2>\n<p>prcomp()\u51fd\u6570\u3001princomp()\u51fd\u6570\u53ef\u4ee5\u5bf9\u4e0a\u8ff0\u6b65\u9aa4\u8fdb\u884c\u4e00\u6b21\u6027\u5206\u6790<\/p>\n<p>#\u4f7f\u7528prcomp()\u51fd\u6570\u8fdb\u884c\u76f4\u63a5PCA\u7684\u8ba1\u7b97<\/p>\n<p>com1 &lt;- prcomp(data[,1:4], center = TRUE,scale. = TRUE)<\/p>\n<p>summary(com1)<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"640\" height=\"162\" class=\"wp-image-23679\" src=\"https:\/\/i0.wp.com\/www.biocloudservice.com\/wordpress\/wp-content\/uploads\/2024\/01\/2023-06-09-16-42-48.png?resize=640%2C162\" alt=\"\u622a\u5c4f2023-06-09 16.42.48\" srcset=\"https:\/\/i0.wp.com\/www.biocloudservice.com\/wordpress\/wp-content\/uploads\/2024\/01\/2023-06-09-16-42-48.png?w=948 948w, https:\/\/i0.wp.com\/www.biocloudservice.com\/wordpress\/wp-content\/uploads\/2024\/01\/2023-06-09-16-42-48.png?resize=300%2C76 300w, https:\/\/i0.wp.com\/www.biocloudservice.com\/wordpress\/wp-content\/uploads\/2024\/01\/2023-06-09-16-42-48.png?resize=768%2C194 768w, https:\/\/i0.wp.com\/www.biocloudservice.com\/wordpress\/wp-content\/uploads\/2024\/01\/2023-06-09-16-42-48.png?resize=600%2C152 600w\" sizes=\"(max-width: 640px) 100vw, 640px\" data-recalc-dims=\"1\" \/><\/p>\n<p>\u540e\u7eed\u7684\u9700\u8981\u5408\u5e76\u9e22\u5c3e\u82b1\u5c5e\u7684\u5217\uff08\u57305\u5217\uff09\uff0c\u6700\u540e\u505a\u51fa\u5b9e\u73b0PCA\u53ef\u89c6\u5316\u5373\u53ef\u3002<\/p>\n<h2>PCA\u901a\u4fd7\u6613\u61c2\u7684\u7406\u89e3<\/h2>\n<p>Principal Component Analysis(PCA)\u4e3b\u6210\u5206\u5206\u6790\u5b83\u672c\u8d28\u662f\u628a\u6570\u636e\u89c6\u4e3a\u4e00\u4e2a\u591a\u7ef4\u5ea6\u7684\u5b58\u5728\uff0c\u4f46\u662f\u6bcf\u4e2a\u7eac\u5ea6\u5bf9\u6574\u4e2a\u6570\u636e\u96c6\u7684\u8d21\u732e\u5ea6\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u4e3a\u4e86\u65b9\u4fbf\u89c2\u5bdf\uff0c\u6211\u4eec\u4f1a\u5bf9\u8d21\u732e\u7387\u4f4e\u7684\u7eac\u5ea6\u8fdb\u884c\u5ffd\u7565\uff0c\u4fdd\u75592\u7ef4\u6216\u80053\u7ef4\u7684\u5185\u5bb9\uff0c\u90a3\u7559\u4e0b\u7684\u4e09\u4e2a\u7eac\u5ea6\u90fd\u662f\u8d77\u5230\u91cd\u8981\u4f5c\u7528\u7684\uff0c\u56e0\u6b64\u79f0\u4e3a\u4e3b\u6210\u5206\uff0c\u4f46\u662f\u4ed6\u5bf9\u4e8e\u6574\u4e2a\u6570\u636e\u96c6\u7684\u8d21\u732e\u5e76\u4e0d\u662f1\uff0c\u6240\u4ee5\u6211\u4eec\u770b\u5230\u6a2a\u7eb5\u5750\u6807\u5e76\u4e0d\u662f1\uff0c\u800c\u662f&lt;1\u7684\u6570\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u5bf9\u4e8eR\u4ee3\u7801\u4e0d\u719f\u6089\u7684\uff0c\u73b0\u5728\u4e5f\u4f1a\u6709\u5f88\u591a\u7f51\u4e0a\u7684\u5c0f\u5de5\u5177\u4e00\u952e\u53ef\u4ee5\u51fa\u56fehttp:\/\/www.biocloudservice.com\/794\/794.php<\/p>\n<p>\u603b\u800c\u8a00\u4e4b\uff0c\u6211\u4eec\u662f\u8981\u505a\u51fa\u56fe\u6765\u652f\u6491\u6211\u4eec\u7684\u8bba\u6587\u89c2\u70b9\uff0c\u4f46\u662f\u5bf9\u4e8e\u4e00\u4e9b\u7edf\u8ba1\u5b66\u7684\u77e5\u8bc6\u4e5f\u5e94\u8be5\u6709\u4e00\u5b9a\u7684\u4e86\u89e3\u548c\u638c\u63e1\uff0c\u624d\u80fd\u66f4\u597d\u7684\u5206\u6790\u6570\u636e\u3001\u7406\u89e3\u56fe\u7684\u542b\u4e49\u7b49\u3002<\/p>\n<p>\u4eca\u5929\u7684\u5206\u4eab\u5c31\u5230\u8fd9\u91cc\u4e86\uff0c\u5c0f\u4f19\u4f34\u4eec\u5982\u679c\u6709\u4ec0\u4e48\u95ee\u9898\u5c31\u548c\u5c0f\u679c\u8ba8\u8bba\u5427\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PCA\u5206\u6790\u662f\u964d\u7eac\u5206\u6790\u4e2d\u5e38\u7528\u7684\u624b\u6bb5\uff0c\u5f80\u5f80\u5927\u91cf\u7684\u6570\u636e\u96c6\u4e0d\u80fd\u76f4\u89c2\u7684\u4f53\u73b0\u51fa\u95ee\u9898\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u964d\u7ef4\u5904\u7406\uff0c\u5206\u6790\u8d77 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":""},"categories":[1],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/23677"}],"collection":[{"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=23677"}],"version-history":[{"count":1,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/23677\/revisions"}],"predecessor-version":[{"id":23680,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/23677\/revisions\/23680"}],"wp:attachment":[{"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=23677"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.biocloudservice.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=23677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}