2017-01-31 3 views
0

Я создаю свое первое блестящее приложение, и я изо всех сил пытаюсь построить четыре теста пригодности из пакета fitdistrplus. (Ссылки, я пытаюсь воспроизвести сюжеты из стр.7 из ссылки здесь:fitdist in r shiny, error нужны конечные значения «xlim»

https://www.jstatsoft.org/article/view/v064i04/v64i04.pdf

Вкратце, я хочу, чтобы пользователь мог выбирать подмножество данных на основе переменных М, а затем оценить различную плотность распределений вероятностей для переменной Q. Я использовал код для создания хороших подгонок за пределами Shiny, и он отлично работает. В R Shiny я могу индивидуально подгонять (fw, fg, fl), но когда дело доходит до использования denscomp, qqcomp, cdfcomp и ppcomp, у меня есть сообщение об ошибке:

Error: need finite 'xlim' values

Я попытался добавить xlim и ylim в код (например: xlim =c(0,300), ylim=c(0.008)), но все же я получил сообщение об ошибке.

Кто-нибудь знает, как это решить?

Мой код ниже:

library(fitdistrplus) 
library(shiny) 
library(dplyr) 

ui<- shinyUI(pageWithSidebar(

headerPanel("Distribution analysis"), 

sidebarPanel(
    selectInput("input1", 
       label = "M", 
       choices = data$m, 
       selected = "M1"), 

mainPanel(
    tabsetPanel(
    tabPanel("Fit", plotOutput("fit1")), 
    tabPanel("Distribution", plotOutput("hist1")), 
    tabPanel("Table", tableOutput("table")) 
)) 
)) 

server<- shinyServer(function(input, output) { 

dataInput <- reactive({ 
    data %>% 
    filter(m==input$input1) 
}) 

fw<- eventReactive(input$input1 { 
    fitdist(dataInput()$Q, "weibull") 
    }) 

fg<- eventReactive(input$input1 { 
    fitdist(dataInput()$Q, "gamma") 
    }) 

fln<- eventReactive(input$input1 { 
    fitdist(dataInput()$Q, "lnorm") 
    }) 

output$fit1 <- renderPlot({ 
    if (!is.null(dataInput())) { 
    par(mfrow = c(2, 2)) 
    plot.legend <- c("Weibull", "lognormal", "gamma") 
    denscomp(list(fw, fln, fg), legendtext = plot.legend) 
    qqcomp(list(fw, fln, fg), legendtext = plot.legend) 
    cdfcomp(list(fw, fln, fg), legendtext = plot.legend) 
    ppcomp(list(fw, fln, fg), legendtext = plot.legend) 

    } 
}) 
}) 

shinyApp(ui=ui, server=server) 

И данные воссоздать пример:

m<- c("M1","M3","M3", "M2", "M3","M2","M2","M1","M1","M1","M1","M3","M3","M2","M2","M1","M3","M3", "M3","M2","M2","M2","M1","M1","M1","M1","M1","M3","M3","M3") 
Q<- c(265, 65, 40, 245,230,175, 185, 190, 290, 85, 75, 155, 110, 60, 35, 245, 300,175, 180, 265, 55, 200, 95, 185, 165, 55, 90, 190, 235, 200) 
data<- data.frame(m,Q) 

ответ

0

Я исправил некоторые вещи у вас есть, но так как я не знаком с пакетом fitdistrplus я не могу полностью отладить другие предупреждения. Обратите внимание, что все реактивы являются функциями так должны быть использованы в качестве таких, например: fln() и не fln

#rm(list = ls()) 
library(fitdistrplus) 
library(shiny) 
library(dplyr) 

m<- c("M1","M3","M3", "M2", "M3","M2","M2","M1","M1","M1","M1","M3","M3","M2","M2","M1","M3","M3", "M3","M2","M2","M2","M1","M1","M1","M1","M1","M3","M3","M3") 
Q<- c(265, 65, 40, 245,230,175, 185, 190, 290, 85, 75, 155, 110, 60, 35, 245, 300,175, 180, 265, 55, 200, 95, 185, 165, 55, 90, 190, 235, 200) 
data<- data.frame(m,Q) 

ui<- shinyUI(pageWithSidebar(
    headerPanel("Distribution analysis"), 
    sidebarPanel( selectInput("input1", label = "M",choices = data$m,selected = "M1")), 

    mainPanel(
    tabsetPanel(
     tabPanel("Fit", plotOutput("fit1")), 
     tabPanel("Distribution", plotOutput("hist1")), 
     tabPanel("Table", tableOutput("table")) 
    )) 
)) 

server<- shinyServer(function(input, output) { 

    dataInput <- reactive({ 
    if(is.null(input$input1)){ 
     return() 
    } 
    data %>% filter(m==input$input1) 
    }) 

    fw <- eventReactive(input$input1, { 
    fitdist(dataInput()$Q, "weibull") 
    }) 

    fg <- eventReactive(input$input1, { 
    fitdist(dataInput()$Q, "gamma") 
    }) 

    fln <- eventReactive(input$input1, { 
    fitdist(dataInput()$Q, "lnorm") 
    }) 

    output$fit1 <- renderPlot({ 
    if(is.null(dataInput()) | nrow(dataInput()) ==0){ 
     return() 
    } 
    par(mfrow = c(2, 2)) 
    plot.legend <- c("Weibull", "lognormal", "gamma") 
    denscomp(list(fw(), fln(), fg()), legendtext = plot.legend) 
    qqcomp(list(fw(), fln(), fg()), legendtext = plot.legend) 
    cdfcomp(list(fw(), fln(), fg()), legendtext = plot.legend) 
    ppcomp(list(fw(), fln(), fg()), legendtext = plot.legend) 
    }) 
}) 

shinyApp(ui=ui, server=server) 

enter image description here

Примечание этих сообщений об ошибках, которые вы должны заботиться о, возможно, начать здесь https://stats.stackexchange.com/questions/158163/why-does-this-data-throw-an-error-in-r-fitdistr

enter image description here

+0

Благодарим за ответ. Это решило проблему. Спасибо за совет. Очень ценю. –

+0

Примите ответ, если он решит вашу проблему –

Смежные вопросы