Skip to content

Instantly share code, notes, and snippets.

@shabbychef
Created December 12, 2012 21:22
Show Gist options
  • Save shabbychef/4271760 to your computer and use it in GitHub Desktop.
Save shabbychef/4271760 to your computer and use it in GitHub Desktop.
testing ipython notebook and magic.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "footest"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"# to get the R<->python magic"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext rmagic"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# now you can code in R:"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%R\n",
"library(survival)\n",
"amod <- coxph(Surv(lung$time,lung$status) ~ lung$sex + lung$ph.karno)\n",
"print(summary(amod))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "display_data",
"text": [
"Loading required package: splines\n",
"Call:\n",
"coxph(formula = Surv(lung$time, lung$status) ~ lung$sex + lung$ph.karno)\n",
"\n",
" n= 227, number of events= 164 \n",
" (1 observation deleted due to missingness)\n",
"\n",
" coef exp(coef) se(coef) z Pr(>|z|) \n",
"lung$sex -0.504210 0.603982 0.167723 -3.006 0.00265 **\n",
"lung$ph.karno -0.015155 0.984959 0.005727 -2.646 0.00814 **\n",
"---\n",
"Signif. codes: 0 \u2018***\u2019 0.001 \u2018**\u2019 0.01 \u2018*\u2019 0.05 \u2018.\u2019 0.1 \u2018 \u2019 1 \n",
"\n",
" exp(coef) exp(-coef) lower .95 upper .95\n",
"lung$sex 0.604 1.656 0.4348 0.8391\n",
"lung$ph.karno 0.985 1.015 0.9740 0.9961\n",
"\n",
"Concordance= 0.638 (se = 0.026 )\n",
"Rsquare= 0.072 (max possible= 0.999 )\n",
"Likelihood ratio test= 17.05 on 2 df, p=0.0001981\n",
"Wald test = 17.18 on 2 df, p=0.000186\n",
"Score (logrank) test = 17.5 on 2 df, p=0.0001583\n",
"\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# to get the octave<->python magic"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%load_ext octavemagic"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%octave -o y\n",
"x = randn(100,20);\n",
"y = sum(x,1);"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "display_data",
"text": [
"warning: mark_as_command is obsolete and will be removed from a future version of Octave\n",
"********************************\n",
"adding ~/lang/octave/m// to path\n",
"********************************"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"y"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 7,
"text": [
"array([[ -8.83067733, -3.13032074, -2.91806939, 1.14890723,\n",
" 5.44526298, 10.23667501, 13.29263306, -4.92289999,\n",
" 2.36629103, 5.51218616, 17.05002896, 3.99914408,\n",
" -11.06913111, -5.63449047, -4.2435471 , -5.57522994,\n",
" -4.37502494, -10.54391426, 10.11748001, -0.2811483 ]])"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%octave -i y -o z\n",
"z = sum(y);"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"z"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 9,
"text": [
"7.6441549485344096"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment