---
output: html_document
---

```{r, echo=FALSE, results='asis'}
  
sql <- "select dm.cdm_source_abbreviation as DATABASE_NAME, 
a.PROBAST_ID, a.result_value 
from @result_schema.@my_table_appenddiagnostic_summary as a 
inner join @result_schema.@my_table_appenddiagnostics as b
on a.DIAGNOSTIC_ID = b.DIAGNOSTIC_ID
inner join @result_schema.@my_table_appenddatabase_details as c
on c.database_id = b.database_id
inner join @result_schema.@database_table_appenddatabase_meta_data as dm
on c.database_meta_data_id = dm.database_id
where b.model_design_id = @model_design_id
;"  
  
  res <- connectionHandler$queryDb(
    sql = sql, 
    result_schema = params$resultSchema,
    my_table_append = params$myTableAppend,
    database_table_append = params$databaseTableAppend,
    model_design_id = modelDesignId
  )
  
  if(nrow(res) >0 ){
  # pivot to get databases as columns 
  res <- tidyr::pivot_wider(
    data = res, 
    id_cols = 'probastId', 
    names_from = 'databaseName', 
    values_from = 'resultValue'
    )
  } else{
    
    res <- data.frame(
      probastId = c('1.1', '1.2.2', '1.2.4', '2.1', '2.2', '2.3','3.1','3.2',
                    '3.3', '3.4', '3.5', '3.6', '4.1'),
      none = c('Pass', rep('No diagnostics run', 2), 'Pass', rep('No diagnostics run', 5),
               'Pass', rep('No diagnostics run', 3) )
    )
    
  }


  probastDefs <- data.frame(
    rbind(
     c('1.1','Participants', 'Were appropriate data sources used, e.g. cohort, RCT or nested case-control study data?', 'PatientLevelPrediction uses a cohort design'),
     
     c('1.2.2','Participants','Were all inclusions and exclusions of participants appropriate?', "Check the demographic differences between inclusion criteria in popualtion settings and no additional inclusions (1 = similar, 0 = disimilar)"),
     
     c('1.2.4','Participants','Were all inclusions and exclusions of participants appropriate?', "Check the demographic differences between inclusion criteria in popualtion settings and no additional inclusions (1 = similar, 0 = disimilar)"),
     
     c('2.1','Predictors','Were predictors defined and assessed in a similar way for all participants?', "PatientLevelPrediction uses standardized feature extraction to consistently engineer the predictors"),
     
      c('2.2','Predictors','Were predictor assessments made without knowledge of outcome data?', "Rule check is the last date used to engineer predictors before the time-at-risk start? (if it is then pass)"),
     
   c('2.3','Predictors','Are all predictors available at the time the model is intended to be used?', "This fails if the last date used to engineer predictors is after index but this requires the user to think about their data and design to determine whether this passes"),

    c('3.1','Outcome','Was the outcome determined appropriately?', "This needs to be check via cohort diagnostic of the outcome cohort"),  
   
  c('3.2','Outcome','Was a pre-specified or standard outcome definition used?', "This passes if the outcome was from the OHDSI phenotype library (needs to be manually confirmed)"),

  c('3.3','Outcome','Were predictors excluded from the outcome definition?', "Check Kaplan Meier plots to see whether outcomes occur close to index (which may mean predictors and outcome overlap) or not"),
   
  c('3.4','Outcome','Was the outcome defined and determined in a similar way for all participants?', "PatientLevelPrediction determines the outcome based on an outcome phenotype, so the same logic is used for all patients."),   
  
  c('3.5','Outcome','Was the outcome determined without knowledge of predictor information?', "PatientLevelPrediction framework requires defining the outcome independently of the covariates - manually confirm ... "),
  
   c('3.6','Outcome','Was the time interval between predictor assessment and outcome determination appropriate?', "Rule: does the time-at-risk start on or after target index?  If so, pass."), 
  
   c('4.1','Design','Were there a reasonable number of participants with the outcome?', "Rule: If less than 100 fail.")
     
     
    )
    
  )
  
  names(probastDefs) <- c('probastId', 'probast category', 'consideration', 'explanation')
  row.names(probastDefs) <- NULL
  
  res <- merge(probastDefs, res, by = 'probastId', all.x = TRUE)

```
  
## Diagnostic

The PatientLevelPrediction objective assessment of PROBAST risk of bias:

```{r, echo=FALSE, result = 'asis'}
  knitr::kable(x = res, caption = 'Diagnostic results')
```

