1. #-------------------------------------------------------------------#
  2. # R in Action (2nd ed): Chapter 9 #
  3. # Analysis of variance #
  4. # requires packages multcomp, gplots, car, HH, effects, #
  5. # rrcov, mvoutlier to be installed #
  6. # install.packages(c("multcomp", "gplots", "car", "HH", "effects", #
  7. # "rrcov", "mvoutlier")) #
  8. #-------------------------------------------------------------------#
  9.  
  10. par(ask=TRUE)
  11. opar <- par(no.readonly=TRUE) # save original parameters
  12.  
  13. # Listing 9.1 - One-way ANOVA
  14. library(multcomp)
  15. attach(cholesterol)
  16. table(trt)
  17. aggregate(response, by=list(trt), FUN=mean)
  18. aggregate(response, by=list(trt), FUN=sd)
  19. fit <- aov(response ~ trt)
  20. summary(fit)
  21. library(gplots)
  22. plotmeans(response ~ trt, xlab="Treatment", ylab="Response",
  23. main="Mean Plot\nwith 95% CI")
  24. detach(cholesterol)
  25.  
  26. # Listing 9.2 - Tukey HSD pairwise group comparisons
  27. TukeyHSD(fit)
  28. par(las=2)
  29. par(mar=c(5,8,4,2))
  30. plot(TukeyHSD(fit))
  31. par(opar)
  32.  
  33. # Multiple comparisons the multcomp package
  34. library(multcomp)
  35. par(mar=c(5,4,6,2))
  36. tuk <- glht(fit, linfct=mcp(trt="Tukey"))
  37. plot(cld(tuk, level=.05),col="lightgrey")
  38. par(opar)
  39.  
  40. # Assessing normality
  41. library(car)
  42. qqPlot(lm(response ~ trt, data=cholesterol),
  43. simulate=TRUE, main="Q-Q Plot", labels=FALSE)
  44.  
  45. # Assessing homogeneity of variances
  46. bartlett.test(response ~ trt, data=cholesterol)
  47.  
  48. # Assessing outliers
  49. library(car)
  50. outlierTest(fit)
  51.  
  52. # Listing 9.3 - One-way ANCOVA
  53. data(litter, package="multcomp")
  54. attach(litter)
  55. table(dose)
  56. aggregate(weight, by=list(dose), FUN=mean)
  57. fit <- aov(weight ~ gesttime + dose)
  58. summary(fit)
  59.  
  60. # Obtaining adjusted means
  61. library(effects)
  62. effect("dose", fit)
  63.  
  64. # Listing 9.4 - Multiple comparisons using user supplied contrasts
  65. library(multcomp)
  66. contrast <- rbind("no drug vs. drug" = c(3, -1, -1, -1))
  67. summary(glht(fit, linfct=mcp(dose=contrast)))
  68.  
  69. # Listing 9.5 - Testing for homegeneity of regression slopes
  70. library(multcomp)
  71. fit2 <- aov(weight ~ gesttime*dose, data=litter)
  72. summary(fit2)
  73.  
  74. # Visualizing a one-way ANCOVA
  75. library(HH)
  76. ancova(weight ~ gesttime + dose, data=litter)
  77.  
  78. # Listing 9.6 - Two way ANOVA
  79. attach(ToothGrowth)
  80. table(supp,dose)
  81. aggregate(len, by=list(supp,dose), FUN=mean)
  82. aggregate(len, by=list(supp,dose), FUN=sd)
  83. dose <- factor(dose)
  84. fit <- aov(len ~ supp*dose)
  85. summary(fit)
  86.  
  87. # plotting interactions
  88. interaction.plot(dose, supp, len, type="b",
  89. col=c("red","blue"), pch=c(16, 18),
  90. main = "Interaction between Dose and Supplement Type")
  91. library(gplots)
  92. plotmeans(len ~ interaction(supp, dose, sep=" "),
  93. connect=list(c(1, 3, 5),c(2, 4, 6)),
  94. col=c("red","darkgreen"),
  95. main = "Interaction Plot with 95% CIs",
  96. xlab="Treatment and Dose Combination")
  97. library(HH)
  98. interaction2wt(len~supp*dose)
  99.  
  100. # Listing 9.7 - Repeated measures ANOVA with one between and within groups factor
  101. CO2$conc <- factor(CO2$conc)
  102. w1b1 <- subset(CO2, Treatment=='chilled')
  103. fit <- aov(uptake ~ (conc*Type) + Error(Plant/(conc)), w1b1)
  104. summary(fit)
  105. par(las=2)
  106. par(mar=c(10,4,4,2))
  107. with(w1b1,
  108. interaction.plot(conc,Type,uptake,
  109. type="b", col=c("red","blue"), pch=c(16,18),
  110. main="Interaction Plot for Plant Type and Concentration"))
  111. boxplot(uptake ~ Type*conc, data=w1b1, col=(c("gold","green")),
  112. main="Chilled Quebec and Mississippi Plants",
  113. ylab="Carbon dioxide uptake rate (umol/m^2 sec)")
  114. par(opar)
  115.  
  116. # Listing 9.8 - One-way MANOVA
  117. library(MASS)
  118. attach(UScereal)
  119. shelf <- factor(shelf)
  120. y <- cbind(calories, fat, sugars)
  121. aggregate(y, by=list(shelf), FUN=mean)
  122. cov(y)
  123. fit <- manova(y ~ shelf)
  124. summary(fit)
  125. summary.aov(fit)
  126.  
  127. # Listing 9.9 - Assessing multivariate normality
  128. center <- colMeans(y)
  129. n <- nrow(y)
  130. p <- ncol(y)
  131. cov <- cov(y)
  132. d <- mahalanobis(y,center,cov)
  133. coord <- qqplot(qchisq(ppoints(n),df=p),
  134. d, main="QQ Plot Assessing Multivariate Normality",
  135. ylab="Mahalanobis D2")
  136. abline(a=0,b=1)
  137. identify(coord$x, coord$y, labels=row.names(UScereal))
  138.  
  139. # multivariate outliers
  140. library(mvoutlier)
  141. outliers <- aq.plot(y)
  142. outliers
  143.  
  144. # Listing 9.10 - Robust one-way MANOVA
  145. library(rrcov)
  146. Wilks.test(y,shelf, method="mcd") # this can take a while
  147.  
  148. # Listing 9.11 - A regression approach to the Anova problem
  149. fit.lm <- lm(response ~ trt, data=cholesterol)
  150. summary(fit.lm)
  151. contrasts(cholesterol$trt)

吴裕雄--天生自然 R语言开发学习:方差分析的更多相关文章

  1. 吴裕雄--天生自然 R语言开发学习:R语言的安装与配置

    下载R语言和开发工具RStudio安装包 先安装R

  2. 吴裕雄--天生自然 R语言开发学习:数据集和数据结构

    数据集的概念 数据集通常是由数据构成的一个矩形数组,行表示观测,列表示变量.表2-1提供了一个假想的病例数据集. 不同的行业对于数据集的行和列叫法不同.统计学家称它们为观测(observation)和 ...

  3. 吴裕雄--天生自然 R语言开发学习:导入数据

    2.3.6 导入 SPSS 数据 IBM SPSS数据集可以通过foreign包中的函数read.spss()导入到R中,也可以使用Hmisc 包中的spss.get()函数.函数spss.get() ...

  4. 吴裕雄--天生自然 R语言开发学习:使用键盘、带分隔符的文本文件输入数据

    R可从键盘.文本文件.Microsoft Excel和Access.流行的统计软件.特殊格 式的文件.多种关系型数据库管理系统.专业数据库.网站和在线服务中导入数据. 使用键盘了.有两种常见的方式:用 ...

  5. 吴裕雄--天生自然 R语言开发学习:R语言的简单介绍和使用

    假设我们正在研究生理发育问 题,并收集了10名婴儿在出生后一年内的月龄和体重数据(见表1-).我们感兴趣的是体重的分 布及体重和月龄的关系. 可以使用函数c()以向量的形式输入月龄和体重数据,此函 数 ...

  6. 吴裕雄--天生自然 R语言开发学习:基础知识

    1.基础数据结构 1.1 向量 # 创建向量a a <- c(1,2,3) print(a) 1.2 矩阵 #创建矩阵 mymat <- matrix(c(1:10), nrow=2, n ...

  7. 吴裕雄--天生自然 R语言开发学习:图形初阶(续二)

    # ----------------------------------------------------# # R in Action (2nd ed): Chapter 3 # # Gettin ...

  8. 吴裕雄--天生自然 R语言开发学习:图形初阶(续一)

    # ----------------------------------------------------# # R in Action (2nd ed): Chapter 3 # # Gettin ...

  9. 吴裕雄--天生自然 R语言开发学习:图形初阶

    # ----------------------------------------------------# # R in Action (2nd ed): Chapter 3 # # Gettin ...

  10. 吴裕雄--天生自然 R语言开发学习:基本图形(续二)

    #---------------------------------------------------------------# # R in Action (2nd ed): Chapter 6 ...

随机推荐

  1. Java--Json解析

    普通Json {"code":"S0000", "describe":"数据正常返回", "result&qu ...

  2. python集合运算

    用 |,& 代替 并 和交 的运算.+, -代替并和差集.

  3. Qt 使用QGraphicsPixmapItem、QGraphicsScene、QMatrix 的QGraphicsView的显示,缩放

    .h QGraphicsScene *scene; QGraphicsPixmapItem *theFrame; QMatrix matrix; .cpp MainWindow::MainWindow ...

  4. 用命令修改Oracle数据库密码

    1.改密码    (1).打开doc命令框键入:sqlplus /nolog     (2).输入:connect / as sysdba     (3).修改密码:alter user userNa ...

  5. VSFTP服务搭建

    title date tags layout CentOS6.5 Vsftp服务安装与配置 2018-09-04 Centos6.5服务器搭建 post 1.安装vsftp服务 [root@local ...

  6. PyTorch基础——预测共享单车的使用量

    预处理实验数据 读取数据 下载数据 网盘链接:https://pan.baidu.com/s/1n_FtZjAswWR9rfuI6GtDhA 提取码:y4fb #导入需要使用的库 import num ...

  7. 51)PHP,一个数据库操作类的代码

    <?php //类名,也习惯上(推荐)使用跟文件名相似的名字 //定义一个mysql连接类,该类可以连接mysql数据库 //并实现其单例模式 //该类的功能还能够完成如下基本mysql操作: ...

  8. day46-守护线程

    #1.守护线程要注意的坑:下面代码只能打印出子线程开始,无法打印出子线程执行完毕,因为主线程在t.start()以后就结束了, #而子线程要睡眠1秒,所以子线程守护线程随着主线程的结束而结束了. fr ...

  9. Tript协议|伯尔尼公约|著作权|立法宗旨|自动保护|著作权集体管理|

    知识产权 国际条约: Tript协议是国际性公约,<与贸易有关的知识产权协定>(英文:Agreement on Trade-Related Aspects of Intellectual ...

  10. “大屏,您好!” SONIQ声光揭新品“U•F•O”神秘面纱

    作为全球第一批做互联网智能电视的传媒企业,SONIQ声光于4月22日在中国大饭店举行了盛大的新品发布会.其中的重头戏就是当天发布会上作为先锋部队入驻中国电视市场的"UFO".笔者作 ...