36
Econometric Theory (I) A Quick Introduction to R Yen, Chia-Yi NTU Sep 18, 2014

[計一] Basic r programming final0918

  • Upload
    yency

  • View
    101

  • Download
    1

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: [計一] Basic r programming   final0918

Econometric Theory (I)

A Quick Introduction to R

Yen, Chia-YiNTU

Sep 18, 2014

Page 2: [計一] Basic r programming   final0918

TA info

顏嘉儀

經研所碩二

研究大樓209 研究室

[email protected]@ntu.edu.tw

#R user : Taiwan R user group

Page 3: [計一] Basic r programming   final0918

Installatioan

Having trouble?http://homepage.ntu.edu.tw/~ckuan/courses.html

(prof. Chung-Ming Kuan ‘s website)

optional, but believe me it’s super charming!

R Engine http://cran.rstudio.com/

R IDE: Rstudiohttp://www.rstudio.com/ide/download/desktop

Page 4: [計一] Basic r programming   final0918

a FREE

software environment for statistical computing and graphics

R is ….

Page 5: [計一] Basic r programming   final0918

Why R

Data Science is Hot ! 最多人使用的統計語言

最多人用它分析資料

# 矩陣運算

# 統計分析

# 與C++對接容易

Page 6: [計一] Basic r programming   final0918

Software used in data analysis competitions in 2011.

source :http://r4stats.com/articles/popularity/

http://blog.revolutionanalytics.com/2012/08/r-language-popularity-for-data-mining.html

Page 7: [計一] Basic r programming   final0918

Why R:demo

source: http://www.rstudio.com/shiny/

Page 8: [計一] Basic r programming   final0918

Why R: demo

Page 9: [計一] Basic r programming   final0918

Why R:demo

Page 10: [計一] Basic r programming   final0918

R Engine

Page 11: [計一] Basic r programming   final0918

Rstudio

script mode

Help & Graphics

Debug

Interactive mode

Page 12: [計一] Basic r programming   final0918

a quick look at R

Expression

Variable (變數)

Function (函式)

Module (模組)

Package (套件)

calculation

numeric, string, booleanvector,matrix, data.frame

built-in, package, self-defined

# Input / Output (I/O)# Linear Algebra# Regression

Page 13: [計一] Basic r programming   final0918

Let’s Demo !

Open "0918_firstR.R” in Rstudio.

What’s the difference between...

Interactive mode

v.s.

Script mode

Page 14: [計一] Basic r programming   final0918

Expression

# 其實很像計算機...

# 回傳 TRUE 或 FALSE

# R code

1+1(1+1)^4((1+1)^4-2)%%3sin(((1+1)^4-2)%%3)

# R code

2<32==32!=3

Page 15: [計一] Basic r programming   final0918

data

Variable 變數

like a

container that stores

# 注意: "=" & "==" 功能不同

# "=":給值 (assign) # "==": 相等於 (equal to)

numericstring boolean

# R code

x = 3 # numeric 數值

x = "Hi, Everyone ~~" # string 字串

x = 'Hi, Everyone ~~' # 一定要有引號. x = TRUE # boolean 布林值

x = T # 一定要大寫

x = 2<3

Page 16: [計一] Basic r programming   final0918

data

Variable 變數

like a

container that stores

vectormatrix data.frameetc.

vec mat

Page 17: [計一] Basic r programming   final0918

Variable 變數

1

2

3

4

# R code

# vectora = c(1,2,3,4) # numeric vector 數值向量

b = c("1", "2", "3","4") # string vector 字串向量

c = c( T, F, T, T) # boolean vector 布林向量

# matrixd = matrix(a, nrow=2, ncol=2)dim(a) = c(2,2)

# data.framee = data.frame(string = b, booling = c) #it can store different type data

1 3

2 4

“1” T

“2” F

“3” T

“4” F

numericvector

numeric matrix

data.frame

Page 18: [計一] Basic r programming   final0918

Function 函式

like a collection of computation 也就是說, 把一堆運算包起來

do some computation

a function:length

[1,2,3,4] 4return

# R codea = c(1,2,3,4)result = length(a)

resultinput

Page 19: [計一] Basic r programming   final0918

Function 函式

do some computation

function: mean

[1,2,3,4] 2.5return

output

Built-in self-defined(package)

# Built-indata = 1:4output= mean(data)

data

# Self-definedMyMean = function(data){ total = sum(data) len = length(data) result = total / len return(result)}data = 1:4output = MyMean(data)

input

Page 20: [計一] Basic r programming   final0918

Module 模組

like a collection of function[example] data_preprocess.R

Page 21: [計一] Basic r programming   final0918

Package 套件

you can expand your built-in function by installing a packages

like a collection of module

Page 22: [計一] Basic r programming   final0918

Package 套件

how to use PACKAGES ?

# R code

x = 1:10 # 設定x軸y = sin(3*x) # 設定y軸

plot(x,y) # 原本R預設的畫圖函式

# 為了畫比較漂亮的圖....install.packages(“ggplot2”) # 將 ggplot2這個套件從官網上載到本機端

#括號是必要的

library(ggplot2) # 從本機端 load 到這份程式碼裡

qplot(x,y) # 可以使用 ggplot2裡面寫好的函式 qplot了

Page 23: [計一] Basic r programming   final0918

別忘了你的好朋友...

Help & Google

# R code

help(mean)?meanexample(mean)

Page 24: [計一] Basic r programming   final0918

Flow Control

#1 if

if (expression){statement

}

# R code

data = rnorm(100) #從標準常態分配中抽

100個樣本點

mu = mean(data)mu > 0

if ( mu > 0 ){ print("mean is greater than 0")}else{ print("mean is less than 0")}

如果偵測到TRUE,就執行大括弧內敘述;否則不執行

Page 25: [計一] Basic r programming   final0918

Flow Control

#2 while

while (expression){statement

}

# R code

for ( i in 1:3){ data = rnorm(i) print(data)}

只要偵測到TRUE,執行大括弧內敘述;否則不執行

Page 26: [計一] Basic r programming   final0918

Flow Control

#3 For

For( i in 1: 3){statement (i)

}

# R code

data = rnorm(100) #從標準常態分配中抽

100個樣本點

mu = mean(data)mu > 0

while (mu > 0){ print("mean is greater than 0") # mu = "tested"}# 發生無窮迴圈,試著把while內的註解打

當 i = 1 , 執行一次當 i = 2 ,再執行一次當 i = 3 ,再執行一次結束迴圈

Page 27: [計一] Basic r programming   final0918

Homework (optional)

Code School http://tryr.codeschool.com

DataMind http://www.datamind.org/#/

Page 28: [計一] Basic r programming   final0918

Thanks for your listening :)

Page 29: [計一] Basic r programming   final0918

Appendix: object(物件)

所有的物件(objects)都有兩種基本屬性(intrinsic attributes): 格式(mode)與長度(length)

Page 30: [計一] Basic r programming   final0918

Appendix: operator(運算子)

Page 31: [計一] Basic r programming   final0918

Appendix: sampling (隨機抽樣)

Page 32: [計一] Basic r programming   final0918

Appendix: Most-used function(1)

Page 33: [計一] Basic r programming   final0918

Appendix: Most-used function(2)

Page 34: [計一] Basic r programming   final0918

Appendix: Basic Graphics (1)

Page 35: [計一] Basic r programming   final0918

Appendix: Basic Graphics (2)

Page 36: [計一] Basic r programming   final0918

Appendix: Low-level Graphics