# Hiermodel: Three stages Bayesian Hierarchical Model In ganluan123/FlagAE: Flag adverse events by Bayesian methods

## Description

Functions here are to take the orginized data (output from `preprocess`) and apply the three stages Bayesian Hierarchical Model. See details for model description and difference between each function.

## Usage

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24``` ```Hier_history(aedata, n_burn, n_iter, thin, n_adapt, n_chain, alpha.gamma = 3, beta.gamma = 1, alpha.theta = 3, beta.theta = 1, mu.gamma.0.0 = 0, tau.gamma.0.0 = 0.1, alpha.gamma.0.0 = 3, beta.gamma.0.0 = 1, lambda.alpha = 0.1, lambda.beta = 0.1, mu.theta.0.0 = 0, tau.theta.0.0 = 0.1, alpha.theta.0.0 = 3, beta.theta.0.0 = 1) sum_Hier(hierraw) Hier(aedata, n_burn, n_iter, thin, n_adapt, n_chain, alpha.gamma = 3, beta.gamma = 1, alpha.theta = 3, beta.theta = 1, mu.gamma.0.0 = 0, tau.gamma.0.0 = 0.1, alpha.gamma.0.0 = 3, beta.gamma.0.0 = 1, lambda.alpha = 0.1, lambda.beta = 0.1, mu.theta.0.0 = 0, tau.theta.0.0 = 0.1, alpha.theta.0.0 = 3, beta.theta.0.0 = 1) Hiergetpi(aedata, hierraw) Hierplot(hierdata, ptnum = 10, param = "risk difference", OR_xlim = c(0, 5)) Hiertable(hierdata, ptnum = 10, param = "risk difference") Compareplot(modeldata, ptnum = 50, param = "risk difference") ```

## Arguments

 `aedata` output from function `preprocess` `n_burn` integer, number of interations without saving posterior samples `n_iter` integer, number of interations saving posterior samples with every `thin`th iteration `thin` integer, samples are saved for every `thin`th iteration `n_adapt` integer, number of adaptations `n_chain` number of MCMC chains `hierdata` utput from function `Hier` `ptnum` positive integer, number of AEs to be selected or plotted, default is 10 `param` a string, either "odds ratio" or "risk difference", indicate which summary statistic to be based on to plot the top AEs, default is "risk difference" `modeldata` output from function `Hier` or `Ising` `alpha.gamma...` alpha.gamma and other parameters are hyperparameters for prior distribution, see the reference for the meaning of each parameters `OR_ylim` a numeric vector of two elements, used to set y-axis limit for plotting based on "odds ratio"

## Details

Model:
Here the 3-stage hierarchical bayesian model was used to model the probability of AEs. It is model 1b (Bayesian Logistic Regression Model with Mixture Prior on Log-OR) in H. Amy Xia , Haijun Ma & Bradley P. Carlin (2011) Bayesian Hierarchical Modeling for Detecting SaBCIy Signals in Clinical Trials, Journal of Biopharmaceutical Statistics, 21:5, 1006-1029, DOI: 10.1080/10543406.2010.520181)
`Hier_history`:
This function takes formatted Binomial data and output Gibbs sample of the defined parametes. The output is a dataframe with each column represent one parameter and each row is the output from one sampling/one iteration. Diff, OR, gamma, and theta are the parameters recorded.
Diff: is the difference of incidence of AE between treatment and control group (treatment - control)
OR: is the odds ratio for the incidence of AE between treatment and control group (treatment over control): t(1-c)/c(1-t), where t,c are the incidences of one AE for treatment and control group, respectively.
gamma: logit(incidence of AE in control group) = gamma
theta: logit(incidence of AE in treatment group) = gamma + theta; and OR = exp(theta)
The result for Diff, OR, gamma, and theta are ordered by j and then by b. For example the result is like Diff.1.1, Diff.2.1, Diff.3.1, Diff.4.1 Diff.1.2, Diff.2.2, Diff.3.2 and so on
`sum_Hier`:
This function takes the output from `Hier_history` and return the summary statistics for each parameter recorded by `Hier_history`. The summary function is applied on each column.
`Hier`:
This function takes the same input as `Hier_history` and calculate the summary statistics for output from `Hier_history`. It outputs the summary statistics for each AE, combining with raw data, and also the Raw risk difference, Raw odds ratio calculated from raw data.
`Hiergetpi`:
This function calculates pit (incidence of AE in treatment group) and pic (incidence of AE in control group) from the output of `Hier_history` The output is used for Loss function.
`Hierplot` first selects the top `ptnum` (an integer) AE based on the selected statistic (either "odds ratio" or "risk difference"). Then it plots the mean, 2.5 color.
`Hiertable` creates a table for the detailed information for AE plotted in `Hierplot`. `Compareplot` creates a plot to compare the mean risk difference (or odds ratio meadian) from posterior distribution with raw risk difference (or row risk difference) from raw data. This function can be used for both bayesian models.

## Value

`Hier_history`
It returns a dataframe with each column represent one parameter and each row is the output from one sampling/one iteraction (like the output of `coda.samples`)
`sum_Hier`
It returns the summary statistics for each parameter recorded by `Hier_history`.
`Hier`
It returns the summary statistics for each AE, combining with raw data. The summary statistics including: summary statistics for incidence rate difference (mean, 2.5% and 97.5% percentile); summary statistics for odds ratio (mean, 2.5% and 97.5% percentile). The other columns include SoC, PT, Nt, Nc, AEt, and AEc. `Hiergetpi`:
This function calculates pit (incidence of AE in treatment group) and pic (incidence of AE in control group) from the output of `Hier_history`.

## References

H. Amy Xia , Haijun Ma & Bradley P. Carlin (2011) Bayesian Hierarchical Modeling for Detecting Safety Signals in Clinical Trials, Journal of Biopharmaceutical Statistics, 21:5, 1006-1029, DOI: 10.1080/10543406.2010.520181)

`preprocess`
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24``` ```## Not run: data(ADAE) data(ADSL) AEdata<-preprocess(adsl=ADSL, adae=ADAE) HIERRAW<-Hier_history(aedata=AEdata, n_burn=1000, n_iter=1000, thin=20, n_adapt=1000, n_chain=2) HIERRAW2<-Hier_history(aedata=AEdata, n_burn=1000, n_iter=1000, thin=20, n_adapt=1000, n_chain=1) # you can specify the hyperparameter as shown below HIERRAW3<-Hier_history(aedata=AEdata, n_burn=1000, n_iter=1000, thin=20, n_adapt=1000, n_chain=1, alpha.gamma=5, beta.gamma=1, alpha.theta=3, beta.theta=1, mu.gamma.0.0=0.1, tau.gamma.0.0=0.1, alpha.gamma.0.0=5, beta.gamma.0.0=1, lambda.alpha=0.1, lambda.beta=0.1, mu.theta.0.0=0.1, tau.theta.0.0=0.1,alpha.theta.0.0=3, beta.theta.0.0=1) HIERDATA<-Hier(aedata=AEdata, n_burn=1000, n_iter=1000, thin=20, n_adapt=1000, n_chain=2) HIERPI<-Hiergetpi(aedata=AEdata, hierraw=HIERRAW) Hierplot(HIERDATA) Hierplot(HIERDATA, ptnum=15, param="odds ratio") HIERTABLE<-Hiertable(HIERDATA) HIERTABLE2<-Hiertable(HIERDATA, ptnum=15, param="odds ratio") Compareplot(HIERDATA) # user can use a very big number(bigger than total PTs in dataset) to plot out all the PTs Compareplot(HIERDATA, ptnum=5000, param='odds ratio') ## End(Not run) ```