abril 25, 2017

ANOVA de medidas repetidas para animales y forestales


Los ANOVAs de medidas repetidas con animales o forestales emplean análisis más avanzados. Este y este otro sitio ofrecen discusiones. 


# Gráfico de líneas para cada cerdo
with(enrofloxacin, interaction.plot(position, pig, conc, legend = T, 
                                    lty = rep(1:12), col = rep(1:12), 
                                    leg.bty = "o", lwd = 2))


El primer sitio aporta ejemplos en R. Los códigos siguientes, los ajustamos del eNotes-11 para resolver el ejemplo “13.7 Repeated measures”: tratamientos de enrofloxacina en cerdos. El manual del segundo sitio ofrece códigos en SAS para forestales.


## Course  : 02429 Analysis of correlated data: Mixed Linear Models
## Teacher : Per Bruun Brockhoff 
## DTU Compute - Section for Statistics and Data Analysis 
## Technical University of Denmark (DTU); E-mail:perbb@dtu.dk
## Repeated measures, part 1, simple methods
## http://02429.compute.dtu.dk/enote/afsnit/NUID191/
## 13.7 Repeated measures
## Enrofloxacin treatmentents of pigs
## http://02429.compute.dtu.dk/enote/afsnit/NUID193/

# Importando datos
enrofloxacin <- read.table("C:/Users/Administrator/Desktop/enrofloxacin.txt", header = TRUE, sep = ",", dec=".")
attach(enrofloxacin)

# Factores para variables independientes del ANOVA
position  <- factor(position)
treatment <- factor(treatment)
pig       <- factor(pig)
enrofloxacin
##    treatment pig position conc
## 1         PO   1       e1  0.4
## 2         PO   2       e1  1.3
## 3         PO   3       e1  0.8
## 4         PO   4       e1  4.2
## 5         PO   5       e1  1.4
## 6         PO   6       e1  1.4
## 7         IM   7       e1  2.3
## 8         IM   8       e1  1.7
## 9         IM   9       e1  2.2
## 10        IM  10       e1  1.8
## 11        IM  11       e1  1.7
## 12        IM  12       e1  1.7
## 13        PO   1       e2  0.3
## 14        PO   2       e2  2.1
## 15        PO   3       e2  0.8
## 16        PO   4       e2  4.4
## 17        PO   5       e2  2.5
## 18        PO   6       e2  0.8
## 19        IM   7       e2  2.9
## 20        IM   8       e2  2.1
## 21        IM   9       e2  2.1
## 22        IM  10       e2  1.9
## 23        IM  11       e2  1.6
## 24        IM  12       e2  1.8
## 25        PO   1       e3  0.2
## 26        PO   2       e3  0.9
## 27        PO   3       e3  0.6
## 28        PO   4       e3  1.4
## 29        PO   5       e3  1.0
## 30        PO   6       e3  0.7
## 31        IM   7       e3  1.7
## 32        IM   8       e3  2.0
## 33        IM   9       e3  2.0
## 34        IM  10       e3  1.5
## 35        IM  11       e3  1.5
## 36        IM  12       e3  1.5
## 37        PO   1       e4  0.1
## 38        PO   2       e4  0.8
## 39        PO   3       e4  0.4
## 40        PO   4       e4  1.1
## 41        PO   5       e4  0.8
## 42        PO   6       e4  0.6
## 43        IM   7       e4  1.5
## 44        IM   8       e4  1.3
## 45        IM   9       e4  1.2
## 46        IM  10       e4  1.2
## 47        IM  11       e4  1.2
## 48        IM  12       e4  1.0
## 49        PO   1       e5  0.2
## 50        PO   2       e5  0.7
## 51        PO   3       e5  0.5
## 52        PO   4       e5  1.1
## 53        PO   5       e5  0.6
## 54        PO   6       e5  0.5
## 55        IM   7       e5  1.2
## 56        IM   8       e5  1.2
## 57        IM   9       e5  1.3
## 58        IM  10       e5  1.0
## 59        IM  11       e5  0.9
## 60        IM  12       e5  1.0
# Gráfico de líneas para cada cerdo
with(enrofloxacin, interaction.plot(position, pig, conc, legend = T, 
                                    lty = rep(1:12), col = rep(1:12), 
                                    leg.bty = "o", lwd = 2))

# Gráfico de líneas empleando ggplot2
library(ggplot2)
enrofloxacin$positionQ <- as.numeric(enrofloxacin$position)
p <- qplot(positionQ, conc, data = enrofloxacin)
p <- p + geom_line(aes(x=positionQ, y = conc, group = pig, colour = treatment));print(p)
# Gráfico de líneas agrupadas por medias
library(doBy)
## Warning: package 'doBy' was built under R version 3.3.2
mns <- summaryBy(conc ~ position + treatment, id=~positionQ, keep.names=TRUE, data=enrofloxacin)
p   <- qplot(positionQ, conc, data = mns)
p   <- p + geom_line(aes(x=positionQ, y=conc, group=treatment, colour=treatment));print(p)
head(enrofloxacin)
##   treatment pig position conc positionQ
## 1        PO   1       e1  0.4         1
## 2        PO   2       e1  1.3         1
## 3        PO   3       e1  0.8         1
## 4        PO   4       e1  4.2         1
## 5        PO   5       e1  1.4         1
## 6        PO   6       e1  1.4         1
tail(enrofloxacin)
##    treatment pig position conc positionQ
## 55        IM   7       e5  1.2         5
## 56        IM   8       e5  1.2         5
## 57        IM   9       e5  1.3         5
## 58        IM  10       e5  1.0         5
## 59        IM  11       e5  0.9         5
## 60        IM  12       e5  1.0         5
library(plyr)
lmbyposition <- lmBy(conc~treatment|position, data=enrofloxacin)
ldaov <- ldply(lmbyposition, anova) # presenta data frame de resultados
ldaov[(1:10)*2-1,5:6]
##         F value       Pr(>F)
## 1     0.3204332 0.5838261303
## 3     0.1487898 0.7077732524
## 5    21.6964286 0.0008971212
## 7    14.4642857 0.0034676215
## 9    13.3928571 0.0043918058
# ANOVA
sumMeasure <- summaryBy(exp(conc) ~ pig, id=~treatment, 
                        keep.names=TRUE, FUN = sum, data=enrofloxacin)
sumMeasure$conc <- log(sumMeasure$"exp(conc)")
anova(lm(conc ~ treatment, data = sumMeasure))
## Analysis of Variance Table
## 
## Response: conc
##           Df Sum Sq Mean Sq F value Pr(>F)
## treatment  1 0.3568 0.35681  0.5251 0.4853
## Residuals 10 6.7948 0.67948
library(lmerTest)
modellmer1 <- lmer(conc ~ position + treatment + position:treatment  
                   +(1 | pig), data = enrofloxacin); anova(modellmer1)
## Analysis of Variance Table of type III  with  Satterthwaite 
## approximation for degrees of freedom
##                     Sum Sq Mean Sq NumDF DenDF F.value    Pr(>F)    
## position           11.2423 2.81058     4    40 12.7377 8.945e-07 ***
## treatment           0.5594 0.55941     1    10  2.5353    0.1424    
## position:treatment  0.7957 0.19892     4    40  0.9015    0.4722    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
VarCorr(modellmer1)
##  Groups   Name        Std.Dev.
##  pig      (Intercept) 0.51738 
##  Residual             0.46973
-2*logLik(modellmer1)
## 'log Lik.' 103.8052 (df=12)
# ANOVA con nlme y la funcion lme
library(nlme)
model1 <- lme(conc ~ position + treatment + position:treatment, 
              random = ~1 | pig, data = enrofloxacin); anova(model1)
##                    numDF denDF  F-value p-value
## (Intercept)            1    40 69.44710  <.0001
## position               4    40 12.73774  <.0001
## treatment              1    10  2.53528  0.1424
## position:treatment     4    40  0.90150  0.4722
VarCorr(model1)
## pig = pdLogChol(1) 
##             Variance  StdDev   
## (Intercept) 0.2676833 0.5173812
## Residual    0.2206500 0.4697340
-2*logLik(model1)
## 'log Lik.' 103.8052 (df=12)
intervals(model1, which = "var-cov")
## Approximate 95% confidence intervals
## 
##  Random Effects:
##   Level: pig 
##                     lower      est.     upper
## sd((Intercept)) 0.3101369 0.5173812 0.8631134
## 
##  Within-group standard error:
##     lower      est.     upper 
## 0.3772990 0.4697340 0.5848146
ins <- intervals(model1, which = "var-cov")
lins <- log(ins$sigma)
lins[2]-lins[1]
##      est. 
## 0.2191284
lins[3]-lins[2]
##     upper 
## 0.2191284


Datos y códigos adjuntos:
cerdos ANOVA medidas repetidas.renrofloxacin.txt



No hay comentarios:

Publicar un comentario