tidy_df <- tidy(glm_mod, conf.int = T, exponentiate = T) %>%
filter(term != "(Intercept)")
ggplot(tidy_df, aes(y = term, x = estimate)) +
geom_vline(xintercept = 1, linetype = "dashed", color = "red") +
geom_errorbarh(aes(xmin = conf.low, xmax = conf.high), height = 0.15) +
geom_point(size = 3) +
theme_minimal() +
theme(axis.text.y = element_text(face = "bold"))Lab 3 Instructions
BSTA 513/613
Ready to be worked on!! (5/1/2026)
1 Directions
You can download the .qmd file for this lab here.
The above link will take you to your editing file. Please do not remove anything from this editing file!! You will only add your code and work to this file.
1.1 Purpose
The purpose of this lab is to fit a multiple logistic regression model and practice how we would interpret our results for this study.
2 Lab activities
I have left it up to you to load the needed packages for this lab.
2.1 Restate project type
Restate your project type: LASSO or purposeful model selection.
Purposeful model selection only: Please restate your research question below using the provided format (1 sentence). You can change the wording if you’d like, but please make sure it is still clear. It’s repetitive, but it helps me contextualize my feedback as I look through your lab.
Project type: LASSO or purposeful model selection
Purposeful model selection only: In this study, we will investigate the association between food insecurity and ________.
2.2 Can I make this categorical variable into a numeric one?
This is NOT a required part of the lab! You can leave these variables as categorical if you want.
I want us to follow the steps laid out in our Lab 2 Discussion to determine if we can use a numeric representation for a few categorical variables.
There are a few variables in our dataset that are categorical but have a natural ordering:
PPEDUCATPPINCIMPNUM_CHILD_HHNUM_OWN_CHILDNUM_FAM_SIZE
If you want to use these as numeric values, you need to convince yourself that the relationship between the predictor and log-odds of food insecurity is linear. Please complete the following steps:
- Fit a logistic regression model with the predictor of interest as the only covariate (as categorical) and food insecurity as the outcome.
- Use
predict()to get the predicted log-odds of food insecurity for each observation. - Plot the predicted log-odds against the predictor. If the levels are not equally spaced, you can plot the predicted log-odds against the numeric representation of the predictor.
- Examine the plot to determine if the relationship looks roughly linear. If it does, you can use the numeric representation of the predictor in your model. If it does not, you should keep the predictor as categorical.
If you decide to use a numeric representation for any categorical variables, please complete the steps for that variable and include the plot from the final step.
2.3 Fit a multiple logistic regression model
Recall, we have not removed missing observations. In homework 1, we discussed what glm() does with missing values automatically.
Fit a main effects model with 5 of your covariates. This will not necessarily be your final model, but we can construct interpretations and code that will be useful in your final model.
2.4 Present the odds ratio in a table
Use the tbl_regression() function to make a table of the odds ratios from your fitted model.
2.5 Present the odds ratio in a forest plot
If you are using LASSO, the output from the R functions will not be in the same format as a typical glm() output. You will need to extract the coefficients and confidence intervals from the LASSO output and create a forest plot manually. So we won’t use ggplot the same way we’ve learned with glm() output. The following code is a good place to start for a forest plot mirroring the output type we’d get from LASSO. I’ve named the output of my model glm_mod.
If you are doing Purposeful model selection, you can use the code from our lesson on Multiple Logistic Regression (Lesson 9) to make a forest plot of the odds ratio from your fitted model.
Make a forest plot of the odds ratio from your fitted model. Make sure your main variable looks presentable! The other ones do not need to look perfect since they may not be in your final model.
2.6 Interpret the odds ratio for your main covariate
Interpret the estimated odds ratio(s) for the covariate from your research question. Make sure to include the 95% confidence intervals and what other variables you adjusted for.
Remember that this is not necessarily your final model. I just want to set us up with a good template for how to interpret our results after Lab 4.