This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(shiny)
## Warning: package 'shiny' was built under R version 4.4.3
library(car)
## Warning: package 'car' was built under R version 4.4.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.4.3
#1
df <- read.csv("health_indicators_omn.csv", skip = 1) # by pass the header column.
df <- na.omit(df)
num_variables <- names(df)[sapply(df, is.numeric)]
#set user interface:
ui <- fluidPage(
#set the app title:
titlePanel("Health Indicators Regression"),
sidebarLayout(
sidebarPanel(
# menu for selecting the response variable:
selectInput("response", "Response Variable:", choices = num_variables),
# check box group to select predictor variable :
checkboxGroupInput("predictors", "Predictor(s):", choices = num_variables)
),
#set the menu panel:
mainPanel(
verbatimTextOutput("summary"),
tableOutput("vif"),
plotOutput("residualPlot"),
plotOutput("qqPlot")
)))
#set the server function:
server <- function(input, output) {
model <- reactive({
# reactive to fit the lin model based on the user input:
req(input$response, input$predictors)
formula <- as.formula(paste(input$response, "~", paste(in_put$predictors, collapse = "+")))
lm(formula, data = df )
})
# output the model summary:
output$summary <- renderPrint({ summary(model()) })
output$vif <- renderTable({ as.data.frame(vif(model())) })
# output the vif (variance inflation factor):
output$residualPlot <- renderPlot({
plot(model(), which = 1) # Residuals vs Fitted
})
# plot for residuals vs fitted values:
output$qqPlot <- renderPlot({
plot(model(), which = 2)
})
}
shinyApp(ui = ui, server = server)
#Interpretation:
#Coefficients: The model coefficients represent the effect of each predictor on
#the response variable. Significant predictors (p < 0.05) are significant in
#explaining the response.
#VIF (Variance Inflation Factor): High VIF values (greater than 5 or 10) indicate
#multicollinearity among predictors, which suggests potential instability in the
#model's coefficient estimates.
#Diagnostic Plots:
#Residuals vs Fitted Plot: Check for non-linearity and homoscedasticity. Random
#scatter suggests a good fit, while patterns suggest issues.
#Q-Q Plot: Checks normality of residuals. Non-normality occurs where the line
#is not followed.
#Model Fit:
#R-squared: Checks proportion of variance in response captured by the model.
#Large values show a good fit.
#Assumptions: Model makes assumptions of linearity, homoscedasticity, and
#normality of residuals. Violation means going back to the model or
#transformations.
#Conclusion: Validity of the model depends on significance of predictors,
#VIF values, and check of assumptions
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.