Ozge Tugrul Sonmez
date: 25.07.2020
You can find the details of the assignment from the web adress:
In this assignment, I prepared a shiny application and the link is :
https://ozgetugrulsonmez.shinyapps.io/shiny_assignment/
Also, the codes of server.R and ui.R are on the link:
The data used for Shiny Application is mtcars data set.
head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
library(shiny)
shinyUI(
navbarPage("Developing Data Products Assignment",
tabPanel("Simple Regression",
(fluidPage(
titlePanel("Predict Miles per Gallon (mpg) with Simple Regression Models"),
sidebarLayout(
sidebarPanel(
selectInput("variable", "Select Input for Simple Regression",
c("am","cyl","hp","wt","disp","drat","qsec","gear","carb")),
checkboxInput("simple_model","show simple model",value=FALSE),
submitButton("Submit")
),
mainPanel(
h3("Simple Regression Model"),
textOutput("model"),
tabsetPanel(type = "tabs",
tabPanel("BoxPlot", plotOutput("simpleboxplot"),textOutput("simpletext")),
tabPanel("Summary", verbatimTextOutput("simplesummary")),
tabPanel("Residual Plots", plotOutput("simpleresidual"))
)))))),
tabPanel("Multivariable Regression (Full)",
fluidPage(
titlePanel("Regression with All Variables for mpg Prediction"),
sidebarLayout(
sidebarPanel(
checkboxInput("multimodel","show full regression model",value=FALSE),
submitButton("Submit")
),
mainPanel(
h3("Multivariable Regression Model"),
textOutput("fullmodel"),
tabsetPanel(type = "tabs",
tabPanel("Summary Full", verbatimTextOutput("multisummary")),
tabPanel("Residual Plots Full", plotOutput("multiresidual"))
))))),
tabPanel("Multivariable Regression (Variable Selection)",
fluidPage(
titlePanel("Regression with Best Variables for mpg Prediction"),
sidebarLayout(
sidebarPanel(
checkboxInput("show","Show/Hide Best Variable Subsets",value = FALSE),
checkboxInput("variablenum","Show/Hide Best Variable Number",value = FALSE),
checkboxInput("variables","Show/Hide Best Variables",value = FALSE),
submitButton("Submit (it may take a while for processing)")
),
mainPanel(
h3("Best Subset Regression Model"),
verbatimTextOutput("bestvariablesubsets"),
verbatimTextOutput("variablenumber"),
verbatimTextOutput("bestvariables"),
tabsetPanel(type = "tabs",
tabPanel("Summary ", verbatimTextOutput("multisummary2")),
tabPanel("Residual Plots", plotOutput("multiresidual2"))
)))))))
<!–html_preserve–>
library(shiny)
library(ggplot2)
library(olsrr)
data(mtcars)
mtcars$cyl <- factor(mtcars$cyl)
mtcars$vs <- factor(mtcars$vs)
mtcars$gear <- factor(mtcars$gear)
mtcars$carb <- factor(mtcars$carb)
mtcars$am <- factor(mtcars$am,labels=c("Automatic","Manual"))
shinyServer(function(input, output) {
full_model<-lm(mpg ~ am+cyl+hp+wt+disp+hp+drat+qsec+gear+carb,data=mtcars)
best_model<-lm(mpg ~ am+hp+wt+disp+qsec,data=mtcars)
formula<-reactive({
paste("mpg ~", input$variable)
})
fit_simple<-reactive({
lm(as.formula(formula()),data=mtcars)
})
output$model<-renderText({
if(input$simple_model)
{formula()}
})
output$simpleboxplot <- renderPlot({
# check for the input variable
if (input$variable == "am") {
# am
mpgData <- data.frame(mpg = mtcars$mpg, var = factor(mtcars[[input$variable]], labels = c("Automatic", "Manual")))
p <- ggplot(mpgData, aes(var, mpg,fill=var)) +
geom_boxplot(alpha=0.3) +
xlab(input$variable)+scale_fill_brewer(palette="BuPu")
print(p)
}
else if(input$variable == "cyl"|input$variable == "vs"|input$variable == "gear"|input$variable == "carb"){
# cyl and gear
mpgData <- data.frame(mpg = mtcars$mpg, var = factor(mtcars[[input$variable]]))
p <- ggplot(mpgData, aes(var, mpg,fill=var)) +
geom_boxplot(alpha=0.3) +
xlab(input$variable)+scale_fill_brewer(palette="BuPu")
print(p)
}
else{
output$simpletext<-renderText({
if (input$variable!= "am"|input$variable != "cyl"|input$variable!= "vs"|input$variable!= "gear"|input$variable!= "carb"){
print("We don't have a categorical grouping variable!")
}
})
}
})
output$simplesummary<-renderPrint({
summary(fit_simple())
})
output$simpleresidual<-renderPlot({
par(mfrow = c(2, 2))
plot(fit_simple())
})
output$multisummary<-renderPrint({
summary(full_model)
})
output$multiresidual<-renderPlot({
par(mfrow = c(2, 2))
plot(full_model)
})
output$bestvariablesubsets<-renderPrint({
if(input$show)
{ols_step_best_subset(full_model,details=TRUE)}
else{"Check Show Hide Best Variable Subsets and Press Submit Button"}
})
output$fullmodel<-renderText({
if(input$multimodel)
{print("mpg ~ am+cyl+hp+wt+disp+hp+drat+qsec+gear+carb")}
})
output$variablenumber<-renderPrint({
if(input$variablenum)
{adjr<-ols_step_best_subset(fit_multivariable_full,details = TRUE)$adjr
which(adjr==max(adjr))}
else{"Check Show Hide Best Variable Number and Press Submit Button"}
})
output$bestvariables<-renderPrint({
if(input$variables)
{
adjr<-ols_step_best_subset(fit_multivariable_full,details = TRUE)$adjr
var<-ols_step_best_subset(fit_multivariable_full,details = TRUE)$predictors[which(adjr==max(adjr))]
print(var)
}
else{"Check Show Hide Best Variables and Press Submit Button"}
})
output$multisummary2<-renderPrint({
summary(best_model)
})
output$multiresidual2<-renderPlot({
par(mfrow = c(2, 2))
plot(best_model)
})
})