df_name %>% map(class)Lab 2 Instructions
BSTA 513/613
Ready to be worked on (Nicky 4/17/26)
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 explore our data further, set up the unadjusted odds ratio, and create code to later help us present our final model.
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 Make sure variables are coded correctly
Use class() to determine the class of the variables you selected from Lab 1 (including the outcome). A tidyverse equivalent to the apply() function is map(). Please take a look at the description of the map() function.
Make sure the class that R recognizes is the class that you expect the variable to be. Categorical variables should be factors and numeric variables should be numeric. It is very important that your outcome, food insecurity, is a factor with the reference level set to “No.” For example, if I am using age, but the class is character, I will need to convert age to a numeric variable. If I have a categorical covariate that is recognized as a character, I should convert it to a factor with a specific reference level.
- Use
class()to determine the class of the variables you selected from Lab 1 (including the outcome). - Change the variable type to the appropriate type.
2.3 For purposeful model selection only
Let’s redo the Table 1 with correct variable types.
Here’s a change to the tbl_summary() function so that Yes and No show:
wbns3 %>% tbl_summary(type = all_dichotomous() ~ "categorical")Redo your Table 1 with correct variable types and showing Yes/No categories.
2.4 Consider potential confounders and effect modifiers
2.4.1 For purposeful model selection only
For each of the 10 predictor variables, fill out the below table. Determine whether you think each variable will be a confounder, effect modifier, or nothing in relation to your main variable and food insecurity. This does not need to be extensive reasoning. If you would like to present this information in another way, you may.
Fill in the below table (or any other way you wish to present the same information).
| Variable name | Confounder, Effect modifier, or nothing? | Reasoning (1-2 sentences) |
|---|---|---|
2.4.2 For LASSO only
For the 5-10 predictors you identified in Lab 1 as potentially important predictors of food insecurity, rank the predictors in order of importance and include reasoning.
Fill in the below table (or any other way you wish to present the same information).
| Rank | Variable name | Reasoning (1-2 sentences) |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 | ||
| 7 | ||
| 8 | ||
| 9 | ||
| 10 |
2.5 Bivariate exploratory data analysis
We want to explore the relationship between our predictors and food insecurity before we get into regression. This will help us understand the data better and identify any potential issues.
Use your predictors from the activity above. If you are doing LASSO, you do not need to use ALL the variables in your dataset.
2.5.1 Categorical predictors: Create contingency tables
For each categorical covariate, create a contingency table between it and food insecurity. You can create a table (using table()) for each categorical variable with the outcome. No need to make these tables pretty.
We can also use gtsummary and tbl_summary(by = FOOD_INSEC) to get a table stratified by food insecurity, essentially creating a contingency table for each categorical variable. Here’s a tutorial on tbl_summary(). You can also use add_p() to get the p-value for a chi-squared or Fisher’s exact test.
Take note of any cells that have less than 10 observations. This is something we have not covered yet, but it’s important to check before we get too far into our analysis. We will discuss this further in Lesson 13: Numerical Problems.
- Create contingency tables for all categorical covariates with food insecurity.
- Take note of any cell counts that are less than 10
2.5.2 Bivariate exploratory data analysis
For numeric variables, you can use ggpairs() to quickly look at the relationship with food insecurity. If you have trouble seeing or interpreting the individual plots, try recreating them in ggplot(), but remember that you can select specific columns before using ggpairs() to limit the number of variables.
- Use
ggpairs()to quickly look at the relationship between numeric variables and food insecurity. - Think back to your Intro to Biostats course. How could you test the difference in the distribution of a numeric variable between food insecurity groups?
2.6 Fit simple logistic regression
- Using
glm(), run a logistic regression with food insecurity and your main variable of interest. (For LASSO: pick one variable that you think will be an important predictor) - Display the unadjusted odds ratio(s) of the regression. You can use
tidy()orlogistic.display() - Interpret the unadjusted odds ratio(s) (with 95% confidence interval). If you’re main variable is multi-level and has more than 5 categories, then you can limit your interpretations to 5 odds ratios.
2.7 Plot the predicted probability
I want us to plot the predicted probability across our main independent variable. If your main variable of interest (from your research question) is continuous, then you can follow the code from Lesson 7 to construct a plot of the predicted probability. If your main variable of interest in categorical, then you can try plotting the predicted probability in the same way as Lesson 7. You may prefer to present the predicted probabilities for each group as a table.
This plot will serve as a good foundation if we have any interactions in the model!
Plot or make a table of your predicted probabilities.