繪製類別因素的交互作用

在這個範例中,我們將視覺化類別因素之間的交互作用。首先,我們將建立一些類別資料。然後,我們將使用 interaction_plot 函數繪製它,該函數會在內部將 x 因素類別重新編碼為整數。

[1]:
%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from statsmodels.graphics.factorplots import interaction_plot
[2]:
np.random.seed(12345)
weight = pd.Series(np.repeat(["low", "hi", "low", "hi"], 15), name="weight")
nutrition = pd.Series(np.repeat(["lo_carb", "hi_carb"], 30), name="nutrition")
days = np.log(np.random.randint(1, 30, size=60))
[3]:
fig, ax = plt.subplots(figsize=(6, 6))
fig = interaction_plot(
    x=weight,
    trace=nutrition,
    response=days,
    colors=["red", "blue"],
    markers=["D", "^"],
    ms=10,
    ax=ax,
)
../../../_images/examples_notebooks_generated_categorical_interaction_plot_4_0.png

上次更新:2024 年 10 月 03 日