0

我创建了一个闪亮的应用程序,我想在其中使用马赛克图显示对数线性模型的残差。我需要使用反应式表达式中的数据并将其传递给loglm. 看起来很困难,但是当我这样做时,我收到以下错误:“objet 'mod' introuvable”。

我已经知道是哪条线路导致了问题,但我不知道如何解决它。按原样运行下面的代码应该可以正常工作。但是,在 server 中取消注释该行# mod <- loglm( formula = reformulate(f), data = mod ),您应该得到与我相同的错误。

任何帮助将不胜感激。

ui <- fluidPage(
 titlePanel("Shiny Viz!"),

  fluidRow( class= "R1",
        tabsetPanel(type= "pills",

                    tabPanel("Log-linear model",
                             fluidRow( 
                               column(3, offset=1,
                                      selectInput("model", label= "Choose model to fit:",
                                                  choices= c("(SPT)","(SP,ST,PT)","(ST,PT)","(SP,PT)","(SP,ST)")),
                                      selectInput("type", label= "Visualise the expected or observed values?",
                                                  choices = c("observed", "expected")), 
                                      sliderInput("n_breaks", label = "Degree Celcius per bin:",
                                                  min = .5, max = 5, value = 1, step = .5)),
                               column(8, plotOutput("loglinear.mosaic",  height= "600px") ) 
                             )))) 
 )




library(ggplot2)
library(data.table)
library(vcd)
library(vcdExtra)

server <- function(input, output) {  

 # Create data
 DF <- data.table( Temp = runif(5000, 0, 30),
                Presence = factor(rbinom(5000, 1, runif(20, 0.1, 0.60))), 
                Period =  factor(as.integer(runif(5000, 1, 9))) ) 

 # Reactive expression
 loglinear <- reactive({  
   DF[ , Temperature.category := cut_interval(Temp, length= input$n_breaks)]


   Tab <- xtabs(formula= ~ Period + Temperature.category + Presence,
                      data = DF)


   return(Tab)
 })


 # mosaic plot
 output$loglinear.mosaic <- renderPlot({

   mod <- loglinear()

   f <- switch(input$model,
            "(SPT)"= c("Presence*Period*Temperature.category"),
            "(SP,ST,PT)" = c("Presence*Period","Presence*Temperature.category","Period*Temperature.category"), 
            "(ST,PT)" = c("Presence*Temperature.category","Period*Temperature.category"),
            "(SP,PT)" = c("Presence*Period","Period*Temperature.category"),
            "(SP,ST)" = c("Presence*Period","Presence*Temperature.category"))

    # mod <-  loglm( formula = reformulate(f), data = mod )

    mosaic(mod, 
         gp= shading_hcl,
         spacing = spacing_highlighting,
         type= input$type,
         labeling_args= list(offset_varnames = c(right = 1, left=.5), 
                             offset_labels = c(right = .1),
                             set_varnames = c(Temperature.category="Temperature", Period="Period",
                                            Presence="Status")),
         set_labels=list(Presence = c("Ab","Pr")), 
         margins = c(right = 5, left = 3, bottom = 1, top =3))

  })

} 


shinyApp(ui = ui, server = server)
4

1 回答 1

0

I still haven't found what is causing the problem with loglm, but I've figured another way of getting the result I wanted.

I used glm to fit the model instead of loglm, then used mosaic.glm from the vcdExtra package to create the mosaic plot. The code is pretty much the same except that the data as to be a data.frame and the column 'Temperature.category', 'Period' and 'Presence' must be factor to be used with glm.

However, I am still clueless as to why loglm can't find the object 'mod', but glm can? I'd realy want to know the reason. Since my answers doesn't answer that question, I'll accept an other answer if someone has an explanation.

Here's what the code using glm:

ui <- fluidPage(
  titlePanel("Shiny Viz!"),

  fluidRow( class= "R1",
        tabsetPanel(type= "pills",

                    tabPanel("Log-linear model",
                             fluidRow( 
                               column(3, offset=1,
                                      selectInput("model", label= "Choose model to fit:",
                                                  choices= c("(SPT)","(SP,ST,PT)","(ST,PT)","(SP,PT)","(SP,ST)")),
                                      selectInput("type", label= "Visualise the expected or observed values?",
                                                  choices = c("observed", "expected")), 
                                      sliderInput("n_breaks", label = "Degree Celcius per bin:",
                                                  min = .5, max = 5, value = 1, step = .5)),
                               column(8, plotOutput("loglinear.mosaic",  height= "800px") ) 
                             )))) 
)

library(ggplot2)
library(data.table)
library(vcd)
library(vcdExtra)


server <- function(input, output) {  

   DF <- data.table( Temp = runif(5000, 0, 30),
                 Presence = factor(rbinom(5000, 1, runif(20, 0.1, 0.60))), 
                 Period =  factor(as.integer(runif(5000, 1, 9)) ) ) 


  # data to data.frame format
  loglinear <- reactive({ 

    DF[ , Temperature.category := cut_interval(Temp, length= input$n_breaks)]

    # add 'Freq' column
    dat <- data.frame(as.table(xtabs(formula= ~ Period + Temperature.category + Presence,
             data = DF)), stringsAsFactors = T) 


    return(dat)
   })


  # mosaic plot
  output$loglinear.mosaic <- renderPlot({

    mod <- loglinear()

    f <- switch(input$model,
            "(SPT)"= c("Presence*Period*Temperature.category"),
            "(SP,ST,PT)" = c("Presence*Period","Presence*Temperature.category","Period*Temperature.category"), 
            "(ST,PT)" = c("Presence*Temperature.category","Period*Temperature.category"),
            "(SP,PT)" = c("Presence*Period","Period*Temperature.category"),
            "(SP,ST)" = c("Presence*Period","Presence*Temperature.category"))

    # fit model using glm
    mod.glm <- glm(formula = reformulate(f, response = "Freq"), data= mod, family= poisson)

    mosaic.glm(mod.glm, 
               formula =  ~ Temperature.category + Period + Presence,
               gp= shading_hcl,
               spacing = spacing_highlighting,
               type= input$type,
               labeling_args= list(rot_labels = c(left = 0,  right = 0),
                                 offset_varnames = c(left=1.5, right = 1), 
                                 offset_labels = c(left=.5, right = .1),
                                 set_varnames = c(Temperature.category="Temperature", Period="Period",
                                                Presence="Status")),
             set_labels=list(Presence = c("Ab","Pr")), 
             margins = c(right = 5, left = 4, bottom = 1, top =3))

   })

} 
于 2015-07-15T16:38:20.747 回答