Ejercicios Día 4


Reportes estadisticos


Carga de Librerias - Arsenal

library(arsenal)
require(knitr)

data(mockstudy) # cargar los datos
dim(mockstudy)  # número de filas y columnas 
## [1] 1499   14

Número de filas

## 'data.frame':    1499 obs. of  14 variables:
##  $ case       : int  110754 99706 105271 105001 112263 86205 99508 90158 88989 90515 ...
##  $ age        : int  67 74 50 71 69 56 50 57 51 63 ...
##   ..- attr(*, "label")= chr "Age in Years"
##  $ arm        : chr  "F: FOLFOX" "A: IFL" "A: IFL" "G: IROX" ...
##   ..- attr(*, "label")= chr "Treatment Arm"
##  $ sex        : Factor w/ 2 levels "Male","Female": 1 2 2 2 2 1 1 1 2 1 ...
##  $ race       : chr  "Caucasian" "Caucasian" "Caucasian" "Caucasian" ...
##   ..- attr(*, "label")= chr "Race"
##  $ fu.time    : int  922 270 175 128 233 120 369 421 387 363 ...
##  $ fu.stat    : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ ps         : int  0 1 1 1 0 0 0 0 1 1 ...
##  $ hgb        : num  11.5 10.7 11.1 12.6 13 10.2 13.3 12.1 13.8 12.1 ...
##  $ bmi        : num  25.1 19.5 NA 29.4 26.4 ...
##   ..- attr(*, "label")= chr "Body Mass Index (kg/m^2)"
##  $ alk.phos   : int  160 290 700 771 350 569 162 152 231 492 ...
##  $ ast        : int  35 52 100 68 35 27 16 12 25 18 ...
##  $ mdquality.s: int  NA 1 1 1 NA 1 1 1 1 1 ...
##  $ age.ord    : Ord.factor w/ 8 levels "10-19"<"20-29"<..: 6 7 4 7 6 5 4 5 5 6 ...
## [1] "F: FOLFOX" "A: IFL"    "G: IROX"
## 
##    A: IFL F: FOLFOX   G: IROX 
##       428       691       380
## 
##    A: IFL F: FOLFOX   G: IROX 
## 0.2855237 0.4609740 0.2535023

Distribución de la varia arm en función del sexo y la edad


## tableby Object
## 
## Function Call:
## tableby(formula = arm ~ sex + age, data = mockstudy)
## 
## Variable(s):
## arm ~ sex, age
## 
## 
## |                            | A: IFL (N=428)  | F: FOLFOX (N=691) | G: IROX (N=380) | Total (N=1499)  | p value|
## |:---------------------------|:---------------:|:-----------------:|:---------------:|:---------------:|-------:|
## |**sex**                     |                 |                   |                 |                 |   0.190|
## |&nbsp;&nbsp;&nbsp;Male      |   277 (64.7%)   |    411 (59.5%)    |   228 (60.0%)   |   916 (61.1%)   |        |
## |&nbsp;&nbsp;&nbsp;Female    |   151 (35.3%)   |    280 (40.5%)    |   152 (40.0%)   |   583 (38.9%)   |        |
## |**Age in Years**            |                 |                   |                 |                 |   0.614|
## |&nbsp;&nbsp;&nbsp;Mean (SD) | 59.673 (11.365) |  60.301 (11.632)  | 59.763 (11.499) | 59.985 (11.519) |        |
## |&nbsp;&nbsp;&nbsp;Range     | 27.000 - 88.000 |  19.000 - 88.000  | 26.000 - 85.000 | 19.000 - 88.000 |        |
##   group.term   group.label strata.term variable     term        label
## 1        arm Treatment Arm                  sex      sex          sex
## 2        arm Treatment Arm                  sex countpct         Male
## 3        arm Treatment Arm                  sex countpct       Female
## 4        arm Treatment Arm                  age      age Age in Years
## 5        arm Treatment Arm                  age   meansd    Mean (SD)
## 6        arm Treatment Arm                  age    range        Range
##   variable.type              A: IFL           F: FOLFOX            G: IROX
## 1   categorical                                                           
## 2   categorical 277.00000, 64.71963 411.00000, 59.47902            228, 60
## 3   categorical 151.00000, 35.28037 280.00000, 40.52098            152, 40
## 4       numeric                                                           
## 5       numeric  59.67290, 11.36454  60.30101, 11.63225 59.76316, 11.49930
## 6       numeric              27, 88              19, 88             26, 85
##                Total                       test   p.value
## 1                    Pearson's Chi-squared test 0.1904388
## 2  916.0000, 61.1074 Pearson's Chi-squared test 0.1904388
## 3  583.0000, 38.8926 Pearson's Chi-squared test 0.1904388
## 4                            Linear Model ANOVA 0.6143859
## 5 59.98532, 11.51877         Linear Model ANOVA 0.6143859
## 6             19, 88         Linear Model ANOVA 0.6143859
##  [1] ""                 "916"              "61.1074049366244" "583"             
##  [5] "38.8925950633756" ""                 "59.9853235490327" "11.5187684866331"
##  [9] "19"               "88"
## [1] 916
##   Nombre_Columna
## 1       916.0000
## 2        61.1074
## NULL
## NULL
## [1] "1" "2"
##       Nombre_Columna
## Media       916.0000
## SD           61.1074
## 
## 
## |                            | A: IFL (N=428)  | F: FOLFOX (N=691) | G: IROX (N=380) | Total (N=1499)  | p value|
## |:---------------------------|:---------------:|:-----------------:|:---------------:|:---------------:|-------:|
## |**sex**                     |                 |                   |                 |                 |   0.190|
## |&nbsp;&nbsp;&nbsp;Male      |   277 (64.7%)   |    411 (59.5%)    |   228 (60.0%)   |   916 (61.1%)   |        |
## |&nbsp;&nbsp;&nbsp;Female    |   151 (35.3%)   |    280 (40.5%)    |   152 (40.0%)   |   583 (38.9%)   |        |
## |**Age in Years**            |                 |                   |                 |                 |   0.614|
## |&nbsp;&nbsp;&nbsp;Mean (SD) | 59.673 (11.365) |  60.301 (11.632)  | 59.763 (11.499) | 59.985 (11.519) |        |
## |&nbsp;&nbsp;&nbsp;Range     | 27.000 - 88.000 |  19.000 - 88.000  | 26.000 - 85.000 | 19.000 - 88.000 |        |
##  [1]        NA 916.00000  61.10740 583.00000  38.89260        NA  59.98532
##  [8]  11.51877  19.00000  88.00000

Resumen de variables de tiempo


## 
## 
## |                            |   Time Point 1 (N=4)    |   Time Point 2 (N=4)    | Difference (N=4) | p value|
## |:---------------------------|:-----------------------:|:-----------------------:|:----------------:|-------:|
## |**Cat**                     |                         |                         |                  |   1.000|
## |&nbsp;&nbsp;&nbsp;A         |        2 (50.0%)        |        2 (50.0%)        |    1 (50.0%)     |        |
## |&nbsp;&nbsp;&nbsp;B         |        2 (50.0%)        |        2 (50.0%)        |    1 (50.0%)     |        |
## |**Fac**                     |                         |                         |                  |   0.261|
## |&nbsp;&nbsp;&nbsp;A         |        2 (50.0%)        |        1 (25.0%)        |    2 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;B         |        1 (25.0%)        |        2 (50.0%)        |    1 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;C         |        1 (25.0%)        |        1 (25.0%)        |    1 (100.0%)    |        |
## |**Num**                     |                         |                         |                  |   0.391|
## |&nbsp;&nbsp;&nbsp;Mean (SD) |      2.750 (1.258)      |      3.250 (0.957)      |  0.500 (1.000)   |        |
## |&nbsp;&nbsp;&nbsp;Range     |      1.000 - 4.000      |      2.000 - 4.000      |  -1.000 - 1.000  |        |
## |**Ord**                     |                         |                         |                  |   0.174|
## |&nbsp;&nbsp;&nbsp;I         |        2 (50.0%)        |        0 (0.0%)         |    2 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;II        |        1 (25.0%)        |        1 (25.0%)        |    1 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;III       |        1 (25.0%)        |        3 (75.0%)        |     0 (0.0%)     |        |
## |**Lgl**                     |                         |                         |                  |   1.000|
## |&nbsp;&nbsp;&nbsp;FALSE     |        2 (50.0%)        |        1 (25.0%)        |    2 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;TRUE      |        2 (50.0%)        |        3 (75.0%)        |    1 (50.0%)     |        |
## |**Dat**                     |                         |                         |                  |   0.182|
## |&nbsp;&nbsp;&nbsp;Median    |       2018-05-03        |       2018-05-04        |      0.500       |        |
## |&nbsp;&nbsp;&nbsp;Range     | 2018-05-02 - 2018-05-06 | 2018-05-02 - 2018-05-07 |  0.000 - 1.000   |        |
## 
## 
## |                            |   Time Point 1 (N=4)    |   Time Point 2 (N=4)    | Difference (N=4) | p value|
## |:---------------------------|:-----------------------:|:-----------------------:|:----------------:|-------:|
## |**Cat**                     |                         |                         |                  |   1.000|
## |&nbsp;&nbsp;&nbsp;A         |        2 (50.0%)        |        2 (50.0%)        |    1 (50.0%)     |        |
## |&nbsp;&nbsp;&nbsp;B         |        2 (50.0%)        |        2 (50.0%)        |    1 (50.0%)     |        |
## |**Fac**                     |                         |                         |                  |   0.261|
## |&nbsp;&nbsp;&nbsp;A         |        2 (50.0%)        |        1 (25.0%)        |    2 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;B         |        1 (25.0%)        |        2 (50.0%)        |    1 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;C         |        1 (25.0%)        |        1 (25.0%)        |    1 (100.0%)    |        |
## |**Num**                     |                         |                         |                  |   0.391|
## |&nbsp;&nbsp;&nbsp;Mean (SD) |      2.750 (1.258)      |      3.250 (0.957)      |  0.500 (1.000)   |        |
## |&nbsp;&nbsp;&nbsp;Range     |      1.000 - 4.000      |      2.000 - 4.000      |  -1.000 - 1.000  |        |
## |**Ord**                     |                         |                         |                  |   0.174|
## |&nbsp;&nbsp;&nbsp;I         |        2 (50.0%)        |        0 (0.0%)         |    2 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;II        |        1 (25.0%)        |        1 (25.0%)        |    1 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;III       |        1 (25.0%)        |        3 (75.0%)        |     0 (0.0%)     |        |
## |**Lgl**                     |                         |                         |                  |   1.000|
## |&nbsp;&nbsp;&nbsp;FALSE     |        2 (50.0%)        |        1 (25.0%)        |    2 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;TRUE      |        2 (50.0%)        |        3 (75.0%)        |    1 (50.0%)     |        |
## |**Dat**                     |                         |                         |                  |   0.182|
## |&nbsp;&nbsp;&nbsp;Median    |       2018-05-03        |       2018-05-04        |      0.500       |        |
## |&nbsp;&nbsp;&nbsp;Range     | 2018-05-02 - 2018-05-06 | 2018-05-02 - 2018-05-07 |  0.000 - 1.000   |        |

Variables por puntos de tiempo (ejercicio)


## # A tibble: 5 x 3
## # Groups:   tp [2]
##   tp         Cat   `n()`
##   <chr>      <chr> <int>
## 1 Trimestre1 A         2
## 2 Trimestre1 B         2
## 3 Trimestre1 <NA>      1
## 4 Trimestre2 A         2
## 5 Trimestre2 B         3
## # A tibble: 2 x 2
##   tp         NumeroClientes
##   <chr>               <int>
## 1 Trimestre1              5
## 2 Trimestre2              5
## # A tibble: 10 x 2
## # Groups:   tp [2]
##    tp         Clientes
##    <chr>         <dbl>
##  1 Trimestre1        1
##  2 Trimestre1        2
##  3 Trimestre1        3
##  4 Trimestre1        4
##  5 Trimestre1        5
##  6 Trimestre2        1
##  7 Trimestre2        2
##  8 Trimestre2        3
##  9 Trimestre2        4
## 10 Trimestre2        6
##  [1] I    II   II   X    III  III  I    III  II   I    <NA>
## Levels: I < II < III < X
##  [1] 1    2    2    10   3    <NA> 3    1    3    2    1   
## Levels: 1 < 2 < 3 < 10
##  [1] 1    2    2    10   3    3    1    <NA> 3    2    1   
## Levels: 10 < 3 < 2 < 1
##  [1] Primaria      Secundaria    Secundaria    Postgrado     Universitaria
##  [6] Universitaria Primaria      Universitaria Secundaria    Primaria     
## Levels: Postgrado < Primaria < Secundaria < Universitaria
##  [1] Primaria      Secundaria    Secundaria    Postgrado     Universitaria
##  [6] Universitaria Primaria      Universitaria Secundaria    Primaria     
## Levels: Postgrado < Primaria < Secundaria < Universitaria
##  [1] Primaria      Secundaria    Secundaria    Postgrado     Universitaria
##  [6] Universitaria Primaria      Universitaria Secundaria    Primaria     
## [11] <NA>          <NA>         
## Levels: Primaria < Secundaria < Universitaria < Postgrado
## 
## 
## |                            |    Trimestre1 (N=4)     |    Trimestre2 (N=4)     | Difference (N=4) | p value|
## |:---------------------------|:-----------------------:|:-----------------------:|:----------------:|-------:|
## |**Cat**                     |                         |                         |                  |   1.000|
## |&nbsp;&nbsp;&nbsp;A         |        2 (50.0%)        |        2 (50.0%)        |    1 (50.0%)     |        |
## |&nbsp;&nbsp;&nbsp;B         |        2 (50.0%)        |        2 (50.0%)        |    1 (50.0%)     |        |
## |**Fac**                     |                         |                         |                  |   0.261|
## |&nbsp;&nbsp;&nbsp;A         |        2 (50.0%)        |        1 (25.0%)        |    2 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;B         |        1 (25.0%)        |        2 (50.0%)        |    1 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;C         |        1 (25.0%)        |        1 (25.0%)        |    1 (100.0%)    |        |
## |**Num**                     |                         |                         |                  |   0.391|
## |&nbsp;&nbsp;&nbsp;Mean (SD) |      2.750 (1.258)      |      3.250 (0.957)      |  0.500 (1.000)   |        |
## |&nbsp;&nbsp;&nbsp;Range     |      1.000 - 4.000      |      2.000 - 4.000      |  -1.000 - 1.000  |        |
## |**Ord**                     |                         |                         |                  |   0.174|
## |&nbsp;&nbsp;&nbsp;I         |        2 (50.0%)        |        0 (0.0%)         |    2 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;II        |        1 (25.0%)        |        1 (25.0%)        |    1 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;III       |        1 (25.0%)        |        3 (75.0%)        |     0 (0.0%)     |        |
## |**Lgl**                     |                         |                         |                  |   1.000|
## |&nbsp;&nbsp;&nbsp;FALSE     |        2 (50.0%)        |        1 (25.0%)        |    2 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;TRUE      |        2 (50.0%)        |        3 (75.0%)        |    1 (50.0%)     |        |
## |**Dat**                     |                         |                         |                  |   0.182|
## |&nbsp;&nbsp;&nbsp;Median    |       2018-05-03        |       2018-05-04        |      0.500       |        |
## |&nbsp;&nbsp;&nbsp;Range     | 2018-05-02 - 2018-05-06 | 2018-05-02 - 2018-05-07 |  0.000 - 1.000   |        |
## 
## 
## |                            |    Trimestre1 (N=6)     |    Trimestre2 (N=6)     | Difference (N=6) | p value|
## |:---------------------------|:-----------------------:|:-----------------------:|:----------------:|-------:|
## |**Cat**                     |                         |                         |                  |   1.000|
## |&nbsp;&nbsp;&nbsp;N-Miss    |            2            |            1            |        2         |        |
## |&nbsp;&nbsp;&nbsp;A         |        2 (50.0%)        |        2 (40.0%)        |    1 (50.0%)     |        |
## |&nbsp;&nbsp;&nbsp;B         |        2 (50.0%)        |        3 (60.0%)        |    1 (50.0%)     |        |
## |**Fac**                     |                         |                         |                  |   0.261|
## |&nbsp;&nbsp;&nbsp;N-Miss    |            1            |            1            |        2         |        |
## |&nbsp;&nbsp;&nbsp;A         |        2 (40.0%)        |        2 (40.0%)        |    2 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;B         |        1 (20.0%)        |        2 (40.0%)        |    1 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;C         |        2 (40.0%)        |        1 (20.0%)        |    1 (100.0%)    |        |
## |**Num**                     |                         |                         |                  |   0.391|
## |&nbsp;&nbsp;&nbsp;N-Miss    |            1            |            2            |        2         |        |
## |&nbsp;&nbsp;&nbsp;Mean (SD) |      2.200 (1.643)      |      3.250 (0.957)      |  0.500 (1.000)   |        |
## |&nbsp;&nbsp;&nbsp;Range     |      0.000 - 4.000      |      2.000 - 4.000      |  -1.000 - 1.000  |        |
## |**Ord**                     |                         |                         |                  |   0.174|
## |&nbsp;&nbsp;&nbsp;N-Miss    |            1            |            1            |        2         |        |
## |&nbsp;&nbsp;&nbsp;I         |        2 (40.0%)        |        1 (20.0%)        |    2 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;II        |        2 (40.0%)        |        1 (20.0%)        |    1 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;III       |        1 (20.0%)        |        3 (60.0%)        |     0 (0.0%)     |        |
## |**Lgl**                     |                         |                         |                  |   1.000|
## |&nbsp;&nbsp;&nbsp;N-Miss    |            1            |            1            |        2         |        |
## |&nbsp;&nbsp;&nbsp;FALSE     |        3 (60.0%)        |        2 (40.0%)        |    2 (100.0%)    |        |
## |&nbsp;&nbsp;&nbsp;TRUE      |        2 (40.0%)        |        3 (60.0%)        |    1 (50.0%)     |        |
## |**Dat**                     |                         |                         |                  |   0.182|
## |&nbsp;&nbsp;&nbsp;N-Miss    |            1            |            1            |        2         |        |
## |&nbsp;&nbsp;&nbsp;Median    |       2018-05-04        |       2018-05-05        |      0.500       |        |
## |&nbsp;&nbsp;&nbsp;Range     | 2018-05-02 - 2018-05-06 | 2018-05-02 - 2018-05-07 |  0.000 - 1.000   |        |
##          id a b c
## rn1 person1 a 1 f
## rn2 person2 b 3 e
## rn3 person3 c 4 d
## 'data.frame':    3 obs. of  4 variables:
##  $ id: chr  "person1" "person2" "person3"
##  $ a : chr  "a" "b" "c"
##  $ b : num  1 3 4
##  $ c : chr  "f" "e" "d"
##          id a b   d
## rn1 person3 c 1 rn1
## rn3 person2 b 3 rn2
## rn2 person1 a 4 rn3
## Compare Object
## 
## Function Call: 
## comparedf(x = df1, y = df2)
## 
## Shared: 3 non-by variables and 3 observations.
## Not shared: 2 variables and 0 observations.
## 
## Differences found in 2/3 variables compared.
## 0 variables compared have non-identical attributes.