## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)


## ----setup--------------------------------------------------------------------
#| echo: TRUE
#install.packages("dtametaTMB")
library(dtametaTMB)


## -----------------------------------------------------------------------------
#| echo: TRUE
data("anticcp")
reitsma <- fitReitsma(data=anticcp,
                      TP=TP,
                      FP=FP,
                      FN=FN,
                      TN=TN,
                      study=study)
reitsma
summary(reitsma)


## -----------------------------------------------------------------------------
#| fig.height: 8
#| fig.width: 8
#| echo: TRUE
plot(reitsma)
plot(reitsma,scale=0.01)


## -----------------------------------------------------------------------------
#| fig.height: 11
#| fig.width: 13
#| echo: TRUE
forest(reitsma)


## -----------------------------------------------------------------------------
#| echo: TRUE
as_revman(reitsma)


## -----------------------------------------------------------------------------
#| echo: TRUE
#| results: 'hide'
data("RF")
ruttergatsonis <- fitRutterGatsonis(data=RF,
                                    TP=TP,
                                    FP=FP,
                                    FN=FN,
                                    TN=TN,
                                    study=study)


## -----------------------------------------------------------------------------
#| echo: TRUE
ruttergatsonis
summary(ruttergatsonis)


## -----------------------------------------------------------------------------
#| fig.height: 8
#| fig.width: 8
#| echo: TRUE
plot(ruttergatsonis)
plot(ruttergatsonis,size="se",specrange=c(0.3,0.995),scale=0.01)


## -----------------------------------------------------------------------------
#| fig.height: 14
#| fig.width: 13
#| echo: TRUE
forest(ruttergatsonis)


## -----------------------------------------------------------------------------
#| echo: TRUE
reitsmacons <- fitReitsma(data = anticcp, 
                          TP = TP,
                          FP = FP,
                          FN = FN,
                          TN = TN,
                          study = study,
                          constrain = "sigma_AB")
summary(reitsmacons)$estimates


## -----------------------------------------------------------------------------
#| echo: TRUE
ruttergatsoniscons <- fitRutterGatsonis(data = RF, 
                                        TP = TP,
                                        FP = FP,
                                        FN = FN,
                                        TN = TN,
                                        study = study,
                                        constrain = c("sigma2_theta","shape"))
summary(ruttergatsoniscons)$estimates


## -----------------------------------------------------------------------------
anova(ruttergatsoniscons,ruttergatsonis)


## -----------------------------------------------------------------------------
#| echo: TRUE
data("diabetes")
head(diabetes)
summary(diabetes$threshold)


## -----------------------------------------------------------------------------
#| echo: TRUE
#| results: 'hide'
hoyer <- fitHoyer(data=diabetes,
                  TP=TP,
                  FP=FP,
                  FN=FN,
                  TN=TN,
                  threshold=threshold,
                  study=study,
                  dist="loglogistic",
                  testdirection="greater",
                  eval_threshold=c(5.0,5.5,6.0,6.5,7.0,7.5),
                  smallest=2,
                  largest=10)


## -----------------------------------------------------------------------------
#| echo: TRUE
hoyer
summary(hoyer)


## -----------------------------------------------------------------------------
#| echo: TRUE
#| fig.height: 8
#| fig.width: 8
plot(hoyer,
     main="Diagnostic Test Accuracy Meta-Analysis\nLoglogistic Model")
plot(hoyer,size="sampsize",scale=0.025,
     main="Diagnostic Test Accuracy Meta-Analysis\nLoglogistic Model")
plot(hoyer,size="se",scale=0.025,
     main="Diagnostic Test Accuracy Meta-Analysis\nLoglogistic Model")


## -----------------------------------------------------------------------------
#| fig.height: 32
#| fig.width: 12
#| echo: TRUE
forest(hoyer)


## -----------------------------------------------------------------------------
#| echo: TRUE
#| fig.height: 8
#| fig.width: 8
data("FENO")
head(FENO)
summary(FENO$cutoff)
# feno <- fitHoyer(data=FENO,
#                  TP=TP,
#                  FP=FP,
#                  FN=FN,
#                  TN=TN,
#                  threshold=cutoff,
#                  study=study,
#                  dist="loglogistic",
#                  testdirection="greater",
#                  eval_threshold=seq(16,40,1),
#                  smallest=2,
#                  largest=120)
#Error in `FUN()`:
#! Inconsistent diseased counts (TP+FN) within study 'Schneider 2013'.
#Rows: 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122
#Counts: 154, 155
#
# Let's check the Schneider 2013 study
check_schneider    <- FENO[110:122,]
check_schneider$n1 <- with(check_schneider,TP+FN)
check_schneider$n0 <- with(check_schneider,TN+FP)
check_schneider
# Inspection suggests that the TP and/or FN counts
# in rows 118, 119, 120 may need to be changed.
# Let's redcue the TP counts from 39 to 38.
# But other corrections are conceivable as well.
FENO[118:120,"TP"] <- 38
### Let's try again
feno <- fitHoyer(data=FENO,
                 TP=TP,
                 FP=FP,
                 FN=FN,
                 TN=TN,
                 threshold=cutoff,
                 study=study,
                 dist="loglogistic",
                 testdirection="greater",
                 eval_threshold=seq(16,40,1),
                 smallest=2,
                 largest=120)
plot(feno,size="se",scale=0.03,
     main="Diagnostic Test Accuracy Meta-Analysis\nLoglogistic Model")


## -----------------------------------------------------------------------------
#| echo: TRUE
data("anaemia")
summary(anaemia$threshold)

## -----------------------------------------------------------------------------
#| echo: TRUE
#| results: 'hide'
less <-  fitHoyer(data=anaemia,
                  TP=TP,
                  FP=FP,
                  FN=FN,
                  TN=TN,
                  threshold=threshold,
                  study=study,
                  dist="loglogistic",
                  testdirection="less",
                  eval_threshold=c(8,9,10,11,12,13),
                  smallest=5,
                  largest=17)

## -----------------------------------------------------------------------------
#| echo: TRUE
#| fig.height: 8
#| fig.width: 8
less
summary(less)
plot(less,main="Diagnostic Test Accuracy Meta-Analysis\nLoglogistic Model")


## -----------------------------------------------------------------------------
#| echo: TRUE
res <- restructure_data(
    data = diabetes,
    TP = TP,
    FP = FP,
    FN = FN,
    TN = TN,
    study = study,
    threshold = threshold,
    testdirection = "greater",
    smallest = 2,
    largest = 10)
res


## -----------------------------------------------------------------------------
#| echo: TRUE
init <- initHoyerAFT(res$restructured,
                     dist="loglogistic")
init


## -----------------------------------------------------------------------------
#| echo: TRUE
#| results: 'hide'
hoyerAFT <- fitHoyerAFT(data=res,
                        init=init,
                        eval_threshold=c(5.0,5.5,6.0,6.5,7.0,7.5))


## -----------------------------------------------------------------------------
#| echo: TRUE
hoyerAFT
summary(hoyerAFT)

