R语言的单因素方差分析

# This code installs and loads the "multcomp" package in R.
#? install.packages("multcomp")
# Load the "multcomp" package in R.
library(multcomp)
# Attach the "cholesterol" dataset to the R environment.
attach(cholesterol)
# Create a table of the cholesterol levels by treatment group.
table(trt)
# Create a table of the mean cholesterol levels by treatment group.
aggregate(response,list(trt),FUN=mean)
# Create a table of the standard deviation of cholesterol levels by treatment group.
aggregate(response, by=list(trt), FUN=sd)
# Create a one-way ANOVA model of the cholesterol levels by treatment group.
fit <- aov(response ~ trt)
# Print the summary of the ANOVA model.
summary(fit)
# Load the "gplots" package in R.
library(gplots)
# Create a boxplot of the cholesterol levels by treatment group.
plotmeans(response ~ trt, xlab="Treatment", ylab="Response", main="Mean plot\nwith 95% CI")
# detach the "cholesterol" dataset from the R environment.
detach(cholesterol)
    展开/折叠结果     
> # This code installs and loads the "multcomp" package in R.
> #? install.packages("multcomp")
> # Load the "multcomp" package in R.
> library(multcomp)
载入需要的程序包:mvtnorm
载入需要的程序包:survival
载入需要的程序包:TH.data
载入需要的程序包:MASS

载入程序包:'TH.data'

The following object is masked from 'package:MASS':

    geyser

> # Attach the "cholesterol" dataset to the R environment.
> attach(cholesterol)
> # Create a table of the cholesterol levels by treatment group.
> table(trt)
trt
 1time 2times 4times  drugD  drugE
    10     10     10     10     10
> # Create a table of the mean cholesterol levels by treatment group.
> aggregate(response,list(trt),FUN=mean)
  Group.1        x
1   1time  5.78197
2  2times  9.22497
3  4times 12.37478
4   drugD 15.36117
5   drugE 20.94752
> # Create a table of the standard deviation of cholesterol levels by treatmen$
> aggregate(response, by=list(trt), FUN=sd)
  Group.1        x
1   1time 2.878113
2  2times 3.483054
3  4times 2.923119
4   drugD 3.454636
5   drugE 3.345003
> # Create a one-way ANOVA model of the cholesterol levels by treatment group.
> fit <- aov(response ~ trt)
> # Print the summary of the ANOVA model.
> summary(fit)
            Df Sum Sq Mean Sq F value   Pr(>F)
trt          4 1351.4   337.8   32.43 9.82e-13 ***
Residuals   45  468.8    10.4
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
> # Load the "gplots" package in R.
> library(gplots)

载入程序包:'gplots'

The following object is masked from 'package:stats':

    lowess

> # Create a boxplot of the cholesterol levels by treatment group.
> plotmeans(response ~ trt, xlab="Treatment", ylab="Response", main="Mean plot$
> # detach the "cholesterol" dataset from the R environment.
> detach(cholesterol)

评论

发表回复