# A simple class for linear regression

#### Question P4.6.4

Design and implement a class, Experiment, to read in and store a simple series of $(x,y)$ data as pylab (i.e. NumPy) arrays from a text file. Include in your class methods for transforming the data series by some simple function (for example, $x' = \ln x$, $y' = 1/y$) and to perform a linear least-squares regression on the transformed data (returning the gradient and intercept of the best-fit line, $y_\mathrm{fit}' = mx'+c$). NumPy provides methods for performing linear regression (see Section 6.5.3 of the book), but for this exercise the following equations can be implemented directly: \begin{align*} m &= \frac{\overline{xy} - \bar{x}\bar{y}}{\overline{x^2} - \bar{x}^2}\\ c &= \bar{y} - m\bar{x} \end{align*} where the bar notation, $\bar{\cdot}$, denotes the arithmetic mean of the quantity under it. (Hint: use pylab.mean(arr) to return the mean of array arr).

Chloroacetic acid is an important compound in the synthetic production of phamaceuticals, pesticides and fuels. At high concentration under strong alkaline conditions its hydrolysis may be considered as the following reaction: $$\mathrm{ClCH_2COO^-} + \mathrm{OH^-} \rightleftharpoons \mathrm{HOCH_2COO^-} + \mathrm{Cl^-}.$$ Data giving the concentration of $\mathrm{ClCH_2COO^-}$, $c$ (in M), as a function of time, $t$ (in s), are provided for this reaction carried out in excess alkalai at five different temperatures in the data files caa-T.txt (T = 40, 50, 60, 70, 80 in $\mathrm{^\circ C}$): these may be obtained from this zip archive. The reaction is known to be second order and so obeys the integrated rate law $$\frac{1}{c} = \frac{1}{c_0} + kt$$ where $k$ is the effective rate constant and $c_0$ the initial ($t=0$) concentration of chloroacetic acid.

Use your Experiment class to interpret these data by linear regression of $1/c$ against $t$, determining $m(\equiv k)$ for each temperature. Then, for each value of $k$ determine the activation energy of the reaction through a second linear regression of $\ln k$ against $1/T$ in accordance with the Arrhenius law: $$k = Ae^{-E_\mathrm{a}/RT} \quad \Rightarrow \quad \ln k = \ln A - \frac{E_\mathrm{a}}{RT},$$ where $R = 8.314\;\mathrm{J\,K^{-1}\,mol^{-1}}$ is the gas constant.

#### Solution

To access solutions, please obtain an access code from Cambridge University Press at the Lecturer Resources page for my book (registration required) and then sign up to scipython.com providing this code.