Published
Edited
Oct 5, 2022
1 fork
Importers
1 star
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
Insert cell
ss.sampleCorrelation(
data.map((item) => item.x1),
data.map((item) => item.y1)
)
Insert cell
Insert cell
dataLong = {
const arrayTable = data.map((item) => Object.values(item)); //convert to an array (removing column headings)
const resultTable = [];
for (let i = 0; i < 4; i++) {
for (let rowArray of arrayTable) {
resultTable.push({
x: rowArray[i * 2],
y: rowArray[i * 2 + 1],
set: i + 1
});
}
}
return resultTable;
}
Insert cell
Insert cell
Insert cell
T = require("@tidyjs/tidy/dist/umd/tidy.min.js")
Insert cell
ss = require("simple-statistics")
Insert cell
Insert cell
summaryStatistics = T.tidy(
dataLong,
T.groupBy(
"set",
T.summarize({
//TidyJs's built-in T.mean() function, in this case that finds the mean of all y values in the subset
yMean: T.mean("y"),

xMean: T.mean("x"),

yMean: T.mean("y"),

xVariance: T.variance("x"),

yVariance: T.variance("y"),

//Here, I've written an inline lamba function (a one liner) where TidyJS provides the subset of rows
//and I extract the xs and ys as required for simple-statistics' sampleCorrelation
//function https://simplestatistics.org/docs/#sampleCorrelation
correlation: (subset) =>
ss.sampleCorrelation(
subset.map((item) => item.x),
subset.map((item) => item.y)
),

//linear regression grident
regressionM: (subset) =>
ss.linearRegression(subset.map((item) => [item.x, item.y])).m,

//linear regression y intercept
regressionB: (subset) =>
ss.linearRegression(subset.map((item) => [item.x, item.y])).b,

//Here, I've written a multi-line inline function (between braces with a return statement)
//which calculates the rSquared value function https://simplestatistics.org/docs/#rSquared
rSquared: (subset) => {
const samples = subset.map((item) => [item.x, item.y]);
const regressionLine = ss.linearRegressionLine(
ss.linearRegression(samples)
);
return ss.rSquared(samples, regressionLine);
}
})
)
)
Insert cell
Inputs.table(summaryStatistics)
Insert cell
Insert cell
vlEmbed = require("vega-embed")
Insert cell
Insert cell
summaryStatisticsLonger = T.tidy(
summaryStatistics,
T.pivotLonger({ cols: "-set", namesTo: "statistic", valuesTo: "value" })
)
Insert cell
Insert cell
summaryStatsChart=vlEmbed({
data: { values: summaryStatisticsLonger },
mark: "bar",
encoding: {
y: { field: "value", type: "quantitative" },
x: { field: "set" },
facet: { field: "statistic" },
color: { field: "set" }
}
})
Insert cell
Insert cell
vlEmbed({
data: { values: dataLong },
mark: "point",
encoding: {
x: { field: "x", type: "quantitative" },
y: { field: "y", type: "quantitative" },
facet: { field: "set" },
color: { field: "set" }
}
})
Insert cell
Insert cell
Insert cell
Insert cell
moreSummaryStatisticsLonger = T.tidy(
moreSummaryStatistics,
T.pivotLonger({ cols: "-set", namesTo: "statistic", valuesTo: "value" })
)
Insert cell
vlEmbed({
data: { values: moreSummaryStatisticsLonger },
mark: "bar",
encoding: {
y: { field: "value", type: "quantitative" },
x: { field: "set" },
facet: { field: "statistic" },
color: { field: "set" }
}
})
Insert cell
Insert cell
vl
.markBar()
.data(summaryStatisticsLonger)
.encode(
vl.x().fieldO("set"),
vl.y().fieldQ("value"),
vl.facet().fieldO("statistic"),
vl.color().fieldN("set")
)
.render()
Insert cell
vl
.markPoint()
.data(dataLong)
.encode(
vl.x().fieldQ("x"),
vl.y().fieldQ("y"),
vl.facet().fieldO("set"),
vl.color().fieldN("set")
)
.render()
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