{ "cells": [ { "cell_type": "markdown", "id": "vocal-lemon", "metadata": {}, "source": [ "Nonlinear Regression Part 2\n", "=========================" ] }, { "attachments": {}, "cell_type": "markdown", "id": "japanese-vaccine", "metadata": {}, "source": [ "``````{admonition} Overview\n", ":class: overview\n", "\n", "Questions:\n", "\n", "* How can I perform statistically robust non-linear regression in a Jupyter notebook?\n", "\n", "* How can I prepare accurate and attractive plots of my regression results that include the best-fit line?\n", "\n", "Objectives:\n", "\n", "* Import data from a csv file.\n", "\n", "* Create a function based on the equation to be fitted.\n", "\n", "* Generate the best-fit curve with parameters and statistical descriptors.\n", "\n", "* Create plots of the data.\n", "``````" ] }, { "cell_type": "markdown", "id": "loaded-tomato", "metadata": {}, "source": [ "## Data Fitting\n", "In an earlier module, we used the `linregress` function from the scipy.stats library to perform linear regression. This is sufficient for a least squares linear fit to the data. Python contains tools that also enable us to fit data to an equation that we have defined, with two or more paramaters. So it is ideal for nonlinear curve fitting to the Michaelis-Menten equation.\n", "\n", "In this module, we will use some familiar tools: importing csv data into a pandas dataframe and plotting results using the matplotlib.pyplot library. We will also create a function based on the Michaelis-Menten equation. The exciting part is learning how to fit the data to the function using `curve_fit` from scipy.optimize.\n", "\n", "Here are the steps in our process:\n", "\n", "1. Import the libraries and functions that you need, including `curve_fit` from scipy.optimize.\n", "1. Set up a pandas dataframe from the MM_data.csv file you saved from the previous module.\n", "1. Create your independent and dependent variables for curve fitting.\n", "1. Create a function for the equation you want to fit. The function should accept as inputs the independent variable(s) and all the parameters to be fit.\n", "1. Use the function `curve_fit` from the scipy.optimize library to fit your data.\n", "1. Extract the fit parameters from the output of `curve_fit`.\n", "1. Use your function to calculate y values using your fit model to see how well your model fits the data.\n", "1. Graph your original data and the best-fit equation." ] }, { "cell_type": "code", "execution_count": 6, "id": "opposite-marathon", "metadata": {}, "outputs": [], "source": [ "# Import the libraries and functions that you need, including curve_fit from scipy.optimize.\n", "import os\n", "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from scipy.optimize import curve_fit" ] }, { "cell_type": "markdown", "id": "moderate-russell", "metadata": {}, "source": [ "## The Michaelis-Menten equation\n", "\n", "The Michaelis-Menten equation (which is probably very familiar to you) describes kinetics for enzyme-catalyzed reactions. We will use the simplest version of the equation, based on initial reaction velocities with a single substrate.\n", "\n", "In our equation, there are two parameters, V$_{max}$ (maximum velocity) and K$_m$ (the Michaelis constant, which equals the substrate concentration at half maximal velocity), an independent variable, \\[S\\] = substrate concentration, and a dependent variable, V$_i$, the initial velocity.\n", "\n", "$$\n", "V_i = \\frac{V_{max} * [S]}{K_m + [S]}\n", "$$\n", "\n", "We will define the two parameters and two variables for our equation:\n", "\n", "- Vmax = the maximum velocity (the first parameter)\n", "- Km = the Michaelis constant (the second parameter)\n", "- SubConc = substrate concentration (the independent variable)\n", "- Vi = initial velocity (the dependent variable)" ] }, { "cell_type": "code", "execution_count": 7, "id": "traditional-sitting", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | pNPP (mM) | \n", "slopes | \n", "initial velocities | \n", "
---|---|---|---|
0 | \n", "20.00 | \n", "0.514091 | \n", "34.272718 | \n", "
1 | \n", "10.00 | \n", "0.503140 | \n", "33.542678 | \n", "
2 | \n", "7.00 | \n", "0.470435 | \n", "31.362310 | \n", "
3 | \n", "4.00 | \n", "0.417021 | \n", "27.801416 | \n", "
4 | \n", "2.00 | \n", "0.342445 | \n", "22.829652 | \n", "
5 | \n", "1.00 | \n", "0.246260 | \n", "16.417302 | \n", "
6 | \n", "0.70 | \n", "0.202843 | \n", "13.522863 | \n", "
7 | \n", "0.40 | \n", "0.136443 | \n", "9.096204 | \n", "
8 | \n", "0.20 | \n", "0.076442 | \n", "5.096143 | \n", "
9 | \n", "0.10 | \n", "0.078070 | \n", "5.204653 | \n", "
10 | \n", "0.05 | \n", "0.022319 | \n", "1.487957 | \n", "