Public
Edited
Jul 26, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
marCasadoAir = FileAttachment("marCasadoAir@5.csv").csv({typed: true}) // Date issue
Insert cell
marCasadoSea = FileAttachment("marCasadoSea@4.csv").csv({typed: true}) // Date issue
Insert cell
Insert cell
// Write code to create a database called marCasadoDB, with tables 'air' and 'sea':
marCasadoDB = DuckDBClient.of({air: marCasadoAir, sea:marCasadoSea})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
marCasadoDB
--dont forget to name the cell to make it accessible as an array for later
Select air.month as date_month
, air.meanPressure
, air.PAR
, air.meanHumidity
, air.windDirection
, sea.maxTide
, sea.minTide
, sea.salinity
, sea.seaSurfaceTemp as SST
--, case when month(air.month) >=10 or month(air.month) <=3 then 'hot moist' else 'cool dry' end as season
, case when date_part('month', air.month) in (10,11,12,1,2,3) then 'hot moist' else 'cool dry' end as season
from air join sea on air.month = sea.month

Insert cell
Insert cell
Insert cell
marCasado
select * from marCasado
Insert cell
Insert cell
Insert cell
// Write Plot code to create a heatmap of sea surface temperature (SST) by year and month, starting from the 'cell' snippet:

Insert cell
//by default it uses a functional example with built-in dataset
Plot.plot({
marks: [
Plot.cell(weather.slice(-365), {
x: d => d.date.getUTCDate(),
y: d => d.date.getUTCMonth(),
fill: "temp_max"
})
]
})
Insert cell
Plot.plot({
marks: [
Plot.cell(marCasado, {
x: d => d.date_month.getUTCMonth(),
y: d => d.date_month.getUTCFullYear(),
fill: "SST"
})
]
})
Insert cell
Plot.plot({
marks: [
Plot.cell(marCasado, {
x: d => d.date_month.getUTCMonth(),
y: d => d.date_month.getUTCFullYear(),
fill: "SST",
tip:true
})
],
y:{tickFormat: "Y", padding:0},
x:{padding:0, tickFormat:Plot.formatMonth()}
})
Insert cell
Plot.plot({
marks: [
Plot.cell(marCasado, {
x: d => d.date_month.getUTCMonth(),
y: d => d.date_month.getUTCFullYear(),
fill: "meanPressure",
tip:true
})
],
y:{tickFormat: "Y", padding:0},
x:{padding:0, tickFormat:Plot.formatMonth()}
})
Insert cell
Insert cell
Insert cell
//avoid making copies of the dataset, we keep our marCasado but it's used as it's called data with the imported feaure
import {PlotMatrix} with {marCasado as data} from "@observablehq/autoplot-matrix"
Insert cell
// Use the PlotMatrix function (expecting marCasado) to create a pair plot:
PlotMatrix(marCasado)

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
import {scale, asMatrix} from "@chrispahm/hierarchical-clustering"
Insert cell
Insert cell
Insert cell
// Create a scaled version of the numeric variables
//exclude 2 columns marCasado.map(({season, date_month,...rest})=> rest)
marCasadoScaled = scale(marCasado.map(({season, date_month,...rest})=> rest))
Insert cell
Insert cell
// Convert to an array of arrays, just containing the values (no keys):
marCasadoArray = marCasadoScaled.map(Object.values)
Insert cell
Insert cell
// Perform principal component analysis:
//marCasadoPCA = new ML.PCA(marCasadoArray,{scale:true})
marCasadoPCA = new ML.PCA(marCasadoArray)
Insert cell
Insert cell
Insert cell
// Get variance explained by each PC:
marCasadoPCA.getExplainedVariance()
//35% of variance is explained by principal component 1
//19.9% of variance is explained by principal component 2
Insert cell
Insert cell
// Get cumulative variance explained:
marCasadoPCA.getCumulativeVariance()
//almost 70% of variance is captured with 3 principal components
Insert cell
Insert cell
Insert cell
Insert cell
// Import viewof loadings from the notebook, with marCasadoScaled as food_scaled:
import {viewof loadings} with {marCasadoScaled as food_scaled} from "@chrispahm/principal-component-analysis"
Insert cell
// Look at viewof loadings:
viewof loadings
Insert cell
// import viewof scores from the notebook, with marCasadoScaled as food_scaled and marCasado as food:
import {viewof scores} with {marCasadoScaled as food_scaled, marCasado as food} from "@chrispahm/principal-component-analysis"
Insert cell
// Look at viewof scores:
viewof scores
Insert cell
//It is looking for a column called Name and we have date_month instead
Insert cell
// Do some wrangling to get the month and season alongside scores:
scoresCombined = scores.map((d,i) => ({...d
, Name:marCasado[i].date_month
, season:marCasado[i].season}))
Insert cell
// Create a PCA biplot with the scores and loadings
Plot.plot({
marks:[
Plot.dot(scoresCombined, {x:"PC1", y:"PC2", fill: "season", r:4})
],
color:{legend:true}
})
Insert cell
// Create a PCA biplot with the scores and loadings
Plot.plot({
marks:[
Plot.dot(scoresCombined, {x:"PC1", y:"PC2", fill: "season", r:4}),
Plot.arrow(loadings, {x1:0
, x2: d=> d.PC1*scalingFactor
, y1:0
, y2: d=> d.PC2*scalingFactor})
],
color:{legend:true}
})
Insert cell
// Create a PCA biplot with the scores and loadings
Plot.plot({
marks:[
Plot.dot(scoresCombined, {x:"PC1", y:"PC2", fill: "season", r:4}),
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+1),
y: d => d.PC2*(scalingFactor+1),
text: "Variable"})
],
color:{legend:true}
})
Insert cell
scalingFactor = 5
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Alternatively (without an import):

// loadingsOption = marCasadoPCA
// .getEigenvectors()
// .data.map((d, i) => ({
// PC1: d[0],
// PC2: d[1],
// Variable: Object.keys(marCasadoScaled[0])[i]
// }))
Insert cell
// Alternatively to get score (projections into PC space):

// scoresOption = marCasadoPCA.predict(marCasadoArray).data.map((d,i) => ({month: marCasado[i].month,
// season: marCasado[i].season,
// PC1: d[0],
// PC2: d[1]}))
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