関数の作成

金融のための中級R

Lore Dirick

Manager of Data Science Curriculum at Flatiron School

関数の構造

func_name <- function(arguments) {
  body
}
金融のための中級R

1 を足す

add_one <- function(x) {
  x_plus_one <- x + 1
  return(x_plus_one)
}

add_one(7)
8
add_one <- function(x) {
  x + 1
}

add_one(7)
8
金融のための中級R

オプション引数を使う

add <- function(x, value = 1) {
  x + value
}

add(7)
8
add(7, value = 3)
10
金融のための中級R

単純リターンの計算

ch4_vid2_slides.027.png

prices <- c(23.4, 23.8, 22.3)

# S_(t) - S(t-1) ベクトル diff(prices)
0.4 -1.5
# S_(t-1) ベクトル
prices[-length(prices)]
23.4 23.8
# 単純リターン
diff(prices) / prices[-length(prices)]
0.01709402 -0.06302521
金融のための中級R

単純リターンの計算

prices <- c(23.4, 23.8, 22.3)

arith_returns <- function(x) { diff(x) / x[-length(x)] }
arith_returns(prices)
0.01709402 -0.06302521
金融のための中級R

練習しましょう!

金融のための中級R

Preparing Video For Download...