{"id":445,"date":"2023-04-25T03:17:58","date_gmt":"2023-04-25T03:17:58","guid":{"rendered":"https:\/\/yunshangtulv.com.cn\/?p=445"},"modified":"2023-04-25T03:17:58","modified_gmt":"2023-04-25T03:17:58","slug":"r%e8%af%ad%e8%a8%80%e8%bf%9b%e9%98%b6%e7%bb%98%e5%9b%be%ef%bc%9apca","status":"publish","type":"post","link":"https:\/\/yunshangtulv.com.cn\/?p=445","title":{"rendered":"R\u8bed\u8a00\u8fdb\u9636\u7ed8\u56fe\uff1aPCA"},"content":{"rendered":"<p>\u4e3b\u6210\u5206\u5206\u6790\uff08PCA\uff09\u662f\u4e00\u79cd\u964d\u7ef4\u6280\u672f\uff0c\u7528\u4e8e\u5728\u6570\u636e\u4e2d\u53d1\u73b0\u6f5c\u5728\u7684\u6a21\u5f0f\uff0c\u964d\u4f4e\u6570\u636e\u7684\u590d\u6742\u6027\uff0c\u540c\u65f6\u5c3d\u53ef\u80fd\u5730\u4fdd\u7559\u539f\u59cb\u6570\u636e\u7684\u4fe1\u606f\u3002PCA\u901a\u8fc7\u627e\u5230\u6570\u636e\u4e2d\u65b9\u5dee\u6700\u5927\u7684\u6b63\u4ea4\uff08\u5782\u76f4\uff09\u65b9\u5411\u6765\u521b\u5efa\u65b0\u7684\u5750\u6807\u7cfb\uff0c\u8fd9\u4e9b\u65b9\u5411\u88ab\u79f0\u4e3a\u4e3b\u6210\u5206\u3002\u7b2c\u4e00\u4e2a\u4e3b\u6210\u5206\uff08PC1\uff09\u662f\u89e3\u91ca\u6570\u636e\u4e2d\u6700\u5927\u65b9\u5dee\u7684\u65b9\u5411\uff0c\u7b2c\u4e8c\u4e2a\u4e3b\u6210\u5206\uff08PC2\uff09\u662f\u4e0e\u7b2c\u4e00\u4e2a\u4e3b\u6210\u5206\u6b63\u4ea4\u5e76\u89e3\u91ca\u5c3d\u53ef\u80fd\u591a\u5269\u4f59\u65b9\u5dee\u7684\u65b9\u5411\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528R\u8bed\u8a00\u8fdb\u884cPCA\u7684\u793a\u4f8b\u3002\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u96c6\uff0c\u5e76\u4f7f\u7528R\u7684prcomp\u51fd\u6570\u8fdb\u884cPCA\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u751f\u6210\u4e00\u4e2a\u5e26\u6709\u4e09\u4e2a\u53d8\u91cf\u7684\u6570\u636e\u96c6\uff0c\u5e76\u5c06\u5176\u964d\u81f3\u4e8c\u7ef4\u7a7a\u95f4\u3002<\/p>\n<pre class=\"corepress-code-pre\"><code># \u5b89\u88c5\u5fc5\u8981\u7684\u5e93\r\ninstall.packages(\"ggplot2\")\r\ninstall.packages(\"ggrepel\")\r\nlibrary(ggplot2)\r\nlibrary(ggrepel)\r\n\r\n# \u521b\u5efa\u6570\u636e\u96c6\r\nset.seed(42)\r\nn &lt;- 50\r\nx1 &lt;- rnorm(n, mean = 0, sd = 1)\r\nx2 &lt;- x1 * 0.8 + rnorm(n, mean = 0, sd = 0.5)\r\nx3 &lt;- x1 * (-0.6) + rnorm(n, mean = 0, sd = 0.5)\r\ndata &lt;- data.frame(x1, x2, x3)\r\n\r\n# \u6267\u884cPCA\r\npca &lt;- prcomp(data, scale. = TRUE)\r\n\r\n# \u8f6c\u6362\u4e3a\u6570\u636e\u6846\r\npca_data &lt;- data.frame(Sample = 1:n, PC1 = pca$x[,1], PC2 = pca$x[,2])\r\n\r\n# \u6dfb\u52a0\u5206\u7ec4\u53d8\u91cf\r\ndata$group &lt;- ifelse(data$x1 &gt; 0, \"A\", \"B\")\r\npca_data$group &lt;- data$group\r\n\r\n# \u8ba1\u7b97\u89e3\u91ca\u65b9\u5dee\u6bd4\u4f8b\r\nexplained_variance_ratio &lt;- pca$sdev^2 \/ sum(pca$sdev^2)\r\n\r\n# \u7ed8\u5236PCA\u56fe\r\np &lt;- ggplot(data = pca_data, aes(x = PC1, y = PC2, label = Sample, color = group)) +\r\n  geom_point(size = 3) +\r\n  geom_text_repel() +\r\n  stat_ellipse(aes(fill = group), level = 0.95, alpha = 0.2) + # \u6dfb\u52a0\u5e26\u6709\u989c\u8272\u7684\u7f6e\u4fe1\u5708\r\n  labs(title = \"PCA Plot\", x = paste0(\"PC1 (\", round(explained_variance_ratio[1] * 100, 1), \"%)\"), y = paste0(\"PC2 (\", round(explained_variance_ratio[2] * 100, 1), \"%)\")) +\r\n  scale_color_manual(values = c(\"A\" = \"red\", \"B\" = \"blue\"), name = \"Group\") +\r\n  scale_fill_manual(values = c(\"A\" = \"red\", \"B\" = \"blue\"), name = \"Group\") + # \u6dfb\u52a0\u989c\u8272\u6620\u5c04\r\n  theme_bw() +\r\n  theme(plot.title = element_text(hjust = 0.5),\r\n        panel.background = element_rect(fill = \"lightgray\", color = \"black\")) # \u6dfb\u52a0\u80cc\u666f\u8272\r\n\r\nprint(p)\r\n\r\n# \u4fdd\u5b58\u4e3aPDF\u6587\u4ef6\r\nggsave(\"pca_plot_with_colored_confidence_ellipse_and_background.pdf\", plot = p, width = 8, height = 6)\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/yunshangtulv.com.cn\/wp-content\/uploads\/2023\/04\/1682392597383.webp\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3b\u6210\u5206\u5206\u6790\uff08PCA\uff09\u662f\u4e00\u79cd\u964d\u7ef4\u6280\u672f\uff0c\u7528\u4e8e\u5728\u6570\u636e\u4e2d\u53d1\u73b0\u6f5c\u5728\u7684\u6a21\u5f0f\uff0c\u964d\u4f4e\u6570\u636e\u7684\u590d\u6742\u6027\uff0c\u540c\u65f6\u5c3d\u53ef\u80fd\u5730\u4fdd\u7559\u539f\u59cb\u6570\u636e\u7684\u4fe1 [&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-445","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/445","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=445"}],"version-history":[{"count":0,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/445\/revisions"}],"wp:attachment":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}