{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Linear Mixed Effect Modeling\n", "\n", "\n", "{note}\n", "This chapter written by [Todd Gureckis](http://gureckislab.org/~gureckis) who adapted it from Gabriela K Hajduk and the University of Edinburgh [coding club](https://ourcodingclub.github.io) tutorial on Mixed Effect Linear Models and the [mixed models](https://www.kaggle.com/ojwatson/mixed-models/comments) Kaggle notebook by [OJ Watson](https://www.kaggle.com/ojwatson). The former is release under CC BY-SA 4.0, the latter under the [Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0) license. The text is a mashup of these two resources with various editing to connect to the rest of the book for the current class. The notebook released under the [license](/LICENSE.html) for the book.\n", "\n", "\n", "The goal in this chapter is to introduce **_linear mixed effect_** modeling (aka LME). Linear mixed effect models are among the most useful in psychological science. While previous chapters explore the virtues of linear modeling (e.g., linear regression), this chapter deals with a very common scenario where different observations might fall into various subgroups. For example, we might have data that are trials in an experiment which are grouped into which subject they came from. The subjects might be grouped by which condition of an experiment they were assigned to or how old they are. Linear mixed effect models are an useful tool for analyzing these types of data because they help to minimize the number of independent tests that are performed across groups (the multiple comparison problem) while also helping to ensure that the data is not broken into many small group (lowering power)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [ "hide_input" ] }, "outputs": [], "source": [ "# import modules needed\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns; sns.set()\n", "import numpy as np\n", "import pandas as pd\n", "import sklearn as sk\n", "from math import sqrt\n", "import warnings\n", "warnings.filterwarnings('ignore')\n", "\n", "class Caption:\n", " def __init__(self, fig_no, text, c_type='f'):\n", " self.fig_no = fig_no\n", " self.text = text\n", " if c_type=='t':\n", " self.c_type = 'Table'\n", " else:\n", " self.c_type = 'Figure'\n", " \n", " def _repr_html_(self):\n", " return f\"
{ self.c_type } { self.fig_no }. { self.text }
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
bounce_timeagecountylocation
0165.54852016devona
1167.55931434devona
2165.8829526devona
3167.68552519devona
4169.95968134devona
5168.68874747devona
6169.6193827devona
7164.4162738devona
8167.5104308devona
9179.6060687devona
10174.46548540devona
11164.78124838devona
12172.55946723devona
13179.0007747devona
14175.97040918devona
\n", "