Design and Data Analysis in Cancer Research

Download Report

Transcript Design and Data Analysis in Cancer Research

2009 Regression
R: Short Introduction
林
建
甫
台 北 大 學 統 計 系 助 理 教 授
2015/7/20
Jeff Lin, MD. PhD.
1
R 是完全免費
R is Free
2015/7/20
Jeff Lin, MD. PhD.
2
R 程式語言與統計軟體
•
•
•
•
2015/7/20
R 系統是由 Ross Ihaka 與 Robert Gentleman 從 S 語
言所發展出來, 主要是為了統計分析與統計繪圖.
R 除了資料處理與分析, R 擁有一完整陣列和矩陣
的操作運算, 完整圖形工具, 也是一種相當完善的
程式設計語言.
S 語言在 1980 年代末期, 由 AT&T 實驗室,
Rick Becker, John Chambers, 與 Allan Wilks 發展用
來進行統計分析與統計繪圖, Insightful 公司將 S 商
品化, 並加入許多方便的操作介面, 稱為 S-Plus.
Jeff Lin, MD. PhD.
3
Why R?
為何使用 R?
• R 是科學研究人員為科學研究人員本身所撰
寫的程式語言.
• R 可已在許多作業系統使用, Windows, Mac,
Unix, Linux, ...
• R 容易安裝.
• R 有上千件套件, 可供特殊研究領域使用.
• 許多發表的研究論文使用 R 分析
2015/7/20
Jeff Lin, MD. PhD.
4
Why R?
為何使用 R?
•
•
•
•
2015/7/20
R 有許多協助系統與文件可供使用者參考.
R 有線上 email協助系統支援.
R 社群有許多熱心與善心助人的研究人員
R 可以同時使用 Fortran, C, Java, ... libraries
Jeff Lin, MD. PhD.
5
R: 安裝
•
•
•
•
•
2015/7/20
www.r-project.org/
從 CRAN 網站下載
選擇下載地點
安裝 base 套件
安裝必須的統計套件
Jeff Lin, MD. PhD.
6
R: 簡單實例
2015/7/20
Jeff Lin, MD. PhD.
7
R: 計算機
2015/7/20
Jeff Lin, MD. PhD.
8
R: 繪圖與模擬
> z<-rchisq(1000,3)
> hist(z)
2015/7/20
Jeff Lin, MD. PhD.
9
R: 繪圖與模擬
2015/7/20
Jeff Lin, MD. PhD.
10
R: 繪圖與模擬
>y <- sin(x)
>plot(x,y,
col="red",
cex=2)
>lines(x,y, lwd=2)
2015/7/20
Jeff Lin, MD. PhD.
11
R: 繪圖與模擬
x <- seq(from=1,
to=2*pi,
length=41)
y <- sin(x)
#
col <- c("red",
"blue")
plot(x,y, col=col,
cex=1:3, lwd=4)
2015/7/20
Jeff Lin, MD. PhD.
12
R: 統計軟體
2015/7/20
Jeff Lin, MD. PhD.
13
R 統計軟體: Two Samples t Test
2015/7/20
Jeff Lin, MD. PhD.
14
R: 統計軟體與套件
x<-rnorm(20,1.5,2)
y<-x+rnorm(20)
cbind(y,x)
Summary(lm(y~x))
2015/7/20
Jeff Lin, MD. PhD.
15
R 統計軟體與套件: 迴歸分析
2015/7/20
Jeff Lin, MD. PhD.
16
R: 程式語言
> mean.fun<-function(x){
+ sum(x)/length(x)}
# R Build-In Function
> mean(x)
[1] 1.597616
# Home Made Function
> mean.fun(x)
[1] 1.597616
2015/7/20
Jeff Lin, MD. PhD.
17
R: 物件導向程式語言
R as Objective-Oriented Language
2015/7/20
Jeff Lin, MD. PhD.
18
R Basics
•
•
•
•
•
•
•
2015/7/20
物件命名與指派 (Naming and Assign)
變數類型 (Type of Variables)
缺失值 (Missing Values)
資料輸入與指派 (Assignment and Input Data)
讀入外部資料檔案 (Importing external file to R)
使用內建資料集 (Use Default Data Set)
輸出資料至外部檔案 (Exporting R data to
external file)
Jeff Lin, MD. PhD.
19
物件命名 Naming Convention
•
•
•
必須以英文字母起始 (A-Z 或 a-z)
中間可以任何文字與數字, 點 (periods) “.”
大小寫有差別
– mydata 與 MyData 不同
• 不可使用下線 (do not use use underscore “_”)
2015/7/20
Jeff Lin, MD. PhD.
20
物件指派
<- 指派
= (避免使用)
> xyz.vector <- c(1, 2, 3)
2015/7/20
Jeff Lin, MD. PhD.
21
物件 Objects
•
•
物件名稱 (names)
物件種類 (Type of Variables):
– 向量, 因子, 陣列, 矩陣, 資料框架, 時間序列, 列
表. (vector, factor, array, matrix, data.frame, ts, list)
• 屬性 (attributes)
– 模式 (mode): 邏輯, 整數, 倍精準度, 單精準度, 複
數, 文字. (numeric, character, complex, logical)
– 長度 (Length):與物件的模式有關
• 物件產生 (creation): 指派數值或空物件
2015/7/20
Jeff Lin, MD. PhD.
22
未完成的指令或程式
R 出現 “+”
•
•
> sqrt(
若鍵入 “return”, 而 R
+
出現 “+” , 表示輸入指 + )))))
令未完成, 若查不出錯誤
Error in parse(text = txt): Syntax error:
處, 可議連續鍵入
No opening parenthesis, before
“return”, 直到 R 出現
")" at this point:
“>” 符號.
sqrt(
如此再重新輸入 R 指令 ))
Dumped
或程式.
> sqrt(100)
2015/7/20
Jeff Lin, MD. PhD.
23
變數的分類
•
•
•
•
•
•
•
2015/7/20
"numeric", 實數向量
"integer", 整數向量 (有時需特別指定)
"logical", 邏輯變數向量 (true or false), 以 TRUE (T)
或 FALSE (F) 呈現,
(也可以是 1 (T) 與 0 (F).
"complex", 複數向量
"character", 文字或字串向量, 通常輸入時, 在文字
或字串兩側加上雙引號.
"list", 列表, 是一個由 R (S) 物件所組成的向量.
Jeff Lin, MD. PhD.
24
基本模式變數命名與指派
> a <- 49
> sqrt(a)
[1] 7
數值 numeric
> b <- "The dog ate my homework"
文字與字串
> sub("dog","cat",b)
[1] "The cat ate my homework" character
string
> x <- (1+1==3)
> x
邏輯 logical
[1] FALSE
> as.character(b)
[1] "FALSE"
2015/7/20
Jeff Lin, MD. PhD.
25
基本模式變數命名與指派
•
•
2015/7/20
•
Character 文字與字串
Logical (邏輯)
> a <- "1"; b <> x <- T; y <1
F
> a; b
> x; y
[1] "1"
[1] TRUE
[1] 1
[1] FALSE
> a <"character"
Numerical (數值)
> b <- "a"; x <> a <- 5; b <a
sqrt(2)
> a; b; x
> a; b
[1] "character"
[1] 5
[1] "a"
Jeff Lin, MD. PhD.
26
缺失值 NA, NaN, 與 Null
•
NA 或 “Not Available”
– 可用在許多模式 (modes) – character,
numeric, etc.
• NaN 或 “Not a Number”
– 只用在數值模式 (numeric modes)
• NULL
列表 (lists) 的長度為 0 (zero length)
2015/7/20
Jeff Lin, MD. PhD.
27
缺失值 Missing Values, NA, NaN, 與 Null
NA or “Not Available”
– Applies to many modes – character, numeric,
etc.
• NaN or “Not a Number”
– Applies only to numeric modes
• NULL
– Lists with zero length
•
2015/7/20
Jeff Lin, MD. PhD.
28
缺失值 Missing Values
•
•
2015/7/20
NA “not available”
> x <- c(1, 2, 3, NA)
> x + 3
[1] 4 5 6 NA
非數字 “Not a number”
> log(c(0, 1, 2))
[1]
-Inf 0.0000000 0.6931472
> 0/0
[1] NaN
Jeff Lin, MD. PhD.
29
R: 缺失值 Missing Values
•
NA 或 “Not Available”
– NA 不是 0
– NA 不是 “ “ (空格, 或 空字串)
– NA不是 FALSE
•
•
2015/7/20
任何與 NA 的計算, 可能或不可能產生 NA
> 1+NA
[1] NA
> max(c(NA, 4, 7))
[1] NA
> max(c(NA, 4, 7), na.rm=T)
[1] 7
Jeff Lin, MD. PhD.
30
Questions?!
2015/7/20
Jeff Lin, MD. PhD.
31
向量, 矩陣
向量 Vectors:
> x <- c(6,5,4,3,2,1)
> x
[1] 6 5 4 3 2 1
> sum(x)
[1] 21
> x[c(1,3,5)]
[1] 6 4 2
> x <- 6:1
> x
[1] 6 5 4 3 2 1
> x[1:3]
[1] 6 5 4
> x[1:3]+x[6:4]
[1] 7 7 7
> y <- c(0,10)
> x+y
[1] 6 15 4 13 2 11
2015/7/20
矩陣
Matrices (default is to fill column by column):
> x <- matrix(1:18, nrow=3)
> x
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]
1
4
7
10
13
16
[2,]
2
5
8
11
14
17
[3,]
3
6
9
12
15
18
> x[,3]
[1] 7 8 9
> x[-1,4:6]
[,1] [,2] [,3]
[1,]
11
14
17
[2,]
12
15
18
> str(x)
int [1:3, 1:6] 1 2 3 4 5 6 7 8 9 10 ...
Jeff Lin, MD. PhD.
32
R: 向量, 矩陣 乘法有許多種
Matrices:
> X <- matrix(1:9, ncol=3, byrow=TRUE)
> X
[,1] [,2] [,3]
[1,]
1
2
3
[2,]
4
5
6
[3,]
7
8
9
> I <- diag(1, nrow=3)
> I
[,1] [,2] [,3]
[1,]
1
0
0
[2,]
0
1
0
[3,]
0
0
1
> I * X
[,1] [,2] [,3]
[1,]
1
0
0
[2,]
0
5
0
[3,]
0
0
9
> I %*% X
[,1] [,2] [,3]
[1,]
1
2
3
[2,]
4
5
6
[3,]
7
8
9
2015/7/20
Why?
Because it turns out that it is
more common than vector
and matrix multiplications.
Jeff Lin, MD. PhD.
33
R: 向量, 矩陣 與 缺失值 自動補齊
Vectors:
> x
> x
[1]
> y
> y
[1]
> x
[1]
<- 7:1
7 6 5 4 3 2 1
<- 1:3
1 2 3
+ y
8 8 8 5 5 5 2
R will expand the
shortest vector before
adding!
# Zero out every 2nd:
> x <- 3:8
> y <- c(1,0)
> x * y
[1] 3 0 5 0 7 0
2015/7/20
Matrices:
> x <- matrix(1:16, nrow=3)
Warning message:
Replacement length not a multiple of
the elements to replace in matrix(...)
> x
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]
1
4
7
10
13
16
[2,]
2
5
8
11
14
1
[3,]
3
6
9
12
15
2
> y <- c(-1,1)
# Multiply (“moving down the columns”)
> x * y
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]
-1
4
-7
10 -13
16
[2,]
2
-5
8 -11
14
-1
[3,]
-3
6
-9
12 -15
2
Jeff Lin, MD. PhD.
34
Questions?!
2015/7/20
Jeff Lin, MD. PhD.
35
R: 向量, 矩陣
向量 Vectors:
> x <- c(6,5,4,3,2,1)
> x
[1] 6 5 4 3 2 1
> sum(x)
[1] 21
> x[c(1,3,5)]
[1] 6 4 2
> x <- 6:1
> x
[1] 6 5 4 3 2 1
> x[1:3]
[1] 6 5 4
> x[1:3]+x[6:4]
[1] 7 7 7
> y <- c(0,10)
> x+y
[1] 6 15 4 13 2 11
2015/7/20
矩陣
Matrices (default is to fill column by column):
> x <- matrix(1:18, nrow=3)
> x
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]
1
4
7
10
13
16
[2,]
2
5
8
11
14
17
[3,]
3
6
9
12
15
18
> x[,3]
[1] 7 8 9
> x[-1,4:6]
[,1] [,2] [,3]
[1,]
11
14
17
[2,]
12
15
18
> str(x)
int [1:3, 1:6] 1 2 3 4 5 6 7 8 9 10 ...
Jeff Lin, MD. PhD.
36
R: 向量, 矩陣 可加上 變數名稱 辨識
Vectors:
計算
> x <- c(87,76.3,1.67)
> x
[1] 87.0 76.3 1.67
> names(x) <- c(“age”, “weight”, “height”)
> x
age weight height
87.0
76.3 1.67
# Alternatively
> x <- c(age=87, weight=76.3, height=1.67)
> x[“age”]
[1] 87.0
> bmi <- x[“weight”]/x[“height”]^2
比較
> x[1]
[1] 87.0
> bmi <- x[2]/x[3]^2
2015/7/20
Jeff Lin, MD. PhD.
37
R: 向量, 矩陣 可加上 變數名稱 辨識
Matrices:
2015/7/20
>
>
>
>
>
x1 <- c(87,76.3,1.67)
x2 <- c(78,96.3,1.84)
x3 <- c(45,62.9,1.54)
x <- matrix(c(x1,x2,x3), nrow=3, byrow=TRUE)
x
[,1] [,2] [,3]
[1,]
87 76.3 1.67
[2,]
78 96.3 1.84
[3,]
45 62.9 1.54
> colnames(x) <- c("age", "weight", "height")
> rownames(x) <- c(“jon”, “kim”, “dan”)
> x
age weight height
jon 87
76.3
1.67
kim 78
96.3
1.84
dan 45
62.9
1.54
> x[“jon”,]
age weight height
87.00 76.30
1.67
> x[,c("weight","age")]
weight age
jon
76.3 87
kim
96.3 78
dan
62.9 45
> bmi <- x[,"weight"]/x[,"height"]^2
> bmi
jon
kim
dan
27.35846 28.44400 26.52218
Jeff Lin, MD. PhD.
38
> x <- c(a=1:2, b=3,
c=5:8)
> x
a1 a2 b c1 c2 c3 c4
1 2 3 5 6 7 8
> x <- list(a=1:2, b=3,
c=5:8)
> x
$a
[1] 1 2
$b
[1] 3
$c
[1] 5 6 7 8
> x$a
[1] 1 2
> x[[“a”]]
[1] 1 2
> x[2:3]
$b
[1] 3
$c
[1] 5 6 7 8
> x[c(“b”,”c”)]
$b
[1] 3
$c
2015/7/20
[1] 5 6 7 8
列表 Lists
向量, 矩陣 僅能包含相同類型資料.
列表 物件是由資料物件組成, 列表物
件中的成份 (component) 是物件, 是
有順序的,成份物件的元素模式, 沒有
任合限制, 每一個別成份之原型模式
可以不相同.
> x <- list(a=1:4, b=4:6+2i,
src=“GenePix”)
> x
$a
[1] 1 2 3 4
$b
[1] 4+2i 5+2i 6+2i
$src
[1] "GenePix“
Jeff Lin, MD. PhD.
39
Questions?!
2015/7/20
Jeff Lin, MD. PhD.
40
Thanks !
2015/7/20
Jeff Lin, MD. PhD.
41