Published
Edited
Jun 16, 2021
1 fork
Importers
11 stars
Also listed in…
Agriculture
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
cleaned = cleanIQR(full_dataset, [`${crop}_yield`, `${crop}_${indicator}`])
Insert cell
Insert cell
yield_soilQuality_smi_plot = pairplot(cleaned, {
vars: [`${crop}_yield`, `${crop}_${indicator}`, "smi"],
width: 800,
height: 600,
hue: "year",
hueVarType: "nominal",
markers: true
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
summary(linearModel)
Insert cell
Insert cell
Insert cell
Insert cell
summary(interactionModel)
Insert cell
Insert cell
Insert cell
summary(detailedModel)
Insert cell
coefficients_detailedModel = coefficientPlot(detailedModel)
Insert cell
stargazer([linearModel, interactionModel])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Object.keys(pricesYieldsDirectCosts).map((c) => c.split(/::/g)[1])
Insert cell
Insert cell
table(cropData)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
summary(lm("Yield ~ SQR", yields, { weights: weights }))
Insert cell
Insert cell
Insert cell
weights = {
const dataPerRegions = [...d3.group(yields, (r) => r.Region)];
const weightsPerRegion = dataPerRegions.reduce((obj, reg) => {
obj[reg[0]] = 1 / d3.variance(reg[1].map((r) => r.Yield));
return obj;
}, {});

const weights = yields.map((r) => weightsPerRegion[r.Region]);
return weights;
}
Insert cell
Insert cell
Insert cell
Insert cell
Object.keys(pricesYieldsDirectCosts).map((c) => c)
Insert cell
stargazer(regData, {
modelNames: cropData.map((c) => c.Crop)
})
Insert cell
stargazer(regData.slice(5), {
modelNames: cropData.slice(5).map((c) => c.Crop)
})
Insert cell
stargazer(regData.slice(0, 5), {
modelNames: cropData.slice(0, 5).map((c) => c.Crop)
})
Insert cell
regData = Object.keys(pricesYieldsDirectCosts).reduce((obj, curCrop) => {
const minYear = 2010;
const curCropYields = pricesYieldsDirectCosts[curCrop]
.filter((r) => r.year > minYear)
.map((r) => ({
Yield: r.yield,
Region: r.region,
NUTS2: nuts2_sqrs.find((f) => f.NUTS_NAME === r.region)
? nuts2_sqrs.find((f) => f.NUTS_NAME === r.region).NUTS_CODE
: 0,
SQR: nuts2_sqrs.find((f) => f.NUTS_NAME === r.region)
? nuts2_sqrs.find((f) => f.NUTS_NAME === r.region)._mean
: 0
}))
.filter((r) => r.SQR);

const dataPerRegions = [...d3.group(curCropYields, (r) => r.Region)];
const weightsPerRegion = dataPerRegions.reduce((obj, reg) => {
obj[reg[0]] = 1 / d3.variance(reg[1].map((r) => r.Yield));
return obj;
}, {});

const weights = curCropYields.map((r) => weightsPerRegion[r.Region]);

const model = lm(`Yield ~ SQR`, curCropYields, { weights: weights });
obj.push(model);
return obj;
}, [])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
lm = (await import(await FileAttachment("linear-models.esm@59.js").url()))
.default
Insert cell
import { table } from "@tmcw/tables/2"
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more