* preample 							     
  :ignore:

  #+TITLE: Utility functions
  #+AUTHOR: Klaus K. Holst and Thomas Scheike
  #+email: k.k.holst@biostat.ku.dk
  #+LATEX_CLASS: tufte-handout+listings
  #+LATEX_CLASS_OPTIONS: [a4paper]
  #+PROPERTY: header-args:R  :session *R* :cache yes :width 550 :height 450
  #+PROPERTY: header-args  :eval never-export :exports both :results output :tangle yes :comments yes 
  #+PROPERTY: header-args:R+ :colnames yes :rownames no :hlines yes
  #+OPTIONS: timestamp:t title:t date:t author:t creator:nil toc:nil 
  #+OPTIONS: h:4 num:t tags:nil d:t ^:{}
  #+LATEX_HEADER: \lstset{language=R,keywords={},morekeywords={}}
  #+LATEX_HEADER: \usepackage{zlmtt}
  #+LATEX_HEADER: \setlength{\parindent}{0em}
  #+LATEX_HEADER: %%\setlength{\parindent}{default}
  #+LaTeX: \setlength{\parindent}{0em} %\setlength{\parindent}{default}

#+BEGIN_SRC emacs-lisp :results silent :exports results :eval
(setq org-latex-listings t)
(setq org-latex-compiler-file-string 
"%%\\VignetteIndexEntry{dutils overview}\n%%\\VignetteEngine{R.rsp::tex}\n%%\\VignetteKeyword{R}\n%%\\VignetteKeyword{package}\n%%\\VignetteKeyword{vignette}\n%%\\VignetteKeyword{LaTeX}\n")
#+END_SRC

* Introduction

Data summaries

| =dhead=        | a |
| =dtail=        | a |
| =dsummary=     |   |
| =dprint,dlist= |   |
| =dlevels=      |   |
| =dunique=      |   |
| =dstr=         |   |


Data processing

| =dsort=             | a |
| =dreshape=          |   |
| =dcut=              |   |
| =drm=               |   |
| =dkeep=             |   |
| =dnames=, =drename=   |   |
| =ddrop=             |   |
| =dfactor=, =dnumeric= |   |
| =dsubset=           |   |
| =dlag=              |   |
| =drelevel=          |   |
| =dsample=           |   |


Aggregation

| =dby=                         |   |
| =daggregate=                  |   |
| =deval=                       |   |
| =deval2=                      |   |
| =dscalar=                     |   |
| =dmean=, =dsum=, =dsd=, =dquantile= |   |
| =dcor=                        |   |
| =dcount=                      |   |
| =dtable=                      |   |


#+BEGIN_SRC R :cache no
library(mets)
#+END_SRC

#+RESULTS:
#+begin_example
Loading required package: timereg
Loading required package: survival
Loading required package: lava
lava version 1.5.1
mets version 1.2.1.2

Attaching package: ‘mets’

The following object is masked _by_ ‘.GlobalEnv’:

    object.defined

Warning message:
failed to assign RegisteredNativeSymbol for cor to cor since cor is already defined in the ‘mets’ namespace
#+end_example


* Data summary

We will use a subset of the TRACE data available in the =timereg= package, which consists of a random sub-sample of 300 patients
out of the full cohort consisting of approximately 6000 patients. It
contains data relating survival of patients after myocardial
infarction to various risk factors.

#+BEGIN_SRC R
data(sTRACE, package="timereg")
dhead(sTRACE)
#+END_SRC

#+RESULTS[01b4952643bb968b9b1e194a2056b925ff7c5052]:
:         no wmi status chf    age sex diabetes     time vf
: X1944 1944 1.5      9   0 84.924   1        1 1.345000  0
: X5783 5783 1.9      0   1 74.193   0        0 6.910000  0
: X784   784 0.8      9   0 78.081   0        1 0.196000  0
: X3763 3763 1.3      0   0 55.479   1        0 7.543000  0
: X2927 2927 1.6      0   1 62.997   0        0 7.126000  0
: X4511 4511 1.0      9   1 67.644   1        0 4.532606  0

\citet{TRACE}
This data frame contains the following columns:

- id :: Patient code (numeric).
- wmi :: Measure of heart pumping effect based on ultrasound measurements where 2 is normal and 0 is worst.
- status :: Survival status. 9: dead from myocardial infarction, 0: alive, 7: dead from other causes.
- time :: Survival time in years.
- chf :: Clinical heart pump failure, 1: present, 0: absent.
- diabetes :: Diabetes, 1: present, 0: absent.
- vf ::e. Ventricular fibrillation, 1: present, 0: absent.
- sex :: Gender, 1: female, 0: male.
- age :: Age of patient.


  #+BEGIN_mnote
  Here a margin note
  #+END_mnote

** a 

#+BEGIN_SRC R
n <- 20
m <- lava::lvm(letters)
d <- lava::sim(m,n)
 
dlist(d,~a+b+c | a>0)
dlist(d, a+b~c>0 | a>0)

#+END_SRC

#+RESULTS[84fda9ca89b2ebc100e10fa6305cd1c85694c607]:
#+begin_example
   a      b        c        
1  0.2931  0.08600 -1.523897
3  0.7401 -1.52052  0.602183
7  0.9079 -0.50419  0.001286
10 0.3768  0.42186 -0.824860
11 1.3748 -0.09775  1.386935
14 0.7545 -1.80065  1.494348
15 0.4461  0.54516 -0.337498
17 0.5001 -0.23887 -0.562333
20 0.7586  1.66598  0.482238
c > 0: FALSE
   a      b      
1  0.2931  0.0860
10 0.3768  0.4219
15 0.4461  0.5452
17 0.5001 -0.2389
------------------------------------------------------------ 
c > 0: TRUE
   a      b       
3  0.7401 -1.52052
7  0.9079 -0.50419
11 1.3748 -0.09775
14 0.7545 -1.80065
20 0.7586  1.66598
#+end_example


#+BEGIN_SRC R
dmean('Petal' ~ Species, data=iris, regex=TRUE)
#+END_SRC

#+RESULTS[62e576971a5c3a003bbb36ab4ea834eb293e063d]:
:      Species Petal.Length Petal.Width
: 1     setosa        1.462       0.246
: 2 versicolor        4.260       1.326
: 3  virginica        5.552       2.026



#+NAME: fig1
#+BEGIN_SRC R :exports both :file figs/fig1.png :results output graphics
 plot(1)
#+END_SRC

#+RESULTS[e3904b17cae30c3ef0f5d112eb46725fac469094]: fig1
[[file:figs/fig1.png]]

#+ATTR_LaTeX: :width \textwidth :center t
#+CAPTION: Important figure. label:fig1
\vspace*{1em}

#+BEGIN_marginfigure
#+ATTR_LATEX: :width 2cm :float nil :center t
#+CAPTION: Important margin figure. label:fig2
#+END_marginfigure


* Tables

#+BEGIN_SRC R
data(sTRACE, package="timereg")
dhead(sTRACE)
dcut(sTRACE) <-  wmicat~wmi
dtable(sTRACE, sex+diabetes+wmicat~vf | age<60)
dby(sTRACE, wmi ~ diabetes+sex, m=mean, q50=median, sd=sd, REDUCE=T)
dhead(sTRACE, 'wmi*' ~ sex)
#+END_SRC

#+RESULTS[b5f8d7f29e1ed8fbda4349938306044b44e90c62]:
#+begin_example
        no wmi status chf    age sex diabetes     time vf
X1944 1944 1.5      9   0 84.924   1        1 1.345000  0
X5783 5783 1.9      0   1 74.193   0        0 6.910000  0
X784   784 0.8      9   0 78.081   0        1 0.196000  0
X3763 3763 1.3      0   0 55.479   1        0 7.543000  0
X2927 2927 1.6      0   1 62.997   0        0 7.126000  0
X4511 4511 1.0      9   1 67.644   1        0 4.532606  0
vf: 0

             wmicat [0.4,1.1] (1.1,1.4] (1.4,1.8] (1.8,2.7]
sex diabetes                                               
0   0                       4         2         3         5
    1                       0         2         1         1
1   0                      15        17        37        24
    1                       0         3         2         1
------------------------------------------------------------ 
vf: 1

             wmicat [0.4,1.1] (1.1,1.4] (1.4,1.8] (1.8,2.7]
sex diabetes                                               
0   0                       1         1         0         1
1   0                       1         2         2         0
  diabetes sex        m  q50        sd
1        0   0 1.437762 1.50 0.3810298
2        1   0 1.384211 1.30 0.4272173
3        0   1 1.434839 1.45 0.4017105
4        1   1 1.150000 1.15 0.4299009
sex: 0
      wmi    wmicat
X5783 1.9 (1.8,2.7]
X784  0.8 [0.4,1.1]
X2927 1.6 (1.4,1.8]
X1085 0.9 [0.4,1.1]
X5249 1.7 (1.4,1.8]
X6311 0.7 [0.4,1.1]
------------------------------------------------------------ 
sex: 1
      wmi    wmicat
X1944 1.5 (1.4,1.8]
X3763 1.3 (1.1,1.4]
X4511 1.0 [0.4,1.1]
X3122 1.9 (1.8,2.7]
X5441 1.4 (1.1,1.4]
X1280 1.1 [0.4,1.1]
#+end_example


#+BEGIN_SRC R
library("magrittr")
library("mets")

op <- par(mfrow=c(1,3))
l <- 
    iris %>% dsubset('*Length'~Species | Sepal.Width>mean(Sepal.Width)) %>%
    lapply(function(x,...) lm(Sepal.Length~Petal.Length,x)) %>%
    lapply(plotConf)
par(op)

dtable(iris, Species+dcut(Petal.Width,4)~1)
dtable(iris, Species+dcut(Petal.Width,4)~1|Sepal.Width>median(Sepal.Width))
dtable(iris, Species+dcut(Petal.Width,4)~
                 dcut(Petal.Length,breaks=2)|
                 Sepal.Width>mean(Sepal.Width))

#+END_SRC

#+RESULTS[14a09cf4e6531704516d9d76305b8cb3355e7a04]:
#+begin_example

           dcut(Petal.Width, 4) [0.1,0.3] (0.3,1.3] (1.3,1.8] (1.8,2.5]
Species                                                                
setosa                                 41         9         0         0
versicolor                              0        28        22         0
virginica                               0         0        16        34

           dcut(Petal.Width, 4) [0.1,0.2] (0.2,0.4] (0.4,1.8] (1.8,2.5]
Species                                                                
setosa                                 28        12         2         0
versicolor                              0         0         8         0
virginica                               0         0         2        15
dcut(Petal.Length, breaks = 2): [1,1.6]

           dcut(Petal.Width, 4) [0.1,0.2] (0.2,0.4] (0.4,1.8] (1.8,2.5]
Species                                                                
setosa                                 26         9         1         0
versicolor                              0         0         0         0
virginica                               0         0         0         0
------------------------------------------------------------ 
dcut(Petal.Length, breaks = 2): (1.6,6.7]

           dcut(Petal.Width, 4) [0.1,0.2] (0.2,0.4] (0.4,1.8] (1.8,2.5]
Species                                                                
setosa                                  2         3         1         0
versicolor                              0         0         8         0
virginica                               0         0         2        15
#+end_example

* dby
#+BEGIN_SRC R
library(magrittr)
sTRACE %>% dby2(chf+vf~1, mean, median) %>% dhead
#+END_SRC

#+RESULTS[66b2d4af5d5a7bb11ea96d4d0101df9819e74cc2]:
#+begin_example
        no wmi status chf    age sex diabetes     time vf    wmicat mean.chf
X1944 1944 1.5      9   0 84.924   1        1 1.345000  0 (1.4,1.8]    0.522
X5783 5783 1.9      0   1 74.193   0        0 6.910000  0 (1.8,2.7]    0.522
X784   784 0.8      9   0 78.081   0        1 0.196000  0 [0.4,1.1]    0.522
X3763 3763 1.3      0   0 55.479   1        0 7.543000  0 (1.1,1.4]    0.522
X2927 2927 1.6      0   1 62.997   0        0 7.126000  0 (1.4,1.8]    0.522
X4511 4511 1.0      9   1 67.644   1        0 4.532606  0 [0.4,1.1]    0.522
      mean.vf median.chf median.vf
X1944   0.058          1         0
X5783   0.058          1         0
X784    0.058          1         0
X3763   0.058          1         0
X2927   0.058          1         0
X4511   0.058          1         0
#+end_example

#+BEGIN_SRC R
library(magrittr)
sTRACE %>% dby(chf+vf~1, mean, median,REDUCE=TRUE) 
#+END_SRC

#+RESULTS[1f8eaa03cb32b30d5b54c3910cbb531355aef819]:
:  mean median
:  0.29      0

#+BEGIN_SRC R :eval never
dby(iris, 'Length' ~ Species, mean, REGEX=T, COLUMN=T, REDUCE=T)
dby(iris, 'Length' ~ Species, mean, REGEX=T, COLUMN=T, REDUCE=T)
dby(iris, '*Length' ~ Species, mean, COLUMN=T, REDUCE=T)
dby(iris, '*Length' ~ Species, mean)
dby(iris, 'Length' ~ Species, mean, REGEX=T)
dby(iris, 'Length' ~ Species, mean, COLUMN=T, REGEX=T, REDUCE=T)
   
dby(iris, 'Length' ~ Species, mean, REGEX=T, REDUCE=1)
dby(iris, 'Length' ~ Species, mean, REGEX=T, REDUCE=1, COLUMN=T)
dby(iris, 'Length' ~ Species, mean, REGEX=T, REDUCE=1, COLUMN=T)

#+END_SRC

#+BEGIN_SRC R
lapply(list(median, mean), function(f) dscalar(sTRACE, chf+vf~sex, fun=f))
#+END_SRC

#+RESULTS[e727cd7a1e3e789e19c8941719fef4e5d47c3172]:
: [[1]]
:   sex chf vf
: 1   0   1  0
: 2   1   0  0
: 
: [[2]]
:   sex       chf         vf
: 1   0 0.6172840 0.07407407
: 2   1 0.4763314 0.05029586


#+BEGIN_SRC R
dbyr(sTRACE, wmi ~ vf+sex|age>80, mean(x^2), mean(log(x)), mean, n=length)
#+END_SRC

#+RESULTS[cb7e817a19236a46a94c2b2ebd27d49dce7be1cb]:
:   vf sex mean(x^2) mean(log(x))     mean  n
: 1  0   0  2.344286   0.33534719 1.471429 21
: 2  1   0  1.370000   0.02439508 1.100000  2
: 3  0   1  2.212162   0.33285730 1.445946 37
: 4  1   1  0.745000  -0.17833747 0.850000  2


* backmatter
  :ignore:

bibliography:mets.bib
bibliographystyle:plain
