#Generating normal distribution (Pseudo) random number x<-rnorm(10) x x2<-rnorm(10,2,1) x2 set.seed() #Generating Poisson data rpois(10,1) rpois(10,2) rpois(10,20) ppois(2,2) #Cumulative distribution ##P r(x <= 2) 平均發生率為2 ppois(4,2) #Cumulative distribution ##P r(x <= 4) 平均發生率為4 #線性 y = B0+B1X+e #e~N(0,2^2) 標準差為2正態分布 #assume x~N(0,1^2) B0=o.5 B1=2 set.seed(20) x <- rnorm(100) e <- rnorm(100,0,2) y <- 0.5 + 2 * x + e summary(y) plot(x,y) # 若x為binary ex性別 set.seed(20) x <- rbinom(100,1,0.5) #得到1的機率為0.5 e <- rnorm(100,0,2) y <- 0.5 + 2 * x + e summary(y) plot(x,y) #廣義線性模組可能服從poisson分布 Y~Poisson(m) #log mu = B0 + B1X #log mu 服從線性 #B0 = 0.5 B1 =0.3 #y服從 平均值為mu 的 PD set.seed(20) x <- rnorm(100) log.mu <- 0.5 + 0.3 * x y <- rpois(100,exp(log.mu)) summary(y) plot(x,y) # sample(range vector, numbers) # sample(range vector) 重新排列 # sample(range vector, replace = T) 重複性抽樣 #Profiler profiling is better than guessing #Premature optimization is the root of all evil system.time() #proc_time (class) #user time: time charged to the CPU(s) for this expression #elapsed time: "wall clock" time 運行時間 #parallel processing via parallel package ##elapsed time > user time system.time(readlines("http://www.google.com")) #elapsed time < user time hilbert <- function(n){ i <- 1:n 1/outer(i -1, i, "+") } x <- hilbert(1000) system.time(svd(x)) # svd 多線程線性代數