Define the hazard model as an equation 'h = ...', where h>0 hazard 'h' can be function of time 't'







        
#-------------------------------------------------------------------------
#  This application is governed by the CeCILL-B license. 
#  You can  use, modify and/ or redistribute this code under the terms
#  of the CeCILL license:  http://www.cecill.info/index.en.html
#
#  Marc Lavielle, Inria Saclay
#  April 29th, 2015
#-------------------------------------------------------------------------

shinyUI(fluidPage(
  #   titlePanel(
  #     list(HTML('<p style="color:#4C0B5F; font-size:24px" fontsize=14>Survival model</p>' )),
  #     windowTitle="Survival model"),
  
  navbarPage("", selected="Plot", id="nav", inverse="FALSE", 
             title="Survival model",
             tabPanel("Plot",                      
                      fluidRow(
                        br(),
                        column(3,
                               br(),
                               br(),
                               helpText("Define the hazard model as an equation 'h = ...', where h>0"),                      
                               helpText("hazard 'h' can be function of time 't'"),                      
                               textInput("hazard","",  "h = 0.2 - 0.01*t"),
                               #                       tags$textarea(id="hazard", rows=3, cols=30,  "h = 0.2 + 0.1*t"),
                               actionButton("action1", label = "Run"),
                               br(),
                               br(),
                               sliderInput("tmax", label=h5("censoring time"), value=10, min=2, max=100, step=2),
                               br(),
                               checkboxInput("checksim", label = "Simulation", value = 0),
                               conditionalPanel(condition = "input.checksim == 1",
                                                selectInput("N", label = h5("Sample size"),choices = c(10,50,100,500),selected = "50"),
                                                checkboxInput("checklevel", label = "Confidence interval", value = 0),
                                                conditionalPanel(condition = "input.checklevel == 1",
                                                                 sliderInput("level", label=h5("Level of the confidence interval (%)"), min=5, max=95, value=90, step=5))
                               ), 
                               br(),
                               br(),                     
                               br()
                        ),
                        column(8,
                               plotOutput("plot",  height="500px")
                        )
                      )
             ),
             tabPanel("Table", tableOutput("table")),
             navbarMenu("Codes",
                        tabPanel("Mlxtran", verbatimTextOutput("mlxtran")),
                        tabPanel("ui.R", pre(includeText("ui.R"))),
                        tabPanel("server.R", pre(includeText("server.R"))
                        )
             ),
             tabPanel("ReadMe", withMathJax(), includeMarkdown("readMe.Rmd")),
             tabPanel("About", includeMarkdown("../../about/about.Rmd"))
  )   
))
#-------------------------------------------------------------------------
#  This application is governed by the CeCILL-B license. 
#  You can  use, modify and/ or redistribute this code under the terms
#  of the CeCILL license:  http://www.cecill.info/index.en.html
#
#  Marc Lavielle, Inria Saclay
#  May 1st, 2015
#-------------------------------------------------------------------------

library(shinydashboard)
library(mlxR)
library(gridExtra)

shinyServer(function(input, output) {
  
  mlxtran.text1 <- ("
[LONGITUDINAL]

EQUATION:
t0  = 0
H_0 = 0
")
  mlxtran.text2 <- ("
ddt_H = h
S = exp(-H)
")
  mlxtran.text3 <- reactive({
    input$action2
    tmax <- input$tmax
    txt <- paste0("DEFINITION:
e = {type=event, maxEventNumber=1, rightCensoringTime=",tmax,", hazard=h}")
    return(txt)  
  })
  
  r <- reactive({ 
    input$action1
    input$action2
    tmax <- input$tmax
    h.text <- isolate({input$hazard})
    out1 <- list(name=c('h','S'), time=seq(0,tmax,length=100))
    mlxtran.text <- paste0(mlxtran.text1,h.text,mlxtran.text2)
    if (input$checksim==0){
      write(mlxtran.text,"temp_hazard.txt")
      res <- simulx(model="temp_hazard.txt", output=out1)      
    }else{
      mlxtran.text <- paste0(mlxtran.text,"\n",mlxtran.text3())
      write(mlxtran.text,"temp_hazard.txt")
      out2 <- list(name='e',time=0)
      g <- list(size=as.numeric(input$N))
      res <- simulx(model="temp_hazard.txt", output=list(out1, out2), group=g)
    }
    mlxtran.text <- includeText("temp_hazard.txt")
    p <- list(res, mlxtran.text)
    return(p)
  })
  
  output$plot <- renderPlot({
    res <- r()[[1]]
    if (min(res$h$h)>=0) {
    pl1=ggplotmlx(data=res$h, aes(x=time, y=h))  + geom_line(size=0.75) 
    if (input$checksim==0){
      pl2=ggplotmlx(data=res$S)  + geom_line(aes(x=time, y=S),size=0.75) 
    }else{
      if (input$checklevel==0){
        pl2 <- kmplotmlx(res$e) + geom_line(data=res$S, aes(x=time, y=S),size=0.75)
      }else{
        pl2 <- kmplotmlx(res$e, level=input$level/100) + geom_line(data=res$S, aes(x=time, y=S),size=0.75)
      }
    }
    grid.arrange(pl1,pl2,ncol=2)
    }else{
      plot(ggplot(data=res$h, aes(x=time, y=h))  + geom_line(size=0.75) + ggtitle("h should be >=0 !!!"))
    }
  })
  
  
  output$table <- renderTable({     
    res <- r()[[1]]
    S <- res$S$S
    r.table <- cbind(res$h,S)
    return(r.table)
  })
  output$mlxtran <- renderText(r()[[2]])
  
})


Marc Lavielle,
February 10, 2015


  1. Define the hazard function \(h\) in the edit area. \(h\) can be function of time \(t\) but \(h\) should be positive.

    Use for instance, h = 3/20*(t/20)^2 for a Weibull model with parameters \(k=3\) and \(\lambda=20\)

  2. Click on Run to display the hazard function \(h\) and the survival function \(S\) defined as \[S(t) = P(T>t) = \exp(- \int_0^t h(u) du)\]

  3. Check the checkbox Simulation to simulate events and display the Kaplan Meir plot,

  4. Modify the Sample size and/or display a confidence interval for the Kaplan Meir plot by checking the checkbox Confidence interval,

  5. Modify the right censoring time with the slider


This application is governed by the CeCILL-B license.
You can use, modify and/or redistribute these codes under the terms of the CeCILL license.


This Shiny application requires the mlxR package.


Marc Lavielle
Inria Saclay, Popix team
April 29th, 2015