This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)# Impor Package
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(PerformanceAnalytics)
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
library(dplyr)
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:xts':
##
## first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(ggrepel)
library(patchwork)
library(scales)
library(tidyr)
# Impor Data
start_date <- "2020-01-01"
end_date <- "2024-12-31"
tickers <- c("ANTM.JK", "PTBA.JK", "ADRO.JK", "MDKA.JK", "^JKSE")
getSymbols(tickers, src = "yahoo", from = start_date, to = end_date, auto.assign = TRUE)
## [1] "ANTM.JK" "PTBA.JK" "ADRO.JK" "MDKA.JK" "JKSE"
antm <- Ad(ANTM.JK)
ptba <- Ad(PTBA.JK)
adro <- Ad(ADRO.JK)
mdka <- Ad(MDKA.JK)
jkse <- Ad(JKSE)
# Daily Return
antm_ret <- dailyReturn(antm, type = "log")
ptba_ret <- dailyReturn(ptba, type = "log")
adro_ret <- dailyReturn(adro, type = "log")
mdka_ret <- dailyReturn(mdka, type = "log")
mkt_ret <- dailyReturn(jkse, type = "log")
returns <- merge(antm_ret, ptba_ret, adro_ret, mdka_ret, mkt_ret)
colnames(returns) <- c("ANTM", "PTBA", "ADRO", "MDKA", "Market")
returns <- na.omit(returns)
# Risk-Free Rate (BI Rate 5%)
Rf_annual <- 0.05
Rf_daily <- Rf_annual / 252
# Excess Return
ex_antm <- returns$ANTM - Rf_daily
ex_ptba <- returns$PTBA - Rf_daily
ex_adro <- returns$ADRO - Rf_daily
ex_mdka <- returns$MDKA - Rf_daily
ex_mkt <- returns$Market - Rf_daily
# Regresi OLS - Beta & Alpha
reg_antm <- lm(ex_antm ~ ex_mkt)
reg_ptba <- lm(ex_ptba ~ ex_mkt)
reg_adro <- lm(ex_adro ~ ex_mkt)
reg_mdka <- lm(ex_mdka ~ ex_mkt)
# Matriks
Rm_annual <- mean(returns$Market) * 252
MRP <- Rm_annual - Rf_annual
hitung_capm <- function(reg, ret_series, nama) {
alpha_daily <- coef(reg)[1]
beta <- coef(reg)[2]
alpha_annual <- alpha_daily * 252 * 100 # Jensen's Alpha % Ann.
r2 <- summary(reg)$r.squared * 100 # R² %
p_beta <- summary(reg)$coefficients[2, 4] # p-value beta
# E(R) CAPM tahunan %
e_return <- (Rf_annual + beta * MRP) * 100
# Actual Return tahunan %
actual_r <- mean(ret_series) * 252 * 100
# Klasifikasi Beta
klasif <- ifelse(beta > 1, "Agresif (β>1)",
ifelse(beta == 1, "Netral (β=1)", "Defensif (β<1)"))
# Status
status <- ifelse(actual_r > e_return, "Undervalued", "Overvalued")
# Treynor Ratio (harian → tahunan)
treynor <- ((mean(ret_series) - Rf_daily) * 252) / beta
data.frame(
Saham = nama,
Beta = round(beta, 4),
Klasifikasi = klasif,
Jensen_Alpha = round(alpha_annual, 2),
E_R_CAPM = round(e_return, 2),
Actual_R = round(actual_r, 2),
R2 = round(r2, 2),
Treynor_Ratio = round(treynor, 4),
P_Value_Beta = round(p_beta, 4),
Status = status
)
}
hasil <- rbind(
hitung_capm(reg_antm, returns$ANTM, "ANTM.JK"),
hitung_capm(reg_ptba, returns$PTBA, "PTBA.JK"),
hitung_capm(reg_adro, returns$ADRO, "ADRO.JK"),
hitung_capm(reg_mdka, returns$MDKA, "MDKA.JK")
)
# Output Tabel
cat("\n")
cat(" HASIL CAPM - Capital Asset Pricing Model\n")
## HASIL CAPM - Capital Asset Pricing Model
cat(" Saham: ANTM, PTBA, ADRO, MDKA | Periode: 2020-2024 | Daily\n")
## Saham: ANTM, PTBA, ADRO, MDKA | Periode: 2020-2024 | Daily
cat(" Rf = 5%/tahun | Market = IHSG (^JKSE)\n")
## Rf = 5%/tahun | Market = IHSG (^JKSE)
print(hasil, row.names = FALSE)
## Saham Beta Klasifikasi Jensen_Alpha E_R_CAPM Actual_R R2
## ANTM.JK 1.5174 Agresif (β>1) 14.32 1.18 15.50 25.75
## PTBA.JK 1.2496 Agresif (β>1) 16.37 1.85 18.23 25.19
## ADRO.JK 1.3690 Agresif (β>1) 31.17 1.55 32.72 22.47
## MDKA.JK 1.0419 Agresif (β>1) 6.32 2.38 8.70 12.60
## Treynor_Ratio P_Value_Beta Status
## 0.0692 0 Undervalued
## 0.1058 0 Undervalued
## 0.2025 0 Undervalued
## 0.0355 0 Undervalued
cat("\n")
cat(" RINGKASAN MATRIKS UTAMA\n")
## RINGKASAN MATRIKS UTAMA
cat(sprintf("\n Beta Tertinggi : %.4f → %s\n",
max(hasil$Beta), hasil$Saham[which.max(hasil$Beta)]))
##
## Beta Tertinggi : 1.5174 → ANTM.JK
cat(sprintf(" Beta Terendah : %.4f → %s\n",
min(hasil$Beta), hasil$Saham[which.min(hasil$Beta)]))
## Beta Terendah : 1.0419 → MDKA.JK
cat(sprintf(" Jensen Alpha Terbaik : %.2f%% → %s\n",
max(hasil$Jensen_Alpha), hasil$Saham[which.max(hasil$Jensen_Alpha)]))
## Jensen Alpha Terbaik : 31.17% → ADRO.JK
cat(sprintf(" R² Terbaik : %.2f%% → %s\n",
max(hasil$R2), hasil$Saham[which.max(hasil$R2)]))
## R² Terbaik : 25.75% → ANTM.JK
cat("\n")
cat(" KEPUTUSAN INVESTASI\n")
## KEPUTUSAN INVESTASI
for (i in 1:nrow(hasil)) {
cat(sprintf("\n %s:\n", hasil$Saham[i]))
cat(sprintf(" E(R) CAPM : %.2f%%\n", hasil$E_R_CAPM[i]))
cat(sprintf(" Return Aktual: %.2f%%\n", hasil$Actual_R[i]))
cat(sprintf(" Status : %s\n", hasil$Status[i]))
cat(sprintf(" Beta : %.4f (%s)\n", hasil$Beta[i], hasil$Klasifikasi[i]))
cat(sprintf(" Jensen Alpha : %.2f%% per tahun\n", hasil$Jensen_Alpha[i]))
cat(sprintf(" Treynor Ratio: %.4f\n", hasil$Treynor_Ratio[i]))
}
##
## ANTM.JK:
## E(R) CAPM : 1.18%
## Return Aktual: 15.50%
## Status : Undervalued
## Beta : 1.5174 (Agresif (β>1))
## Jensen Alpha : 14.32% per tahun
## Treynor Ratio: 0.0692
##
## PTBA.JK:
## E(R) CAPM : 1.85%
## Return Aktual: 18.23%
## Status : Undervalued
## Beta : 1.2496 (Agresif (β>1))
## Jensen Alpha : 16.37% per tahun
## Treynor Ratio: 0.1058
##
## ADRO.JK:
## E(R) CAPM : 1.55%
## Return Aktual: 32.72%
## Status : Undervalued
## Beta : 1.3690 (Agresif (β>1))
## Jensen Alpha : 31.17% per tahun
## Treynor Ratio: 0.2025
##
## MDKA.JK:
## E(R) CAPM : 2.38%
## Return Aktual: 8.70%
## Status : Undervalued
## Beta : 1.0419 (Agresif (β>1))
## Jensen Alpha : 6.32% per tahun
## Treynor Ratio: 0.0355
cat(" KETERANGAN STATUS\n")
## KETERANGAN STATUS
cat(" Undervalued - Actual Return > E(R) CAPM - LAYAK DIBELI\n")
## Undervalued - Actual Return > E(R) CAPM - LAYAK DIBELI
cat(" Overvalued - Actual Return < E(R) CAPM - TIDAK DISARANKAN\n")
## Overvalued - Actual Return < E(R) CAPM - TIDAK DISARANKAN
# ── VISUALISASI ──────────────────────────────────────────────────────────────
warna_saham <- c("ANTM" = "#E63946", "PTBA" = "#2196F3",
"ADRO" = "#4CAF50", "MDKA" = "#FF9800")
# Rebuild hasil dengan nama pendek untuk grafik
hasil_plot <- rbind(
hitung_capm(reg_antm, returns$ANTM, "ANTM"),
hitung_capm(reg_ptba, returns$PTBA, "PTBA"),
hitung_capm(reg_adro, returns$ADRO, "ADRO"),
hitung_capm(reg_mdka, returns$MDKA, "MDKA")
)
# GRAFIK 1 — BETA vs E(R) CAPM (Scatter)
p1 <- ggplot(hasil_plot, aes(x = Beta, y = E_R_CAPM, color = Saham, label = Saham)) +
geom_point(size = 6, alpha = 0.9) +
geom_text_repel(size = 4.5, fontface = "bold", nudge_y = 0.3,
box.padding = 0.4, show.legend = FALSE) +
scale_color_manual(values = warna_saham) +
labs(
title = "Beta vs E(R) CAPM",
subtitle = "Semakin tinggi beta, semakin tinggi ekspektasi return",
x = "Beta (β)",
y = "E(R) CAPM (%/tahun)",
color = "Saham"
) +
theme_minimal(base_size = 13) +
theme(
plot.title = element_text(face = "bold", size = 14),
plot.subtitle = element_text(color = "grey50", size = 10),
legend.position = "bottom"
)
# GRAFIK 2 — JENSEN'S ALPHA PER SAHAM (Bar Chart)
p2 <- ggplot(hasil_plot, aes(x = reorder(Saham, Jensen_Alpha),
y = Jensen_Alpha, fill = Saham)) +
geom_col(width = 0.55, alpha = 0.9) +
geom_hline(yintercept = 0, linetype = "dashed", color = "grey40", linewidth = 0.8) +
geom_text(aes(label = paste0(Jensen_Alpha, "%"),
vjust = ifelse(Jensen_Alpha >= 0, -0.5, 1.3)),
fontface = "bold", size = 4.5) +
scale_fill_manual(values = warna_saham) +
labs(
title = "Jensen's Alpha per Saham",
subtitle = "Alpha > 0 - manajer menghasilkan return di atas CAPM",
x = NULL,
y = "Jensen's Alpha (%/tahun)",
fill = "Saham"
) +
theme_minimal(base_size = 13) +
theme(
plot.title = element_text(face = "bold", size = 14),
plot.subtitle = element_text(color = "grey50", size = 10),
legend.position = "none"
)
# GRAFIK 3 — SECURITY MARKET LINE (SML)
beta_range <- seq(0, max(hasil_plot$Beta) * 1.3, length.out = 200)
sml_df <- data.frame(
Beta = beta_range,
E_R_SML = (Rf_annual + beta_range * MRP) * 100
)
p3 <- ggplot() +
geom_line(data = sml_df, aes(x = Beta, y = E_R_SML),
color = "#333333", linewidth = 1.2, linetype = "solid") +
geom_point(aes(x = 0, y = Rf_annual * 100),
color = "black", size = 3, shape = 18) +
annotate("text", x = 0.03, y = Rf_annual * 100 + 0.3,
label = paste0("Rf = ", Rf_annual * 100, "%"),
size = 3.5, hjust = 0, color = "grey30") +
geom_point(aes(x = 1, y = Rm_annual * 100),
color = "black", size = 3, shape = 18) +
annotate("text", x = 1.03, y = Rm_annual * 100 + 0.3,
label = paste0("Rm = ", round(Rm_annual * 100, 2), "%"),
size = 3.5, hjust = 0, color = "grey30") +
geom_point(data = hasil_plot, aes(x = Beta, y = Actual_R, color = Saham),
size = 7, alpha = 0.9) +
geom_text_repel(
data = hasil_plot,
aes(x = Beta, y = Actual_R,
label = paste0(Saham, "\n(", Status, ")"),
color = Saham),
size = 3.8, fontface = "bold", box.padding = 0.5,
nudge_x = 0.05, show.legend = FALSE
) +
geom_segment(data = hasil_plot,
aes(x = Beta, xend = Beta,
y = E_R_CAPM, yend = Actual_R,
color = Saham),
linetype = "dotted", linewidth = 0.9, show.legend = FALSE) +
scale_color_manual(values = warna_saham) +
labs(
title = "Security Market Line (SML) — CAPM",
subtitle = "Di atas SML = Undervalued | Di bawah SML = Overvalued",
x = "Beta (β)",
y = "Return (%/tahun)",
color = "Saham"
) +
theme_minimal(base_size = 13) +
theme(
plot.title = element_text(face = "bold", size = 14),
plot.subtitle = element_text(color = "grey50", size = 10),
legend.position = "bottom"
)
# GRAFIK 4 — ACTUAL vs E(R) CAPM (Grouped Bar)
hasil_long <- hasil_plot %>%
select(Saham, Actual_R, E_R_CAPM) %>%
pivot_longer(cols = c(Actual_R, E_R_CAPM),
names_to = "Tipe",
values_to = "Return") %>%
mutate(Tipe = recode(Tipe,
"Actual_R" = "Return Aktual",
"E_R_CAPM" = "E(R) CAPM"))
p4 <- ggplot(hasil_long, aes(x = Saham, y = Return, fill = Tipe)) +
geom_col(position = position_dodge(width = 0.6), width = 0.5, alpha = 0.9) +
geom_text(aes(label = paste0(round(Return, 1), "%")),
position = position_dodge(width = 0.6),
vjust = -0.4, size = 3.8, fontface = "bold") +
scale_fill_manual(values = c("Return Aktual" = "#1565C0", "E(R) CAPM" = "#EF6C00")) +
labs(
title = "Return Aktual vs E(R) CAPM per Saham",
subtitle = "Return Aktual > E(R) CAPM - Undervalued (layak beli)",
x = NULL,
y = "Return (%/tahun)",
fill = NULL
) +
theme_minimal(base_size = 13) +
theme(
plot.title = element_text(face = "bold", size = 14),
plot.subtitle = element_text(color = "grey50", size = 10),
legend.position = "bottom"
)
p1
p2
p3
p4
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.