aboutsummaryrefslogtreecommitdiff
path: root/apps/rappor-sim/server.R
blob: f4a847b00ef4b5c902afde6f2386fd9251e1a34a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
library(shiny)
source("../../analysis/R/decode.R")
source("../../analysis/R/simulation.R")
source("../../analysis/R/encode.R")

Plot <- function(x, color = "grey") {
  n <- nrow(x)
  if (n < 16) {
    par(mfrow = c(n, 1), mai = c(0, .5, .5, 0))
  } else if (n < 64) {
    par(mfrow = c(n / 2, 2), mai = c(0, .5, .5, 0))
  } else {
    par(mfrow = c(n / 4, 4), mai = c(0, .5, .5, 0))
  }
  for (i in 1:nrow(x)) {
    barplot(x[i, ], main = paste0("Cohort ", i), col = color, border = color)
  }
}

shinyServer(function(input, output) {
  # Example state global variable.
  es <- list()

  # Example buttons states.
  ebs <- rep(0, 3)

  Params <- reactive({
    list(k = as.numeric(input$size),
         h = as.numeric(input$hashes),
         m = as.numeric(input$instances),
         p = as.numeric(input$p),
         q = as.numeric(input$q),
         f = as.numeric(input$f))
  })

  PopParams <- reactive({
    list(as.numeric(input$nstrs),
      as.numeric(input$nonzero),
      input$decay,
      as.numeric(input$expo),
      as.numeric(input$background)
      )
  })

  DecodingParams <- reactive({
    list(as.numeric(input$alpha),
         input$correction)
  })

  Sample <- reactive({
    input$sample
    N <- input$N
    params <- Params()
    pop_params <- PopParams()
    decoding_params <- DecodingParams()
    prop_missing <- input$missing
    fit <- GenerateSamples(N, params, pop_params,
                    alpha = decoding_params[[1]],
                    correction = decoding_params[[2]],
                    prop_missing = prop_missing)
    fit
  })

  # Results summary.
  output$pr <- renderTable({
    Sample()$summary
  },
                           include.rownames = FALSE, include.colnames = FALSE)

  # Results table.
  output$tab <- renderDataTable({
     Sample()$fit
   },
                                options = list(iDisplayLength = 100))

  # Epsilon.
  output$epsilon <- renderTable({
    Sample()$privacy
  },
                                include.rownames = FALSE, include.colnames = FALSE, digits = 4)

  # True distribution.
  output$probs <- renderPlot({
    samp <- Sample()
    probs <- samp$probs
    detected <- match(samp$fit[, 1], samp$strs)
    detection_frequency <- samp$privacy[7, 2]
    PlotPopulation(probs, detected, detection_frequency)
  })

  # True bits patterns.
  output$truth <- renderPlot({
    truth <- Sample()$truth
    Plot(truth[, -1, drop = FALSE], color = "darkblue")
  })

  # Lasso plot.
  output$lasso <- renderPlot({
    fit <- Sample()$lasso
    if (!is.null(fit)) {
      plot(fit)
    }
  })

  output$resid <- renderPlot({
    resid <- Sample()$residual
    params <- Params()
    plot(resid, xlab = "Bloom filter bits", ylab = "Residuals")
    abline(h = c(-1.96, 1.96), lty = 2, col = 2)
    sq <- qnorm(.025 / length(resid))
    abline(h = c(sq, -sq), lty = 2, col = 3, lwd = 2)
    abline(h = c(-3, 3), lty = 2, col = 4, lwd = 2)
    abline(v = params$k * (0:params$m), lty = 2, col = "blue")
    legend("topright", legend = paste0("SD = ", round(sd(resid), 2)), bty = "n")
  })

  # Estimated bits patterns.
  output$ests <- renderPlot({
    ests <- Sample()$ests
    Plot(ests, color = "darkred")
  })

  # Estimated vs truth.
  output$ests_truth <- renderPlot({
    plot(unlist(Sample()$ests), unlist(Sample()$truth[, -1]),
         xlab = "Estimates", ylab = "Truth", pch = 19)
    abline(0, 1, lwd = 4, col = "darkred")
  })

  output$example <- renderPlot({
    params <- Params()
    strs <- Sample()$strs
    map <- Sample()$map
    samp <- Sample()

    # First run on app start.
    value <- sample(strs, 1)
    res <- Encode(value, map, strs, params, N = input$N)

    if (input$new_user > ebs[1]) {
      res <- Encode(es$value, map, strs, params, N = input$N)
      ebs[1] <<- input$new_user
    } else if (input$new_value > ebs[2]) {
      res <- Encode(value, map, strs, params, cohort = es$cohort, id = es$id,
                    N = input$N)
      ebs[2] <<- input$new_value
    } else if (input$new_report > ebs[3]) {
      res <- Encode(es$value, map, strs, params, B = es$B,
                    BP = es$BP, cohort = es$cohort, id = es$id, N = input$N)
      ebs[3] <<- input$new_report
    }
    es <<- res
    ExamplePlot(res, params$k, c(ebs, input$new_user, input$new_value, input$new_report))
  })

})