Does One Change the Other
df has columns y, dose and age. The effect of dose on y may itself depend on age. Fit y on dose, age and their interaction with a statsmodels formula, and return the coefficient of the interaction: how much the slope on dose changes per unit of age.
Example 1
df {"dose":[0,1,2,0,1,2,0,1,2,3],"age":[1,1,1,2,2,2,3,3,3,3],"y":[0,1,2,0,2,4,0,3,6,9]}
returns 1.0
Here y is exactly dose times age, so each year of age adds 1 to the slope on dose.
Example 2
df {"dose":[1,2,3,4,1,2,3,4,2,3],"age":[10,10,10,10,20,20,20,20,30,30],"y":[5,7,9,11,5,7,9,11,7,9]}
returns 0.0
Here dose adds 2 per unit at every age.
Constraints
10 <= len(df) <= 40
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More statsmodels problems
- Model for CountsMedium
- Odds of YesMedium
- Pattern in the LeftoversMedium
- Point That Pulls HardestMedium
- Tangled PredictorsMedium
- Top of the CurveMedium
- Trust Some Points MoreMedium
- Uneven SpreadMedium