I am trying to run models by certain factor levels or groups of these levels by using a select input widget in Shiny.
When I subset by one factor level, I get the correct model results. But when I try to run a model that includes all factor levels or groups of the levels, I do not get the correct model estimates.
For example, the correct model estimates when all factor levels are included (i.e. the model is run over the entire data frame) are:
But when I run my app and select all the levels of my factor variable, which represents different geographical regions, I obtain different results:
My question is how can I specify my reactive sub-setting function to accommodate all factor levels or groups of the levels?
Code for individual models that includes all levels and models by factor level for reference:
library(mlogit)
data("Heating", package = "mlogit")
mlogit(depvar ~ ic + oc | 0, data= Heating, shape = "wide", choice = "depvar", varying = c(3:12))
mlogit(depvar ~ ic + oc | 0, data= Heating[Heating$region=="ncostl" , ], shape = "wide", choice = "depvar", varying = c(3:12))
mlogit(depvar ~ ic + oc | 0, data= Heating[Heating$region=="scostl" , ], shape = "wide", choice = "depvar", varying = c(3:12))
mlogit(depvar ~ ic + oc | 0, data= Heating[Heating$region=="mountn" , ], shape = "wide", choice = "depvar", varying = c(3:12))
mlogit(depvar ~ ic + oc | 0, data= Heating[Heating$region=="valley" , ], shape = "wide", choice = "depvar", varying = c(3:12))
Shiny code:
### PART 1 - Load Libraries and Data
library(shiny) # For running the app
library(mlogit)
#### data
data("Heating", package = "mlogit")
#### PART 2 - Define User Interface for application
ui <- fluidPage(
## Application title
titlePanel("Housing Preference"),
## Sidebar with user input elements
sidebarLayout(
sidebarPanel(
p("Select the inputs"), # Header
# Speciality
selectInput('regiontype', 'Region', choices = c("northern coastal region"= "ncostl",
"southern coastal region" = "scostl",
"mountain region" = "mountn",
"central valley region"= "valley"), multiple=TRUE, selectize=TRUE)
),
## Show a plot
mainPanel(
verbatimTextOutput("summary")
)
)
)
#### PART 3 - Define server logic required to run calculations and draw plots
server <- function(input, output) {
output$summary <- renderPrint({
df <- Heating
### Subset data
df.subset <- reactive({ a <- subset(df, region == input$regiontype)
return(a)})
### Model
estimates <- mlogit(depvar ~ ic + oc | 0, data= df.subset(), shape = "wide", choice = "depvar", varying = c(3:12))
summary(estimates)
})
}
### PART 4 - Run the application
shinyApp(ui = ui, server = server)