2. Diseño de medidas repetidas de dos vias: Una via es el “tiempo”, y la otra es el “fertilizantes”(Factor)

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(tidyr)
library(rstatix)
## 
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
## 
##     filter
data("selfesteem2", package = "datarium")

datos2 = selfesteem2
print(datos2)
## # A tibble: 24 × 5
##    id    treatment    t1    t2    t3
##    <fct> <fct>     <dbl> <dbl> <dbl>
##  1 1     ctr          83    77    69
##  2 2     ctr          97    95    88
##  3 3     ctr          93    92    89
##  4 4     ctr          92    92    89
##  5 5     ctr          77    73    68
##  6 6     ctr          72    65    63
##  7 7     ctr          92    89    79
##  8 8     ctr          92    87    81
##  9 9     ctr          95    91    84
## 10 10    ctr          92    84    81
## # ℹ 14 more rows
datos2 = selfesteem2
datos2$tratamiento = gl(2,12,24, c('con fert', 'sin fert'))
#Un control es sin fert y otro con fert
#Vamos a convertirlos de formato ancho a largo

datos2 = datos2 %>% 
  gather(key='tiempo', value = 'rto',
         t1,t2,t3)
datos2 %>%
  group_by(tratamiento, tiempo) %>%
  summarise(media = mean(rto),
            desv = sd(rto),
            n = n(),
            cv = 100*desv/media)
## `summarise()` has grouped output by 'tratamiento'. You can override using the
## `.groups` argument.
## # A tibble: 6 × 6
## # Groups:   tratamiento [2]
##   tratamiento tiempo media  desv     n    cv
##   <fct>       <chr>  <dbl> <dbl> <int> <dbl>
## 1 con fert    t1      88    8.08    12  9.18
## 2 con fert    t2      83.8 10.2     12 12.2 
## 3 con fert    t3      78.7 10.5     12 13.4 
## 4 sin fert    t1      87.6  7.62    12  8.70
## 5 sin fert    t2      87.8  7.42    12  8.45
## 6 sin fert    t3      87.7  8.14    12  9.28

*Fertilice y me da un buen valor en la media(88)

VISUALIZACION POR DIAGRAMA DE CAJA

library(ggplot2)

ggplot(datos2)+
  aes(tiempo, rto, fill=tratamiento)+
  geom_boxplot()

*Sin fertilizantes son mejores para t2 y t3, que con fertlizantes

DETECION DE DATOS ATIPICOS

library(rstatix)

datos2 %>%
  group_by(tratamiento, tiempo) %>%
  identify_outliers(rto)
## [1] tratamiento tiempo      id          treatment   rto         is.outlier 
## [7] is.extreme 
## <0 rows> (or 0-length row.names)

*No hay datos atipicos

SUPUESTO DE NORMALIDAD

datos2 %>%
  group_by(tratamiento, tiempo) %>%
  shapiro_test(rto)
## # A tibble: 6 × 5
##   tratamiento tiempo variable statistic      p
##   <fct>       <chr>  <chr>        <dbl>  <dbl>
## 1 con fert    t1     rto          0.828 0.0200
## 2 con fert    t2     rto          0.868 0.0618
## 3 con fert    t3     rto          0.887 0.107 
## 4 sin fert    t1     rto          0.919 0.279 
## 5 sin fert    t2     rto          0.923 0.316 
## 6 sin fert    t3     rto          0.886 0.104

Los supuestos de normalidad estan bien p-value mayor 5%

ANALISIS DE VARIANZA

res.aov= anova_test(data=datos2,
                    dv=rto,
                    wid = id,
                    within =c( tratamiento, tiempo))
get_anova_table(res.aov)
## ANOVA Table (type III tests)
## 
##               Effect  DFn   DFd      F        p p<.05   ges
## 1        tratamiento 1.00 11.00 15.541 2.00e-03     * 0.059
## 2             tiempo 1.31 14.37 27.369 5.03e-05     * 0.049
## 3 tratamiento:tiempo 2.00 22.00 30.424 4.63e-07     * 0.050
#ges: Tamaño de efecto

*Si hay interaccion porque el p-value (4.63e-07) es menor al 5%

PROCEDIMEINTO CON INTERACCION

datos2 %>% 
  group_by(tiempo, tratamiento) %>% 
  summarise(mean_rto = mean(rto)) %>% 
  ggplot()+
  aes(tiempo, mean_rto,
      color=tratamiento,
      group=tratamiento)+
  geom_point(size=5)+
  geom_line(linewidth=3)
## `summarise()` has grouped output by 'tiempo'. You can override using the
## `.groups` argument.

CONCLUSION El mejor tratamiento en t1 con fert (88) El mejor tratamiento es t2 y t3 es no fertilizar