Skip to content

Instantly share code, notes, and snippets.

@fonnesbeck
Last active March 30, 2017 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fonnesbeck/258401eaf104eb0ff58cbf4ba72aba3f to your computer and use it in GitHub Desktop.
Save fonnesbeck/258401eaf104eb0ff58cbf4ba72aba3f to your computer and use it in GitHub Desktop.
Untitled.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"collapsed": true,
"trusted": true,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "import numpy as np",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"editable": true,
"deletable": true
},
"cell_type": "markdown",
"source": "I will implement the 10-year risk equation for women. Here are the betas."
},
{
"metadata": {
"collapsed": true,
"trusted": true,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "betas = np.array([2.72107, # log age\n0.51125, # log BMI\n2.81291, # log SBP (not treated)\n2.88267, # log SBP (treated)\n0.61868, # smoking\n0.77763, # diabetes\n])",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"editable": true,
"deletable": true
},
"cell_type": "markdown",
"source": "Here is the formula. The `dot` function is the dot product, or inner product of the coefficients and the data array (i.e. multiply them element-wise, and add the products)"
},
{
"metadata": {
"collapsed": true,
"trusted": true,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "def frs(X, b, surv, const):\n return 1 - surv** np.exp(X.dot(b) - const)",
"execution_count": 16,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Below are special cases of the general formula, using the survival and constants specified for men and women:"
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "def frs_women(X, b):\n return frs(X, b, 0.94833, 26.0145)",
"execution_count": 17,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "def frs_men(X, b):\n return frs(X, b, 0.88431, 23.9388)",
"execution_count": 18,
"outputs": []
},
{
"metadata": {
"editable": true,
"deletable": true
},
"cell_type": "markdown",
"source": "Here is some sample data"
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "x_sample = np.array([np.log(30), np.log(22.5), 0, np.log(125), 0, 0])\nx_sample",
"execution_count": 19,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([ 3.40119738, 3.11351531, 0. , 4.82831374, 0. , 0. ])"
},
"metadata": {},
"execution_count": 19
}
]
},
{
"metadata": {
"editable": true,
"deletable": true
},
"cell_type": "markdown",
"source": "Here is the corresponding risk scores (men and women)"
},
{
"metadata": {
"collapsed": false,
"trusted": true,
"editable": true,
"deletable": true
},
"cell_type": "code",
"source": "frs_women(X=x_sample, b=betas)",
"execution_count": 20,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "0.015094153510678776"
},
"metadata": {},
"execution_count": 20
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "frs_men(X=x_sample, b=betas)",
"execution_count": 21,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "0.24491217871245496"
},
"metadata": {},
"execution_count": 21
}
]
}
],
"metadata": {
"_draft": {
"nbviewer_url": "https://gist.github.com/258401eaf104eb0ff58cbf4ba72aba3f"
},
"gist": {
"id": "258401eaf104eb0ff58cbf4ba72aba3f",
"data": {
"description": "Untitled.ipynb",
"public": true
}
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.0",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment