<jupyter_start><jupyter_text>SmoothingSmoothing can help to discover trends that otherwise might be hard to see in raw data.<jupyter_code>import pandas as pd

from lets_plot import *
LetsPlot.setup_html()
mpg_df = pd.read_csv('https://github.com/JetBrains/lets-plot/raw/master/docs/examples/data/mpg.csv')
mpg_df<jupyter_output><empty_output><jupyter_text>The default smoothing method is `'linear model'` (or `'lm'`)<jupyter_code>mpg_plot = ggplot(mpg_df, aes(x='displ', y='hwy'))
mpg_plot + geom_point() + geom_smooth()<jupyter_output><empty_output><jupyter_text>`LOESS` model does seem to better fit MPG data than the linear model.<jupyter_code>mpg_plot + geom_point() + geom_smooth(method='loess', size=1)<jupyter_output><empty_output><jupyter_text>Applying smoothing to groupsLet's map the vehicle `drivetrain type` (variable 'drv') to the color of points.This makes it easy to see that points with the same type of the drivetrain are forming some kind of groups or clusters.<jupyter_code>mpg_plot + geom_point(aes(color='drv'))\
         + geom_smooth(aes(color='drv'), method='loess', size=1)<jupyter_output><empty_output><jupyter_text>Apply linear model with 2nd degree polynomial.As `LOESS` prediction looks a bit weird let's try 2nd degree polinomial regression.<jupyter_code>mpg_plot + geom_point(aes(color='drv'))\
         + geom_smooth(aes(color='drv'), method='lm', deg=2, size=1)<jupyter_output><empty_output><jupyter_text>Using `as_discrete()` function with numeric data seriesIn the previous examples we were using a discrete (or categorical) variable `'drv'` to split the data into a groups.Now let's try to use a numeric variable `'cyl'` for the same purpose.<jupyter_code>mpg_plot + geom_point(aes(color='cyl'))\
         + geom_smooth(aes(color='cyl'), method='lm', deg=2, size=1)<jupyter_output><empty_output><jupyter_text>Easy to see that the data wasn't split into groups. `Lets-Plot` offers two solutions in this situation: * Use the `group` aesthetic * Use the `as_discrete()` function The **group** aesthetic helps to create a groups.<jupyter_code>mpg_plot + geom_point(aes(color='cyl'))\
         + geom_smooth(aes(color='cyl', group='cyl'), method='lm', deg=2, size=1)<jupyter_output><empty_output><jupyter_text>The **as_discrete('cyl')** function will "annotate" the `'cyl'` variable as `discrete`.This leads to creation of the groups and to assigning of a `discrete` color scale instead of a `continuous`.<jupyter_code>from lets_plot.mapping import as_discrete

mpg_plot + geom_point(aes(color='cyl'))\
         + geom_smooth(aes(color=as_discrete('cyl')), method='lm', deg=2, size=1)<jupyter_output><empty_output><jupyter_text>Effect of `span` parameter on the "wiggliness" the LOESS smoother.The span is the fraction of points used to fit each local regression.Small numbers make a wigglier curve, larger numbers make a smoother curve.<jupyter_code>import math
import random
import numpy as np
n = 150
x_range = np.arange(-2 * math.pi, 2 * math.pi, 4 * math.pi / n)
y_range = np.sin(x_range) + np.array([random.uniform(-.5, .5) for i in range(n)])
df = pd.DataFrame({ 'x' : x_range, 'y' : y_range })
p = ggplot(df, aes(x='x', y='y')) + geom_point(shape=21, fill='yellow', color='#8c564b')
p1 = p + geom_smooth(method='loess', size=1.5, color='#d62728') + ggtitle('default (span = 0.5)')
p2 = p + geom_smooth(method='loess', span=.2, size=1.5, color='#9467bd') + ggtitle('span = 0.2')
p3 = p + geom_smooth(method='loess', span=.7, size=1.5, color='#1f77b4') + ggtitle('span = 0.7')
p4 = p + geom_smooth(method='loess', span=1, size=1.5, color='#2ca02c') + ggtitle('span = 1')

bunch = GGBunch()
bunch.add_plot(p1, 0, 0, 400, 300)
bunch.add_plot(p2, 400, 0, 400, 300)
bunch.add_plot(p3, 0, 300, 400, 300)
bunch.add_plot(p4, 400, 300, 400, 300)
bunch.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1456641341367566, 'fraction_numerical': 0.02829447209850668, 'mean_word_length': 6.314176245210728, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '181362', 'license': 'MIT', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1356, 'n_words': 360, 'path': 'docs/examples/jupyter-notebooks/geom_smooth.ipynb', 'repo_name': 'CyberFlameGO/lets-plot', 'size': '3817'}