0

I created a shiny app, in which I want to display the residual of a log-linear model using a mosaic plot. I need to use the data from a reactive expression and pass it to loglm. It seem pretty strait forward, but when I do that I get the following error : "objet 'mod' introuvable".

I've already figured which line is causing the problem, but I don't know how to fix it. Running the code below as is should work fine. However, uncomment the line # mod <- loglm( formula = reformulate(f), data = mod ), in server and you should get the same error I get.

Any help would be greatly appreciated.

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)
S. Venne
  • 71
  • 1
  • 4
  • I guess that is a problem with the data format that `loglm()` allows – Shiva Jul 14 '15 at 14:30
  • The problem doesn't appear to be related to the data format, since the code works perfectly fine out of the shiny app. Moreover, the `loglm` help page say that the "data" argument can be the result of a call to `xtabs`, as is the case here. – S. Venne Jul 14 '15 at 22:18

1 Answers1

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))

   })

} 
S. Venne
  • 71
  • 1
  • 4