# Characterization

## Method

```{r}
#| echo: false
#| results: 'asis'
if(!is.null(params$webAPI)){
val <- paste0("\n\nThe index breakdown section shows how often each outcome concept id occurred for the patients in ",targetName," with ",outcomeName," during the time-at-risk after exposure.  We restrict to concept ids that occurs for one percent of the popualtion or more.  This gives insight into how the outcome is being observed in the data.\n\n")
cat(val)
}
```

The demographics (age/gender), prior observation and post observation will be calculated for the patients in `r targetName`, for the indications, the outcome `r outcomeName` and the comparators: `r comparatorText`.

The risk factors section shows the demographics, medication or comorbidities that are associated to having the `r outcomeName` during the time-at-risk after `r targetName` exposure.  We present the top 50 covariates that are associated to having `r outcomeName` during the time-at-risk for the patients exposed to `r targetName`.

## Results
```{r}
#| label: index_breakdown
#| echo: false
#| results: 'asis'

if(!is.null(params$webAPI)){
result <- knitr::knit_child(text = knitr::knit_expand("characterization/index_breakdown.qmd", quiet = TRUE, envir = environment()), quiet = TRUE)
cat(result, sep = '\n')
}
```

```{r}
#| label: demo
#| echo: false
#| results: 'asis'


  # TODO add tte?
  if(FALSE){
    tte <- OhdsiReportGenerator::getTimeToEvent(
      connectionHandler = connectionHandler, 
      schema = params$schema, 
      targetIds = unique(c(targetIdsOfInterest, comparatorIdsOfInterest, indicationIdsOfInterest)), 
    outcomeIds = outcomeIdsOfInterest
    )
  }
  
  targetContinuousOfInterest <- OhdsiReportGenerator::getTargetContinuousFeatures(
    connectionHandler = connectionHandler, 
    schema = params$schema, 
    targetIds = unique(c(targetIdsOfInterest, comparatorIdsOfInterest, indicationIdsOfInterest)), 
    analysisIds = c(2,8,9,10)
  )
  
  targetBinaryOfInterest <- do.call('rbind', lapply(unique(c(targetIdsOfInterest, comparatorIdsOfInterest, indicationIdsOfInterest)), function(x){
  OhdsiReportGenerator::getTargetBinaryFeatures(
    connectionHandler = connectionHandler, 
    schema = params$schema, 
    targetId = x, 
    analysisIds = c(1,2,3,4,5)
)}))
  
# run this per type - 
demoList <- list(
  `Exposure Target` = targetIdsOfInterest,
  `Outcome/s` = outcomeIdsOfInterest,
  `Indication/s` = indicationIdsOfInterest, # what about null?
  `Comparator/s` = comparatorIdsOfInterest
)

cat('\n\n### Index Demographics\n\n')
for(demoListInd in 1:length(demoList)){
  result <- knitr::knit_child(
    text = knitr::knit_expand(
      file = "characterization/demographics.qmd", 
      quiet = TRUE, 
      envir = environment()), 
    quiet = TRUE
    )
cat(result, sep = '\n')
}
```

```{r}
#| label: risk_factors
#| echo: false
#| results: 'asis'

result <- knitr::knit_child(text = knitr::knit_expand("characterization/risk_factors.qmd", quiet = TRUE, envir = environment()), quiet = TRUE)
#result <- knitr::knit_child("characterization/risk_factors.qmd", quiet = TRUE)
cat(result, sep = '\n')
```
