/ipython_notebooks/2015_10/2015_10_26_Distance_effects.ipynb
Jupyter | 527 lines | 527 code | 0 blank | 0 comment | 0 complexity | 8d2bf8f09068b6dd636a3db035866a6b MD5 | raw file
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "**Author**: J W Debelius<br>\n",
- "**Date**: 26 October 2015<br>\n",
- "**Virtualenv**: power\\_play"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "import numpy as np\n",
- "import matplotlib.pyplot as plt\n",
- "import scipy\n",
- "import pandas as pd\n",
- "import skbio\n",
- "\n",
- "import absloute_power.distance as dist"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "I'm going to simulate distances between locations which should be significantly different when the two categories are compared, apply a PERMANOVA and ANOSIM (cites needed), and then determine how to better evaluate effect size on microbiome data. "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "def simulate_distance(num_samps, within1=[0.01, 0.25], within2, between, var):\n",
- " # Simulates the mapping file\n",
- " grouping = np.random.binomial(1, 0.5, (num_samps))\n",
- " \n",
- " # Determines the number of observations in each group\n",
- " num1 = grouping.sum()\n",
- " num0 = num_samps - num1\n",
- " \n",
- " # Simulates the "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 222,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "def simulate_poisson_distance(length, scale, param):\n",
- " return np.random.poisson(param, length) * scale"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 221,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([3, 4, 3, 2, 4])"
- ]
- },
- "execution_count": 221,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.random.poisson(3, 5)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "locations = np.random.binomial(1, 0.5, (30))\n",
- "locations.sort()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "within0 = 0.15\n",
- "within1 = 0.2\n",
- "between = 0.5"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,\n",
- " 1, 1, 1, 1, 1, 1, 1])"
- ]
- },
- "execution_count": 20,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "locations"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 25,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "# num0 = np.sum(locations == 0)\n",
- "# num1 = np.sum(locations == 1)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 55,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "in0 = np.random.uniform(0, 1, np.square(num0 - 1)/2)*within0"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 142,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "num0 = 4"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 174,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "vec [1]\n",
- "pos1 [1]\n",
- "pos2 [0]\n",
- "[[ 0. 1.]\n",
- " [ 1. 0.]]\n"
- ]
- }
- ],
- "source": [
- "num0 = 2\n",
- "vec = np.arange((np.square(num0)-1)/2) + 1\n",
- "dm0 = np.zeros((num0, num0))\n",
- "print 'vec', vec\n",
- "\n",
- "pos_count = 0\n",
- "dm0 = np.zeros((num0, num0))\n",
- "for i in xrange(num0-1):\n",
- " pos1 = np.arange(i+1, num0)\n",
- " print 'pos1', pos1\n",
- " pos2 = np.arange(i, num0-1) + pos_count\n",
- " print 'pos2', pos2\n",
- " pos_count = pos_count + len(pos2) - 1\n",
- " dm0[i, pos1] = vec[pos2]\n",
- " dm0[pos1, i] = vec[pos2]\n",
- "print dm0"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 179,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "vec [1 2 3 4]\n",
- "pos1 [1 2]\n",
- "pos2 [0 1]\n",
- "pos1 [2]\n",
- "pos2 [2]\n",
- "[[ 0. 1. 2.]\n",
- " [ 1. 0. 3.]\n",
- " [ 2. 3. 0.]]\n"
- ]
- }
- ],
- "source": [
- "num0 = 3\n",
- "vec = np.arange((np.square(num0)-1)/2) + 1\n",
- "dm0 = np.zeros((num0, num0))\n",
- "print 'vec', vec\n",
- "\n",
- "pos_count = 0\n",
- "dm0 = np.zeros((num0, num0))\n",
- "for i in xrange(num0-1):\n",
- " pos1 = np.arange(i+1, num0)\n",
- " print 'pos1', pos1\n",
- " pos2 = np.arange(i, num0-1) + pos_count\n",
- " print 'pos2', pos2\n",
- " pos_count = pos_count + len(pos2) - 1\n",
- " dm0[i, pos1] = vec[pos2]\n",
- " dm0[pos1, i] = vec[pos2]\n",
- "print dm0"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 181,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "vec [1 2 3 4 5 6 7]\n",
- "[[ 0. 1. 2. 3.]\n",
- " [ 1. 0. 4. 5.]\n",
- " [ 2. 4. 0. 6.]\n",
- " [ 3. 5. 6. 0.]]\n"
- ]
- }
- ],
- "source": [
- "num0 = 4\n",
- "vec = np.arange((np.square(num0)-1)/2) + 1\n",
- "dm0 = np.zeros((num0, num0))\n",
- "print 'vec', vec\n",
- "\n",
- "pos_count = 0\n",
- "dm0 = np.zeros((num0, num0))\n",
- "for i in xrange(num0-1):\n",
- " pos1 = np.arange(i+1, num0)\n",
- " pos2 = np.arange(i, num0-1) + pos_count\n",
- " pos_count = pos_count + len(pos2) - 1\n",
- " dm0[i, pos1] = vec[pos2]\n",
- " dm0[pos1, i] = vec[pos2]\n",
- "print dm0"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 239,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "def convert_to_dm(length, vec):\n",
- " \"\"\"Converts a condensed distance matrix to a table format\"\"\"\n",
- " # Stacks the data\n",
- " vec = np.hstack(vec)\n",
- " # Creates the output matrix\n",
- " dm = np.zeros((length, length))\n",
- " # Adds a counter to watch the positon\n",
- " pos_count = 0\n",
- " \n",
- " # Populates the distance matrix\n",
- " for idx in xrange(length-1):\n",
- " pos1 = np.arange(idx+1, length)\n",
- " pos2 = np.arange(idx, length-1) + pos_count\n",
- " pos_count = pos_count + len(pos2) - 1\n",
- " dm[idx, pos1] = vec[pos2]\n",
- " dm[pos1, idx] = vec[pos2]\n",
- " \n",
- " return dm"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Now, I need to be able to simulate distance matrix vectors."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 253,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "num0 = 4\n",
- "num1 = 4\n",
- "num_d = num0 * num1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 259,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[11 12 13 14 15 16]\n",
- "[21 22 23 24 25 26]\n",
- "[101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116]\n"
- ]
- }
- ],
- "source": [
- "vec0 = np.arange(0, (num0/2)*(num0-1)) + 11\n",
- "vec1 = np.arange(0, (num1/2)*(num1-1)) + 21\n",
- "veci = np.arange(0, num0 * num1) + 101\n",
- "print vec0\n",
- "print vec1\n",
- "print veci"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 328,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "vec0 = np.array([1, 2, 3])\n",
- "vec1 = np.array([10, 20, 30])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 329,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "dm0 = convert_to_dm(3, vec0)\n",
- "dm1 = convert_to_dm(3, vec1)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 330,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[ 0. 1. 2.]\n",
- " [ 1. 0. 3.]\n",
- " [ 2. 3. 0.]]\n",
- "[[ 0. 10. 20.]\n",
- " [ 10. 0. 30.]\n",
- " [ 20. 30. 0.]]\n"
- ]
- }
- ],
- "source": [
- "print dm0\n",
- "print dm1"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 331,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[11, 21, 31],\n",
- " [12, 22, 32],\n",
- " [13, 23, 33]])"
- ]
- },
- "execution_count": 331,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dmi = np.array([[11, 21, 31],\n",
- " [12, 22, 32],\n",
- " [13, 23, 33],\n",
- " ])\n",
- "\n",
- "dmi"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 336,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[13, 23, 33],\n",
- " [12, 22, 32],\n",
- " [11, 21, 31]])"
- ]
- },
- "execution_count": 336,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "dmi[::-1]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 332,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[ 0., 1., 2., 11., 21., 31.],\n",
- " [ 1., 0., 3., 12., 22., 32.],\n",
- " [ 2., 3., 0., 13., 23., 33.]])"
- ]
- },
- "execution_count": 332,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.vstack([np.hstack([dm0, dmi]),\n",
- " [np.hstack([])]])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 2",
- "language": "python",
- "name": "python2"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 2
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython2",
- "version": "2.7.3"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
- }