โมเดลเดียวที่มี interaction

Regression ระดับกลางด้วย statsmodels ใน Python

Maarten Van den Broeck

Content Developer at DataCamp

Interaction คืออะไร?

ในชุดข้อมูลปลา

  • ปลาแต่ละสายพันธุ์มีอัตราส่วนมวลต่อความยาวต่างกัน
  • ผลของความยาวต่อมวลที่คาดหวังแตกต่างกันตามสายพันธุ์

 

โดยทั่วไป

ผลของตัวแปรอธิบายหนึ่งต่อค่าตอบสนองที่คาดหวังเปลี่ยนแปลงไปตามค่าของตัวแปรอธิบายอีกตัวหนึ่ง

Regression ระดับกลางด้วย statsmodels ใน Python

การระบุ interaction

ไม่มี interaction

response ~ explntry1 + explntry2

มี interaction (แบบนัย)

response_var ~ explntry1 * explntry2

มี interaction (แบบชัดแจ้ง)

response ~ explntry1 + explntry2 + explntry1:explntry2

ไม่มี interaction

mass_g ~ length_cm + species

มี interaction (แบบนัย)

mass_g ~ length_cm * species

มี interaction (แบบชัดแจ้ง)

mass_g ~ length_cm + species + length_cm:species
Regression ระดับกลางด้วย statsmodels ใน Python

รันโมเดล

mdl_mass_vs_both = ols("mass_g ~ length_cm * species", data=fish).fit()

print(mdl_mass_vs_both.params)
Intercept                    -1035.3476
species[T.Perch]               416.1725
species[T.Pike]               -505.4767
species[T.Roach]               705.9714
length_cm                       54.5500
length_cm:species[T.Perch]     -15.6385
length_cm:species[T.Pike]       -1.3551
length_cm:species[T.Roach]     -31.2307
Regression ระดับกลางด้วย statsmodels ใน Python

ค่าสัมประสิทธิ์ที่เข้าใจง่ายขึ้น

mdl_mass_vs_both_inter = ols("mass_g ~ species + species:length_cm + 0", data=fish).fit()

print(mdl_mass_vs_both_inter.params)
species[Bream]             -1035.3476
species[Perch]              -619.1751
species[Pike]              -1540.8243
species[Roach]              -329.3762
species[Bream]:length_cm      54.5500
species[Perch]:length_cm      38.9115
species[Pike]:length_cm       53.1949
species[Roach]:length_cm      23.3193
Regression ระดับกลางด้วย statsmodels ใน Python

ตัวเลขที่คุ้นเคย

print(mdl_mass_vs_both_inter.params)
species[Bream]             -1035.3476
species[Perch]              -619.1751
species[Pike]              -1540.8243
species[Roach]              -329.3762
species[Bream]:length_cm      54.5500
species[Perch]:length_cm      38.9115
species[Pike]:length_cm       53.1949
species[Roach]:length_cm      23.3193
print(mdl_bream.params)
Intercept   -1035.3476
length_cm      54.5500
Regression ระดับกลางด้วย statsmodels ใน Python

มาฝึกกันเถอะ!

Regression ระดับกลางด้วย statsmodels ใน Python

Preparing Video For Download...