{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"better to use the sqrt function"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"expr | time |
\n",
"\n",
"\tsqrt(x) | 3271 |
\n",
"\tsqrt(x) | 649 |
\n",
"\tx^0.5 | 20648 |
\n",
"\tx^0.5 | 8490 |
\n",
"\tsqrt(x) | 570 |
\n",
"\tx^0.5 | 8289 |
\n",
"\tx^0.5 | 8337 |
\n",
"\tsqrt(x) | 698 |
\n",
"\tx^0.5 | 8309 |
\n",
"\tsqrt(x) | 459 |
\n",
"\tx^0.5 | 8246 |
\n",
"\tsqrt(x) | 832 |
\n",
"\tx^0.5 | 8214 |
\n",
"\tsqrt(x) | 562 |
\n",
"\tx^0.5 | 8103 |
\n",
"\tsqrt(x) | 460 |
\n",
"\tx^0.5 | 8259 |
\n",
"\tsqrt(x) | 563 |
\n",
"\tx^0.5 | 8353 |
\n",
"\tx^0.5 | 8214 |
\n",
"\tsqrt(x) | 627 |
\n",
"\tsqrt(x) | 537 |
\n",
"\tsqrt(x) | 568 |
\n",
"\tsqrt(x) | 533 |
\n",
"\tsqrt(x) | 629 |
\n",
"\tx^0.5 | 8416 |
\n",
"\tx^0.5 | 8272 |
\n",
"\tx^0.5 | 8257 |
\n",
"\tx^0.5 | 8259 |
\n",
"\tx^0.5 | 8238 |
\n",
"\t⋮ | ⋮ |
\n",
"\tx^0.5 | 8073 |
\n",
"\tsqrt(x) | 626 |
\n",
"\tx^0.5 | 8138 |
\n",
"\tx^0.5 | 8209 |
\n",
"\tx^0.5 | 8054 |
\n",
"\tx^0.5 | 8238 |
\n",
"\tsqrt(x) | 448 |
\n",
"\tsqrt(x) | 542 |
\n",
"\tx^0.5 | 8207 |
\n",
"\tsqrt(x) | 541 |
\n",
"\tsqrt(x) | 431 |
\n",
"\tsqrt(x) | 583 |
\n",
"\tsqrt(x) | 416 |
\n",
"\tx^0.5 | 8232 |
\n",
"\tx^0.5 | 8123 |
\n",
"\tx^0.5 | 8259 |
\n",
"\tx^0.5 | 8087 |
\n",
"\tsqrt(x) | 633 |
\n",
"\tsqrt(x) | 410 |
\n",
"\tsqrt(x) | 558 |
\n",
"\tx^0.5 | 8150 |
\n",
"\tsqrt(x) | 564 |
\n",
"\tx^0.5 | 8102 |
\n",
"\tsqrt(x) | 579 |
\n",
"\tsqrt(x) | 448 |
\n",
"\tx^0.5 | 8286 |
\n",
"\tx^0.5 | 8077 |
\n",
"\tsqrt(x) | 653 |
\n",
"\tx^0.5 | 8158 |
\n",
"\tsqrt(x) | 570 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|ll}\n",
" expr & time\\\\\n",
"\\hline\n",
"\t sqrt(x) & 3271 \\\\\n",
"\t sqrt(x) & 649 \\\\\n",
"\t x\\textasciicircum{}0.5 & 20648 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8490 \\\\\n",
"\t sqrt(x) & 570 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8289 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8337 \\\\\n",
"\t sqrt(x) & 698 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8309 \\\\\n",
"\t sqrt(x) & 459 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8246 \\\\\n",
"\t sqrt(x) & 832 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8214 \\\\\n",
"\t sqrt(x) & 562 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8103 \\\\\n",
"\t sqrt(x) & 460 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8259 \\\\\n",
"\t sqrt(x) & 563 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8353 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8214 \\\\\n",
"\t sqrt(x) & 627 \\\\\n",
"\t sqrt(x) & 537 \\\\\n",
"\t sqrt(x) & 568 \\\\\n",
"\t sqrt(x) & 533 \\\\\n",
"\t sqrt(x) & 629 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8416 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8272 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8257 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8259 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8238 \\\\\n",
"\t ⋮ & ⋮\\\\\n",
"\t x\\textasciicircum{}0.5 & 8073 \\\\\n",
"\t sqrt(x) & 626 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8138 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8209 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8054 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8238 \\\\\n",
"\t sqrt(x) & 448 \\\\\n",
"\t sqrt(x) & 542 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8207 \\\\\n",
"\t sqrt(x) & 541 \\\\\n",
"\t sqrt(x) & 431 \\\\\n",
"\t sqrt(x) & 583 \\\\\n",
"\t sqrt(x) & 416 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8232 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8123 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8259 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8087 \\\\\n",
"\t sqrt(x) & 633 \\\\\n",
"\t sqrt(x) & 410 \\\\\n",
"\t sqrt(x) & 558 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8150 \\\\\n",
"\t sqrt(x) & 564 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8102 \\\\\n",
"\t sqrt(x) & 579 \\\\\n",
"\t sqrt(x) & 448 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8286 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8077 \\\\\n",
"\t sqrt(x) & 653 \\\\\n",
"\t x\\textasciicircum{}0.5 & 8158 \\\\\n",
"\t sqrt(x) & 570 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"expr | time | \n",
"|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n",
"| sqrt(x) | 3271 | \n",
"| sqrt(x) | 649 | \n",
"| x^0.5 | 20648 | \n",
"| x^0.5 | 8490 | \n",
"| sqrt(x) | 570 | \n",
"| x^0.5 | 8289 | \n",
"| x^0.5 | 8337 | \n",
"| sqrt(x) | 698 | \n",
"| x^0.5 | 8309 | \n",
"| sqrt(x) | 459 | \n",
"| x^0.5 | 8246 | \n",
"| sqrt(x) | 832 | \n",
"| x^0.5 | 8214 | \n",
"| sqrt(x) | 562 | \n",
"| x^0.5 | 8103 | \n",
"| sqrt(x) | 460 | \n",
"| x^0.5 | 8259 | \n",
"| sqrt(x) | 563 | \n",
"| x^0.5 | 8353 | \n",
"| x^0.5 | 8214 | \n",
"| sqrt(x) | 627 | \n",
"| sqrt(x) | 537 | \n",
"| sqrt(x) | 568 | \n",
"| sqrt(x) | 533 | \n",
"| sqrt(x) | 629 | \n",
"| x^0.5 | 8416 | \n",
"| x^0.5 | 8272 | \n",
"| x^0.5 | 8257 | \n",
"| x^0.5 | 8259 | \n",
"| x^0.5 | 8238 | \n",
"| ⋮ | ⋮ | \n",
"| x^0.5 | 8073 | \n",
"| sqrt(x) | 626 | \n",
"| x^0.5 | 8138 | \n",
"| x^0.5 | 8209 | \n",
"| x^0.5 | 8054 | \n",
"| x^0.5 | 8238 | \n",
"| sqrt(x) | 448 | \n",
"| sqrt(x) | 542 | \n",
"| x^0.5 | 8207 | \n",
"| sqrt(x) | 541 | \n",
"| sqrt(x) | 431 | \n",
"| sqrt(x) | 583 | \n",
"| sqrt(x) | 416 | \n",
"| x^0.5 | 8232 | \n",
"| x^0.5 | 8123 | \n",
"| x^0.5 | 8259 | \n",
"| x^0.5 | 8087 | \n",
"| sqrt(x) | 633 | \n",
"| sqrt(x) | 410 | \n",
"| sqrt(x) | 558 | \n",
"| x^0.5 | 8150 | \n",
"| sqrt(x) | 564 | \n",
"| x^0.5 | 8102 | \n",
"| sqrt(x) | 579 | \n",
"| sqrt(x) | 448 | \n",
"| x^0.5 | 8286 | \n",
"| x^0.5 | 8077 | \n",
"| sqrt(x) | 653 | \n",
"| x^0.5 | 8158 | \n",
"| sqrt(x) | 570 | \n",
"\n",
"\n"
],
"text/plain": [
" expr time \n",
"1 sqrt(x) 3271\n",
"2 sqrt(x) 649\n",
"3 x^0.5 20648\n",
"4 x^0.5 8490\n",
"5 sqrt(x) 570\n",
"6 x^0.5 8289\n",
"7 x^0.5 8337\n",
"8 sqrt(x) 698\n",
"9 x^0.5 8309\n",
"10 sqrt(x) 459\n",
"11 x^0.5 8246\n",
"12 sqrt(x) 832\n",
"13 x^0.5 8214\n",
"14 sqrt(x) 562\n",
"15 x^0.5 8103\n",
"16 sqrt(x) 460\n",
"17 x^0.5 8259\n",
"18 sqrt(x) 563\n",
"19 x^0.5 8353\n",
"20 x^0.5 8214\n",
"21 sqrt(x) 627\n",
"22 sqrt(x) 537\n",
"23 sqrt(x) 568\n",
"24 sqrt(x) 533\n",
"25 sqrt(x) 629\n",
"26 x^0.5 8416\n",
"27 x^0.5 8272\n",
"28 x^0.5 8257\n",
"29 x^0.5 8259\n",
"30 x^0.5 8238\n",
"⋮ ⋮ ⋮ \n",
"171 x^0.5 8073 \n",
"172 sqrt(x) 626 \n",
"173 x^0.5 8138 \n",
"174 x^0.5 8209 \n",
"175 x^0.5 8054 \n",
"176 x^0.5 8238 \n",
"177 sqrt(x) 448 \n",
"178 sqrt(x) 542 \n",
"179 x^0.5 8207 \n",
"180 sqrt(x) 541 \n",
"181 sqrt(x) 431 \n",
"182 sqrt(x) 583 \n",
"183 sqrt(x) 416 \n",
"184 x^0.5 8232 \n",
"185 x^0.5 8123 \n",
"186 x^0.5 8259 \n",
"187 x^0.5 8087 \n",
"188 sqrt(x) 633 \n",
"189 sqrt(x) 410 \n",
"190 sqrt(x) 558 \n",
"191 x^0.5 8150 \n",
"192 sqrt(x) 564 \n",
"193 x^0.5 8102 \n",
"194 sqrt(x) 579 \n",
"195 sqrt(x) 448 \n",
"196 x^0.5 8286 \n",
"197 x^0.5 8077 \n",
"198 sqrt(x) 653 \n",
"199 x^0.5 8158 \n",
"200 sqrt(x) 570 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"library(microbenchmark)\n",
"set.seed(1)\n",
"x <- runif(100)\n",
"\n",
"#sqrt(x)\n",
"#x^0.5\n",
"\n",
"microbenchmark(sqrt(x), x^0.5)\n",
"\n",
"# last \"cld\" column (provide ranking) available only if other package installed"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"other command:\n",
"system.time()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### read matrix col then row, vs. row then col\n",
"\n",
"pre-fetching: if you start reading the first element, it is very likely that you will read the adjacent one, in parallel the processor is already getting the next one\n",
"=> if you go by column: very efficient, you benefit from that"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Rprof()\n",
"#[... code ...]\n",
"Rprof(NULL)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"you can have amount of time spent in each function\n",
"percentages do not add to 100%"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"garbage collector"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" | used | (Mb) | gc trigger | (Mb) | max used | (Mb) |
\n",
"\n",
"\tNcells | 569890 | 30.5 | 940480 | 50.3 | 940480 | 50.3 |
\n",
"\tVcells | 1001300 | 7.7 | 1921967 | 14.7 | 1299510 | 10.0 |
\n",
"\n",
"
\n"
],
"text/latex": [
"\\begin{tabular}{r|llllll}\n",
" & used & (Mb) & gc trigger & (Mb) & max used & (Mb)\\\\\n",
"\\hline\n",
"\tNcells & 569890 & 30.5 & 940480 & 50.3 & 940480 & 50.3 \\\\\n",
"\tVcells & 1001300 & 7.7 & 1921967 & 14.7 & 1299510 & 10.0 \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"| | used | (Mb) | gc trigger | (Mb) | max used | (Mb) | \n",
"|---|---|\n",
"| Ncells | 569890 | 30.5 | 940480 | 50.3 | 940480 | 50.3 | \n",
"| Vcells | 1001300 | 7.7 | 1921967 | 14.7 | 1299510 | 10.0 | \n",
"\n",
"\n"
],
"text/plain": [
" used (Mb) gc trigger (Mb) max used (Mb)\n",
"Ncells 569890 30.5 940480 50.3 940480 50.3\n",
"Vcells 1001300 7.7 1921967 14.7 1299510 10.0"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"gc()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Map a function to different group: tapply()\n",
"does not apply to matrices, apply to 2 vectors\n",
"1) the one to which apply the function \n",
"2) the grouping variable\n",
"\n",
"tapply(data$height, data$sex, FUN=MEAN)\n",
"\n",
"=> mean of height stratify for the sex"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### useful functions\n",
"\n",
"- for converting from width to short format:\n",
"stack() function\n",
"\n",
"- for combining data frames\n",
"merge() function\n",
"\n",
"- which element of a list present in a second list\n",
"match()\n",
"for each element of the first list, returns its index in the second list\n",
"(if present multiple times, returns only the 1st\n",
"if just want to know if they are present (not interested in the position): %in%"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"some functions are hidden, because not supposed to use them\n",
"retrieve their source with getAnywhere"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"A single object matching ‘format.pval’ was found\n",
"It was found in the following places\n",
" package:base\n",
" registered S3 method for format from namespace base\n",
" namespace:base\n",
"with value\n",
"\n",
"function (pv, digits = max(1L, getOption(\"digits\") - 2L), eps = .Machine$double.eps, \n",
" na.form = \"NA\", ...) \n",
"{\n",
" if ((has.na <- any(ina <- is.na(pv)))) \n",
" pv <- pv[!ina]\n",
" r <- character(length(is0 <- pv < eps))\n",
" if (any(!is0)) {\n",
" rr <- pv <- pv[!is0]\n",
" expo <- floor(log10(ifelse(pv > 0, pv, 1e-50)))\n",
" fixp <- expo >= -3 | (expo == -4 & digits > 1)\n",
" if (any(fixp)) \n",
" rr[fixp] <- format(pv[fixp], digits = digits, ...)\n",
" if (any(!fixp)) \n",
" rr[!fixp] <- format(pv[!fixp], digits = digits, ...)\n",
" r[!is0] <- rr\n",
" }\n",
" if (any(is0)) {\n",
" digits <- max(1L, digits - 2L)\n",
" if (any(!is0)) {\n",
" nc <- max(nchar(rr, type = \"w\"))\n",
" if (digits > 1L && digits + 6L > nc) \n",
" digits <- max(1L, nc - 7L)\n",
" sep <- if (digits == 1L && nc <= 6L) \n",
" \"\"\n",
" else \" \"\n",
" }\n",
" else sep <- if (digits == 1) \n",
" \"\"\n",
" else \" \"\n",
" r[is0] <- paste(\"<\", format(eps, digits = digits, ...), \n",
" sep = sep)\n",
" }\n",
" if (has.na) {\n",
" rok <- r\n",
" r <- character(length(ina))\n",
" r[!ina] <- rok\n",
" r[ina] <- na.form\n",
" }\n",
" r\n",
"}\n",
"\n",
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"getAnywhere(format.pval)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<<- => double arrow to use to force assignment to work on global variable, not on local variable"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sample(1:100, 10, replace=T)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"if we are really sure of the values we pass to the mean function, we can directly use .Internal(mean(x)) => this is the function called by mean.default() after having all the checks"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "ir"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "3.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}