Spaces:
Sleeping
Sleeping
Update app.R
Browse files
app.R
CHANGED
@@ -1,15 +1,10 @@
|
|
1 |
# -----------------------------
|
2 |
# R Shiny App (K=3)
|
3 |
# "largest-group-wins" scenario
|
4 |
-
|
5 |
-
# CHANGES:
|
6 |
-
# 1. alpha-tilde3 = 1 - (tilde1 + tilde2).
|
7 |
-
# 2. Checks that alpha0 > 0, all tilde-params > 0, and they sum to 1.
|
8 |
-
# 3. Immediate computation on load (no action button).
|
9 |
-
# 4. nsim up to 10k.
|
10 |
-
# -----------------------------
|
11 |
|
12 |
library(shiny)
|
|
|
13 |
|
14 |
# Helper: sample Dirichlet( alpha1, alpha2, alpha3 ) via Gamma shape draws.
|
15 |
rdirichlet_3 <- function(n, alpha_vec) {
|
@@ -19,26 +14,34 @@ rdirichlet_3 <- function(n, alpha_vec) {
|
|
19 |
|
20 |
# -----------------------------
|
21 |
ui <- fluidPage(
|
22 |
-
titlePanel("Representation Index under Largest-
|
23 |
|
24 |
sidebarLayout(
|
25 |
sidebarPanel(
|
26 |
-
|
27 |
-
|
|
|
28 |
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
helpText("Note: alpha-tilde3 is automatically 1 - (tilde1 + tilde2)."),
|
33 |
-
helpText("All tilde-params must be positive and sum
|
34 |
|
35 |
-
|
36 |
-
|
|
|
37 |
),
|
38 |
|
39 |
mainPanel(
|
40 |
verbatimTextOutput("resultsText"),
|
41 |
-
plotOutput("plotPi")
|
|
|
42 |
)
|
43 |
)
|
44 |
)
|
@@ -46,6 +49,10 @@ ui <- fluidPage(
|
|
46 |
# -----------------------------
|
47 |
server <- function(input, output) {
|
48 |
|
|
|
|
|
|
|
|
|
49 |
dataSim <- reactive({
|
50 |
# Read user inputs
|
51 |
alpha0 <- input$alpha0
|
@@ -55,7 +62,7 @@ server <- function(input, output) {
|
|
55 |
# Compute alpha-tilde3
|
56 |
at3 <- 1 - (at1 + at2)
|
57 |
|
58 |
-
#
|
59 |
validate(
|
60 |
need(alpha0 > 0,
|
61 |
"alpha0 must be > 0"),
|
@@ -75,20 +82,19 @@ server <- function(input, output) {
|
|
75 |
# Summation check (should be alpha0 if at1+at2+at3=1)
|
76 |
alpha0_check <- sum(alpha_vec)
|
77 |
|
78 |
-
# Number of MC draws
|
79 |
-
nsim <- input$nsim
|
80 |
-
|
81 |
# Sample from Dirichlet
|
82 |
-
X <- rdirichlet_3(
|
83 |
|
84 |
# Identify which group is largest
|
85 |
largest_index <- apply(X, 1, which.max)
|
86 |
-
pi_hat <- as.numeric(table(factor(largest_index, levels = c(1,2,3)))) /
|
87 |
-
names(pi_hat) <- c("
|
88 |
|
89 |
# Population fraction
|
90 |
popFrac <- alpha_vec / alpha0_check
|
91 |
-
names(popFrac) <- c("
|
|
|
|
|
92 |
|
93 |
# Representation index
|
94 |
sq_diff <- (popFrac - pi_hat)^2
|
@@ -104,7 +110,7 @@ server <- function(input, output) {
|
|
104 |
)
|
105 |
})
|
106 |
|
107 |
-
# Display text results
|
108 |
output$resultsText <- renderPrint({
|
109 |
res <- dataSim()
|
110 |
|
@@ -118,17 +124,17 @@ server <- function(input, output) {
|
|
118 |
round(res$alpha0_check, 4),
|
119 |
"(should match alpha0 if they sum to 1)\n\n")
|
120 |
|
121 |
-
cat("Estimated seat-winning probabilities
|
122 |
print(round(res$pi_hat, 4))
|
123 |
|
124 |
-
cat("\nPopulation Fractions
|
125 |
print(round(res$popFrac, 4))
|
126 |
|
127 |
-
cat("\nRepresentation Index
|
128 |
cat(round(res$representation_index, 4), "\n")
|
129 |
})
|
130 |
|
131 |
-
# Basic barplot comparison
|
132 |
output$plotPi <- renderPlot({
|
133 |
res <- dataSim()
|
134 |
popFrac <- res$popFrac
|
@@ -137,16 +143,76 @@ server <- function(input, output) {
|
|
137 |
op <- par(mfrow=c(1,2))
|
138 |
|
139 |
barplot(popFrac,
|
140 |
-
main="
|
|
|
141 |
ylim=c(0,1), col="steelblue", ylab="Fraction")
|
142 |
|
143 |
barplot(pi_hat,
|
144 |
-
main="
|
|
|
145 |
ylim=c(0,1), col="tomato", ylab="Probability")
|
146 |
|
147 |
par(op)
|
148 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
}
|
150 |
|
151 |
-
# -----------------------------
|
152 |
shinyApp(ui=ui, server=server)
|
|
|
1 |
# -----------------------------
|
2 |
# R Shiny App (K=3)
|
3 |
# "largest-group-wins" scenario
|
4 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
library(shiny)
|
7 |
+
library(scales)
|
8 |
|
9 |
# Helper: sample Dirichlet( alpha1, alpha2, alpha3 ) via Gamma shape draws.
|
10 |
rdirichlet_3 <- function(n, alpha_vec) {
|
|
|
14 |
|
15 |
# -----------------------------
|
16 |
ui <- fluidPage(
|
17 |
+
titlePanel("Representation Index under SMD (Largest-Vote-Share-Wins) with K=3"),
|
18 |
|
19 |
sidebarLayout(
|
20 |
sidebarPanel(
|
21 |
+
# Use sliderInput for alpha0
|
22 |
+
sliderInput("alpha0", "Concentration Parameter (alpha0):",
|
23 |
+
min = 0.1, max = 200, value = 6, step = 0.1),
|
24 |
|
25 |
+
# Use sliderInput for alphaTilde1
|
26 |
+
sliderInput("alphaTilde1", "alpha-tilde1:",
|
27 |
+
min = 0.01, max = 0.90, value = 0.45, step = 0.01),
|
28 |
+
|
29 |
+
# Use sliderInput for alphaTilde2
|
30 |
+
sliderInput("alphaTilde2", "alpha-tilde2:",
|
31 |
+
min = 0.01, max = 0.90, value = 0.34, step = 0.01),
|
32 |
|
33 |
helpText("Note: alpha-tilde3 is automatically 1 - (tilde1 + tilde2)."),
|
34 |
+
helpText("All tilde-params must be positive and sum to 1."),
|
35 |
|
36 |
+
# Removed the nsim slider; fix nsim = 10,000 internally
|
37 |
+
# sliderInput("nsim", "Number of Dirichlet samples (Monte Carlo):",
|
38 |
+
# min = 1000, max = 10000, value = 5000, step = 1000)
|
39 |
),
|
40 |
|
41 |
mainPanel(
|
42 |
verbatimTextOutput("resultsText"),
|
43 |
+
plotOutput("plotPi"),
|
44 |
+
plotOutput("plotIndex") # Representation Index vs. varied alpha0
|
45 |
)
|
46 |
)
|
47 |
)
|
|
|
49 |
# -----------------------------
|
50 |
server <- function(input, output) {
|
51 |
|
52 |
+
# We fix the number of MC draws at 10k
|
53 |
+
nsim_fixed <- 10000
|
54 |
+
|
55 |
+
# Reactive expression computing the simulation results for the *current* alpha0
|
56 |
dataSim <- reactive({
|
57 |
# Read user inputs
|
58 |
alpha0 <- input$alpha0
|
|
|
62 |
# Compute alpha-tilde3
|
63 |
at3 <- 1 - (at1 + at2)
|
64 |
|
65 |
+
# Validation checks
|
66 |
validate(
|
67 |
need(alpha0 > 0,
|
68 |
"alpha0 must be > 0"),
|
|
|
82 |
# Summation check (should be alpha0 if at1+at2+at3=1)
|
83 |
alpha0_check <- sum(alpha_vec)
|
84 |
|
|
|
|
|
|
|
85 |
# Sample from Dirichlet
|
86 |
+
X <- rdirichlet_3(nsim_fixed, alpha_vec)
|
87 |
|
88 |
# Identify which group is largest
|
89 |
largest_index <- apply(X, 1, which.max)
|
90 |
+
pi_hat <- as.numeric(table(factor(largest_index, levels = c(1,2,3)))) / nsim_fixed
|
91 |
+
names(pi_hat) <- c("k=1", "k=3", "k=3")
|
92 |
|
93 |
# Population fraction
|
94 |
popFrac <- alpha_vec / alpha0_check
|
95 |
+
names(popFrac) <- c("k=1",
|
96 |
+
"k=2",
|
97 |
+
"k=3")
|
98 |
|
99 |
# Representation index
|
100 |
sq_diff <- (popFrac - pi_hat)^2
|
|
|
110 |
)
|
111 |
})
|
112 |
|
113 |
+
# Display text results for the *current* alpha0
|
114 |
output$resultsText <- renderPrint({
|
115 |
res <- dataSim()
|
116 |
|
|
|
124 |
round(res$alpha0_check, 4),
|
125 |
"(should match alpha0 if they sum to 1)\n\n")
|
126 |
|
127 |
+
cat("Estimated seat-winning probabilities:\n")
|
128 |
print(round(res$pi_hat, 4))
|
129 |
|
130 |
+
cat("\nPopulation Fractions:\n")
|
131 |
print(round(res$popFrac, 4))
|
132 |
|
133 |
+
cat("\nRepresentation Index:\n")
|
134 |
cat(round(res$representation_index, 4), "\n")
|
135 |
})
|
136 |
|
137 |
+
# Basic barplot comparison for the *current* alpha0
|
138 |
output$plotPi <- renderPlot({
|
139 |
res <- dataSim()
|
140 |
popFrac <- res$popFrac
|
|
|
143 |
op <- par(mfrow=c(1,2))
|
144 |
|
145 |
barplot(popFrac,
|
146 |
+
main="Pop. Frac.",
|
147 |
+
cex.lab = 1.5, cex.main = 1.80,
|
148 |
ylim=c(0,1), col="steelblue", ylab="Fraction")
|
149 |
|
150 |
barplot(pi_hat,
|
151 |
+
main="Win Seat Prob.",
|
152 |
+
cex.lab = 1.5, cex.main = 1.80,
|
153 |
ylim=c(0,1), col="tomato", ylab="Probability")
|
154 |
|
155 |
par(op)
|
156 |
})
|
157 |
+
|
158 |
+
# New plot: Representation Index vs. scaled alpha0 (log scale w/ scientific notation)
|
159 |
+
output$plotIndex <- renderPlot({
|
160 |
+
# We use the user's alpha0 and multiply by a range of factors
|
161 |
+
alpha0_base <- input$alpha0
|
162 |
+
alpha0_list <- alpha0_base * c(0.01, 0.1, 1, 10, 100, 1000)
|
163 |
+
|
164 |
+
# We'll extract the tilde values from the *current* inputs as well
|
165 |
+
at1 <- input$alphaTilde1
|
166 |
+
at2 <- input$alphaTilde2
|
167 |
+
at3 <- 1 - (at1 + at2)
|
168 |
+
|
169 |
+
# For each alpha0 in alpha0_list, compute the representation index
|
170 |
+
rep_index_vals <- sapply(alpha0_list, function(a0) {
|
171 |
+
alpha_vec <- c(a0 * at1, a0 * at2, a0 * at3)
|
172 |
+
|
173 |
+
# Use the fixed 10k draws
|
174 |
+
X <- rdirichlet_3(nsim_fixed, alpha_vec)
|
175 |
+
|
176 |
+
largest_index <- apply(X, 1, which.max)
|
177 |
+
pi_hat <- as.numeric(table(factor(largest_index, levels = c(1,2,3)))) / nsim_fixed
|
178 |
+
|
179 |
+
popFrac <- alpha_vec / sum(alpha_vec)
|
180 |
+
sq_diff <- (popFrac - pi_hat)^2
|
181 |
+
1 - 0.5 * sum(sq_diff)
|
182 |
+
})
|
183 |
+
|
184 |
+
# Helper to format scientific notation nicely
|
185 |
+
sci_lab <- function(x) format(x, scientific=TRUE, digits=2)
|
186 |
+
|
187 |
+
# Plot with log scale on x-axis, plus scientific notation
|
188 |
+
# Create the plot with a logarithmic x-axis
|
189 |
+
options(scipen=999)
|
190 |
+
par(mar=c(5,5,3,1))
|
191 |
+
plot(alpha0_list, rep_index_vals,
|
192 |
+
type="b", pch=19, col="blue",
|
193 |
+
log="x", axes=FALSE,
|
194 |
+
cex.axis = 1.5,
|
195 |
+
cex = 1.5,
|
196 |
+
cex.lab = 1.75,
|
197 |
+
cex.main = 1.8,
|
198 |
+
xlab="",#expression(alpha[0]),
|
199 |
+
ylab="SMD Representation Index",
|
200 |
+
main=expression("Representation Index vs."~alpha[0]))
|
201 |
+
mtext(text = expression(alpha[0]:" Concentrated"%<->%"Dispersed"),
|
202 |
+
side = 1,
|
203 |
+
line = 3,
|
204 |
+
cex=1.5)
|
205 |
+
x_ticks <- axTicks(1)
|
206 |
+
abline(h=1, lty = 2, col = "gray")
|
207 |
+
text(x = mean(alpha0_list),
|
208 |
+
y = 1,
|
209 |
+
col = "gray",
|
210 |
+
cex = 1.5,
|
211 |
+
label = "MMD Index")
|
212 |
+
axis(1, at=x_ticks, labels=(x_ticks))
|
213 |
+
axis(2)
|
214 |
+
box()
|
215 |
+
})
|
216 |
}
|
217 |
|
|
|
218 |
shinyApp(ui=ui, server=server)
|