Jying-Nan Wang
2016-11-05
主要分成四個常用的部分
library(quantmod)
# 基本抓取資料與繪圖
getSymbols("AAPL",from="2015-09-01",to="2015-10-31")## [1] "AAPL"
head(AAPL)## AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
## 2015-09-01 110.15 111.88 107.36 107.72 76845900
## 2015-09-02 110.23 112.34 109.13 112.34 61888800
## 2015-09-03 112.49 112.78 110.04 110.37 53233900
## 2015-09-04 108.97 110.45 108.51 109.27 49996300
## 2015-09-08 111.75 112.56 110.32 112.31 54843600
## 2015-09-09 113.76 114.02 109.77 110.15 85010800
## AAPL.Adjusted
## 2015-09-01 104.9263
## 2015-09-02 109.4265
## 2015-09-03 107.5076
## 2015-09-04 106.4361
## 2015-09-08 109.3973
## 2015-09-09 107.2933
chartSeries(AAPL,theme="white")X <- getSymbols("2330.tw", auto.assign=FALSE, from="2015-10-01",to="2015-11-13")
chartSeries(X) ## Just try it
getSymbols("AAPL",from="2015-06-01",to="2015-10-31")## [1] "AAPL"
# barChart(AAPL)
# Add multi-coloring and change background to white
candleChart(AAPL,multi.col=TRUE,theme="white")
addMACD()
addBBands()+-*/^%%範例
# 符號 # 在 R 中表示註解
# 加法 addition
5 + 5
# 減法 subtraction
5 - 5
# 乘法 multiplication
3 * 5
# 除法 division
(5 + 5) / 2
# 計算次方Exponentiation
3^4
# 計算餘數 Modulo
12 %% 5# 設定變數x,將100存放到x中
x = 100
# 另一種寫法,可用 "<-" 取代 "="
x <- 100
# 列印出x
print(x)## [1] 100
# 也能直接輸入 x
x## [1] 100
範例
# Assign a value to the variables x1 and x2
x1 <- 7
x2 <- 8
# Add these two variables together
x1+x2
# Create the variable my_x
my_x <- x1+x2
print(my_x)範例
# Change my_numeric1 to be 42
my_numeric1 <- 42
# Check class of my_numeric1
class(my_numeric1)
# Change my_numeric2 to be 42
my_numeric2 <- 42L
# Check class of my_numeric2
class(my_numeric2)
# Change my_character to be "universe"
my_character <- "universe"
# Check class of my_character
class(my_character)
# Change my_logical to be FALSE
my_logical <- FALSE
# Check class of my_logical
class(my_logical)You create a vector with the combine function c(). You place the vector elements separated by a comma between the parentheses.
# 數值向量
numeric_vector <- c(1,2,3,4,5)
print(numeric_vector)
# 字串向量
character_vector <- c("a", "b", "c")
print(character_vector)
# 布林值向量
boolean_vector <- c(TRUE, FALSE, TRUE, TRUE)
print(boolean_vector)範例
A_vector <- c(1, 2, 3)
B_vector <- c(4, 5, 6)
# 加總 A_vector 和 B_vector
total_vector <- A_vector+B_vector
# 列印 total_vector
total_vector
# 計算 total_vector中元素的總和
sum(total_vector)
# 選擇total_vector中第2個元素
total_vector[2]
# 選擇total_vector中第1、3元素
total_vector[c(1,3)]
# 選擇total_vector中第2、3元素
total_vector[c(2,3)]
total_vector[2:3]
total_vector[-1]
total_vector[c(FALSE,TRUE,TRUE)] #重要觀念Generate regular sequences. seq is a standard generic with a default method.
x <- seq(1,10)
print(x)
y1 <- seq(1,10,2)
print(y1)
y2 <- seq(4,8, length=9)
print(y2)
c(1:12,0,-1)array1 <- 1:3
array2 <- 2:4
array1*array2 # 兩向量中個別元素相乘
array1 %*% array2 # 兩向量的 inner product (內積)
array1+1
array1^2More about vector
x <- c(2, -10, 4, 3, 1, 8)
print(x)
x[1] <- -2
print(x)
x[-2]
x>3
x[x>3]x <- c(2, -10, 4, 3, 1, 8)
x[2] # 選擇第2個元素
x[-2] # 選擇除了第2個元素外所有元素
x[x>2] # 選擇大於2的所有元素
x[x<3] # 那幾個元置小於3 (數值)
x[x<0] <- -x[x<0] # Same result as x=abs(x)A <- matrix(c(1:6),2,3)
#參教依序為資料、列數、行數,是否依列填入資料
show(A)## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
B <- matrix(c(1:6),2,3,byrow=TRUE)
#若byrow=true,則依列填入資料,預設為false
show(B)## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
C <- matrix(seq(5,100,length=20),5,4,byrow=TRUE)
show(C)## [,1] [,2] [,3] [,4]
## [1,] 5 10 15 20
## [2,] 25 30 35 40
## [3,] 45 50 55 60
## [4,] 65 70 75 80
## [5,] 85 90 95 100
C[3,1]## [1] 45
C[1:2,1]## [1] 5 25
C[1,]## [1] 5 10 15 20
# Box office Star Wars (in millions!)
new_hope <- c(460.998, 314.4)
empire_strikes <- c(290.475, 247.900)
return_jedi <- c(309.306, 165.8)
# Construct matrix
star_wars_matrix <- matrix(c(new_hope, empire_strikes, return_jedi), nrow = 3, byrow = TRUE)
# Vectors region and titles, used for naming
region <- c("US", "non-US")
titles <- c("A New Hope", "The Empire Strikes Back", "Return of the Jedi")
# Name the columns with region
colnames(star_wars_matrix) <- region
# Name the rows with titles
rownames(star_wars_matrix) <- titles
# Print out star_wars_matrix
star_wars_matrix## US non-US
## A New Hope 460.998 314.4
## The Empire Strikes Back 290.475 247.9
## Return of the Jedi 309.306 165.8
C <- matrix(seq(5,100,length=20),5,4,byrow=TRUE)
show(C)
C[,2:3]
C[c(1,3),]
C[-1,]X1 <- matrix(c(1:4), nrow=2, ncol=2)
show(X1)## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
X1+1## [,1] [,2]
## [1,] 2 4
## [2,] 3 5
X1^2## [,1] [,2]
## [1,] 1 9
## [2,] 4 16
X2 <- matrix(c(4:9),nrow=2, ncol=3)
show(X2)## [,1] [,2] [,3]
## [1,] 4 6 8
## [2,] 5 7 9
t(X2)## [,1] [,2]
## [1,] 4 5
## [2,] 6 7
## [3,] 8 9
dim(X2)## [1] 2 3
nrow(X2)## [1] 2
ncol(X2)## [1] 3
colSums(X2)## [1] 9 13 17
rowSums(X2)## [1] 18 21
X3 <- solve(X1) # inverse matrix
show(X3)## [,1] [,2]
## [1,] -2 1.5
## [2,] 1 -0.5
X1*X3 # 元素間相加## [,1] [,2]
## [1,] -2 4.5
## [2,] 2 -2.0
X1%*%X3 # 矩陣相乘## [,1] [,2]
## [1,] 1 0
## [2,] 0 1
X1 <- matrix(c(1:6),nrow=2, ncol=3)
X2 <- matrix(c(4:9),nrow=2, ncol=3)
rbind(X1,X2) #Joining Rows 矩陣合併## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
## [3,] 4 6 8
## [4,] 5 7 9
cbind(X1,X2) #Joining Columns 矩陣合併## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 3 5 4 6 8
## [2,] 2 4 6 5 7 9
Let the regression equation be \[ y=\beta_0 + \beta_1 x_1 +\cdots + \beta_k x_k +\varepsilon \] thses can be arrayed in the matrix form \[ y=\mathbf{X} \beta +\varepsilon \] The ordinary least-square regression estimator \[ \hat{\beta}=(\mathbf{X}'\mathbf{X})^{-1} \mathbf{X}' y \] Now, we have the independent variable \(x\) and dependent variable \(y\) as following \[ x=[1,3,5,6,8],\;\;\; y=[2,4,5,8,13] \]
Use the above formula to find the OLS results.
x0 <- seq(1,1,length=5)
x1 <- c(1,3,5,6,8)
X <- t(rbind(x0,x1))
show(X)## x0 x1
## [1,] 1 1
## [2,] 1 3
## [3,] 1 5
## [4,] 1 6
## [5,] 1 8
y <- c(2,4,5,8,13)
show(y)## [1] 2 4 5 8 13
beta <- solve(t(X)%*%X)%*%t(X)%*%y
show(beta)## [,1]
## x0 -0.5
## x1 1.5
Let’s see an example of data frames in R.
mtcars## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
# the structure of mtcars
str(mtcars)## 'data.frame': 32 obs. of 11 variables:
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num 160 160 108 258 360 ...
## $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
## $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec: num 16.5 17 18.6 19.4 17 ...
## $ vs : num 0 0 1 1 0 1 0 1 1 1 ...
## $ am : num 1 1 1 0 0 0 0 0 0 0 ...
## $ gear: num 4 4 4 3 3 3 3 4 4 4 ...
## $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
head(mtcars)## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
tail(mtcars)## mpg cyl disp hp drat wt qsec vs am gear carb
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.7 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.5 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.5 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.6 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.6 1 1 4 2
head(mtcars,10)## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
mtcars[2:5,1:2]## mpg cyl
## Mazda RX4 Wag 21.0 6
## Datsun 710 22.8 4
## Hornet 4 Drive 21.4 6
## Hornet Sportabout 18.7 8
mtcars$cyl[1:5]## [1] 6 6 4 6 8
cly4 <- mtcars$cyl==4
mtcars[cly4,]## mpg cyl disp hp drat wt qsec vs am gear carb
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
subset(mtcars, subset = wt>5)## mpg cyl disp hp drat wt qsec vs am gear carb
## Cadillac Fleetwood 10.4 8 472 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440 230 3.23 5.345 17.42 0 0 3 4
subset(mtcars, subset = cyl==6)## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
subset(mtcars, subset = cyl==6 & wt<3)## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Ferrari Dino 19.7 6 145 175 3.62 2.770 15.50 0 1 5 6
subset(mtcars, subset = cyl==6 | wt<2)## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
a <- c(1,2,3,4)
b <- c(2,4,6,8)
levels <- c("A","B","A","B")
result <- data.frame(first=a,second=b,f=levels)
# 讀取外部資料至R,常常資料會自動轉換為dataframe 的資料格式。
# The vectors can be of all different types.
str(result)## 'data.frame': 4 obs. of 3 variables:
## $ first : num 1 2 3 4
## $ second: num 2 4 6 8
## $ f : Factor w/ 2 levels "A","B": 1 2 1 2
summary(result)## first second f
## Min. :1.00 Min. :2.0 A:2
## 1st Qu.:1.75 1st Qu.:3.5 B:2
## Median :2.50 Median :5.0
## Mean :2.50 Mean :5.0
## 3rd Qu.:3.25 3rd Qu.:6.5
## Max. :4.00 Max. :8.0
result## first second f
## 1 1 2 A
## 2 2 4 B
## 3 3 6 A
## 4 4 8 B
# order() is a function that gives you the ranked position of each element
order(c(30,40,10,20))## [1] 3 4 1 2
order(c(30,40,10,20),decreasing = TRUE)## [1] 2 1 4 3
positions <- order(mtcars$mpg)
head(mtcars[positions,])## mpg cyl disp hp drat wt qsec vs am gear carb
## Cadillac Fleetwood 10.4 8 472 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460 215 3.00 5.424 17.82 0 0 3 4
## Camaro Z28 13.3 8 350 245 3.73 3.840 15.41 0 0 3 4
## Duster 360 14.3 8 360 245 3.21 3.570 15.84 0 0 3 4
## Chrysler Imperial 14.7 8 440 230 3.23 5.345 17.42 0 0 3 4
## Maserati Bora 15.0 8 301 335 3.54 3.570 14.60 0 1 5 8
目前我們已經熟悉了以下三種資料類型:
Lists 可以同時包各種不同型態的資料類型。
# Vector with numerics from 1 up to 10
my_vector <- 1:10
# Matrix with numerics from 1 up to 9
my_matrix <- matrix(1:10, ncol = 2)
# First 10 elements of the built-in data frame mtcars
my_df <- mtcars[1:10,1:3]
# Construct list with these different elements:
my_list <- list(my_vector,my_matrix,my_df)
str(my_list)## List of 3
## $ : int [1:10] 1 2 3 4 5 6 7 8 9 10
## $ : int [1:5, 1:2] 1 2 3 4 5 6 7 8 9 10
## $ :'data.frame': 10 obs. of 3 variables:
## ..$ mpg : num [1:10] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2
## ..$ cyl : num [1:10] 6 6 4 6 8 6 8 4 4 6
## ..$ disp: num [1:10] 160 160 108 258 360 ...
names(my_list) <- c("List1","List2","List3")
# Print out my_list
my_list## $List1
## [1] 1 2 3 4 5 6 7 8 9 10
##
## $List2
## [,1] [,2]
## [1,] 1 6
## [2,] 2 7
## [3,] 3 8
## [4,] 4 9
## [5,] 5 10
##
## $List3
## mpg cyl disp
## Mazda RX4 21.0 6 160.0
## Mazda RX4 Wag 21.0 6 160.0
## Datsun 710 22.8 4 108.0
## Hornet 4 Drive 21.4 6 258.0
## Hornet Sportabout 18.7 8 360.0
## Valiant 18.1 6 225.0
## Duster 360 14.3 8 360.0
## Merc 240D 24.4 4 146.7
## Merc 230 22.8 4 140.8
## Merc 280 19.2 6 167.6
my_list$List1## [1] 1 2 3 4 5 6 7 8 9 10
my_list[[1]]## [1] 1 2 3 4 5 6 7 8 9 10
my_list$List3$mpg## [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2
my_list$List3[1:5,3]## [1] 160 160 108 258 360