#install.packages("readxl")
#install.packages("skimr")
#install.packages("car")
#install.packages("agricolae")
#install.packages("emmeans")
library(readxl)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.2 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.1.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(skimr)
library(summarytools)
## Warning in fun(libname, pkgname): couldn't connect to display ":0"
##
## Attaching package: 'summarytools'
##
## The following object is masked from 'package:tibble':
##
## view
library(car)
## Loading required package: carData
##
## Attaching package: 'car'
##
## The following object is masked from 'package:dplyr':
##
## recode
##
## The following object is masked from 'package:purrr':
##
## some
library(agricolae)
library(emmeans)
## Welcome to emmeans.
## Caution: You lose important information if you filter this package's results.
## See '? untidy'
NOVILLOS<- read_csv("novillos.csv")
## Rows: 45 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): TRATAMIENTO
## dbl (5): CORRAL, ANIMAL_NUM, PESO_INICIAL, PESO_FINAL, GANANCIA
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
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
str(NOVILLOS)
## spc_tbl_ [45 × 6] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ TRATAMIENTO : chr [1:45] "P15" "P15" "P15" "P15" ...
## $ CORRAL : num [1:45] 1 1 1 1 1 2 2 2 2 2 ...
## $ ANIMAL_NUM : num [1:45] 1 2 3 4 5 6 7 8 9 10 ...
## $ PESO_INICIAL: num [1:45] 121 210 200 222 209 315 312 311 309 300 ...
## $ PESO_FINAL : num [1:45] 447 446 445 446 444 432 435 436 432 431 ...
## $ GANANCIA : num [1:45] 326 236 245 224 235 117 123 125 123 131 ...
## - attr(*, "spec")=
## .. cols(
## .. TRATAMIENTO = col_character(),
## .. CORRAL = col_double(),
## .. ANIMAL_NUM = col_double(),
## .. PESO_INICIAL = col_double(),
## .. PESO_FINAL = col_double(),
## .. GANANCIA = col_double()
## .. )
## - attr(*, "problems")=<externalptr>
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…
skim(NOVILLOS)
| Name | NOVILLOS |
| Number of rows | 45 |
| Number of columns | 6 |
| _______________________ | |
| Column type frequency: | |
| character | 1 |
| numeric | 5 |
| ________________________ | |
| Group variables | None |
Variable type: character
| skim_variable | n_missing | complete_rate | min | max | empty | n_unique | whitespace |
|---|---|---|---|---|---|---|---|
| TRATAMIENTO | 0 | 1 | 3 | 3 | 0 | 3 | 0 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| CORRAL | 0 | 1 | 2.00 | 0.83 | 1 | 1 | 2 | 3 | 3 | ▇▁▇▁▇ |
| ANIMAL_NUM | 0 | 1 | 8.00 | 4.37 | 1 | 4 | 8 | 12 | 15 | ▇▇▇▇▇ |
| PESO_INICIAL | 0 | 1 | 231.89 | 78.12 | 112 | 200 | 221 | 310 | 321 | ▅▁▆▁▇ |
| PESO_FINAL | 0 | 1 | 431.36 | 12.95 | 411 | 417 | 433 | 444 | 449 | ▇▁▅▃▇ |
| GANANCIA | 0 | 1 | 199.47 | 77.51 | 97 | 123 | 198 | 245 | 335 | ▇▁▆▁▅ |
NOVILLOS %>%
group_by((TRATAMIENTO)) %>%
descr(GANANCIA)
## Descriptive Statistics
## GANANCIA by (TRATAMIENTO)
## Data Frame: NOVILLOS
## N: 45
##
## P15 P25 P35
## ----------------- -------- -------- --------
## Mean 211.13 216.20 171.07
## Std.Dev 73.22 91.22 62.36
## Min 117.00 97.00 98.00
## Q1 125.00 123.00 122.00
## Median 224.00 223.00 135.00
## Q3 288.00 298.00 231.00
## Max 326.00 335.00 293.00
## MAD 108.23 142.33 54.86
## IQR 138.50 171.50 106.50
## CV 0.35 0.42 0.36
## Skewness -0.01 0.05 0.40
## SE.Skewness 0.58 0.58 0.58
## Kurtosis -1.57 -1.82 -1.39
## N.Valid 15.00 15.00 15.00
## N 15.00 15.00 15.00
## Pct.Valid 100.00 100.00 100.00
ggplot(NOVILLOS, aes(x = TRATAMIENTO, y = GANANCIA, fill = TRATAMIENTO)) +
geom_boxplot() +
labs(title = "Peso de los animales según tratamiento",
x = "Tratamientos", y = "Peso (Kg)") +
theme_minimal()
#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()
## `summarise()` has grouped output by 'TRATAMIENTO'. You can override using the
## `.groups` argument.
# Transformar TRATAMIENTO a factor
NOVILLOS_DIETA <- NOVILLOS_DIETA %>%
mutate(TRATAMIENTO = factor(TRATAMIENTO))
NOVILLOS_DIETA %>%
group_by((TRATAMIENTO)) %>%
descr(GANANCIA_PROMEDIO)
## Descriptive Statistics
## GANANCIA_PROMEDIO by (TRATAMIENTO)
## Data Frame: NOVILLOS_DIETA
## N: 9
##
## P15 P25 P35
## ----------------- -------- -------- --------
## Mean 211.13 216.20 171.07
## Std.Dev 75.65 84.58 43.93
## Min 123.80 124.20 124.80
## Q1 123.80 124.20 124.80
## Median 253.20 233.80 176.20
## Q3 256.40 290.60 212.20
## Max 256.40 290.60 212.20
## MAD 4.74 84.21 53.37
## IQR 66.30 83.20 43.70
## CV 0.36 0.39 0.26
## Skewness -0.38 -0.20 -0.12
## SE.Skewness 1.22 1.22 1.22
## Kurtosis -2.33 -2.33 -2.33
## N.Valid 3.00 3.00 3.00
## N 3.00 3.00 3.00
## Pct.Valid 100.00 100.00 100.00
anova_DCA <- aov(GANANCIA_PROMEDIO ~ TRATAMIENTO, data = NOVILLOS_DIETA)
summary(anova_DCA)
## Df Sum Sq Mean Sq F value Pr(>F)
## TRATAMIENTO 2 3668 1834 0.372 0.704
## Residuals 6 29614 4936
residuos_DCA <- anova_DCA$residuals
predichos_DCA <- anova_DCA$fitted.values
tabla <- data.frame(
Observado = NOVILLOS_DIETA$GANANCIA_PROMEDIO,
Predichos = predichos_DCA,
Residual = residuos_DCA,
Tratamiento = NOVILLOS_DIETA$TRATAMIENTO)
print(tabla)
## Observado Predichos Residual Tratamiento
## 1 253.2 211.1333 42.066667 P15
## 2 123.8 211.1333 -87.333333 P15
## 3 256.4 211.1333 45.266667 P15
## 4 290.6 216.2000 74.400000 P25
## 5 124.2 216.2000 -92.000000 P25
## 6 233.8 216.2000 17.600000 P25
## 7 212.2 171.0667 41.133333 P35
## 8 124.8 171.0667 -46.266667 P35
## 9 176.2 171.0667 5.133333 P35
hist(residuos_DCA, main = "Histograma de residuos")
boxplot(residuos_DCA, main = "Boxplot de residuos")
plot(anova_DCA, which = 2)
plot(anova_DCA, which = 1)
shapiro.test(anova_DCA$residuals)
##
## Shapiro-Wilk normality test
##
## data: anova_DCA$residuals
## W = 0.88492, p-value = 0.1768
leveneTest(GANANCIA_PROMEDIO ~ TRATAMIENTO, data = NOVILLOS_DIETA)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 0.1718 0.8462
## 6
TUKEY <- HSD.test(anova_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)
FISHER <- LSD.test(anova_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)
Poroto<- read_xlsx("poroto.xlsx", sheet = "Hoja2")
Poroto
## # A tibble: 24 × 4
## Variedad Dosis variedad_dosis Rendimiento
## <dbl> <chr> <chr> <dbl>
## 1 1 tratadas 1_tratadas 0.89
## 2 1 tratadas 1_tratadas 0.94
## 3 1 tratadas 1_tratadas 0.85
## 4 1 tratadas 1_tratadas 1.12
## 5 1 tratadas 1_tratadas 1.35
## 6 1 tratadas 1_tratadas 1.05
## 7 1 no tratadas 1_no tratadas 1.78
## 8 1 no tratadas 1_no tratadas 2
## 9 1 no tratadas 1_no tratadas 1.86
## 10 1 no tratadas 1_no tratadas 2.3
## # ℹ 14 more rows
str(Poroto)
## tibble [24 × 4] (S3: tbl_df/tbl/data.frame)
## $ Variedad : num [1:24] 1 1 1 1 1 1 1 1 1 1 ...
## $ Dosis : chr [1:24] "tratadas" "tratadas" "tratadas" "tratadas" ...
## $ variedad_dosis: chr [1:24] "1_tratadas" "1_tratadas" "1_tratadas" "1_tratadas" ...
## $ Rendimiento : num [1:24] 0.89 0.94 0.85 1.12 1.35 1.05 1.78 2 1.86 2.3 ...
Poroto <- Poroto %>%
mutate(
Variedad = factor(Variedad),
Dosis = factor(Dosis),
variedad_dosis = factor(variedad_dosis),
Rendimiento = as.numeric(Rendimiento))
str(Poroto)
## tibble [24 × 4] (S3: tbl_df/tbl/data.frame)
## $ Variedad : Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ Dosis : Factor w/ 2 levels "no tratadas",..: 2 2 2 2 2 2 1 1 1 1 ...
## $ variedad_dosis: Factor w/ 4 levels "1_no tratadas",..: 2 2 2 2 2 2 1 1 1 1 ...
## $ Rendimiento : num [1:24] 0.89 0.94 0.85 1.12 1.35 1.05 1.78 2 1.86 2.3 ...
ggplot(Poroto, aes(x = Dosis, y = Rendimiento, fill = Variedad)) +
geom_boxplot() +
labs(title = "Rendimiento del poroto según dosis",
x = "Dosis curasemillas", y = "Rendimiento (Kg/parcela)") +
theme_minimal()
Poroto %>%
group_by((Variedad)) %>%
descr(Rendimiento)
## Descriptive Statistics
## Rendimiento by (Variedad)
## Data Frame: Poroto
## N: 24
##
## 1 2
## ----------------- -------- --------
## Mean 1.54 2.19
## Std.Dev 0.57 0.52
## Min 0.85 1.33
## Q1 1.00 1.81
## Median 1.56 2.30
## Q3 1.96 2.65
## Max 2.40 2.80
## MAD 0.71 0.59
## IQR 0.92 0.78
## CV 0.37 0.24
## Skewness 0.12 -0.37
## SE.Skewness 0.64 0.64
## Kurtosis -1.73 -1.49
## N.Valid 12.00 12.00
## N 12.00 12.00
## Pct.Valid 100.00 100.00
Poroto %>%
group_by((Dosis)) %>%
descr(Rendimiento)
## Descriptive Statistics
## Rendimiento by (Dosis)
## Data Frame: Poroto
## N: 24
##
## no tratadas tratadas
## ----------------- ------------- ----------
## Mean 2.34 1.39
## Std.Dev 0.36 0.45
## Min 1.78 0.85
## Q1 1.96 1.00
## Median 2.45 1.34
## Q3 2.65 1.81
## Max 2.80 2.10
## MAD 0.37 0.61
## IQR 0.64 0.77
## CV 0.15 0.32
## Skewness -0.31 0.31
## SE.Skewness 0.64 0.64
## Kurtosis -1.62 -1.57
## N.Valid 12.00 12.00
## N 12.00 12.00
## Pct.Valid 100.00 100.00
factorial_dca_poroto <- aov( Rendimiento ~ Dosis + Variedad + Dosis:Variedad, data = Poroto)
summary(factorial_dca_poroto)
## Df Sum Sq Mean Sq F value Pr(>F)
## Dosis 1 5.425 5.425 103.015 2.46e-09 ***
## Variedad 1 2.529 2.529 48.018 9.95e-07 ***
## Dosis:Variedad 1 0.022 0.022 0.422 0.523
## Residuals 20 1.053 0.053
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
emmip(factorial_dca_poroto, Dosis ~ Variedad)
residuos_factorial_dca <- factorial_dca_poroto$residuals
predichos_factorial_dca <- factorial_dca_poroto$fitted.values
hist(residuos_factorial_dca, main = "Histograma de residuos")
boxplot(residuos_factorial_dca, main = "Boxplot de residuos")
plot(factorial_dca_poroto, which = 1:2)
shapiro.test(factorial_dca_poroto$residuals)
##
## Shapiro-Wilk normality test
##
## data: factorial_dca_poroto$residuals
## W = 0.97151, p-value = 0.7043
leveneTest(Rendimiento ~ Dosis * Variedad, data = Poroto)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 3 1.2728 0.3107
## 20
Tukey_temp_nematodos <- HSD.test(factorial_dca_poroto, "Dosis")
Tukey_temp_nematodos
## $statistics
## MSerror Df Mean CV MSD
## 0.0526575 20 1.86375 12.31239 0.1954165
##
## $parameters
## test name.t ntr StudentizedRange alpha
## Tukey Dosis 2 2.949998 0.05
##
## $means
## Rendimiento std r se Min Max Q25 Q50 Q75
## no tratadas 2.339167 0.3596073 12 0.06624292 1.78 2.8 1.9825 2.45 2.6250
## tratadas 1.388333 0.4453157 12 0.06624292 0.85 2.1 1.0225 1.34 1.7875
##
## $comparison
## NULL
##
## $groups
## Rendimiento groups
## no tratadas 2.339167 a
## tratadas 1.388333 b
##
## attr(,"class")
## [1] "group"
plot(Tukey_temp_nematodos)
Tukey_sexo_nematodos <- HSD.test(factorial_dca_poroto, "Variedad")
Tukey_sexo_nematodos
## $statistics
## MSerror Df Mean CV MSD
## 0.0526575 20 1.86375 12.31239 0.1954165
##
## $parameters
## test name.t ntr StudentizedRange alpha
## Tukey Variedad 2 2.949998 0.05
##
## $means
## Rendimiento std r se Min Max Q25 Q50 Q75
## 1 1.539167 0.5682582 12 0.06624292 0.85 2.4 1.0225 1.565 1.9475
## 2 2.188333 0.5176667 12 0.06624292 1.33 2.8 1.8425 2.300 2.6250
##
## $comparison
## NULL
##
## $groups
## Rendimiento groups
## 2 2.188333 a
## 1 1.539167 b
##
## attr(,"class")
## [1] "group"
plot(Tukey_sexo_nematodos)
ggplot(Poroto, aes(x = Variedad, y = Rendimiento, fill = Dosis)) +
stat_summary(fun = mean,
geom = "bar",
position = position_dodge(width = 0.9)) +
labs(x = "Variedad",
y = "Rendimiento") +
theme_minimal()