Spaces:
Running
Running
Update app.R
Browse files
app.R
CHANGED
|
@@ -5,6 +5,7 @@
|
|
| 5 |
options(error = NULL)
|
| 6 |
library(shiny)
|
| 7 |
library(ggplot2)
|
|
|
|
| 8 |
library(strategize)
|
| 9 |
library(dplyr)
|
| 10 |
|
|
@@ -29,18 +30,60 @@ plot_factor <- function(pi_star_list,
|
|
| 29 |
}))
|
| 30 |
|
| 31 |
# Plot with ggplot2
|
| 32 |
-
p <- ggplot(df, aes(x = Level, y = Probability,
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
return(p)
|
| 46 |
}
|
|
@@ -98,12 +141,19 @@ ui <- fluidPage(
|
|
| 98 |
plotOutput("strategy_plot", height = "600px")),
|
| 99 |
tabPanel("Q Value",
|
| 100 |
verbatimTextOutput("q_value"),
|
| 101 |
-
p("Q represents the estimated outcome (e.g.,
|
|
|
|
| 102 |
tabPanel("About",
|
| 103 |
-
h3("About
|
| 104 |
-
p("This
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
)
|
| 108 |
),
|
| 109 |
br(),
|
|
|
|
| 5 |
options(error = NULL)
|
| 6 |
library(shiny)
|
| 7 |
library(ggplot2)
|
| 8 |
+
library(ggthemes)
|
| 9 |
library(strategize)
|
| 10 |
library(dplyr)
|
| 11 |
|
|
|
|
| 30 |
}))
|
| 31 |
|
| 32 |
# Plot with ggplot2
|
| 33 |
+
p <- ggplot(df, aes(x = Level, y = Probability, color = Strategy)) +
|
| 34 |
+
# Thin segment from y=0 to y=Probability
|
| 35 |
+
geom_segment(
|
| 36 |
+
aes(xend = Level, y = 0, yend = Probability),
|
| 37 |
+
position = position_dodge(width = 0.7),
|
| 38 |
+
size = 0.3
|
| 39 |
+
) +
|
| 40 |
+
# Point at the probability
|
| 41 |
+
geom_point(
|
| 42 |
+
position = position_dodge(width = 0.7),
|
| 43 |
+
size = 2.5
|
| 44 |
+
) +
|
| 45 |
+
# Optional text label for the probability value above the point
|
| 46 |
+
geom_text(
|
| 47 |
+
aes(label = sprintf("%.2f", Probability)),
|
| 48 |
+
position = position_dodge(width = 0.7),
|
| 49 |
+
vjust = -0.7,
|
| 50 |
+
size = 3
|
| 51 |
+
) +
|
| 52 |
+
# If you want error bars, re-activate lines below and adjust as desired
|
| 53 |
+
# geom_errorbar(
|
| 54 |
+
# aes(ymin = Probability - zStar * SE, ymax = Probability + zStar * SE),
|
| 55 |
+
# position = position_dodge(width = 0.7),
|
| 56 |
+
# width = 0.2,
|
| 57 |
+
# size = 0.3
|
| 58 |
+
# ) +
|
| 59 |
+
|
| 60 |
+
# Labels
|
| 61 |
+
labs(
|
| 62 |
+
title = sprintf("Optimal Distribution for *%s*",
|
| 63 |
+
gsub(factor_name,pattern = "\\.",replace=" ")),
|
| 64 |
+
x = "Level",
|
| 65 |
+
y = "Probability"
|
| 66 |
+
) +
|
| 67 |
+
|
| 68 |
+
# Apply Tufte's minimalistic theme
|
| 69 |
+
#theme_tufte(base_size = 12) +
|
| 70 |
+
theme_minimal(base_size = 16,
|
| 71 |
+
base_line_size = 0) +
|
| 72 |
+
theme(
|
| 73 |
+
legend.position = "none",
|
| 74 |
+
legend.title = element_blank(),
|
| 75 |
+
# Remove or soften grid lines
|
| 76 |
+
panel.grid.major = element_blank(),
|
| 77 |
+
panel.grid.minor = element_blank(),
|
| 78 |
+
# Keep a minimal axis line
|
| 79 |
+
axis.line = element_line(color = "black", size = 0.3),
|
| 80 |
+
axis.text.x = element_text(angle = 45, hjust = 1)
|
| 81 |
+
) +
|
| 82 |
+
|
| 83 |
+
# Manual color scale for different strategies
|
| 84 |
+
scale_color_manual(values = c("Democrat" = "#89cff0",
|
| 85 |
+
"Republican" = "red",
|
| 86 |
+
"Optimal" = "black"))
|
| 87 |
|
| 88 |
return(p)
|
| 89 |
}
|
|
|
|
| 141 |
plotOutput("strategy_plot", height = "600px")),
|
| 142 |
tabPanel("Q Value",
|
| 143 |
verbatimTextOutput("q_value"),
|
| 144 |
+
p("Q represents the estimated outcome (e.g., strategy probability)
|
| 145 |
+
under the optimal strategy, with 95% confidence interval.")),
|
| 146 |
tabPanel("About",
|
| 147 |
+
h3("About this page"),
|
| 148 |
+
p("This page app explores the ",
|
| 149 |
+
a("strategize R package", href = "https://github.com/cjerzak/strategize-software/", target = "_blank"),
|
| 150 |
+
" R package using Ono forced conjoint experimental data.
|
| 151 |
+
It computes optimal strategies for Average (optimizing for a respondent group)
|
| 152 |
+
and Adversarial (optimizing for both parties in competition) cases on the fly."),
|
| 153 |
+
p(strong("Average Case:"),
|
| 154 |
+
"Optimizes candidate characteristics for a selected respondent group."),
|
| 155 |
+
p(strong("Adversarial Case"),
|
| 156 |
+
"Finds equilibrium strategies for Democrats and Republicans.")
|
| 157 |
)
|
| 158 |
),
|
| 159 |
br(),
|