zoukankan      html  css  js  c++  java
  • [R] [Johns Hopkins] R Programming -- week 4

    #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 多線程線性代數
  • 相关阅读:
    LintCode-35.翻转链表
    LintCode-159.寻找旋转排序数组中的最小值
    LintCode-73.前序遍历和中序遍历树构造二叉树
    LintCode-9.Fizz Buzz 问题
    NOI 2018 归程 (Kruskal重构树)
    模板 NTT 快速数论变换
    模板 FFT 快速傅里叶变换
    BZOJ 3510 首都 (LCT)
    BZOJ 4530 [BJOI2014]大融合 (LCT)
    BZOJ 3282 Link Cut Tree (LCT)
  • 原文地址:https://www.cnblogs.com/pyleu1028/p/10367875.html
Copyright © 2011-2022 走看看