8.3 Exercises#
Exercise 1: The Dataset#
Since this chapter didn’t introduce many new concepts, we will also take some time to review and practice skills you’ve learned in previous chapters.
Today’s dataset includes various variables related to student stress factors and mental health. It is included in the teaching materials, so you can either load it directly from your files, or by providing the URL:
pd.read_csv("data/StressLevelDataset.csv", delimiter=',')
pd.read_csv("https://raw.githubusercontent.com/mibur1/psy111/main/book/statistics/4_Moderated_Reg/data/StressLevelDataset.csv", delimiter=',')
Your research question will be: Does social support moderate the effect of anxiety on self-esteem?
Load and explore the dataset.
Select only the variables relevant to your research question (subset the dataset), and save it to a new DataFrame. Make sure to create a deep copy by using
.copy()
Create a correlation matrix for the three variables of interest (anxiety, social support, and self-esteem).
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# TODO
Exercise 2#
Center the variables anxiety_level and self_esteem by subtracting their mean values. Save the centered variables to new columns in your DataFrame.
Perform a moderated regression modelling the main effects of, as well as interaction effects between,
anxiety_centered
andsocial_support
onself_esteem_centered
.Take some time to interpret the outputs (similar to as shown in the previous section).
Warning: social_support
is NOT a continuous variable. You can, for example, use dummy coding for your model.
import statsmodels.formula.api as smf
# TODO
Exercise 3: Quiz#
Please answer the following questions:
from jupyterquiz import display_quiz
display_quiz('https://raw.githubusercontent.com/mibur1/psy111/main/book/solutions/quiz/question1.json')
display_quiz('https://raw.githubusercontent.com/mibur1/psy111/main/book/solutions/quiz/question2.json')
Voluntary exercise#
Create a scatterplot to visualize the relationship between anxiety
and self_esteem
. Adding a linear regression line. Use social_support
as the hue to differentiate groups.
import seaborn as sns
import matplotlib.pyplot as plt
# TODO