Jeden model s interakcí

Intermediate Regression with statsmodels in Python

Maarten Van den Broeck

Content Developer at DataCamp

Co je interakce?

V datové sadě ryb

  • Různé druhy ryb mají různý poměr hmotnosti k délce.
  • Vliv délky na očekávanou hmotnost se liší podle druhu.

 

Obecněji

Vliv jedné vysvětlující proměnné na očekávanou odezvu závisí na hodnotě jiné vysvětlující proměnné.

Intermediate Regression with statsmodels in Python

Specifikace interakcí

Bez interakcí

response ~ explntry1 + explntry2

S interakcemi (implicitně)

response_var ~ explntry1 * explntry2

S interakcemi (explicitně)

response ~ explntry1 + explntry2 + explntry1:explntry2

Bez interakcí

mass_g ~ length_cm + species

S interakcemi (implicitně)

mass_g ~ length_cm * species

S interakcemi (explicitně)

mass_g ~ length_cm + species + length_cm:species
Intermediate Regression with statsmodels in Python

Spuštění modelu

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
Intermediate Regression with statsmodels in Python

Srozumitelnější koeficienty

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
Intermediate Regression with statsmodels in Python

Známá čísla

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
Intermediate Regression with statsmodels in Python

Pojďme cvičit!

Intermediate Regression with statsmodels in Python

Preparing Video For Download...