import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%0A%20%20%20%20from%20plotnine.data%20import%20penguins%0A%20%20%20%20from%20scipy%20import%20stats%0A%0A%20%20%20%20return%20go%2C%20make_subplots%2C%20mo%2C%20np%2C%20penguins%2C%20stats%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20penguins%2C%20stats)%3A%0A%20%20%20%20%23%20Load%20and%20clean%20Palmer%20Penguins%20dataset%0A%20%20%20%20df_penguins%20%3D%20penguins.dropna(subset%3D%5B%22bill_length_mm%22%2C%20%22bill_depth_mm%22%2C%20%22species%22%5D).copy()%0A%20%20%20%20df_penguins%5B%22species%22%5D%20%3D%20df_penguins%5B%22species%22%5D.astype(str)%0A%0A%20%20%20%20species_list%20%3D%20sorted(df_penguins%5B%22species%22%5D.unique().tolist())%0A%20%20%20%20species_colors%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22Adelie%22%3A%20%22%23f97316%22%2C%20%20%20%20%20%23%20Orange%0A%20%20%20%20%20%20%20%20%22Chinstrap%22%3A%20%22%238b5cf6%22%2C%20%20%23%20Purple%0A%20%20%20%20%20%20%20%20%22Gentoo%22%3A%20%22%2306b6d4%22%2C%20%20%20%20%20%23%20Teal%0A%20%20%20%20%7D%0A%0A%20%20%20%20%23%20Compute%20pooled%20correlation%20and%20linear%20regression%0A%20%20%20%20x_pooled%20%3D%20df_penguins%5B%22bill_length_mm%22%5D.values%0A%20%20%20%20y_pooled%20%3D%20df_penguins%5B%22bill_depth_mm%22%5D.values%0A%20%20%20%20slope_pooled%2C%20intercept_pooled%2C%20r_pooled%2C%20p_pooled%2C%20stderr_pooled%20%3D%20stats.linregress(x_pooled%2C%20y_pooled)%0A%0A%20%20%20%20%23%20Compute%20within-species%20statistics%0A%20%20%20%20within_stats%20%3D%20%7B%7D%0A%20%20%20%20for%20_sp%20in%20species_list%3A%0A%20%20%20%20%20%20%20%20_sub%20%3D%20df_penguins%5Bdf_penguins%5B%22species%22%5D%20%3D%3D%20_sp%5D%0A%20%20%20%20%20%20%20%20_x%20%3D%20_sub%5B%22bill_length_mm%22%5D.values%0A%20%20%20%20%20%20%20%20_y%20%3D%20_sub%5B%22bill_depth_mm%22%5D.values%0A%20%20%20%20%20%20%20%20_sl%2C%20_ic%2C%20_r%2C%20_p%2C%20_se%20%3D%20stats.linregress(_x%2C%20_y)%0A%20%20%20%20%20%20%20%20within_stats%5B_sp%5D%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22n%22%3A%20len(_sub)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22slope%22%3A%20_sl%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22intercept%22%3A%20_ic%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22r%22%3A%20_r%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22r_squared%22%3A%20_r%20**%202%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22p_val%22%3A%20_p%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mean_x%22%3A%20float(np.mean(_x))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mean_y%22%3A%20float(np.mean(_y))%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20df_penguins%2C%0A%20%20%20%20%20%20%20%20intercept_pooled%2C%0A%20%20%20%20%20%20%20%20r_pooled%2C%0A%20%20%20%20%20%20%20%20slope_pooled%2C%0A%20%20%20%20%20%20%20%20species_colors%2C%0A%20%20%20%20%20%20%20%20species_list%2C%0A%20%20%20%20%20%20%20%20within_stats%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20r_pooled%2C%20slope_pooled%2C%20species_list%2C%20within_stats)%3A%0A%20%20%20%20_rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20f%22%7C%20**Pooled%20(All%20Species)**%20%7C%20**%7Bwithin_stats%5B'Adelie'%5D%5B'n'%5D%20%2B%20within_stats%5B'Chinstrap'%5D%5B'n'%5D%20%2B%20within_stats%5B'Gentoo'%5D%5B'n'%5D%7D**%20%7C%20**%7Bslope_pooled%3A.3f%7D**%20%7C%20**%7Br_pooled%3A.3f%7D**%20%7C%20**%7Br_pooled**2%3A.3f%7D**%20%7C%20Negative%20Slope%20(Confounded)%20%7C%22%0A%20%20%20%20%5D%0A%20%20%20%20for%20_sp%20in%20species_list%3A%0A%20%20%20%20%20%20%20%20_ws%20%3D%20within_stats%5B_sp%5D%0A%20%20%20%20%20%20%20%20_rows.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7C%20%7B_sp%7D%20%7C%20%7B_ws%5B'n'%5D%7D%20%7C%20%7B_ws%5B'slope'%5D%3A%2B.3f%7D%20%7C%20%7B_ws%5B'r'%5D%3A%2B.3f%7D%20%7C%20%7B_ws%5B'r_squared'%5D%3A.3f%7D%20%7C%20Positive%20Slope%20(True%20Anatomy)%20%7C%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20_table_str%20%3D%20%22%5Cn%22.join(_rows)%0A%0A%20%20%20%20_table_md%20%3D%20mo.md(%0A%20%20%20%20%20%20%20%20%22%7C%20Group%20%7C%20Sample%20Size%20%24N%24%20%7C%20OLS%20Slope%20%24%5C%5Cbeta%24%20%7C%20Correlation%20%24r%24%20%7C%20Coefficient%20of%20Det%20%24R%5E2%24%20%7C%20Interpretation%20%7C%5Cn%22%0A%20%20%20%20%20%20%20%20%22%7C%20%3A---%20%7C%20%3A---%20%7C%20%3A---%20%7C%20%3A---%20%7C%20%3A---%20%7C%20%3A---%20%7C%5Cn%22%0A%20%20%20%20%20%20%20%20f%22%7B_table_str%7D%22%0A%20%20%20%20)%0A%0A%20%20%20%20_text_md%20%3D%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20Simpson's%20Paradox%3A%20A%20Case%20Study%20in%20Aesthetic%20Grouping%0A%0A%20%20%20%20The%20Palmer%20Penguins%20dataset%20illustrates%20**Simpson's%20Paradox**%20with%20extraordinary%20clarity%3A%0A%20%20%20%20-%20**Pooled%20correlation**%3A%20%24r%20%5Capprox%20-0.235%24%20(longer%20bills%20appear%20to%20have%20*shallower*%20depths).%0A%20%20%20%20-%20**Within-species%20correlation**%3A%20%24r%20%3E%200%24%20for%20all%20three%20species%20(%24%2B0.39%24%20for%20Adelie%2C%20%24%2B0.65%24%20for%20Chinstrap%2C%20%24%2B0.64%24%20for%20Gentoo).%0A%0A%20%20%20%20%23%23%23%20Mathematical%20Decomposition%20of%20Omitted%20Grouping%20Bias%0A%0A%20%20%20%20Let%20%24Y%24%20denote%20bill%20depth%2C%20%24X%24%20bill%20length%2C%20and%20%24Z%20%5Cin%20%5C%7B1%2C%202%2C%203%5C%7D%24%20the%20latent%20species%20indicator.%20Under%20linear%20assumptions%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20Y_i%20%3D%20%5Calpha%20%2B%20%5Cbeta_%7B%5Ctext%7Bwithin%7D%7D%20X_i%20%2B%20%5Csum_%7Bk%3D1%7D%5EK%20%5Cgamma_k%20%5Cmathbf%7B1%7D(Z_i%20%3D%20k)%20%2B%20%5Cepsilon_i%0A%20%20%20%20%24%24%0A%0A%20%20%20%20When%20we%20omit%20the%20grouping%20variable%20%24Z%24%20and%20fit%20the%20univariate%20regression%20%24Y_i%20%3D%20%5Calpha_0%20%2B%20%5Cbeta_%7B%5Ctext%7Bpooled%7D%7D%20X_i%20%2B%20u_i%24%2C%20the%20estimated%20coefficient%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbeta_%7B%5Ctext%7Bpooled%7D%7D%20%3D%20%5Cbeta_%7B%5Ctext%7Bwithin%7D%7D%20%2B%20%5Csum_%7Bk%3D1%7D%5EK%20%5Cgamma_k%20%5Cfrac%7B%5Coperatorname%7BCov%7D(%5Cmathbf%7B1%7D(Z_i%20%3D%20k)%2C%20X_i)%7D%7B%5Coperatorname%7BVar%7D(X_i)%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Because%20Gentoo%20penguins%20have%20significantly%20longer%20bills%20(%24%5Cmu_X%20%5Capprox%2047.5%24%20mm%20vs%20%2438.8%24%20mm%20for%20Adelie)%20but%20substantially%20shallower%20bills%20(%24%5Cmu_Y%20%5Capprox%2015.0%24%20mm%20vs%20%2418.3%24%20mm%20for%20Adelie)%2C%20%24%5Coperatorname%7BCov%7D(%5Cmathbf%7B1%7D(Z_i%20%3D%20%5Ctext%7BGentoo%7D)%2C%20X_i)%20%3E%200%24%20while%20%24%5Cgamma_%7B%5Ctext%7BGentoo%7D%7D%20%3C%200%24.%20This%20negative%20covariance%20term%20overwhelms%20the%20positive%20biological%20slope%20%24%5Cbeta_%7B%5Ctext%7Bwithin%7D%7D%20%3E%200%24!%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(%0A%20%20%20%20df_penguins%2C%0A%20%20%20%20go%2C%0A%20%20%20%20intercept_pooled%2C%0A%20%20%20%20make_subplots%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20np%2C%0A%20%20%20%20slope_pooled%2C%0A%20%20%20%20species_colors%2C%0A%20%20%20%20species_list%2C%0A%20%20%20%20within_stats%2C%0A)%3A%0A%20%20%20%20%23%20Create%20side-by-side%20Plotly%20comparison%3A%20Pooled%20vs%20Grouped%20Grammar%20Layer%0A%20%20%20%20_fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D1%2C%0A%20%20%20%20%20%20%20%20cols%3D2%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Layer%201-4%3A%20Data%20%2B%20Geom%20Point%20%2B%20Pooled%20Stat%20Smooth%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Layer%205%3A%20Aesthetic%20Mapping%20color%20%3D%20species%20%2B%20Grouped%20Smooth%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20horizontal_spacing%3D0.10%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%20Plot%3A%20Pooled%20scatter%20and%20regression%20line%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Ddf_penguins%5B%22bill_length_mm%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Ddf_penguins%5B%22bill_depth_mm%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D7%2C%20color%3D%22%2364748b%22%2C%20opacity%3D0.65)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Penguins%20(Ungrouped)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22Bill%20Length%3A%20%25%7Bx%7D%20mm%3Cbr%3EBill%20Depth%3A%20%25%7By%7D%20mm%3Cextra%3E%3C%2Fextra%3E%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_x_grid_pooled%20%3D%20np.linspace(df_penguins%5B%22bill_length_mm%22%5D.min()%2C%20df_penguins%5B%22bill_length_mm%22%5D.max()%2C%20100)%0A%20%20%20%20_y_pred_pooled%20%3D%20slope_pooled%20*%20_x_grid_pooled%20%2B%20intercept_pooled%0A%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D_x_grid_pooled%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_y_pred_pooled%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23dc2626%22%2C%20width%3D2.5%2C%20dash%3D%22solid%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Pooled%20OLS%20(Slope%3A%20-0.08)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22Length%3A%20%25%7Bx%3A.1f%7D%20mm%3Cbr%3EPredicted%20Depth%3A%20%25%7By%3A.1f%7D%20mm%3Cextra%3E%3C%2Fextra%3E%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Right%20Plot%3A%20Grouped%20by%20species%20with%20colored%20points%20and%20per-species%20regression%20lines%0A%20%20%20%20for%20_sp%20in%20species_list%3A%0A%20%20%20%20%20%20%20%20_sub%20%3D%20df_penguins%5Bdf_penguins%5B%22species%22%5D%20%3D%3D%20_sp%5D%0A%20%20%20%20%20%20%20%20_color%20%3D%20species_colors%5B_sp%5D%0A%20%20%20%20%20%20%20%20_ws%20%3D%20within_stats%5B_sp%5D%0A%0A%20%20%20%20%20%20%20%20%23%20Points%0A%20%20%20%20%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3D_sub%5B%22bill_length_mm%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D_sub%5B%22bill_depth_mm%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D7%2C%20color%3D_color%2C%20opacity%3D0.75)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22%7B_sp%7D%20(r%20%3D%20%7B_ws%5B'r'%5D%3A.2f%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3Df%22%3Cb%3E%7B_sp%7D%3C%2Fb%3E%3Cbr%3ELength%3A%20%25%7B%7Bx%7D%7D%20mm%3Cbr%3EDepth%3A%20%25%7B%7By%7D%7D%20mm%3Cextra%3E%3C%2Fextra%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20%23%20Fitted%20regression%20line%0A%20%20%20%20%20%20%20%20_x_grid_sp%20%3D%20np.linspace(_sub%5B%22bill_length_mm%22%5D.min()%2C%20_sub%5B%22bill_length_mm%22%5D.max()%2C%2080)%0A%20%20%20%20%20%20%20%20_y_pred_sp%20%3D%20_ws%5B%22slope%22%5D%20*%20_x_grid_sp%20%2B%20_ws%5B%22intercept%22%5D%0A%0A%20%20%20%20%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3D_x_grid_sp%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D_y_pred_sp%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D_color%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hoverinfo%3D%22skip%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22The%20Layered%20Grammar%20in%20Action%3A%20Resolving%20Simpson's%20Paradox%22%2C%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D480%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D60%2C%20r%3D40%2C%20t%3D70%2C%20b%3D60)%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(x%3D0.80%2C%20y%3D0.05%2C%20bgcolor%3D%22rgba(255%2C255%2C255%2C0.9)%22%2C%20bordercolor%3D%22%23cbd5e1%22%2C%20borderwidth%3D1)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Bill%20Length%20(mm)%22%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Bill%20Length%20(mm)%22%2C%20row%3D1%2C%20col%3D2)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Bill%20Depth%20(mm)%22%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Bill%20Depth%20(mm)%22%2C%20row%3D1%2C%20col%3D2)%0A%0A%20%20%20%20_md%20%3D%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Interactive%20Comparison%3A%20Pooled%20vs%20Grouped%20Grammar%20Layer%0A%0A%20%20%20%20In%20the%20grammar%20of%20graphics%2C%20grouping%20is%20**not**%20a%20preprocessing%20step%20(such%20as%20splitting%20the%20dataset%20into%20separate%20data%20structures).%20Instead%2C%20it%20is%20an%20**aesthetic%20mapping**%3A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20aes(x%3D%22bill_length_mm%22%2C%20y%3D%22bill_depth_mm%22%2C%20color%3D%22species%22)%0A%20%20%20%20%60%60%60%0A%20%20%20%20Downstream%20statistical%20layers%20(%60geom_smooth%60)%20inherit%20this%20mapping%20automatically%2C%20fitting%20separate%20regressors%20across%20the%20data%20partition%20defined%20by%20the%20scale.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(%0A%20%20%20%20df_penguins%2C%0A%20%20%20%20go%2C%0A%20%20%20%20make_subplots%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20np%2C%0A%20%20%20%20species_colors%2C%0A%20%20%20%20species_list%2C%0A%20%20%20%20within_stats%2C%0A)%3A%0A%20%20%20%20%23%20Faceted%20Small%20Multiples%20visualization%0A%20%20%20%20_fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D1%2C%0A%20%20%20%20%20%20%20%20cols%3D3%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D%5Bf%22Species%3A%20%7B_sp%7D%20(N%20%3D%20%7Bwithin_stats%5B_sp%5D%5B'n'%5D%7D)%22%20for%20_sp%20in%20species_list%5D%2C%0A%20%20%20%20%20%20%20%20shared_yaxes%3DTrue%2C%0A%20%20%20%20%20%20%20%20horizontal_spacing%3D0.04%2C%0A%20%20%20%20)%0A%0A%20%20%20%20for%20_idx%2C%20_sp%20in%20enumerate(species_list%2C%20start%3D1)%3A%0A%20%20%20%20%20%20%20%20_sub%20%3D%20df_penguins%5Bdf_penguins%5B%22species%22%5D%20%3D%3D%20_sp%5D%0A%20%20%20%20%20%20%20%20_color%20%3D%20species_colors%5B_sp%5D%0A%20%20%20%20%20%20%20%20_ws%20%3D%20within_stats%5B_sp%5D%0A%0A%20%20%20%20%20%20%20%20%23%20Points%0A%20%20%20%20%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3D_sub%5B%22bill_length_mm%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D_sub%5B%22bill_depth_mm%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D7%2C%20color%3D_color%2C%20opacity%3D0.75)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3D_sp%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3Df%22%3Cb%3E%7B_sp%7D%3C%2Fb%3E%3Cbr%3ELength%3A%20%25%7B%7Bx%7D%7D%20mm%3Cbr%3EDepth%3A%20%25%7B%7By%7D%7D%20mm%3Cextra%3E%3C%2Fextra%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20col%3D_idx%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20%23%20Fitted%20regression%20line%0A%20%20%20%20%20%20%20%20_x_grid%20%3D%20np.linspace(_sub%5B%22bill_length_mm%22%5D.min()%2C%20_sub%5B%22bill_length_mm%22%5D.max()%2C%2050)%0A%20%20%20%20%20%20%20%20_y_pred%20%3D%20_ws%5B%22slope%22%5D%20*%20_x_grid%20%2B%20_ws%5B%22intercept%22%5D%0A%0A%20%20%20%20%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3D_x_grid%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D_y_pred%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D_color%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hoverinfo%3D%22skip%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20col%3D_idx%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20_fig.update_xaxes(title_text%3D%22Bill%20Length%20(mm)%22%2C%20range%3D%5B30%2C%2060%5D%2C%20row%3D1%2C%20col%3D_idx)%0A%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Bill%20Depth%20(mm)%22%2C%20range%3D%5B12%2C%2023%5D%2C%20row%3D1%2C%20col%3D1)%0A%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Layer%207%3A%20Faceting%20(Small%20Multiples%20Conditioning%20with%20Shared%20Coordinate%20Scales)%22%2C%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D380%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D60%2C%20r%3D40%2C%20t%3D70%2C%20b%3D60)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_md%20%3D%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20Layer%207%3A%20Faceting%20and%20Small%20Multiples%0A%0A%20%20%20%20**Faceting**%20partitions%20the%20dataset%20into%20disjoint%20subsets%20conditioned%20on%20discrete%20categorical%20variables%2C%20rendering%20identical%20sub-geometries%20across%20a%20shared%20coordinate%20grid%20(%60facet_wrap%60%20or%20%60facet_grid%60).%0A%0A%20%20%20%20By%20aligning%20the%20%24x%24%20and%20%24y%24%20axis%20limits%20identically%20across%20all%20facets%2C%20the%20user%20can%20immediately%20perceive%20both%3A%0A%20%20%20%201.%20The%20**within-group%20covariance**%3A%20Every%20species%20exhibits%20a%20positive%20morphological%20relationship%20between%20length%20and%20depth.%0A%20%20%20%202.%20The%20**between-group%20centroid%20displacement**%3A%20Gentoo%20penguins%20form%20a%20distinct%20cluster%20with%20significantly%20higher%20bill%20length%20and%20shallower%20depth.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
46df7e0c77e74cf87f691cc78e5f456a