Why this matters
Many questions involve several factors at once. Does fertiliser raise maize yield once you allow for rainfall? Do borrowers with longer histories repay better once you allow for loan size? Regression estimates how an outcome changes with one factor while holding others steady. It is the workhorse of research, evaluation and credit analysis, and the starting point for predictive models.
The idea
Linear regression fits an equation of the form:
yield = intercept + b1 × fertiliser + b2 × rainfall + errorEach coefficient (b1, b2) says how much the outcome changes, on average, when that factor rises by one unit and the other factors stay the same. The error term is everything the model does not capture.
Fitting one in Python
With statsmodels and a pandas table df holding one row per farm:
import statsmodels.formula.api as smf
model = smf.ols("yield_kg ~ fertiliser_kg + rain_mm",
data=df).fit()
print(model.params.round(2))
print(model.conf_int().round(2))On a made up dataset of 200 farms we generated, the fertiliser coefficient was 3.68 with a 95 percent interval of 3.15 to 4.20, and rainfall was 0.44 (0.33 to 0.54). In words: comparing farms with the same rainfall, each extra kilogram of fertiliser went with about 3.7 kg more maize, and we are fairly confident the true figure lies between about 3.2 and 4.2. prints a fuller table with p values and R squared.