options(repos = c(CRAN = "https://cloud.r-project.org"))
# Solo correr una vez si no se encuentran instalados los siguientes paquetes:
install.packages("skimr")
## package 'skimr' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\vanes\AppData\Local\Temp\Rtmpqq8pE4\downloaded_packages
install.packages("car")
## package 'car' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\vanes\AppData\Local\Temp\Rtmpqq8pE4\downloaded_packages
install.packages("agricolae")
## package 'agricolae' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\vanes\AppData\Local\Temp\Rtmpqq8pE4\downloaded_packages
options(repos = c(CRAN = "https://cloud.r-project.org"))
install.packages("car", dependencies = TRUE)
## package 'car' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\vanes\AppData\Local\Temp\Rtmpqq8pE4\downloaded_packages
``` r
library(tidyverse)
library(skimr)
library(summarytools)
library(car)
library(agricolae)
NOVILLOS <- read_csv("E:/Para D/diplo R/modulo 7 TF/novillos.csv")
NOVILLOS
## # A tibble: 45 × 6
## TRATAMIENTO CORRAL ANIMAL_NUM PESO_INICIAL PESO_FINAL GANANCIA
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 P15 1 1 121 447 326
## 2 P15 1 2 210 446 236
## 3 P15 1 3 200 445 245
## 4 P15 1 4 222 446 224
## 5 P15 1 5 209 444 235
## 6 P15 2 6 315 432 117
## 7 P15 2 7 312 435 123
## 8 P15 2 8 311 436 125
## 9 P15 2 9 309 432 123
## 10 P15 2 10 300 431 131
## # ℹ 35 more rows
library(tidyverse)
# Ver las primeras filas
head(NOVILLOS)
## # A tibble: 6 × 6
## TRATAMIENTO CORRAL ANIMAL_NUM PESO_INICIAL PESO_FINAL GANANCIA
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 P15 1 1 121 447 326
## 2 P15 1 2 210 446 236
## 3 P15 1 3 200 445 245
## 4 P15 1 4 222 446 224
## 5 P15 1 5 209 444 235
## 6 P15 2 6 315 432 117
# Estructura compacta (tipo de cada variable)
glimpse(NOVILLOS)
## Rows: 45
## Columns: 6
## $ TRATAMIENTO <chr> "P15", "P15", "P15", "P15", "P15", "P15", "P15", "P15", "…
## $ CORRAL <dbl> 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 1, 1, 1, 1, …
## $ ANIMAL_NUM <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, …
## $ PESO_INICIAL <dbl> 121, 210, 200, 222, 209, 315, 312, 311, 309, 300, 120, 12…
## $ PESO_FINAL <dbl> 447, 446, 445, 446, 444, 432, 435, 436, 432, 431, 417, 41…
## $ GANANCIA <dbl> 326, 236, 245, 224, 235, 117, 123, 125, 123, 131, 297, 28…
# Resumen estadístico de todas las variables numéricas
summary(NOVILLOS)
## TRATAMIENTO CORRAL ANIMAL_NUM PESO_INICIAL PESO_FINAL
## Length:45 Min. :1 Min. : 1 Min. :112.0 Min. :411.0
## Class :character 1st Qu.:1 1st Qu.: 4 1st Qu.:200.0 1st Qu.:417.0
## Mode :character Median :2 Median : 8 Median :221.0 Median :433.0
## Mean :2 Mean : 8 Mean :231.9 Mean :431.4
## 3rd Qu.:3 3rd Qu.:12 3rd Qu.:310.0 3rd Qu.:444.0
## Max. :3 Max. :15 Max. :321.0 Max. :449.0
## GANANCIA
## Min. : 97.0
## 1st Qu.:123.0
## Median :198.0
## Mean :199.5
## 3rd Qu.:245.0
## Max. :335.0
# Nombres de columnas
names(NOVILLOS)
## [1] "TRATAMIENTO" "CORRAL" "ANIMAL_NUM" "PESO_INICIAL" "PESO_FINAL"
## [6] "GANANCIA"
# Conteo de filas y columnas
dim(NOVILLOS)
## [1] 45 6
# Tipo de objeto
class(NOVILLOS)
## [1] "spec_tbl_df" "tbl_df" "tbl" "data.frame"
#Transformación de variables y agregación por corral
# Agrupar los datos por 'CORRAL' y 'TRATAMIENTO' y calcular la ganancia de peso promedio.
NOVILLOS_DIETA <- NOVILLOS %>%
group_by(TRATAMIENTO, CORRAL) %>%
summarise(GANANCIA_PROMEDIO = mean(GANANCIA)) %>%
ungroup()
# Transformar TRATAMIENTO a factor
NOVILLOS_DIETA <- NOVILLOS_DIETA %>%
mutate(TRATAMIENTO = factor(TRATAMIENTO))
NOVILLOS_DIETA
## # A tibble: 9 × 3
## TRATAMIENTO CORRAL GANANCIA_PROMEDIO
## <fct> <dbl> <dbl>
## 1 P15 1 253.
## 2 P15 2 124.
## 3 P15 3 256.
## 4 P25 1 291.
## 5 P25 2 124.
## 6 P25 3 234.
## 7 P35 1 212.
## 8 P35 2 125.
## 9 P35 3 176.
# Usamos la función skim del paquete "skimr"
skim(NOVILLOS_DIETA)
| Name | NOVILLOS_DIETA |
| Number of rows | 9 |
| Number of columns | 3 |
| _______________________ | |
| Column type frequency: | |
| factor | 1 |
| numeric | 2 |
| ________________________ | |
| Group variables | None |
Variable type: factor
| skim_variable | n_missing | complete_rate | ordered | n_unique | top_counts |
|---|---|---|---|---|---|
| TRATAMIENTO | 0 | 1 | FALSE | 3 | P15: 3, P25: 3, P35: 3 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| CORRAL | 0 | 1 | 2.00 | 0.87 | 1.0 | 1.0 | 2.0 | 3.0 | 3.0 | ▇▁▇▁▇ |
| GANANCIA_PROMEDIO | 0 | 1 | 199.47 | 64.50 | 123.8 | 124.8 | 212.2 | 253.2 | 290.6 | ▇▂▂▇▂ |
NOVILLOS_DIETA %>%
group_by(TRATAMIENTO) %>%
summarise(
media = mean(GANANCIA_PROMEDIO),
sd = sd(GANANCIA_PROMEDIO),
cv = sd(GANANCIA_PROMEDIO)/mean(GANANCIA_PROMEDIO)*100 ,
n = n())
## # A tibble: 3 × 5
## TRATAMIENTO media sd cv n
## <fct> <dbl> <dbl> <dbl> <int>
## 1 P15 211. 75.6 35.8 3
## 2 P25 216. 84.6 39.1 3
## 3 P35 171. 43.9 25.7 3
###Visualización de datos
ggplot(NOVILLOS_DIETA, aes(x = TRATAMIENTO, y = GANANCIA_PROMEDIO, fill = TRATAMIENTO)) +
geom_boxplot() +
labs(title = "Ganancia de Peso según Tratamiento",
x = "Tratamientos", y = "ganancia de peso (kg)") +
theme_minimal()
#B. ANOVA
H₀: todas las medias de los tratamientos son iguales. H₁: al menos una media difiere.
bd_ganancia_peso <- NOVILLOS
bd_ganancia_peso
## # A tibble: 45 × 6
## TRATAMIENTO CORRAL ANIMAL_NUM PESO_INICIAL PESO_FINAL GANANCIA
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 P15 1 1 121 447 326
## 2 P15 1 2 210 446 236
## 3 P15 1 3 200 445 245
## 4 P15 1 4 222 446 224
## 5 P15 1 5 209 444 235
## 6 P15 2 6 315 432 117
## 7 P15 2 7 312 435 123
## 8 P15 2 8 311 436 125
## 9 P15 2 9 309 432 123
## 10 P15 2 10 300 431 131
## # ℹ 35 more rows
# Creamos el objeto modelo_dca
modelo_dca <- aov(GANANCIA_PROMEDIO ~ TRATAMIENTO, data = NOVILLOS_DIETA)
summary(modelo_dca)
## Df Sum Sq Mean Sq F value Pr(>F)
## TRATAMIENTO 2 3668 1834 0.372 0.704
## Residuals 6 29614 4936
Residuos y predichos del modelo para realizar las pruebas de los errores.
residuos_dca <- modelo_dca$residuals # residuos = observado - predicho
predichos_dca <- modelo_dca$fitted.values # predichos
residuos_dca
## 1 2 3 4 5 6 7
## 42.066667 -87.333333 45.266667 74.400000 -92.000000 17.600000 41.133333
## 8 9
## -46.266667 5.133333
tabla <- data.frame(
Observado = bd_ganancia_peso$GANANCIA,
Predichos = predichos_dca,
Residual = residuos_dca,
Tratamiento = bd_ganancia_peso$TRATAMIENTO)
print(tabla)
## Observado Predichos Residual Tratamiento
## 1 326 211.1333 42.066667 P15
## 2 236 211.1333 -87.333333 P15
## 3 245 211.1333 45.266667 P15
## 4 224 216.2000 74.400000 P15
## 5 235 216.2000 -92.000000 P15
## 6 117 216.2000 17.600000 P15
## 7 123 171.0667 41.133333 P15
## 8 125 171.0667 -46.266667 P15
## 9 123 171.0667 5.133333 P15
## 10 131 211.1333 42.066667 P15
## 11 297 211.1333 -87.333333 P15
## 12 288 211.1333 45.266667 P15
## 13 196 216.2000 74.400000 P15
## 14 298 216.2000 -92.000000 P15
## 15 203 216.2000 17.600000 P15
## 16 334 171.0667 41.133333 P25
## 17 329 171.0667 -46.266667 P25
## 18 335 171.0667 5.133333 P25
## 19 223 211.1333 42.066667 P25
## 20 232 211.1333 -87.333333 P25
## 21 123 211.1333 45.266667 P25
## 22 119 216.2000 74.400000 P25
## 23 129 216.2000 -92.000000 P25
## 24 127 216.2000 17.600000 P25
## 25 123 171.0667 41.133333 P25
## 26 190 171.0667 -46.266667 P25
## 27 298 171.0667 5.133333 P25
## 28 295 211.1333 42.066667 P25
## 29 289 211.1333 -87.333333 P25
## 30 97 211.1333 45.266667 P25
## 31 237 216.2000 74.400000 P35
## 32 231 216.2000 -92.000000 P35
## 33 130 216.2000 17.600000 P35
## 34 236 171.0667 41.133333 P35
## 35 227 171.0667 -46.266667 P35
## 36 118 171.0667 5.133333 P35
## 37 126 211.1333 42.066667 P35
## 38 123 211.1333 -87.333333 P35
## 39 122 211.1333 45.266667 P35
## 40 135 216.2000 74.400000 P35
## 41 193 216.2000 -92.000000 P35
## 42 98 216.2000 17.600000 P35
## 43 198 171.0667 41.133333 P35
## 44 99 171.0667 -46.266667 P35
## 45 293 171.0667 5.133333 P35
hist(residuos_dca, main = "Histograma de residuos")
boxplot(residuos_dca, main = "Boxplot de residuos")
plot(modelo_dca, which = 2)
H0: SI se cumple el supuesto de Normalidad.
H1: NO se cumple el supuesto de Normalidad.
shapiro.test(modelo_dca$residuals)
##
## Shapiro-Wilk normality test
##
## data: modelo_dca$residuals
## W = 0.88492, p-value = 0.1768
plot(modelo_dca, which = 1) # Residos vs predichos
H0: SI cumple el supuesto de Homocedasticidad.
H1: NO se cumple el supuesto de Homocedasticidad
search()
## [1] ".GlobalEnv" "package:agricolae" "package:car"
## [4] "package:carData" "package:summarytools" "package:skimr"
## [7] "package:lubridate" "package:forcats" "package:stringr"
## [10] "package:dplyr" "package:purrr" "package:readr"
## [13] "package:tidyr" "package:tibble" "package:ggplot2"
## [16] "package:tidyverse" "package:stats" "package:graphics"
## [19] "package:grDevices" "package:utils" "package:datasets"
## [22] "package:methods" "Autoloads" "package:base"
leveneTest(GANANCIA ~ TRATAMIENTO, data = bd_ganancia_peso)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 1.6502 0.2042
## 42
# Usaremos una función del paquete agricolae
TUKEY <- HSD.test(modelo_dca, "TRATAMIENTO")
TUKEY
## $statistics
## MSerror Df Mean CV MSD
## 4935.636 6 199.4667 35.22096 176.003
##
## $parameters
## test name.t ntr StudentizedRange alpha
## Tukey TRATAMIENTO 3 4.339195 0.05
##
## $means
## GANANCIA_PROMEDIO std r se Min Max Q25 Q50 Q75
## P15 211.1333 75.64981 3 40.56121 123.8 256.4 188.5 253.2 254.8
## P25 216.2000 84.58463 3 40.56121 124.2 290.6 179.0 233.8 262.2
## P35 171.0667 43.92554 3 40.56121 124.8 212.2 150.5 176.2 194.2
##
## $comparison
## NULL
##
## $groups
## GANANCIA_PROMEDIO groups
## P25 216.2000 a
## P15 211.1333 a
## P35 171.0667 a
##
## attr(,"class")
## [1] "group"
plot(TUKEY)
# Usamos librería agricolae
FISHER <- LSD.test(modelo_dca, "TRATAMIENTO")
FISHER
## $statistics
## MSerror Df Mean CV t.value LSD
## 4935.636 6 199.4667 35.22096 2.446912 140.3603
##
## $parameters
## test p.ajusted name.t ntr alpha
## Fisher-LSD none TRATAMIENTO 3 0.05
##
## $means
## GANANCIA_PROMEDIO std r se LCL UCL Min Max Q25
## P15 211.1333 75.64981 3 40.56121 111.88363 310.3830 123.8 256.4 188.5
## P25 216.2000 84.58463 3 40.56121 116.95029 315.4497 124.2 290.6 179.0
## P35 171.0667 43.92554 3 40.56121 71.81696 270.3164 124.8 212.2 150.5
## Q50 Q75
## P15 253.2 254.8
## P25 233.8 262.2
## P35 176.2 194.2
##
## $comparison
## NULL
##
## $groups
## GANANCIA_PROMEDIO groups
## P25 216.2000 a
## P15 211.1333 a
## P35 171.0667 a
##
## attr(,"class")
## [1] "group"
plot(FISHER)
### Tanto con el Test de Tuckey como Fisher muestran que no hay
diferencias significativas para las medias de tratamiento.
Los tratamientos con diferentes porcentajes de inclusión en la dieta de subproductos fibrosos no tuvieron efecto en la variable ganancia de peso en novillos. Si bien los valores medios muestran que P15 y p25 tienen medias superiores, existe una alta variabilidad dentro de los tratamientos, esto se observa en ANOVA donde el error residual es grande, muy superior al de los tratamientos, tambien con altos DS y CV. Además se considera que el tamaño de muestra puede ser insuficiente, con pocas repeticiones (3 corrales) que podrían reducir la potencia estadística y el test no detecta diferencias aun cuando podrían existir.