# DCA con diferente numero de repeticiones
# Ejemplo:5)    Los datos corresponden al tiempo de coagulaci昼㸳n (en segundos) de sangre extra攼㹤da a 24 animales, 
#asignados aleatoriamente a cuatro dietas diferentes.

#Dieta          
#A  B   C   D
#62 63  68  56
#60 67  66  62
#63 71  71  60
#59 64  67  61
#   65  68  63 
#   66  68  64 
#           63
#           59
#concen = tiempo
tiempo<-c(62,60,63,59,63,67,71,64,65,66,68,66,71,67
          ,68,68,56,62,60,61,63,64,63,59) 

trat<-c(rep(1,4),rep(2,6),rep(3,6),rep(4,8))
trat
##  [1] 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 4 4
# creando un data.frame
datos<-data.frame(trat=factor(trat,labels = c("DIETA A","DIETA B","DIETA C","DIETA D")),tiempo)
datos
##       trat tiempo
## 1  DIETA A     62
## 2  DIETA A     60
## 3  DIETA A     63
## 4  DIETA A     59
## 5  DIETA B     63
## 6  DIETA B     67
## 7  DIETA B     71
## 8  DIETA B     64
## 9  DIETA B     65
## 10 DIETA B     66
## 11 DIETA C     68
## 12 DIETA C     66
## 13 DIETA C     71
## 14 DIETA C     67
## 15 DIETA C     68
## 16 DIETA C     68
## 17 DIETA D     56
## 18 DIETA D     62
## 19 DIETA D     60
## 20 DIETA D     61
## 21 DIETA D     63
## 22 DIETA D     64
## 23 DIETA D     63
## 24 DIETA D     59
# Diagrama de cajas
plot(datos,col=terrain.colors(4))

# cuadro ANVA
resp<-aov(tiempo~trat,data=datos)
anova(resp)
## Analysis of Variance Table
## 
## Response: tiempo
##           Df Sum Sq Mean Sq F value    Pr(>F)    
## trat       3    228    76.0  13.571 4.658e-05 ***
## Residuals 20    112     5.6                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# residuales e IC
resp<-lm(tiempo~trat-1,data=datos)
summary(resp)
## 
## Call:
## lm(formula = tiempo ~ trat - 1, data = datos)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
##  -5.00  -1.25   0.00   1.25   5.00 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## tratDIETA A  61.0000     1.1832   51.55   <2e-16 ***
## tratDIETA B  66.0000     0.9661   68.32   <2e-16 ***
## tratDIETA C  68.0000     0.9661   70.39   <2e-16 ***
## tratDIETA D  61.0000     0.8367   72.91   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.366 on 20 degrees of freedom
## Multiple R-squared:  0.9989, Adjusted R-squared:  0.9986 
## F-statistic:  4399 on 4 and 20 DF,  p-value: < 2.2e-16
# intervalos de confianza
confint(resp,level=0.99)
##                0.5 %   99.5 %
## tratDIETA A 57.63335 64.36665
## tratDIETA B 63.25114 68.74886
## tratDIETA C 65.25114 70.74886
## tratDIETA D 58.61942 63.38058
# histograma de frecuencias
h<-hist(resp$residuals, col=terrain.colors(10))
xfit <- seq(min(resp$residuals), max(resp$residuals),length = 20)
yfit <- dnorm(xfit,mean=mean(resp$residuals),sd=sd(resp$residuals))
yfit <- yfit*diff(h$mids[1:2])*length(resp$residuals)
lines(xfit, yfit, col = "blue", lwd = 2)

# graficos de probabilidad normal
qqnorm(resp$residuals)
qqline(resp$residuals,col="blue", lwd=2)

# test de normalidad
shapiro.test(resp$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  resp$residuals
## W = 0.97831, p-value = 0.8629
# grafico de residuales
plot(resp$fitted.values,resp$residuals,main="Estimado vs
 Residual")
abline(h=0,lty=2)

#
plot(resp$residuals~trat,main="Tratamiento vs Residual")

#test de homogeneidad de varianzas
bartlett.test(tiempo~trat)
## 
##  Bartlett test of homogeneity of variances
## 
## data:  tiempo by trat
## Bartlett's K-squared = 1.668, df = 3, p-value = 0.6441
# independencia
plot(resp$residuals,xlab="Orden de corrida")
abline(h=0,lty=2)
# test de Durwin Watson
library(zoo)
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric

library(lmtest)
dwtest(resp)
## 
##  Durbin-Watson test
## 
## data:  resp
## DW = 2.2054, p-value = 0.4505
## alternative hypothesis: true autocorrelation is greater than 0
# comparaciones multiples
library(agricolae)
# LSDL
LSD.test(resp,"trat",alpha=0.05,console=TRUE)
## 
## Study: resp ~ "trat"
## 
## LSD t Test for tiempo 
## 
## Mean Square Error:  5.6 
## 
## trat,  means and individual ( 95 %) CI
## 
##         tiempo      std r      LCL      UCL Min Max
## DIETA A     61 1.825742 4 58.53185 63.46815  59  63
## DIETA B     66 2.828427 6 63.98477 68.01523  63  71
## DIETA C     68 1.673320 6 65.98477 70.01523  66  71
## DIETA D     61 2.618615 8 59.25476 62.74524  56  64
## 
## Alpha: 0.05 ; DF Error: 20
## Critical Value of t: 2.085963 
## 
## Groups according to probability of means differences and alpha level( 0.05 )
## 
## Treatments with the same letter are not significantly different.
## 
##         tiempo groups
## DIETA C     68      a
## DIETA B     66      a
## DIETA A     61      b
## DIETA D     61      b
# duncan test
duncan.test(resp,"trat",alpha=0.05,console=TRUE)
## 
## Study: resp ~ "trat"
## 
## Duncan's new multiple range test
## for tiempo 
## 
## Mean Square Error:  5.6 
## 
## trat,  means
## 
##         tiempo      std r Min Max
## DIETA A     61 1.825742 4  59  63
## DIETA B     66 2.828427 6  63  71
## DIETA C     68 1.673320 6  66  71
## DIETA D     61 2.618615 8  56  64
## 
## Groups according to probability of means differences and alpha level( 0.05 )
## 
## Means with the same letter are not significantly different.
## 
##         tiempo groups
## DIETA C     68      a
## DIETA B     66      a
## DIETA A     61      b
## DIETA D     61      b
# tukey test
cmT <- HSD.test(resp, "trat", console=TRUE)
## 
## Study: resp ~ "trat"
## 
## HSD Test for tiempo 
## 
## Mean Square Error:  5.6 
## 
## trat,  means
## 
##         tiempo      std r Min Max
## DIETA A     61 1.825742 4  59  63
## DIETA B     66 2.828427 6  63  71
## DIETA C     68 1.673320 6  66  71
## DIETA D     61 2.618615 8  56  64
## 
## Alpha: 0.05 ; DF Error: 20 
## Critical Value of Studentized Range: 3.958293 
## 
## Groups according to probability of means differences and alpha level( 0.05 )
## 
## Treatments with the same letter are not significantly different.
## 
##         tiempo groups
## DIETA C     68      a
## DIETA B     66      a
## DIETA A     61      b
## DIETA D     61      b