Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 37 additions & 35 deletions src/party/sda.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,78 @@ index: true

# Simple data analysis

[Simple data analysis](https://nshiab.github.io/simple-data-analysis/), a library maintained by computational journalist Nael Shiab, purports to be “a high-performance and convenient solution in JavaScript for data analysis. It's based on DuckDB and inspired by Pandas (Python) and the Tidyverse (R).”
[Simple data analysis](https://github.com/nshiab/simple-data-analysis), a library maintained by computational journalist Nael Shiab, purports to be “a high-performance and convenient solution in JavaScript for data analysis. Its based on DuckDB and inspired by Pandas (Python) and the Tidyverse (R).”

```js echo
import {SimpleDB} from "npm:simple-data-analysis@2";
import { SimpleWebDB } from "https://esm.sh/jsr/@nshiab/simple-data-analysis@3.15.3/web";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd want this to work:

import {SimpleWebDB} from "jsr:@nshiab/simple-data-analysis@3/web";

unfortunately it fails with Missing "./web" specifier in "@jsr/nshiab__simple-data-analysis" package, which may be an issue with the package or with framework (?). I'm not sure how to fix it yet.

Note that ideally, I'd want this:

import {SimpleWebDB} from "jsr:@nshiab/simple-data-analysis@3";

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also want to use a self-hosted version of DuckDB, instead of loading it from jsdeliver

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I see in the console when I switch to import { SimpleWebDB } from "jsr:@nshiab/simple-data-analysis@3.15.3/web";, it's a problem with the library. I'll investigate and come back to you!

```

_Note: the API of this library has changed in version 3. This page would need a refresher._

```js echo
// We start a new instance of SimpleDB
const sdb = new SimpleDB();

// We load daily temperatures for three cities.
// We put the data in the table dailyTemperatures.
await sdb.loadData(
"dailyTemperatures",
"https://raw.githubusercontent.com/nshiab/simple-data-analysis/main/test/data/files/dailyTemperatures.csv"
// We start a new instance of SimpleWebDB
const sdb = new SimpleWebDB();

// We create a new table.
const tableTemperature = sdb.newTable("temperature");

// We fetch daily temperatures for three cities.
await tableTemperature.fetchData(
"https://raw.githubusercontent.com/nshiab/simple-data-analysis/main/test/data/files/dailyTemperatures.csv",
);
Comment on lines +21 to 23
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: host this file in /data/


// We compute the decade from each date
// and put the result in the decade column.
await sdb.addColumn(
"dailyTemperatures",
await tableTemperature.addColumn(
"decade",
"integer",
"FLOOR(YEAR(time)/10)*10" // This is SQL
"FLOOR(YEAR(time)/10)*10", // This is SQL
);

// We summarize the data by computing
// the average dailyTemperature
// the average temperature
// per decade and per city.
await sdb.summarize("dailyTemperatures", {
await tableTemperature.summarize({
values: "t",
categories: ["decade", "id"],
summaries: "mean"
summaries: "mean",
});

// We run linear regressions
// to check for trends.
await sdb.linearRegressions("dailyTemperatures", {
await tableTemperature.linearRegressions({
x: "decade",
y: "mean",
categories: "id",
decimals: 4
decimals: 4,
});

// The dailyTemperature table does not have
// The tableTemperature does not have
// the name of the cities, just the ids.
// We load another file with the names
// in the table cities.
await sdb.loadData(
"cities",
"https://raw.githubusercontent.com/nshiab/simple-data-analysis/main/test/data/files/cities.csv"
// in a new table.

// We create a new table.
const tableCities = sdb.newTable("cities");

// We load another file with
// the cities ids and names.
await tableCities.fetchData(
"https://raw.githubusercontent.com/nshiab/simple-data-analysis/main/test/data/files/cities.csv",
);

// We join the two tables. By default,
// join searches for a common column
// and does a left join. The result is stored in
// the left table (dailyTemperatures here).
await sdb.join("dailyTemperatures", "cities");
// We join the two tables based on the ids.
// By default, join will automatically look
// for a common column, do a left join, and
// put the result in the left table.
await tableCities.join(tableTemperature);

// We select the columns of interest
// after the join operation.
await sdb.selectColumns("dailyTemperatures", ["city", "slope", "yIntercept", "r2"]);
// We select the columns of interest.
await tableCities.selectColumns(["city", "slope", "yIntercept", "r2"]);

// We log the results table.
await sdb.logTable("dailyTemperatures");
await tableCities.logTable();

// We store the data in a variable.
const results = await sdb.getData("dailyTemperatures");
const results = await tableCities.getData();

display(Inputs.table(results));
```