Lab 4

Visualize, model, interpret

Lab
Due Wed Jun 11 11:59 PM

Introduction

In this lab you’ll start your practice of statistical modeling. You’ll fit models, interpret model output, and make decisions about your data and research question based on the model results. And you’ll wrap up the lab revisiting the topic of data science ethics.

Note

This lab assumes you’ve completed the labs so far and doesn’t repeat setup and overview content from those labs. If you haven’t done those yet, you should review the previous labs before starting on this one!

Learning objectives

By the end of the lab, you will…

  • Fit linear regression models and interpret model coefficients in context of the data and research question.
  • Transform data using a log-transformation for a better model fit.
  • Evaluate the validity of a data visualization and correct any misleading mistakes.

And, as usual, you will also…

  • Get more experience with data science workflow using R, RStudio, Git, and GitHub
  • Further your reproducible authoring skills with Quarto
  • Improve your familiarity with version control using Git and GitHub

Getting started

Log in to RStudio, clone your lab-4 repo from GitHub, open your lab-4.qmd document, and get started!

Step 1: Log in to RStudio

  • Go to https://cmgr.oit.duke.edu/containers and log in with your Duke NetID and Password.
  • Click STA198-199 under My reservations to log into your container. You should now see the RStudio environment.

Step 2: Clone the repo & start a new RStudio project

  • Go to the course organization at github.com/sta199-su25 organization on GitHub. Click on the repo with the prefix lab-4. It contains the starter documents you need to complete the lab.

  • Click on the green CODE button and select Use SSH. This might already be selected by default; if it is, you’ll see the text Clone with SSH. Click on the clipboard icon to copy the repo URL.

  • In RStudio, go to FileNew ProjectVersion ControlGit.

  • Copy and paste the URL of your assignment repo into the dialog box Repository URL. Again, please make sure to have SSH highlighted under Clone when you copy the address.

  • Click Create Project, and the files from your GitHub repo will be displayed in the Files pane in RStudio.

  • Click lab-4.qmd to open the template Quarto file. This is where you will write up your code and narrative for the lab.

Step 3: Update the YAML

In lab-4.qmd, update the author field to your name, render your document and examine the changes. Then, in the Git pane, click on Diff to view your changes, add a commit message (e.g., “Added author name”), and click Commit. Then, push the changes to your GitHub repository, and in your browser confirm that these changes have indeed propagated to your repository.

Important

If you run into any issues with the first steps outlined above, flag a TA for help before proceeding.

Packages

In this lab, we will work with the

  • tidyverse package for doing data analysis in a “tidy” way,
  • tidymodels package for modeling in a “tidy” way, and
  • openintro package for the dataset for Part 1.
  • Run the code cell by clicking on the green triangle (play) button for the code cell labeled load-packages. This loads the package so that its features (the functions and datasets in it) are accessible from your Console.
  • Then, render the document that loads this package to make its features (the functions and datasets in it) available for other code cells in your Quarto document.

Guidelines

As we’ve discussed in lecture, your plots should include an informative title, axes and legends should have human-readable labels, and careful consideration should be given to aesthetic choices.

Additionally, code should follow the tidyverse style. Particularly,

  • there should be spaces before and line breaks after each + when building a ggplot,

  • there should also be spaces before and line breaks after each |> in a data transformation pipeline,

  • code should be properly indented,

  • there should be spaces around = signs and spaces after commas.

Furthermore, all code should be visible in the PDF output, i.e., should not run off the page on the PDF. Long lines that run off the page should be split across multiple lines with line breaks.

Important

Continuing to develop a sound workflow for reproducible data analysis is important as you complete the lab and other assignments in this course. There will be periodic reminders in this assignment to remind you to render, commit, and push your changes to GitHub. You should have at least 3 commits with meaningful commit messages by the end of the assignment.

Important

You are also expected to pay attention to code smell in addition to code style and readability. You should review and improve your code to avoid redundant steps (e.g., grouping, ungrouping, and grouping again by the same variable in a pipeline), using inconsistent syntax (e.g., ! to say “not” in one place and - in another place), etc.

Part 1 - Smoking during pregnancy

Question 1

We are interested in the impact of smoking during pregnancy. Since it is not possible to run a randomized controlled experiment to investigate this impact, we will instead use a data set has been of interest to medical researchers who are studying the relation between habits and practices of expectant mothers and the birth of their children. This is a random sample of 1,000 cases from a data set released in 2014 by the state of North Carolina. The data set is called births14, and it is included in the openintro package you loaded at the beginning of the assignment.

  1. Create a version of the births14 data set dropping observations where there are NAs for habit. You should call this version births14_habitgiven.

  2. Plotting the data is a useful first step because it helps us quickly visualize trends, identify strong associations, and develop research questions. Create an appropriate plot displaying the relationship between weight and habit. In 2-3 sentences, discuss the relationship observed.

Question 2

  1. Fit a linear model that predicts weight from habit and save the model object. Then, provide the tidy summary output.

  2. Write the estimated least squares regression line below using proper notation.

Tip

If you need to type an equation using proper notation, type your answers in-between $$ and $$. You may use \widehat{example} to put a hat on a character.

  1. Interpret the intercept in the context of the data and the research question. Is the intercept meaningful in this context? Why or why not?

  2. Interpret the slope in the context of the data and the research question.

Question 3

  1. Another researcher is interested in assessing the relationship between babies’ weights and the number of hospital visits the mother took during pregnancy (visits) . Fit another linear model predicting weight to investigate this relationship and save the model object. Then, provide the tidy summary output.

  2. In 2-3 sentences, explain how the regression line to model these data is fit, i.e., based on what criteria R determines the regression line.

  3. Interpret the intercept in the context of the data and the research question. Is the intercept meaningful in this context? Why or why not?

  4. Interpret the slope in the context of the data and the research question.

Question 4

Now, we want to use both information about both mothers’ smoking habits and the number of hospital visits to model/predict the baby’s weight.

  1. Before doing any data analysis, do you think that the relationship between number of hospital visits and the baby’s weight would be different depending on whether or not the mother smokes? Why or why not? Based on your answer, which type of model would be more fitting: additive or interaction?
Note

The point of this question is not for you to correctly guess the relationship. Rather, it is to get you thinking about data and the process for choosing models.*

  1. Create an appropriate data visualization to examine the question part (a). Does this change your opinion? Why or why not?
Tip

Adding a facet will likely make the plot MUCH easier to interpret.*

Question 5

  1. Fit an additive model that models birth weight predicted by both mother’s smoking habit and number of hospital visits. Display the model output in a tidy way.

  2. Write the equations of the lines modelling smoking and non-smoking mothers based on the model fit in (a). For each, interpret the slope and intercept in the context of the research question.

  3. Fit an interaction model that models birth weight predicted by both mother’s smoking habit and number of hospital visits. Display the model output in a tidy way.

  4. Write the equations of the lines modelling smoking and non-smoking mothers based on the model fit in (c). For each, interpret the slope and intercept in the context of the research question.

  5. Based on what we have learned in class, what is an appropriate statistic to compare the models in part (a) and (c)? Compute and display this statistic for each model. Based on these computations, which model would you choose to use for predicting birth weight?

  6. Using the model you chose in part (e), write code to predict the weight of a baby born to a smoking mother with 10 hospital visits. Make sure the predicted value is clearly displayed as output.

Part 2 - Parasites

Parasites can cause infectious disease – but not all animals are affected by the same parasites. Some parasites are present in a multitude of species and others are confined to a single host. It is hypothesized that closely related hosts are more likely to share the same parasites. More specifically, it is thought that closely related hosts will live in similar environments and have similar genetic makeup that coincides with optimal conditions for the same parasite to flourish.

In this part of the lab, we will see how much evolutionary history predicts parasite similarity.

The dataset comes from an Ecology Letters paper by Cooper at al. (2012) entitled “Phylogenetic host specificity and understanding parasite sharing in primates” located here. The goal of the paper was to identify the ability of evolutionary history and ecological traits to characterize parasite host specificity.

Each row of the data contains two species, species1 and species2.

Subsequent columns describe metrics that compare the species:

  • divergence_time: how many (millions) of years ago the two species diverged. i.e. how many million years ago they were the same species.

  • distance: geodesic distance between species geographic range centroids (in kilometers)

  • BMdiff: difference in body mass between the two species (in grams)

  • precdiff: difference in mean annual precipitation across the two species geographic ranges (mm)

  • parsim: a measure of parasite similarity (proportion of parasites shared between species, ranges from 0 to 1.)

The data are available in parasites.csv in your data folder.

Question 6

Let’s start by reading in the parasites data and examining the relationship between divergence_time and parsim.

  1. Load the data and save the data frame as parasites.

  2. Based on the goals of the analysis, what is the response variable?

  3. Visualize the relationship between the two variables.

  4. Use the visualization to describe the relationship between the two variables.

Question 7

Next, model this relationship.

  1. Fit the model and write the estimated regression equation.

  2. Interpret the slope and the intercept in the context of the data.

  3. Recreate the visualization from Question 4, this time adding a regression line to the visualization.

  4. What do you notice about the prediction (regression) line that may be strange, particularly for very large divergence times?

Question 8

Since parsim takes values between 0 and 1, we want to transform this variable so that it can range between (−∞,+∞). This will be better suited for fitting a regression model (and interpreting predicted values!)

  1. Using mutate, create a new variable transformed_parsim that is calculated as log(parsim/(1-parsim)). Add this variable to your data frame.

    Note

    log() in R represents the natural log.

  2. Then, visualize the relationship between divergence_time and transformed_parsim. Add a regression line to your visualization. Then, write a 1-2 sentence description of what you observe in the visualization.

  3. Fit a linear regression to model the relationship between transformed_parsim and divergence_time. Display a tidy summary of the model.

  4. Model interpretation:

    • Interpret the intercept of the model, making sure that your interpretation is in the units of the original data (not on log scale).

    • Is the following statement true or false? Why?


      Here, the slope tells us for each additional 1 million year of divergence time how much parasite similarity decreases (in the units of the original data).

Warning

Part d is intended to be a bit tricky! To get the interpretation to be in the original units, you will have to do a little math. Think carefully about what exactly we are modelling.

Question 9

Which individual variable is the strongest individual predictor of parasite similarity between species?

To answer this question, begin by fitting a linear regression model to each pair of variables. Do not report the model outputs in a tidy format but save each one as dt_model, dist_model, and BM_model, respectively.

  • divergence_time and transformed_parsim (already done in Q6! you do not have to repeat the code)

  • distance and transformed_parsim

  • BMdiff and transformed_parsim

  1. Report the slopes for each of these models. You do not need to write the full model outputs - simply reporting the slopes is enough.

  2. To answer the question of interest, would it be useful to compare the slopes in each model to choose the variable that is the strongest predictor of parasite similarity? Why or why not?

  3. Calculate and report \(R^2\) for each of these models.

  4. To answer our question of interest, would it be useful to compare the \(R^2\) in each model to choose the variable that is the strongest predictor of parasite similarity? Why or why not?