R/rbaes-internal.R

ppi <- function(data, fit, fun = identity, form = NULL, prob = 0.9, n = 1) {
  pp <- do.call(rbind, lapply(
    1:n,
    function(i) {
      rstanarm::posterior_predict(
        fit, data,
        fun = fun,
        re.form = form
      )
    }))
  pp_int <- rstantools::posterior_interval(pp, prob = prob)
  yhat <- data.table(apply(pp, 2, median), pp_int)
  names(yhat) <- c("c", "l", "r")
  return(cbind(data, yhat))
}

n_post_draws <- function(stanfit) {
  pss <- stanfit$stanfit@sim$n_save
  sum(pss - stanfit$stanfit@sim$warmup2)
}

cdata_list <- function() {
  library(data.table)
  stanreg <- example_stanreg()
  cdata <- list(
    c1 = contrast_data(
      stanreg, TRUE,
      margin_ignore = "floor",
      subset_expression = . ~ log_uranium < -0.5
    ),
    c2 = contrast_data(stanreg, TRUE,
      margin_ignore = "floor",
      subset_expression = . ~
      log_uranium >= -0.5 &
        log_uranium <= 0.5),
    c3 = contrast_data(stanreg, TRUE,
      margin_ignore = "floor",
      subset_expression = . ~ log_uranium > 0.5))

  assign("stanreg", stanreg, .GlobalEnv)
  assign("cdata", cdata, .GlobalEnv)
  return(NULL)
}


# comparisons functions -----------------------------------------------------------------------

dec2pct <- function(x, digits = 1) {
  sprintf(paste0("%-", digits + 4, ".", digits, "f%%"), x * 100)
}

loo_ci <- function(x, se, N, p = 0.95) {
  if (N <= 1) {
    stop("n must be a sample size greater than 1")
  }

  if (p < 0.5 | p >= 1) {
    stop("p must be greater or equal to than 0.5 and less than 1")
  }

  one_mp <- (1 - p) / 2
  one_pp <- (1 - one_mp)
  q_scaled <- qt(one_pp, N - 1) * se
  ci <- c(x - q_scaled, x + q_scaled)
  dec2pct(one_mp)
  names(ci) <- c(dec2pct(one_mp), dec2pct(one_pp))
  return(ci)
}

get_contrast_rows <- function(form, data) {
  if (nchar(deparse(form[2])) != 3) {
    stop("improper contrast formula specification")
  }

  data[, eval(form[-1])][[1]]
}

apply_contrast2 <- function(pp_list, ccoef) {
  n_pp <- length(pp_list)

  if (length(ccoef) != n_pp) {
    stop("length coef must match length pp_list")
  }

  for (pp in seq_len(n_pp)) {
    pp_list[[pp]] <- pp_list[[pp]] * ccoef[pp]
  }

  as.matrix(apply(do.call(cbind, pp_list), 1, sum))
}

pp_cdata <- function(stanreg, cdata, row_stat = median, ...) {
  if (!"stanreg" %in% class(stanreg)) {
    stop('"stanreg" arg must be a stanreg object')
  }

  if (!is.list(cdata)) {
    stop('"cdata" must be a list of datasets to compare')
  }

  n_datasets <- length(cdata)

  if (n_datasets < 2) {
    stop("need at least 2 contrast data sets")
  }

  cdata <- lapply(
    cdata,
    function(d) {
      if (!is.data.table(d)) {
        d <- as.data.table(d)
      }
      return(d)
    })

  cnames <- names(cdata)

  if (is.null(cnames)) {
    cnames <- paste("contrast", 1:n_datasets)
  }

  no_names <- !nzchar(cnames)

  if (any(no_names)) {
    cnames[no_names] <- paste("contrast", which(no_names))
  }

  # combine all the data to use the same draws
  cdata_bind <- rbindlist(cdata,
    use.names = TRUE,
    fill = TRUE, idcol = "__cont_idx")

  posterior <- rstanarm::posterior_predict(stanreg, newdata = cdata_bind, ...)

  # get indices of which cols to pull from posterior matrix
  cont_grps <- dtbl2list(cdata_bind[, .I, by = "__cont_idx"], `__cont_idx`)

  # mean of rows at cols x
  pp <- lapply(
    cont_grps,
    function(i) {
      cidx <- i$I
      y <- posterior[, cidx]
      if (!is.null(dim(y))) {
        y <- apply(y, 1, row_stat)
      }

      matrix(y)
    })

  return(pp)
}

set_col_order <- function(x, first_names) {
  data.table::setcolorder(x, c(first_names, names(x)[!names(x) %chin%
    first_names]))
  invisible(x)
}

get_unit_attr <- function(x) {
  adj <- attr(x, "adj")
  m <- attr(x, "center")
  s <- attr(x, "scale")

  if (any(is.null(c(adj, m, s)))) {
    stop("could not find attributes from a unit_scale object")
  }

  nlist(adj, m, s)
}

.Random.seed <- c(
  403L,
  10L,
  2099063101L,
  -653950537L,
  2020501086L,
  -709496936L,
  1858182907L,
  1540085401L,
  -1047576792L,
  1094809046L,
  -521274799L,
  385462531L,
  130968722L,
  -1329438172L,
  -316908313L,
  1423905085L,
  -1363360332L,
  -693700070L,
  1834893669L,
  -1640116225L,
  867182214L,
  339615248L,
  1951818931L,
  -2140709039L,
  1400378112L,
  -179033634L,
  -1676938711L,
  -1754422821L,
  -474132758L,
  -1096172404L,
  -708533297L,
  644324229L,
  117577788L,
  1133500690L,
  93130253L,
  703003975L,
  -1442752722L,
  -444552568L,
  -2025497717L,
  1262893929L,
  -1970129608L,
  1220761574L,
  754560897L,
  -718059757L,
  1050857666L,
  1604316020L,
  55727991L,
  -269815699L,
  -589410556L,
  -777662934L,
  -902716523L,
  -165959633L,
  -947740362L,
  -2071363232L,
  -316366941L,
  469412033L,
  -1131145040L,
  -155228210L,
  2080095033L,
  1275073355L,
  604178682L,
  1520652796L,
  1384624703L,
  -330229995L,
  -1267866580L,
  161357058L,
  1169880541L,
  1923269847L,
  -1316278338L,
  616155640L,
  646639067L,
  2077475897L,
  1387111816L,
  450474294L,
  1383205745L,
  536373667L,
  -1965845262L,
  -872806332L,
  -374723833L,
  1753328349L,
  1999254484L,
  2105209594L,
  685155909L,
  -1879834977L,
  -1791163354L,
  240709808L,
  66677075L,
  -1921452943L,
  538454432L,
  1823204670L,
  -1987730359L,
  884814075L,
  1113979082L,
  186998316L,
  138289711L,
  1724521189L,
  -1487552612L,
  -1107584526L,
  -441994131L,
  -1363470937L,
  -203505714L,
  -1782194904L,
  -1271152853L,
  425567561L,
  -355612072L,
  -213974266L,
  1286745825L,
  587295347L,
  -835890462L,
  1026002516L,
  -151185577L,
  -2131760307L,
  -912318748L,
  1087102666L,
  1188601653L,
  -6482545L,
  2069500566L,
  854043200L,
  1158281987L,
  -965407967L,
  480504848L,
  -1846780946L,
  -1038660711L,
  1690637355L,
  -793341670L,
  1938007004L,
  -760609313L,
  1556836789L,
  1257742156L,
  2057909666L,
  -2003823363L,
  1464367607L,
  1545221918L,
  -140883496L,
  1898718011L,
  -1752808231L,
  1172899944L,
  704175510L,
  581289233L,
  1510876867L,
  -856381742L,
  245014372L,
  1936532135L,
  1966083069L,
  -861438476L,
  -1013261606L,
  -1785144283L,
  1579897151L,
  -2140059962L,
  343788368L,
  -607997069L,
  -1112955503L,
  -634853440L,
  -2025614946L,
  -380937367L,
  359157659L,
  -901892438L,
  -1500675892L,
  -1861942897L,
  429086149L,
  504822140L,
  -1194210862L,
  1486402893L,
  -2120570873L,
  -1268840466L,
  -1918521656L,
  -403586613L,
  439694377L,
  895689336L,
  -746451034L,
  -1602549311L,
  5226067L,
  1744295170L,
  -123677644L,
  1299981751L,
  -437990995L,
  441256644L,
  -1592553750L,
  -783998635L,
  -1974216977L,
  1264411894L,
  -1962298336L,
  -1176132125L,
  1191188097L,
  -1745133328L,
  -2099667058L,
  -1948356359L,
  1323399819L,
  -728409414L,
  -1174825540L,
  -89293057L,
  819393109L,
  1887061356L,
  -1750790718L,
  398388765L,
  -709986665L,
  -1474510850L,
  1227008952L,
  729778075L,
  -2107068935L,
  177958216L,
  1174644854L,
  1352160689L,
  -625517597L,
  441870514L,
  1098384644L,
  1491338823L,
  -1959767779L,
  717119380L,
  -1234091206L,
  -193622139L,
  -2002314145L,
  -868264218L,
  -1716494992L,
  -393812333L,
  1298906929L,
  -1015380512L,
  868999806L,
  1184616713L,
  -192384197L,
  -508409078L,
  -1612414228L,
  -936837265L,
  191963301L,
  714443356L,
  -1260901838L,
  982941997L,
  -315947033L,
  -832958706L,
  -1561089708L,
  -1098823918L,
  -882334304L,
  -918975796L,
  -1792063904L,
  1440395202L,
  2013610792L,
  2068159916L,
  668143932L,
  1494765426L,
  -1366056656L,
  -1181728988L,
  1991873848L,
  2043706074L,
  2114934672L,
  -130305284L,
  -1378091116L,
  -202966974L,
  -123360000L,
  1828424764L,
  -1114596016L,
  -129865566L,
  -1679259832L,
  -2145626356L,
  492010204L,
  -735313070L,
  1777132160L,
  -1624307628L,
  1747573736L,
  1716033274L,
  -925439664L,
  1021698860L,
  -70879372L,
  97162130L,
  587429472L,
  -689119348L,
  2055406432L,
  686368738L,
  128806824L,
  2034203852L,
  -172446212L,
  -1098150990L,
  -710186512L,
  1037855172L,
  -96964264L,
  -395876998L,
  1474261424L,
  -1377054788L,
  1360588884L,
  -1496169534L,
  1075045408L,
  -1945695044L,
  287042128L,
  2082607074L,
  897660520L,
  -260437428L,
  -1025949444L,
  1053356018L,
  -510952192L,
  2028279668L,
  -1041473528L,
  -1395288646L,
  168549840L,
  805307372L,
  -693330028L,
  885517266L,
  1508734816L,
  991991116L,
  -1950728928L,
  905483138L,
  -1472091416L,
  24147180L,
  -1063044548L,
  363584370L,
  607185520L,
  -1000962012L,
  1621440312L,
  239926426L,
  -130796848L,
  879419580L,
  -724818284L,
  -1006877054L,
  557407424L,
  755142972L,
  -1659772784L,
  -404202974L,
  -1541908984L,
  1856327948L,
  160678556L,
  807886354L,
  -312939136L,
  1289154068L,
  -1082628568L,
  709596474L,
  1504741328L,
  -331169172L,
  169633588L,
  -1551887726L,
  872511840L,
  -868564660L,
  1328406496L,
  705662882L,
  1545615272L,
  -1081689204L,
  -1038395716L,
  1329585010L,
  1494493424L,
  1390336068L,
  99425624L,
  -1292549062L,
  2032739184L,
  1757717564L,
  -951461100L,
  -2044360254L,
  -683626272L,
  267560828L,
  -1799164208L,
  -1620293982L,
  -367074264L,
  -163837684L,
  334898108L,
  2021977394L,
  976068544L,
  2021216820L,
  1132907464L,
  45696186L,
  -255168624L,
  525256172L,
  1134936148L,
  183602834L,
  -1275938016L,
  -1508639284L,
  -1789219872L,
  -915811646L,
  -629921240L,
  421584556L,
  -470551876L,
  34333170L,
  1527966000L,
  1493998756L,
  -1351331144L,
  66169690L,
  460516752L,
  -1042095748L,
  1550146836L,
  1744554306L,
  624722688L,
  1809275836L,
  924060496L,
  -812179166L,
  1855982920L,
  -906635252L,
  1661253724L,
  1982059730L,
  1719764992L,
  -1153070252L,
  1164816872L,
  1328621562L,
  -717232432L,
  -1836837588L,
  -1637295884L,
  -612935790L,
  1265798240L,
  -427782900L,
  766240480L,
  1492823394L,
  -2009464152L,
  -1707955636L,
  431490940L,
  1518029618L,
  -591393168L,
  -397517500L,
  -518378536L,
  -840245126L,
  -1240738256L,
  -161097028L,
  672298836L,
  -131040702L,
  -2078876768L,
  -1072736068L,
  1002861008L,
  1573668194L,
  1731683304L,
  -508229684L,
  1797621500L,
  1201811442L,
  1713798272L,
  -1690343180L,
  1389933704L,
  1267250490L,
  -218356912L,
  -1098240916L,
  -1885892460L,
  -129144494L,
  1448509152L,
  1218429132L,
  -589244256L,
  -1261004414L,
  -521758488L,
  686557420L,
  -399144900L,
  -1611702158L,
  -1117665168L,
  488038948L,
  -1941503432L,
  -1803806182L,
  -916946736L,
  -901478852L,
  443231380L,
  -941964158L,
  1905330112L,
  -1398375108L,
  1932127888L,
  283444258L,
  2142040072L,
  458546316L,
  -2093297636L,
  -146975598L,
  705145984L,
  -1450753004L,
  886612776L,
  864064570L,
  -406483760L,
  -163081108L,
  -680917324L,
  -393326574L,
  1434542048L,
  -993189264L,
  1069795353L,
  -1276132853L,
  1550682364L,
  -1687177942L,
  -1519023265L,
  167697129L,
  -985349634L,
  1464864236L,
  -1638638275L,
  -178543353L,
  184657136L,
  760944782L,
  -1014271301L,
  1025788573L,
  1983901258L,
  -467676056L,
  513887313L,
  -738977981L,
  -1529236924L,
  -882204430L,
  1483492551L,
  -708471151L,
  751609878L,
  928584308L,
  392881413L,
  138697551L,
  964490568L,
  1157261542L,
  -403948013L,
  -1617542795L,
  -359082670L,
  -1538067040L,
  -1951947639L,
  1223967931L,
  2055714572L,
  299081370L,
  -730881009L,
  956719193L,
  -1903440658L,
  -869300356L,
  1379316717L,
  -681165673L,
  -436320416L,
  -1919885506L,
  540737547L,
  1386743949L,
  535974330L,
  468058104L,
  1237826465L,
  452113747L,
  1544268916L,
  741506562L,
  -1911598953L,
  1605771873L,
  1197095590L,
  -939547996L,
  -1673926827L,
  -1941689345L,
  -325109096L,
  -233136202L,
  307778627L,
  399166853L,
  -657730014L,
  -1140339440L,
  -584409927L,
  53797227L,
  -1060204452L,
  -730817142L,
  -1931122305L,
  -407685943L,
  -318142114L,
  -1699468788L,
  -1395061283L,
  999871719L,
  2116088080L,
  1175523374L,
  -1676597541L,
  -1403330627L,
  -2127693526L,
  -1537151544L,
  385512177L,
  1211161699L,
  -1395824540L,
  -490167406L,
  310585447L,
  -1152944079L,
  -1788606090L,
  1287059092L,
  -547816283L,
  -1763090001L,
  -2002453912L,
  -2058501562L,
  -906008589L,
  1132599125L,
  689113394L,
  1886559488L,
  1565416937L,
  16509019L,
  577977004L,
  343352506L,
  -1291852177L,
  1283355641L,
  265017998L,
  -394945700L,
  -1086826547L,
  2058393399L,
  -1549285952L,
  1407052446L,
  309101419L,
  1295276781L,
  -2052567206L,
  -1342798824L,
  657686913L,
  -271124685L,
  -1356190380L,
  -1921538206L,
  318307319L,
  -785415103L,
  669790406L,
  243760644L,
  -758236747L,
  901170847L,
  -1656846600L,
  876440278L,
  -767721629L,
  -479508699L,
  -912933694L,
  879671728L,
  -285470247L,
  -1546346677L,
  2111100348L,
  -151583254L,
  1100317215L,
  -368170839L,
  -2038570946L,
  -621178580L,
  -856466563L,
  1025257927L,
  -740596560L,
  -1505791282L,
  -1195513861L,
  1979576413L,
  1912674186L,
  1262852008L,
  -994403055L,
  2003772419L,
  -509276412L,
  209560370L,
  1326793095L,
  2140776401L,
  864880982L,
  1416881972L,
  -1224947131L,
  1968780943L,
  1182916616L,
  1071203366L,
  -1089741613L,
  -2111357387L,
  289600914L,
  -1614542496L,
  769896649L,
  1666892795L,
  1013294028L,
  2042977882L,
  -180927665L,
  -1360274919L,
  361142318L,
  334353980L,
  -1107464333L)
iamamutt/rbaes documentation built on May 18, 2019, 1:27 a.m.