R语言程序设计week4
来源:互联网 发布:那些引擎源码能用 编辑:程序博客网 时间:2024/05/17 01:35
4.1 str函数
str:紧凑地显示R对象的内在结构
。 一种诊断函数同时也是"summary"的替代品
。 特别适用于紧密地展示列表的内容
。 大致是一行一个基本对象
如str(lm)
4.2 simulation 模拟
Generating Random Numbers
Functions for probability distributions in R
rnorm
: generate random Normal variates with a given mean and standard deviationdnorm
: evaluate the Normal probability density (with a given mean/SD) at a point (or vector of points)pnorm
: evaluate the cumulative distribution function for a Normal distribution- qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)与pnorm相反
- 同样,也有柏松分布
- dpois(x, lambda, log = FALSE)
ppois(q, lambda, lower.tail = TRUE, log.p = FALSE)
qpois(p, lambda, lower.tail = TRUE, log.p = FALSE)
rpois(n, lambda) - 亦有 The Binomial Distribution二项分布
- dbinom(x, size, prob, log = FALSE)
pbinom(q, size, prob, lower.tail = TRUE, log.p = FALSE)
qbinom(p, size, prob, lower.tail = TRUE, log.p = FALSE)
rbinom(n, size, prob)
Generating Random Numbers
Probability distribution functions usually have four functions associated with them. The functions are prefixed with a
d
for densityr
for random number generationp
for cumulative distributionq
for quantile function
Generating Random Numbers
Working with the Normal distributions requires using these four functions
Generating Random Numbers
Setting the random number seed with set.seed
ensures reproducibility
Always set the random number seed when conducting a simulation!
Generating Random Numbers
Generating Poisson data
Random Sampling
The sample
function draws randomly from a specified set of (scalar) objects allowing you to sample from arbitrary distributions.
Simulation
Summary
- Drawing samples from specific probability distributions can be done with
r*
functions - Standard distributions are built in: Normal, Poisson, Binomial, Exponential, Gamma, etc.
- The
sample
function can be used to draw random samples from arbitrary vectors - Setting the random number generator seed via set.seed is critical for reproducibility
Why is My Code So Slow?
Profiling is a systematic way to examine how much time is spend in different parts of a program
Useful when trying to optimize your code
Often code runs fine once, but what if you have to put it in a loop for 1,000 iterations? Is it still fast enough?
Profiling is better than guessing
On Optimizing Your Code
Getting biggest impact on speeding up code depends on knowing where the code spends most of its time
This cannot be done without performance analysis or profiling
We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil
--Donald Knuth
General Principles of Optimization
Design first, then optimize
Remember: Premature optimization is the root of all evil 过早的优化是万恶之源
Measure (collect data), don’t guess.
If you're going to be scientist, you need to apply the same principles here!
Using system.time()
Takes an arbitrary R expression as input (can be wrapped in curly braces) and returns the amount of time taken to evaluate the expression
Computes the time (in seconds) needed to execute an expression
- If there’s an error, gives time until the error occurred
Returns an object of class
proc_time
- user time: time charged to the CPU(s) for this expression
- elapsed time: "wall clock" time 即我们所能感知到的时间
Using system.time()
Usually, the user time and elapsed time are relatively close, for straight computing tasks
Elapsed time may be greater than user time if the CPU spends a lot of time waiting around
Elapsted time may be smaller than the user time if your machine has multiple cores/processors (and is capable of using them)
- Multi-threaded BLAS libraries (vecLib/Accelerate, ATLAS, ACML, MKL)
- Parallel processing via the parallel package(并行处理)
Using system.time()
Timing Longer Expressions
Beyond system.time()
Using
system.time()
allows you to test certain functions or code blocks to see if they are taking excessive amounts of timeAssumes you already know where the problem is and can call
system.time()
on itWhat if you don’t know where to start?
The R Profiler
The
Rprof()
function starts the profiler in R- R must be compiled with profiler support (but this is usually the case)
The
summaryRprof()
function summarizes the output fromRprof()
(otherwise it’s not readable)DO NOT use
system.time()
andRprof()
together or you will be sad
The R Profiler
Rprof() keeps track of the function call stack at regularly sampled intervals and tabulates(作表) how much time is spend in each function
Default sampling interval is 0.02 seconds
NOTE: If your code runs very quickly, the profiler is not useful, but then you probably don't need it in that case
R Profiler Raw Output
Using summaryRprof()
The
summaryRprof()
function tabulates the R profiler output and calculates how much time is spend in which functionThere are two methods for normalizing the data
"by.total" divides the time spend in each function by the total run time
"by.self" does the same but first subtracts out time spent in functions above in the call stack
顶层往往只调用函数,而不做事,因此它所花的时间往往并不那么重要,真正重要的是做事的那些函数所花的进间,故而by.self
By Total
By Self
summaryRprof()
Output
Summary
Rprof()
runs the profiler for performance of analysis of R codesummaryRprof()
summarizes the output ofRprof()
and gives percent of time spent in each function (with two types of normalization)Good to break your code into functions so that the profiler can give useful information about where time is being spent
C or Fortran code is not profiled
- R语言程序设计week4
- R语言程序设计week2
- R语言程序设计 week3
- Arduino 平台与C语言程序设计-week4-Arduino Programs-Lesson1
- Arduino 平台与C语言程序设计-week4-Arduino Programs-Lesson2
- Arduino 平台与C语言程序设计-week4-Arduino Programs-Lesson3
- week4
- K&R C语言程序设计 第二版
- POMM-week4
- FERMI-week4
- week4-4
- 周报week4
- Oct week4
- Leetcode Week4
- leetcode week4
- week4 神经网络
- 记录week4
- Algorithm-week4
- MH17遇难者遭受最后的侮辱 遗体经三日暴晒后由垃圾车运走
- Uva 591 Box of Bricks
- adb shell dumpsys的使用
- 再测JAVA性能比C/C++好问题
- 紫外线杀菌器:uv紫外线杀菌技术原理
- R语言程序设计week4
- Android震动vibrator系统开发全过程
- poj 1269 Intersecting Lines[直线的关系]
- ubuntu恢复unity桌面
- 二叉树的非递归遍历
- 第十八周周报
- 骨灰级泡妞高手白皮书之升级版
- 学习Swift笔记 (十一)Swift的属性
- NSOperation 的使用(下载相关) 图片和文件都是可以的 断点续传 图片逐渐显示