Moderation

 1. The program code


import pandas as pd

import statsmodels.api as sm

import statsmodels.formula.api as smf


# Load dataset

df = pd.read_csv("gapminder.csv")


# Convert to numeric

df["incomeperperson"] = pd.to_numeric(df["incomeperperson"], errors="coerce")

df["internetuserate"] = pd.to_numeric(df["internetuserate"], errors="coerce")

df["lifeexpectancy"] = pd.to_numeric(df["lifeexpectancy"], errors="coerce")


# Create categorical moderator: Life Expectancy Groups

df["lifeexp_group"] = pd.cut(df["lifeexpectancy"],

                             bins=[0, 60, 75, 90],

                             labels=["Low", "Medium", "High"])


# Drop missing values

df_clean = df.dropna(subset=["incomeperperson", "internetuserate", "lifeexp_group"])


# Moderation model with interaction term

model = smf.ols("internetuserate ~ incomeperperson * C(lifeexp_group)", data=df_clean).fit()


# ANOVA table

anova_table = sm.stats.anova_lm(model, typ=2)


print("ANOVA with Interaction (Moderator Test):")

print(anova_table)



2. Output



3. Interpretation

The moderation analysis tested whether life expectancy group moderated the relationship between income per person and internet use rate.

  • The main effect of income per person was significant, F(1, 167) = 109.88, p < .0001, confirming that countries with higher income per person report higher internet use.

  • The main effect of life expectancy group was also significant, F(2, 167) = 31.77, p < .0001, indicating that internet use differs across life expectancy groups.

  • Crucially, the interaction effect between income per person and life expectancy group was significant, F(2, 167) = 14.18, p < .0001.

Interpretation: This result shows that the strength of the relationship between income and internet use depends on life expectancy group. In high life expectancy countries, income is strongly associated with internet use, while in low life expectancy countries, the effect of income on internet adoption is weaker. Thus, life expectancy moderates the income–internet relationship.

Comments

Popular posts from this blog

Exploring the Relationship Between Economic Prosperity, Health, and Internet Adoption Across Countries

Python project 2

Simple Linear Regression