## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## -----------------------------------------------------------------------------
library(cccrm)
bp <- bpres[, c("ID", "SIS", "METODE", "NM")]
colnames(bp) <- c("id", "sys", "met", "rep")
bp[, c("id", "met", "rep")] <- lapply(bp[, c("id", "met", "rep")], as.factor)

## -----------------------------------------------------------------------------
head(bp)

## -----------------------------------------------------------------------------
library(TDIagree)
descriptive.plots(bp, sys, id, met, rep)

## ----fig.width = 5, fig.height = 5--------------------------------------------
descriptive.plots(bp, sys, id, met, rep, plots = "sc")

## ----fig.show='hold', fig.width = 5, fig.height = 5---------------------------
bp$met <- factor(bp$met, labels = c("Sphygmomanometer", "OMRON 711"))
descriptive.plots(bp, sys, id, met, rep, plots = "scatterplot")

descriptive.plots(bp, sys, id, met, rep, plots = "scatterplot",
                  scatterplot.xlab = "Systolyic BP from sphygmomanometer (mmHg)",
                  scatterplot.ylab = "Systolyic BP from OMRON 711 (mmHg)")

## ----fig.width = 5, fig.height = 5--------------------------------------------
descriptive.plots(bp, sys, id, met, rep, plots = "scatterplot",
                  scatterplot.xlab = "Systolyic BP from sphygmomanometer (mmHg)",
                  scatterplot.ylab = "Systolyic BP from OMRON 711 (mmHg)",
                  line.col = "#bd7cb4")

## ----fig.width = 5, fig.height = 5--------------------------------------------
descriptive.plots(bp, sys, id, met, rep, plots = "scatterplot",
                  scatterplot.xlab = "Systolyic BP from sphygmomanometer (mmHg)",
                  scatterplot.ylab = "Systolyic BP from OMRON 711 (mmHg)",
                  line.col = "#bd7cb4",
                  main = "Scatterplot",
                  xlim = c(80, 200), ylim = c(80, 200),
                  pch = 21,
                  col = "darkgrey",
                  bg = "grey")

## ----fig.width = 5, fig.height = 5--------------------------------------------
descriptive.plots(bp, sys, id, met, rep, plots = "scatterplot",
                  scatterplot.xlab = "Systolyic BP from sphygmomanometer (mmHg)",
                  scatterplot.ylab = "Systolyic BP from OMRON 711 (mmHg)",
                  xlim = c(80, 200), ylim = c(80, 200))

points(bp[bp$met == "Sphygmomanometer" & bp$rep == 1 & bp$id %in% c(24, 199, 226), "sys"], 
       bp[bp$met == "OMRON 711" & bp$rep == 1 & bp$id %in% c(24, 199, 226), "sys"], col = 3)
points(bp[bp$met == "Sphygmomanometer" & bp$rep == 1 & bp$id %in% c(24, 199, 226), "sys"], 
       bp[bp$met == "OMRON 711" & bp$rep == 2 & bp$id %in% c(24, 199, 226), "sys"], col = 3)
points(bp[bp$met == "Sphygmomanometer" & bp$rep == 2 & bp$id %in% c(24, 199, 226), "sys"], 
       bp[bp$met == "OMRON 711" & bp$rep == 1 & bp$id %in% c(24, 199, 226), "sys"], col = 3)
points(bp[bp$met == "Sphygmomanometer" & bp$rep == 2 & bp$id %in% c(24, 199, 226), "sys"], 
       bp[bp$met == "OMRON 711" & bp$rep == 2 & bp$id %in% c(24, 199, 226), "sys"], col = 3)

## -----------------------------------------------------------------------------
bp <- bp[!bp$id %in% c(24, 199, 226), ]

## ----fig.width = 5, fig.height = 5--------------------------------------------
bp$met <- factor(bp$met, labels = c("1", "2"))

descriptive.plots(bp, sys, id, met, rep, plots = "box")

## -----------------------------------------------------------------------------
bp$id <- droplevels(bp$id)

## ----fig.width = 5, fig.height = 5--------------------------------------------
descriptive.plots(bp, sys, id, met, rep, plots = "box",
                  rater.col = c("#edbd57", "#b0cbe6"))

## ----fig.width = 5, fig.height = 5--------------------------------------------
descriptive.plots(bp, sys, id, met, rep, plots = "box",
                  rater.col = c("#edbd57", "#b0cbe6"),
                  boxwex = .65, ylim = c(80, 200),
                  names = rep(c("1st rep.", "2nd rep."), 2), 
                  xlab = "", ylab = "Systolic BP (mmHg)",
                  main= "Boxplots")
text(x = 1.5, y = 45, labels = "Sphygmomanometer", xpd = TRUE, cex = 1)
text(x = 3.5, y = 45, labels = "OMRON 711", xpd = TRUE, cex = 1)

## ----fig.width = 3.17---------------------------------------------------------
descriptive.plots(bp, sys, id, met, rep, plots = "histogram.y")

## ----fig.width = 6.5, fig.height = 6------------------------------------------
par(mfrow = c(2, 2))
descriptive.plots(bp, sys, id, met, rep, plots = "histogram.y")

## ----fig.width = 6.5, fig.height = 6------------------------------------------
bp$met <- factor(bp$met, labels = c("Sphygmomanometer", "OMRON 711"))
bp$rep <- factor(bp$rep, labels = c("1st rep.", "2nd rep."))


par(mfrow = c(2, 2))
descriptive.plots(bp, sys, id, met, rep, plots = "histogram.y")

## ----fig.width = 6.5, fig.height = 6------------------------------------------
par(mfrow = c(2, 2))
descriptive.plots(bp, sys, id, met, rep, plots = "histogram.y",
                  rater.col = c("#edbd57", "#b0cbe6"),
                  histogram.y.main = "Measurements", 
                  histogram.y.xlab = "Systolic BP (mmHg)")

## ----fig.width = 6.5, fig.height = 6------------------------------------------
par(mfrow = c(2, 2))
descriptive.plots(bp, sys, id, met, rep, plots = "histogram.y",
                  rater.col = c("#edbd57", "#b0cbe6"),
                  histogram.y.xlab = "Systolic BP (mmHg)",
                  xlim = c(80, 200), ylim = c(0, 60), 
                  las = 1, breaks = 16)

## ----fig.width = 3.17---------------------------------------------------------
bp$met <- factor(bp$met, labels = c("1", "2"))
bp$rep <- factor(bp$rep, labels = c("1", "2"))

descriptive.plots(bp, sys, id, met, rep, plots = "qqplot.y")

## ----fig.width = 6.5, fig.height = 6------------------------------------------
bp$met <- factor(bp$met, labels = c("Sphygmomanometer", "OMRON 711"))
bp$rep <- factor(bp$rep, labels = c("1st rep.", "2nd rep."))


par(mfrow = c(2, 2))
descriptive.plots(bp, sys, id, met, rep, plots = "qqplot.y")

## ----fig.width = 6.5, fig.height = 6------------------------------------------
par(mfrow = c(2, 2), mar = c(4, 4, 1, 2) + 0.1, las = 1)
descriptive.plots(bp, sys, id, met, rep, plots = "qqplot.y",
                  qqplot.y.main = "", line.col = "green",
                  xlim = c(-4, 4), ylim = c(-4, 4))

## ----fig.height = 13, fig.width = 6-------------------------------------------
par(mfcol = c(4, 2), las = 1)
descriptive.plots(bp, sys, id, met, rep, plots = "histogram.y",
                  histogram.y.xlab = "Systolic BP (mmHg)",
                  xlim = c(80, 200), ylim = c(0, 100))
descriptive.plots(bp, sys, id, met, rep, plots = "qqplot.y",
                  xlim = c(-4, 4), ylim = c(-4, 4))

## ----fig.width = 5, fig.height = 5--------------------------------------------
descriptive.plots(bp, sys, id, met, rep, plots = "histogram.d")

## ----fig.width = 5, fig.height = 5--------------------------------------------
descriptive.plots(bp, sys, id, met, rep, plots = "histogram.d",
                  histogram.d.density = FALSE,
                  xlim = c(-50, 50), ylim = c(0, 0.06),
                  xlab = "Paired-differences",
                  las = 1,
                  main = "",
                  breaks = 15)

## ----fig.width = 5, fig.height = 5--------------------------------------------
descriptive.plots(bp, sys, id, met, rep, plots = "histogram.d",
                  histogram.d.density = FALSE,
                  xlim = c(-50, 50), ylim = c(0, 0.07),
                  xlab = "Paired-differences",
                  las = 1,
                  main = "",
                  breaks = 15,
                  xaxt = "n")
axis(1, at = seq(-50, 50, by = 10), cex.axis = 0.9)

## ----fig.width = 5, fig.height = 5--------------------------------------------
descriptive.plots(bp, sys, id, met, rep, plots = "histogram.d",
                  density.col = "green",
                  histogram.d.bw = 3,
                  xlim = c(-50, 50), ylim = c(0, 0.07),
                  xlab = "Paired-differences",
                  las = 1,
                  main = "",
                  breaks = 15,
                  xaxt = "n")
axis(1, at = seq(-50, 50, by = 10), cex.axis = 0.9)

## ----fig.width = 5, fig.height = 5--------------------------------------------
descriptive.plots(bp, sys, id, met, rep, plots = "qqplot.d")

## ----fig.width = 5, fig.height = 5--------------------------------------------
descriptive.plots(bp, sys, id, met, rep, plots = "qqplot.d",
                  line.col = "green",
                  xlim = c(-5, 5), ylim = c(-5, 5),
                  las = 1,
                  main = "",
                  xaxt = "n", yaxt = "n")
axis(1, at = seq(-5, 5, by = 2))
axis(2, at = seq(-5, 5, by = 2))

## ----fig.height = 3.2, fig.width = 6------------------------------------------
par(mfcol = c(1, 2), las = 1, mar = c(4, 4, 1, 1) + 0.1, cex = 0.8)
descriptive.plots(bp, sys, id, met, rep, plots = "histogram.d",
                  xlim = c(-50, 50), ylim = c(0, 0.07),
                  xlab = "Paired-differences",
                  main = "",
                  breaks = 15,
                  xaxt = "n")
axis(1, at = seq(-50, 50, by = 20))
descriptive.plots(bp, sys, id, met, rep, plots = "qqplot.d",
                  xlim = c(-5, 5), ylim = c(-5, 5),
                  main = "",
                  xaxt = "n", yaxt = "n")
axis(1, at = seq(-5, 5, by = 2))
axis(2, at = seq(-5, 5, by = 2))

