> For the complete documentation index, see [llms.txt](https://laboratory-of-lipid-metabolism-a.gitbook.io/omics-data-visualization-in-r-and-python/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://laboratory-of-lipid-metabolism-a.gitbook.io/omics-data-visualization-in-r-and-python/metabolites-and-lipids-descriptive-statistics-analysis-in-python/scatter-plots-and-linear-regression.md).

# Scatter plots and linear regression

## Required packages

The required packages for this section are pandas, seaborn and statsmodels. These can be installed with the following command in the command window (Windows) / terminal (Mac).

```
pip install pandas seaborn statsmodels
```

## Loading the data

We will again use the demo lipidomics dataset:

{% file src="/files/O8GcvqGYOzpXcfU7NayN" %}

Load the dataset into a Pandas DataFrame named df as described in the basic plotting section:

```python
import pandas as pd
df = pd.read_excel("Lipidomics_dataset.xlsx", decimal=",")
df.set_index("Sample Name", inplace=True)
```

## Basic scatterplots

Creating a basic scatterplot with Pandas is as simple as:

```python
df.plot.scatter(x="TG 50:3", y="TG 50:4");
```

<figure><img src="/files/L7W6StY7kD1VCL8sN4hI" alt=""><figcaption></figcaption></figure>

Related lipid species tend to show high correlation! Lets check if this behaviour is consistent within our sample subgroups, by passing the Label column into the c parameter of the scatter function. Before we can do this, we must make sure that the Label column is converted to panda's categorical data type:

```python
df["Label"] = df["Label"].astype("category")
df.plot.scatter(x="TG 50:3", y="TG 50:4", c="Label", colormap='viridis');
```

<figure><img src="/files/iZKUP103INrQFqQ8v24J" alt=""><figcaption></figcaption></figure>

There does seem to be a slight difference in the way these 2 lipid species correlate in the different sample groups. We'll investigate correlations in more detail in the chapter on correlation analysis.

## Linear regression

Currently Pandas does not offer an option for adding linear regression to scatterplots. Fortunately, the Seaborn package makes this super easy:

```python
import seaborn as sns
import matplotlib.pyplot as plt
sns.lmplot(data=df, x="TG 50:3", y="TG 50:4", ci=None);
plt.show()
```

<figure><img src="/files/7xOcVaYgid7SzIHeJ4Er" alt=""><figcaption></figcaption></figure>

If we want to see the confidence intervals, just leave out "ci=None":

<figure><img src="/files/Dlp7haB9UOK0rufXTQ3D" alt=""><figcaption></figcaption></figure>

And to get separate regressions for the different sample groups, we can just pass "Label" to the hue parameter:

```python
sns.lmplot(data=df,x="TG 50:3", y="TG 50:4", hue="Label");
plt.show()
```

<figure><img src="/files/fyIlMfzwGuRDDKySwPMQ" alt=""><figcaption></figcaption></figure>

For a more formal regression analysis, with estimation of R-squared, the alpha and beta parameters, and calculation of statistical significance, the statsmodels package offers everything we need.

{% code lineNumbers="true" %}

```python
import statsmodels.formula.api as sm

df2 = df.copy()
df2.columns = df2.columns.str.rstrip()
df2.columns = df2.columns.str.replace(' ', '_')
df2.columns = df2.columns.str.replace(':', '_')

mod = sm.ols(formula="CE_16_1 ~ CE_16_0", data=df2)
res = mod.fit()
print(res.summary())
```

{% endcode %}

Let's go through the above code step by step. Statsmodels does not play nice with white spaces or special characters in the column (lipids) names, so we'll make a copy of our DataFrame named df2 (code line 3) in which we remove any trailing whitespaces (line 4),  we substitute spaces and colons with an underscore (lines 5 and 6). Next we'll use the ordinary least squares regression model from statsmodels (sm.ols) and we'll pass in a formula and the DataFrame we just created. The syntax of the formula is similar to R, where we first state the dependent variable, separated by a tilde symbol "\~" from the independent variable(s). Finally we call the fit() method on the model and print the summary results:

<figure><img src="/files/aGALJcPF9qoFQDLhnH6o" alt=""><figcaption></figcaption></figure>

From the summary we can read out that the alpha value (the intercept) is -1.8748 and the beta is 0.2208. Both of these parameters have a p-value smaller then 0.001 (The exact p-values can be obtained with res.pvalues) and the R-squared is 0.874.&#x20;
