Public
Edited
Aug 1, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
maples = FileAttachment("w1_acsa_seed_physical.txt").csv({typed: true})
Insert cell
Insert cell
Insert cell
Insert cell
maples
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
Insert cell
Insert cell
maples2003 = maples.filter(d => d.Year === 2003)
.map(d => ({year: d.Year, watershed: d.Watershed, stemLength: d.StemLength, leafMass: d.LeafDryMass, stemMass: d.StemDryMass, leafArea: d.CorrectedLeafArea}))
Insert cell
maples2003
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Plot.plot({
color: { legend: true },
marks: [
Plot.dot(maples2003, {x: "stemLength", y: "stemMass", fill: "watershed", tip: true}),
//Plot.linearRegressionY(maples2003, {x: "stemLength", y: "stemMass"}),
Plot.linearRegressionY(maples2003, {x: "stemLength", y: "stemMass", stroke: "watershed"})
]
})
Insert cell
Insert cell
Insert cell
Insert cell
data = maples2003
Insert cell
import {PlotMatrix} with {data} from "@observablehq/autoplot-matrix"
Insert cell
PlotMatrix(data)
Insert cell
Insert cell
Insert cell
Insert cell
ML = require("https://www.lactame.com/lib/ml/6.0.0/ml.min.js")
Insert cell
Insert cell
// Create a scaled version of the numeric variables
maples2003Scaled = scale(maples2003.map(({ year, watershed, ...rest }) => rest))
Insert cell
// Convert to an array of arrays, just containing the values (no keys):
maples2003Array = maples2003Scaled.map(Object.values)
Insert cell
// Perform principal component analysis:
maples2003PCA = new ML.PCA(maples2003Array) // Already scaled above - otherwise can add {scale: true} here!
Insert cell
// Get variance explained by each PC:
variancePC = maples2003PCA.getExplainedVariance()
Insert cell
// Get cumulative variance explained:
cumulativeVariance = maples2003PCA.getCumulativeVariance()
Insert cell
Insert cell
loadings = maples2003PCA
.getEigenvectors()
.data.map((d, i) => ({
PC1: d[0],
PC2: d[1],
Variable: Object.keys(maples2003Scaled[0])[i]
}))
Insert cell
scoresCombined = maples2003PCA.predict(maples2003Array).data.map((d,i) => ({watershed: maples2003[i].watershed,
//year: maples2003[i].year,
PC1: d[0],
PC2: d[1]}))
Insert cell
scalingFactor = 5
Insert cell
Plot.plot({
marks: [
Plot.dot(scoresCombined, { x: "PC1", y: "PC2", fill: "watershed", r: 5, tip: true }),
Plot.arrow(loadings, {
x1: 0, x2: d => d.PC1 * scalingFactor, y1: 0, y2: (d) => d.PC2 * scalingFactor
}),
Plot.text(loadings, {
x: (d) => d.PC1 * scalingFactor, y: (d) => d.PC2 * scalingFactor,
text: "Variable",
dy: -5,
dx: 30,
fill: "black",
stroke: "white"
})
],
color: { legend: true }
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more