Pulse Data Hub

Data Science in Agriculture: A Python Crop Yield Analysis

Data Science in Agriculture: A Python Crop Yield Analysis

Data science in agriculture often begins with a classic question: what drives crop yield? This article analyses a real field-trial dataset, ten barley varieties grown at six Minnesota sites over two years, and shows which factor mattered most, how much of the variation a simple model explains, and a famous oddity in the data. Analysis used pandas 3.0.5 and statsmodels 0.15.0.

Bar chart of average barley yield by site: Waseca 48.1, Crookston 37.4, Morris 35.4, University Farm 32.7, Duluth 28.0, Grand Rapids 24.9
Average yield by site. The site gap is bigger than the gap between varieties.

The data

barley.json from the open vega-datasets collection has 120 rows: yield for each of 10 barley varieties at 6 sites in 1931 and 1932. Yield is in bushels per acre, as in the original experiment. It is small and old, which makes it a good teaching dataset and a poor basis for modern farming advice.

import pandas as pd
import statsmodels.formula.api as smf, statsmodels.api as sm

b = pd.read_json('barley.json')
print(b.groupby('site')['yield'].mean().sort_values())

model = smf.ols('Q("yield") ~ C(variety) + C(site) + C(year)', data=b).fit()
print(model.rsquared)                       # 0.671
print(sm.stats.anova_lm(model, typ=2))

Where you farm matters more than what you plant

Average yield was 37.1 in 1931 and 31.8 in 1932. Site averages ranged from 24.9 at Grand Rapids to 48.1 at Waseca, a gap of 23 bushels. Variety averages ranged only from 30.4 (Svansota) to 39.4 (Trebi and Wisconsin No. 38), a gap of 9. The top varieties were Trebi and Wisconsin No. 38 (both 39.4), then No. 457 (35.8) and No. 462 (35.4); Svansota (30.4), Manchuria (31.5) and No. 475 (31.8) were at the bottom.

How much does each factor explain?

A model with variety, site and year explained 67.1% of the variation in yield. In the analysis of variance, site had by far the largest sum of squares (6,634), then variety (1,053) and year (847); each was statistically significant (variety p = 0.004, site and year p below 0.001). Put plainly, soil and climate at the site dominate, which is why variety trials are run at many sites. The other third of the variation is not explained by these three factors, such as plot-level noise or interactions.

Do the best varieties stay the best?

Rankings are not very stable. The rank correlation of variety averages between 1931 and 1932 was 0.54: a variety that did well in one year was only moderately likely to do well in the next. A farmer choosing from one year of trials would get a rough guide, not a guarantee. With 10 varieties and 12 observations each, we also cannot say which differences between neighbouring varieties are real.

A known oddity: Morris

Site19311932
Waseca54.341.9
Crookston43.731.2
University Farm35.829.5
Duluth30.325.7
Grand Rapids29.120.8
Morris29.341.5

Five sites had lower yields in 1932 than 1931. Morris is the only one where yield went up, by 12 bushels. Statisticians who have studied this dataset have long suspected that the years may have been swapped for Morris in the record; we cannot confirm that from the file. The lesson is practical: check any surprising result against the raw data before building a story on it. If Morris is swapped, the year effect and rankings above would change.

What modern agricultural data science adds

Today the same logic runs on far larger data: satellite and drone images, weather, soil sensors and machine telemetry, used for yield forecasting, disease detection and irrigation planning. The core skills are the ones used here: separate site, variety and year effects, quantify uncertainty and test on held-out data. See linear regression in Python for the modelling basics, the exploratory data analysis guide for checking data first, and data science by industry for more fields. The statsmodels ANOVA documentation covers the table used above.

Limits of this analysis

Only 120 plots, two years, one state and ninety-year-old trial data; no soil, weather or management variables; and a possible data-entry issue at Morris. Do not use these varieties for planting decisions.

FAQ

How is data science used in agriculture?

For yield forecasting, field-trial analysis, disease and pest detection from images, irrigation and input planning. This article analyses a real field trial.

What mattered most for barley yield here?

The site: averages ran from 24.9 to 48.1, against 30.4 to 39.4 across varieties.

Is the data reliable?

Mostly, but Morris shows an unusual 1931-32 pattern that statisticians suspect may be a swapped record. We could not verify this.

Can I use these results to choose a variety?

No. It is a small historical dataset with unstable rankings between years.

Newsletter Signup Form

Latest tutorials

Latest articles

On this page
Verified by MonsterInsights