我正在开发一个简单的闪亮应用程序,以可视化方差分析中的变异源(总数、介于之间、之内)。基本上,我希望用户为三组单向方差分析场景输入“组n”、“平均数”和“sds”——然后,应用程序生成一个数据集来创建一个图和相应的方差分析表。
我还不知道如何在用户更改输入参数时同时更新绘图和方差分析表。我的大多数尝试都会导致绘图更新,但表不会更新。
我找到的最接近实际的解决方案是下面的“黑客/欺骗”方法,其中相同的数据集生成两次。然而,这显然是不必要的。我假设该解决方案与在服务器函数中创建一个可用于多个输出的“反应”数据集有关。这在原则上似乎应该是一件相当简单的事情。然而,我还不能在线拼凑教程/材料来找出如何做到这一点。如果您对此有任何帮助,我们将不胜感激。
代码:
# Visualizing partitioning variance for oneway ANOVA
library(shiny)
ui <- fluidPage(
titlePanel("Partitioning Variance in a Oneway ANOVA"),
sidebarLayout(
sidebarPanel(
sliderInput("N", "n for each group:",
min = 2, max = 50, value = 25),
sliderInput("M1", "Mean for Control Group:",
min = 1, max = 100, value = 55),
sliderInput("SD1", "SD for Control Group:",
min = 1, max = 20, value = 10),
sliderInput("M2", "Mean for Treatment Group One:",
min = 1, max = 100, value = 55),
sliderInput("SD2", "SD for Treatment Group One:",
min = 1, max = 20, value = 10),
sliderInput("M3", "Mean for Treatment Group Two:",
min = 1, max = 100, value = 55),
sliderInput("SD3", "SD for Treatment Group Two:",
min = 1, max = 20, value = 10)
),
mainPanel(
plotOutput("varPlot"),
verbatimTextOutput("anovaTable")
)
)
)
server <- function(input, output) {
output$varPlot <- renderPlot({
set.seed(1976)
X1 <- rnorm(input$N, input$M1, input$SD1)
X2 <- rnorm(input$N, input$M2, input$SD2)
X3 <- rnorm(input$N, input$M3, input$SD3)
datOutcome = data.frame(X1, X2, X3)
library(tidyr)
dat <- gather(datOutcome, group, outcome)
dat$group <- factor(dat$group, levels = c("X1", "X2", "X3"),
labels = c("Control", "Treatment One", "Treatment Two"))
# attach the data
attach(dat)
library(car)
mod <- Anova(lm(outcome ~ group, data = dat), type = "III")
# make the stripcharts by group
stripchart(outcome ~ group, method = "jitter", jitter = 0.05,
vertical = TRUE, pch = 1, col = "blue",
group.names = c("Control", "Treatment One", "Treatment Two"),
xlim = c(.5,4.75),
ylim = c((min(dat$outcome) - 5), (max(dat$outcome) + 5)),
ylab = "Outcome Value",
main = paste("Group n =", input$N,
"\nRed = total variation, Blue = within groups variation, Green indicates between groups variation"))
# label group means
text(1.3, mean(X1),
paste("Control \nmean =", format(round(mean(X1), 2), nsmall = 2)),
col = "darkgreen", cex = .9)
text(2.3, mean(X2),
paste("Treatment One \nmean =", format(round(mean(X2), 2), nsmall = 2)),
col = "darkgreen", cex = .9)
text(3.3, mean(X3),
paste("Treatment Two\n mean =", format(round(mean(X3), 2), nsmall = 2)),
col = "darkgreen", cex = .9)
# add diamonds to indicate the means for each group
points(1, mean(X1), pch = 18, cex = 2, col = "darkgreen")
points(2, mean(X2), pch = 18, cex = 2, col = "darkgreen")
points(3, mean(X3), pch = 18, cex = 2, col = "darkgreen")
# plot a stripchart for the grand mean
stripchart( outcome, method="jitter" , jitter=0.05 ,
vertical=TRUE , pch=1 , col="red" ,
at = 4, add = TRUE,
xlim=c(.5,3.75))
# label grand mean and add dimaond to indicate mean
text(4.3, mean(outcome),
paste("Grand \nmean =", format(round(mean(outcome), 2), nsmall = 2)),
col = "red", cex = .9)
points(4, mean(outcome), pch = 18, cex = 2)
})
output$anovaTable <- renderPrint( {
set.seed(1976)
X1 <- rnorm(input$N, input$M1, input$SD1)
X2 <- rnorm(input$N, input$M2, input$SD2)
X3 <- rnorm(input$N, input$M3, input$SD3)
datOutcome = data.frame(X1, X2, X3)
library(tidyr)
dat <- gather(datOutcome, group, outcome)
dat$group <- factor(dat$group, levels = c("X1", "X2", "X3"),
labels = c("Control", "Treatment One", "Treatment Two"))
A <- Anova(aov(outcome ~ group, data = dat), type = "III")
A
})
}
shinyApp(ui = ui, server = server)