ANOVA y Tukey

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

ANOVA y Tukey

This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath
the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it
and pressing Ctrl+Shift+Enter.
Cargar paquetes

library(ggplot2)
library(dplyr)

##
## Attaching package: ’dplyr’

## The following objects are masked from ’package:stats’:


##
## filter, lag

## The following objects are masked from ’package:base’:


##
## intersect, setdiff, setequal, union

library(agricolae)
library(car)

## Loading required package: carData

##
## Attaching package: ’car’

## The following object is masked from ’package:dplyr’:


##
## recode

library(FSA)

## ## FSA v0.8.32. See citation(’FSA’) if used in publication.


## ## Run fishR() for related website and fishR(’IFAR’) for related book.

##
## Attaching package: ’FSA’

1
## The following object is masked from ’package:car’:
##
## bootCase

Cargar la base de datos

data(PlantGrowth)

PlantGrowth

## weight group
## 1 4.17 ctrl
## 2 5.58 ctrl
## 3 5.18 ctrl
## 4 6.11 ctrl
## 5 4.50 ctrl
## 6 4.61 ctrl
## 7 5.17 ctrl
## 8 4.53 ctrl
## 9 5.33 ctrl
## 10 5.14 ctrl
## 11 4.81 trt1
## 12 4.17 trt1
## 13 4.41 trt1
## 14 3.59 trt1
## 15 5.87 trt1
## 16 3.83 trt1
## 17 6.03 trt1
## 18 4.89 trt1
## 19 4.32 trt1
## 20 4.69 trt1
## 21 6.31 trt2
## 22 5.12 trt2
## 23 5.54 trt2
## 24 5.50 trt2
## 25 5.37 trt2
## 26 5.29 trt2
## 27 4.92 trt2
## 28 6.15 trt2
## 29 5.80 trt2
## 30 5.26 trt2

Prueba de Normalidad

hist(PlantGrowth$weight)

2
Histogram of PlantGrowth$weight
8
6
Frequency

4
2
0

3.5 4.0 4.5 5.0 5.5 6.0 6.5

PlantGrowth$weight

shapiro.test(PlantGrowth$weight)

##
## Shapiro-Wilk normality test
##
## data: PlantGrowth$weight
## W = 0.98268, p-value = 0.8915

qqPlot(PlantGrowth$weight)

3
6.0
PlantGrowth$weight

5.5
5.0
4.5
4.0

16
14
3.5

−2 −1 0 1 2

norm quantiles

## [1] 14 16

Prueba de homocedasticidad, se pueden utilizar la prueba de Levene y la prueba de Bartlett para conocer si
nuestros datos presentan homocedasticidad.
Si nuestros datos presentan normalidad lo mas recomendable es utilizar la prueba de Bartlett.
Test de Bartlett

bartlett.test(weight ~ group, PlantGrowth)

##
## Bartlett test of homogeneity of variances
##
## data: weight by group
## Bartlett’s K-squared = 2.8786, df = 2, p-value = 0.2371

Test de Levene

leveneTest(y=PlantGrowth$weight, group = PlantGrowth$group, center = "mean")

## Levene’s Test for Homogeneity of Variance (center = "mean")


## Df F value Pr(>F)
## group 2 1.237 0.3062
## 27

4
Prueba de ANOVA, realizamos la prueba de anova para conocer si exiten diferencias significativas del peso
de las plantas de acuerdo al tratamiento.

planta_modelo<-aov(weight~group,PlantGrowth)

summary(planta_modelo)

## Df Sum Sq Mean Sq F value Pr(>F)


## group 2 3.766 1.8832 4.846 0.0159 *
## Residuals 27 10.492 0.3886
## ---
## Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1

Prueba de Tukey, realizamos la prueba post hoc para conocer entre cuales grupos hay diferencias significativas

TukeyHSD(planta_modelo,"group")

## Tukey multiple comparisons of means


## 95% family-wise confidence level
##
## Fit: aov(formula = weight ~ group, data = PlantGrowth)
##
## $group
## diff lwr upr p adj
## trt1-ctrl -0.371 -1.0622161 0.3202161 0.3908711
## trt2-ctrl 0.494 -0.1972161 1.1852161 0.1979960
## trt2-trt1 0.865 0.1737839 1.5562161 0.0120064

Prueba de Duncan, realizamos la prueba post hoc (lternativa a la prueba de Tukey) para conocer entre cuales
grupos hay diferencias significativas

Plantas_ducan<-duncan.test(planta_modelo,"group",alpha=0.01,console=TRUE)

##
## Study: planta_modelo ~ "group"
##
## Duncan’s new multiple range test
## for weight
##
## Mean Square Error: 0.3885959
##
## group, means
##
## weight std r Min Max
## ctrl 5.032 0.5830914 10 4.17 6.11
## trt1 4.661 0.7936757 10 3.59 6.03
## trt2 5.526 0.4425733 10 4.92 6.31
##
## Alpha: 0.01 ; DF Error: 27
##
## Critical Range
## 2 3

5
## 0.7724154 0.8055987
##
## Means with the same letter are not significantly different.
##
## weight groups
## trt2 5.526 a
## ctrl 5.032 ab
## trt1 4.661 b

plot(Plantas_ducan)

Groups and Range


7

a
ab b
6
5
4
3

trt2 ctrl trt1

Realizamos un grafico ilustrando las diferencias registradas.


Preparamos los datos con dplyr

resumen_data <- PlantGrowth %>%


group_by(group)%>%
summarise(pesoMedio = mean(weight),
sd = sd(weight),
es = sd/sqrt(n()))

Preparamos los datos con FSA

TablaRes <- Summarize(weight ~ group, data= PlantGrowth)

TablaRes$ES <- TablaRes$sd / sqrt(TablaRes$n)

6
Realizamos el grafico con ggplot

ggplot(resumen_data, aes(x=group, y=pesoMedio)) +


geom_bar(stat="identity", width=.5) +
labs(title="diferencia por grupo",
subtitle="Sub titulo",
caption="source: mpg") +
theme(axis.text.x = element_text(angle=0, vjust=0.6))+
ylab("peso")+
geom_errorbar(aes(ymin = pesoMedio - es,
ymax = pesoMedio + es),
width = 0.5,
size = 0.5)

diferencia por grupo


Sub titulo

4
peso

ctrl trt1 trt2


group
source: mpg

Grafico con colores y leyenda

ggplot(resumen_data, aes(x=group, y=pesoMedio)) +


geom_bar(stat="identity", width=.5, aes(fill= group)) +
labs(title="diferencia por grupo",
subtitle="Sub titulo",
caption="source: mpg") +
theme(axis.text.x = element_text(angle=0, vjust=0.6))+
ylab("peso")+
geom_errorbar(aes(ymin = pesoMedio - es,
ymax = pesoMedio + es,
color= group),

7
width = 0.5,
size = 0.5)

diferencia por grupo


Sub titulo

4
group
ctrl
peso

trt1
trt2
2

ctrl trt1 trt2


group
source: mpg

ggplot(resumen_data, aes(x=group, y=pesoMedio)) +


geom_bar(fill= "yellow", stat="identity", width=.5) +
labs(title="diferencia por grupo",
subtitle="Sub titulo",
caption="source: mpg") +
theme(axis.text.x = element_text(angle=0, vjust=0.6))+
ylab("peso")+
geom_errorbar(aes(ymin = pesoMedio - es,
ymax = pesoMedio + es),
width = 0.5,
size = 0.5,
color= "red")

8
diferencia por grupo
Sub titulo

4
peso

ctrl trt1 trt2


group
source: mpg

You might also like