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


datos3 = weightloss %>% 
  rename(FQ = diet,
         FO = exercises) %>%
   pivot_longer(c(t1,t2,t3),
               names_to = 'tiempo',
               values_to = 'rto')

datos3
## # A tibble: 144 × 5
##    id    FQ    FO    tiempo   rto
##    <fct> <fct> <fct> <chr>  <dbl>
##  1 1     no    no    t1      10.4
##  2 1     no    no    t2      13.2
##  3 1     no    no    t3      11.6
##  4 2     no    no    t1      11.6
##  5 2     no    no    t2      10.7
##  6 2     no    no    t3      13.2
##  7 3     no    no    t1      11.4
##  8 3     no    no    t2      11.1
##  9 3     no    no    t3      11.4
## 10 4     no    no    t1      11.1
## # ℹ 134 more rows

Descriptivo Tabular

datos3 %>%
  group_by(FQ, FO, tiempo) %>%
  get_summary_stats(rto, type = "mean_sd")
## # A tibble: 12 × 7
##    FQ    FO    tiempo variable     n  mean    sd
##    <fct> <fct> <chr>  <fct>    <dbl> <dbl> <dbl>
##  1 no    no    t1     rto         12  10.9 0.868
##  2 no    no    t2     rto         12  11.6 1.30 
##  3 no    no    t3     rto         12  11.4 0.935
##  4 no    yes   t1     rto         12  10.8 1.27 
##  5 no    yes   t2     rto         12  13.4 1.01 
##  6 no    yes   t3     rto         12  16.8 1.53 
##  7 yes   no    t1     rto         12  11.7 0.938
##  8 yes   no    t2     rto         12  12.4 1.42 
##  9 yes   no    t3     rto         12  13.8 1.43 
## 10 yes   yes   t1     rto         12  11.4 1.09 
## 11 yes   yes   t2     rto         12  13.2 1.22 
## 12 yes   yes   t3     rto         12  14.7 0.625

Descriptivo Visual

library(ggpubr)
## Loading required package: ggplot2
ggboxplot(
  datos3, x = "FQ", y = "rto",
  color = "tiempo", palette = "jco",
  facet.by = "FO", short.panel.labs = FALSE
  )

ggplot(datos3)+
  aes(rto)+
  geom_density()

ggplot(datos3)+
  aes(rto, fill=tiempo)+
  geom_density(alpha=0.5)

library(nortest)
shapiro.test(datos3$rto)
## 
##  Shapiro-Wilk normality test
## 
## data:  datos3$rto
## W = 0.96377, p-value = 0.0007474
lillie.test(datos3$rto)
## 
##  Lilliefors (Kolmogorov-Smirnov) normality test
## 
## data:  datos3$rto
## D = 0.10013, p-value = 0.001229
cvm.test(datos3$rto)
## 
##  Cramer-von Mises normality test
## 
## data:  datos3$rto
## W = 0.23031, p-value = 0.002205
sf.test(datos3$rto)
## 
##  Shapiro-Francia normality test
## 
## data:  datos3$rto
## W = 0.96406, p-value = 0.001295
ad.test(datos3$rto)
## 
##  Anderson-Darling normality test
## 
## data:  datos3$rto
## A = 1.432, p-value = 0.001021

Test de simetria

# install.packages("lawstat")
library(lawstat)
symmetry.test(datos3$rto)
## 
##  m-out-of-n bootstrap symmetry test by Miao, Gel, and Gastwirth (2006)
## 
## data:  datos3$rto
## Test statistic = 2.2399, p-value = 0.032
## alternative hypothesis: the distribution is asymmetric.
## sample estimates:
## bootstrap optimal m 
##                  19
res.aov <- anova_test(
  data = datos3, dv = rto, wid = id,
  within = c(FQ, FO, tiempo)
  )
get_anova_table(res.aov)
## ANOVA Table (type III tests)
## 
##         Effect  DFn   DFd       F        p p<.05   ges
## 1           FQ 1.00 11.00   6.021 3.20e-02     * 0.028
## 2           FO 1.00 11.00  58.928 9.65e-06     * 0.284
## 3       tiempo 2.00 22.00 110.942 3.22e-12     * 0.541
## 4        FQ:FO 1.00 11.00  75.356 2.98e-06     * 0.157
## 5    FQ:tiempo 1.38 15.17   0.603 5.01e-01       0.013
## 6    FO:tiempo 2.00 22.00  20.826 8.41e-06     * 0.274
## 7 FQ:FO:tiempo 2.00 22.00  14.246 1.07e-04     * 0.147
interaction.plot(datos3$tiempo,
                 datos3$FQ,
                 datos3$rto)

interaction.plot(datos3$tiempo,
                 datos3$FO,
                 datos3$rto)

tapply(datos3$rto,
             list(datos3$FQ,
                  datos3$FO,
                  datos3$tiempo),
             mean)
## , , t1
## 
##           no      yes
## no  10.90917 10.79417
## yes 11.74250 11.39333
## 
## , , t2
## 
##           no      yes
## no  11.56583 13.42083
## yes 12.41583 13.22500
## 
## , , t3
## 
##           no     yes
## no  11.45000 16.8175
## yes 13.78667 14.6550