{"id":5599,"date":"2025-05-17T07:19:00","date_gmt":"2025-05-16T23:19:00","guid":{"rendered":"http:\/\/cnliutz.pgrm.cc\/?p=5599"},"modified":"2025-05-17T07:26:55","modified_gmt":"2025-05-16T23:26:55","slug":"r%e8%af%ad%e8%a8%80anova-and-plot-%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"http:\/\/cnliutz.wicp.vip\/?p=5599","title":{"rendered":"R\u8bed\u8a00ANOVA  and Plot  \u4ee3\u7801"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code><strong>\u8be5\u4ee3\u7801\u4f7f\u7528 R markdown  \u8bed\u6cd5<\/strong>\n---\ntitle: \"ANOVA and Plot\"\noutput:\n  html_document:\n    df_print: paged\n---\n\n## anova oneway\u5355\u56e0\u7d20\u65b9\u5dee\u5206\u6790\n```{r}\n# \u52a0\u8f7ddplyr\u5305\nlibrary(dplyr) \n# \u52a0\u8f7dcholesterol\u6570\u636e\u96c6\ndata(cholesterol, package=\"multcomp\") \n# \u8ba1\u7b97\u5404\u6cbb\u7597\u7ec4\u7684\u6837\u672c\u91cf\u3001\u5747\u503c\u3001\u6807\u51c6\u5dee\u548c95%\u7f6e\u4fe1\u533a\u95f4\nplotdata &lt;- cholesterol %>%\n  group_by(trt) %>%\n  summarize(n = n(),\n            mean = mean(response),\n            sd = sd(response),\n            ci = qt(0.975, df = n - 1) * sd \/ sqrt(n)) \n# \u663e\u793a\u8ba1\u7b97\u7ed3\u679c\nplotdata\n# \u8fdb\u884c\u5355\u56e0\u7d20\u65b9\u5dee\u5206\u6790\nfit &lt;- aov(response ~ trt, data=cholesterol) \n# \u67e5\u770b\u65b9\u5dee\u5206\u6790\u7ed3\u679c\nsummary(fit) \n# \u52a0\u8f7dggplot2\u5305\nlibrary(ggplot2) \n# \u7ed8\u5236\u6cbb\u7597\u7ec4\u5747\u503c\u548c\u7f6e\u4fe1\u533a\u95f4\u7684\u56fe\u5f62\nggplot(plotdata, aes(x = trt, y = mean, group = 1)) +\n  geom_point(size = 3, color=\"red\") +\n  geom_line(linetype=\"dashed\", color=\"darkgrey\") +\n  geom_errorbar(aes(ymin = mean - ci, ymax = mean + ci), width=.1) +\n  theme_bw() +\n  labs(x=\"Treatment\", y=\"Response\", title=\"Mean Plot with 95% Confidence Interval\")\n```\n## one way and two way ANOVA\n```{r}\n# \u52a0\u8f7d\u5fc5\u8981\u7684\u5305\nlibrary(dplyr)\nlibrary(ggplot2)\nlibrary(car)\nlibrary(multcomp)\n\n# \u5355\u56e0\u7d20\u65b9\u5dee\u5206\u6790\u7ed8\u56fe\u793a\u4f8b - \u4f7f\u7528cholesterol\u6570\u636e\u96c6\ndata(cholesterol, package = \"multcomp\")\n\n# 1. \u7ed8\u5236\u5747\u503c\u56fe\u548c\u7f6e\u4fe1\u533a\u95f4\nplotdata &lt;- cholesterol %>%\n  group_by(trt) %>%\n  summarize(\n    n = n(),\n    mean = mean(response),\n    sd = sd(response),\n    se = sd \/ sqrt(n),\n    ci = qt(0.975, df = n - 1) * se\n  )\n\n# \u57fa\u7840\u5747\u503c\u56fe\np1 &lt;- ggplot(plotdata, aes(x = trt, y = mean, group = 1)) +\n  geom_point(size = 3, color = \"red\") +\n  geom_line(linetype = \"dashed\", color = \"darkgrey\") +\n  geom_errorbar(aes(ymin = mean - ci, ymax = mean + ci), width = 0.1) +\n  theme_bw() +\n  labs(\n    x = \"Treatment\",\n    y = \"Response\",\n    title = \"Mean Plot with 95% Confidence Intervals\"\n  )\n\n# 2. \u7ed8\u5236\u7bb1\u7ebf\u56fe\u5c55\u793a\u7ec4\u95f4\u5206\u5e03\np2 &lt;- ggplot(cholesterol, aes(x = trt, y = response)) +\n  geom_boxplot(fill = \"lightblue\", color = \"black\") +\n  theme_bw() +\n  labs(\n    x = \"Treatment\",\n    y = \"Response\",\n    title = \"Boxplot of Treatment Groups\"\n  )\n\n# 3. \u7ed8\u5236\u6563\u70b9\u56fe\u52a0\u6296\u52a8\u4ee5\u663e\u793a\u539f\u59cb\u6570\u636e\u70b9\np3 &lt;- ggplot(cholesterol, aes(x = trt, y = response)) +\n  geom_jitter(width = 0.2, alpha = 0.5, color = \"blue\") +\n  theme_bw() +\n  labs(\n    x = \"Treatment\",\n    y = \"Response\",\n    title = \"Scatter Plot with Jitter\"\n  )\n\n# 4. \u4e24\u56e0\u7d20\u65b9\u5dee\u5206\u6790\u4ea4\u4e92\u6548\u5e94\u56fe\u793a\u4f8b\uff08\u4fee\u6b63\u7248\uff09\nset.seed(123)  # \u8bbe\u7f6e\u968f\u673a\u79cd\u5b50\u4ee5\u786e\u4fdd\u7ed3\u679c\u53ef\u91cd\u73b0\n\n# \u521b\u5efa\u56e0\u5b50\u7ec4\u5408\uff086\u7ec4\uff09\nmydata &lt;- expand.grid(\n  A = factor(c(\"Low\", \"High\")),\n  B = factor(c(\"Control\", \"Treatment1\", \"Treatment2\"))\n)\n\n# \u4e3a\u6bcf\u7ec4\u5b9a\u4e49\u5747\u503c\uff08\u8bbe\u7f6eA\u548cB\u7684\u4e3b\u6548\u5e94\u4ee5\u53ca\u4ea4\u4e92\u6548\u5e94\uff09\ngroup_means &lt;- matrix(\n  c(20, 25, 30,  # A=Low\u65f6B\u5404\u6c34\u5e73\u7684\u5747\u503c\n    35, 45, 50), # A=High\u65f6B\u5404\u6c34\u5e73\u7684\u5747\u503c\uff08\u6ce8\u610fTreatment1\u7684\u5747\u503c\u589e\u52a0\u66f4\u591a\uff0c\u663e\u793a\u4ea4\u4e92\u6548\u5e94\uff09\n  nrow = 2, byrow = TRUE,\n  dimnames = list(c(\"Low\", \"High\"), c(\"Control\", \"Treatment1\", \"Treatment2\"))\n)\n\n# \u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u957f\u683c\u5f0f\u7684\u5747\u503c\u5411\u91cf\nmydata$mean &lt;- as.vector(group_means)\n\n# \u4e3a\u6bcf\u4e2a\u7ec4\u751f\u621010\u4e2a\u89c2\u6d4b\u503c\uff08\u517160\u4e2a\uff09\nmydata &lt;- mydata&#91;rep(1:nrow(mydata), each = 10), ]  # \u590d\u5236\u884c\u5f62\u621060\u884c\nmydata$y &lt;- mydata$mean + rnorm(nrow(mydata), mean = 0, sd = 3)  # \u6dfb\u52a0\u968f\u673a\u566a\u58f0\n\n# \u62df\u5408\u4e24\u56e0\u7d20\u65b9\u5dee\u5206\u6790\u6a21\u578b\nmodel &lt;- aov(y ~ A * B, data = mydata)\n\n# \u4f7f\u7528effect\u5305\u8ba1\u7b97\u9884\u6d4b\u503c\nif (!require(effects)) install.packages(\"effects\")\nlibrary(effects)\n\n# \u8ba1\u7b97A\u548cB\u7684\u4ea4\u4e92\u6548\u5e94\neffect_data &lt;- as.data.frame(effect(\"A:B\", model))\n\n# \u7ed8\u5236\u4ea4\u4e92\u6548\u5e94\u56fe\np4 &lt;- ggplot(effect_data, aes(x = B, y = fit, group = A, color = A)) +\n  geom_line(size = 1.2) +\n  geom_point(size = 3) +\n  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.1, size = 0.8) +\n  theme_bw() +\n  labs(\n    x = \"Factor B\",\n    y = \"Predicted Values\",\n    title = \"Interaction Plot: A x B\",\n    color = \"Factor A\"\n  ) +\n  scale_color_manual(values = c(\"Low\" = \"blue\", \"High\" = \"red\"))\n\n# 5. \u6b8b\u5dee\u8bca\u65ad\u56fe - \u68c0\u67e5\u65b9\u5dee\u5206\u6790\u5047\u8bbe\nfit &lt;- aov(response ~ trt, data = cholesterol)\npar(mfrow = c(2, 2))\nplot(fit)\npar(mfrow = c(1, 1))\n\n# \u663e\u793a\u6240\u6709\u56fe\u5f62\ngridExtra::grid.arrange(p1, p2, p3, p4, ncol = 2)    \n```<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-5599","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5599"}],"version-history":[{"count":3,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5599\/revisions"}],"predecessor-version":[{"id":5602,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5599\/revisions\/5602"}],"wp:attachment":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5599"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}