---
title: 'model4you reproducing: Model-Based Recursive Partitioning for Subgroup Analyses'
author: "Heidi Seibold"
date: "December 22, 2017"
output:
  pdf_document: default
  html_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, fig.width = 13)
```

```{r}
library("model4you")
library("survival")
load("data/ALSFRSdata.rda")
load("data/ALSsurvdata.rda")
```

## Base models
```{r}
bm_alsfrs <- glm(ALSFRS.halfYearAfter ~ Riluzole + offset(log(ALSFRS.Start)), 
                 data = ALSFRSdata, family = gaussian(link = "log"))
summary(bm_alsfrs)
lm_plot(bm_alsfrs)


bm_alssurv_wb <- survreg(Surv(survival.time, cens) ~ Riluzole, 
                         data = ALSsurvdata, dist = "weibull")
summary(bm_alssurv_wb)
survreg_plot(bm_alssurv_wb, yrange = c(0, 1000))

bm_alssurv_cox <- coxph(Surv(survival.time, cens) ~ Riluzole, 
                        data = ALSsurvdata)
summary(bm_alssurv_cox)
coxph_plot(bm_alssurv_cox, yrange = c(0, 1000))
```

## Trees
```{r}
set.seed(123)

(tr_alsfrs <- pmtree(bm_alsfrs, control = ctree_control(maxdepth = 2)))
plot(tr_alsfrs, terminal_panel = node_pmterminal(tr_alsfrs, plotfun = lm_plot, 
                                                 confint = FALSE))


(tr_alssurv_wb <- pmtree(bm_alssurv_wb, control = ctree_control(maxdepth = 2)))
plot(tr_alssurv_wb, terminal_panel = node_pmterminal(tr_alssurv_wb, plotfun = survreg_plot, 
                                                     confint = TRUE, yrange = c(0, 1000)))


(tr_alssurv_cox <- pmtree(bm_alssurv_cox, control = ctree_control(maxdepth = 2)))
plot(tr_alssurv_cox, terminal_panel = node_pmterminal(tr_alssurv_cox, plotfun = coxph_plot, 
                                                     confint = TRUE, yrange = c(0, 1000)))
```



