Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何确保反应概率密度函数的面积固定为1?_R_Ggplot2_Shiny - Fatal编程技术网

R 如何确保反应概率密度函数的面积固定为1?

R 如何确保反应概率密度函数的面积固定为1?,r,ggplot2,shiny,R,Ggplot2,Shiny,我有下面的代码,它允许用户通过闪亮的GUI调整beta分布p,q的参数 但是,由于θ必须在0到1的范围内,我的代码缺少一个步骤,该步骤基本上确保曲线下的面积始终等于1,而不管参数选择如何 我想不出该怎么做,所以非常感谢您的帮助 # Required packages require(shiny) require(ggplot2) require(gridExtra) # Beta plot function betaFun <- function(p = 1.1, q = 1.1){

我有下面的代码,它允许用户通过闪亮的GUI调整beta分布p,q的参数

但是,由于θ必须在0到1的范围内,我的代码缺少一个步骤,该步骤基本上确保曲线下的面积始终等于1,而不管参数选择如何

我想不出该怎么做,所以非常感谢您的帮助

# Required packages
require(shiny)
require(ggplot2)
require(gridExtra)

# Beta plot function
betaFun <- function(p = 1.1, q = 1.1){

  prior <- function(theta){
    (theta ^ (p - 1)) * ((1 - theta) ^ (q - 1))
  }

  priorFill <- function(theta){
    ifelse(prior(theta) > 0 & prior(theta) < 1, prior(theta), NA)
  }

  ggplot(data = data.frame(x = as.double(0)), mapping = aes(x = x)) +
  stat_function(fun = prior, col = 'blue') +
  stat_function(fun = priorFill, geom = "area", fill = "blue", alpha = 0.2) +
  geom_segment(x = 0, y = 0, xend = 1, yend = 0, color = 'blue') +
  xlim(0, 1) +
  ylim(0, 1)

}

# UI
ui <- fluidPage(
  titlePanel("Beta Distribution"),

  sidebarLayout(
    sidebarPanel(
      sliderInput("pInput", "Parameter p", min = 1.1, max = 5, value = 1, step = 0.1),
      sliderInput("qInput", "Parameter q", min = 1.1, max = 5, value = 1, step = 0.1)
  ),

    mainPanel(
      plotOutput("betaPlot")
    )
  )
)

# Server
server <- function(input, output) {
  output$betaPlot <- renderPlot({
    betaFun(p = input$pInput,
            q = input$qInput)
  })
}

# Execution
shinyApp(ui = ui, server = server)

一种方法是在运行ggplot之前计算所有值,而不是在运行时使用stat_函数,这样就可以在前面将区域规格化为一个。然后您可以使用geom_线而不是stat_函数进行绘图。例如:

# Required packages
require(shiny)
require(ggplot2)
require(gridExtra)

# Beta plot function
betaFun <- function(p = 1.1, q = 1.1){

  theta = seq(0,1,length=1000)
  dat = data.frame(theta, prior=(theta ^ (p - 1)) * ((1 - theta) ^ (q - 1)))
  dat$prior = dat$prior/(sum(dat$prior)*mean(diff(theta)))  # Normalize to area = 1

  ggplot(dat, aes(theta, prior)) +
    geom_line(color="blue") +
    geom_area(alpha=0.2, fill="blue") +
    geom_segment(x = 0, y = 0, xend = 1, yend = 0, color = 'blue') +
    xlim(0, 1) +
    ylim(0, 5) +
    theme_classic(base_size=15)

}

# UI
ui <- fluidPage(
  titlePanel("Beta Distribution"),

  sidebarLayout(
    sidebarPanel(
      sliderInput("pInput", "Parameter p", min = 1.1, max = 5, value = 1, step = 0.1),
      sliderInput("qInput", "Parameter q", min = 1.1, max = 5, value = 1, step = 0.1)
    ),

    mainPanel(
      plotOutput("betaPlot")
    )
  )
)

# Server
server <- function(input, output) {
  output$betaPlot <- renderPlot({
    betaFun(p = input$pInput,
            q = input$qInput)
  })
}

# Execution
shinyApp(ui = ui, server = server)

在这里获取常数:非常好的答案,正是我所寻找的。谢谢。@Hong Ooi的确如此,不过测试版函数只是为MWE选择的。