ݺߣ

ݺߣShare a Scribd company logo
Econometric Theory (I)
A Quick
Introduction to R
Yen, Chia-Yi
NTU
Sep 18, 2014
TA info
΃x
T
о209 о
yen.chiayi@gmail.com
r01323019@ntu.edu.tw
R user : Taiwan R user group
Installatioan
Having trouble?
http://homepage.ntu.edu.tw/~ckuan/courses.html
(prof. Chung-Ming Kuan s website)
optional, but believe me
its super charming!
R Engine
http://cran.rstudio.com/
R IDE:
Rstudio
http://www.rstudio.com/ide/download/desktop
a FREE
software environment
for statistical computing and graphics
Ris .
Why R
Data Science is Hot !
ʹõĽyӋZ
Y
# \
# yӋ
# cC++
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
Why Rdemo
source: http://www.rstudio.com/shiny/
Why R: demo
Why Rdemo
R Engine
Rstudio
script mode
Help &
Graphics
Debug
Interactive mode
a quick look at R
Expression
Variable (׃)
Function (ʽ)
Module (ģM)
Package (׼)
calculation
numeric, string, boolean
vector,matrix, data.frame
built-in, package, self-defined
# Input / Output (I/O)
# Linear Algebra
# Regression
Lets Demo !
Open "0918_firstR.R in Rstudio.
Whats the difference between...
Interactive mode
v.s.
Script mode
Expression
# 䌍ӋC...
# ؂ TRUE  FALSE
# R code
1+1
(1+1)^4
((1+1)^4-2)%%3
sin(((1+1)^4-2)%%3)
# R code
2<3
2==3
2!=3
data
Variable ׃
like a
container
that stores
# ע: "=" & "==" ܲͬ
# "="oֵ (assign)
# "==":  (equal to)
numeric
string
boolean
# R code
x = 3 # numeric ֵ
x = "Hi, Everyone ~~" # string ִ
x = 'Hi, Everyone ~~' # һҪ̖.
x = TRUE # boolean ֵ
x = T # һҪ
x = 2<3
data
Variable ׃
like a
container
that stores
vector
matrix
data.frame
etc.
vec mat
Variable ׃
1
2
3
4
# R code
# vector
a = c(1,2,3,4) # numeric vector ֵ
b = c("1", "2", "3","4") # string vector ִ
c = c( T, F, T, T) # boolean vector 
# matrix
d = matrix(a, nrow=2, ncol=2)
dim(a) = c(2,2)
# data.frame
e = data.frame(string = b, booling = c) #it can store
different type data
1 3
2 4
1 T
2 F
3 T
4 F
numeric
vector
numeric
matrix
data.frame
Function ʽ
like a collection of computation
Ҳf, һ\
do some computation
a function:length
[1,2,3,4] 4
return
# R code
a = c(1,2,3,4)
result = length(a)
result
input
Function ʽ
do some computation
function: mean
[1,2,3,4] 2.5
return
output
Built-in
self-defined
(package)
# Built-in
data = 1:4
output= mean(data)
data
# Self-defined
MyMean = function(data){
total = sum(data)
len = length(data)
result = total / len
return(result)
}
data = 1:4
output = MyMean(data)
input
Module ģM
like a collection of function
[example] data_preprocess.R
Package ׼
you can expand your
built-in function
by installing a packages
like a collection of module
Package ׼
how to use PACKAGES ?
# R code
x = 1:10 # OxS
y = sin(3*x) # OyS
plot(x,y) # ԭRAOĮDʽ
# ˮ^ƯĈD....
install.packages(ggplot2) #  ggplot2@׼ĹپWdC
#̖DZҪ
library(ggplot2) # ıC load @ݳʽae
qplot(x,y) # ʹ ggplot2e挑õĺʽ qplot
eĺ...
Help & Google
# R code
help(mean)
?mean
example(mean)
Flow Control
#1 if
if (expression){
statement
}
# R code
data = rnorm(100) #Ę˜ʳBг
100ӱc
mu = mean(data)
mu > 0
if ( mu > 0 ){
print("mean is greater than 0")
}else{
print("mean is less than 0")
}
ɜyTRUE,
͈дȔ

t
Flow Control
#2 while
while (expression){
statement
}
# R code
for ( i in 1:3){
data = rnorm(i)
print(data)
}
ֻҪɜyTRUE,
дȔ
t
Flow Control
#3 For
For( i in 1: 3){
statement (i)
}
# R code
data = rnorm(100) #Ę˜ʳBг
100ӱc
mu = mean(data)
mu > 0
while (mu > 0){
print("mean is greater than 0")
# mu = "tested"
}
# loFޒȦԇwhileȵ]
_
 i = 1 , һ
 i = 2 ,وһ
 i = 3 ,وһ
YޒȦ
Homework (optional)
Code School http://tryr.codeschool.com
DataMind http://www.datamind.org/#/
Thanks for your listening :)
Appendix: object()
е(objects)ЃɷN(intrinsic
attributes): ʽ(mode)cL(length)
Appendix: operator(\)
Appendix: sampling SCӣ
Appendix: Most-used function1
Appendix: Most-used function2
Appendix: Basic Graphics (1)
Appendix: Basic Graphics (2)
Appendix: Low-level Graphics

More Related Content

[Ӌһ] Basic r programming final0918