{ "cells": [ { "cell_type": "markdown", "id": "eff35ebd-8fa2-4b5d-8338-822e440c4289", "metadata": {}, "source": [ "# Smooth Statistics Labels\n", "\n", "Smooth layers (e.g., `geom_smooth()`) now compute additional statistics that describe the fitted model quality and the fitted equation. These values are available as smooth stat variables (such as `..r2..` and `..adjr2..`) and can be displayed in a layer annotation via `smooth_labels`.\n", "\n", "`smooth_labels` is an annotation helper designed specifically for smooth layers. It extends `layer_labels`, so the familiar label-building API (`line()`, `format()`, `size()`, etc.) works the same way, while adding support for smooth-stat variables and the equation marker.\n", "\n", "Supported variables and markers:\n", "\n", "* `..r2..` — **R² (coefficient of determination)**. A goodness-of-fit measure showing what fraction of the variance in the response is explained by the fitted model. Values are typically between 0 and 1 (higher means the model explains more of the observed variation).\n", "* `..adjr2..` — **adjusted R²**. A variant of R² that accounts for model complexity: it penalizes adding extra terms/parameters and is therefore more suitable for comparing models with different numbers of predictors (e.g., different polynomial degrees). Adjusted R² can be lower than R² and may even be negative for a very poor fit.\n", "* `~eq` — **fitted equation**. Inserts the model equation into the annotation (can be configured with `eq()`)." ] }, { "cell_type": "code", "execution_count": 1, "id": "7a5c9a56-3edd-4109-ad8c-ef9ae1d1b64c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "from lets_plot import *\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "id": "bbb9afae-832d-4bdf-8a12-363d2dd26462", "metadata": {}, "outputs": [], "source": [ "np.random.seed(42)" ] }, { "cell_type": "code", "execution_count": 3, "id": "cc74a604-e64a-4e3c-890e-8e12d244886f", "metadata": {}, "outputs": [], "source": [ "t = np.linspace(0, 1, 100)\n", "mean = 1 + np.zeros(2)\n", "cov = np.eye(2)\n", "x, y = np.random.multivariate_normal(mean, cov, t.size).T\n", "df = pd.DataFrame({'t': t, 'x': x, 'y': y})\n", "df = df.melt(id_vars=['t'], value_vars=['x', 'y'])" ] }, { "cell_type": "code", "execution_count": 4, "id": "a3383095-3f77-4a29-8e0b-cbc0fc6c85ba", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "