# 基于R语言股票市场收益的统计可视化分析

## 原文链接：http://tecdat.cn/?p=16453

1. 下载收盘价
2. 计算收益率
3. 计算收益的均值和标准差

library(tidyquant)library(timetk)

netflix <- tq_get("NFLX",
from = '2009-01-01',
to = "2018-03-01",
get = "stock.prices")

netflix %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line() +
ggtitle("Netflix since 2009") +
labs(x = "Date", "Price") +
scale\_x\_date(date\_breaks = "years", date\_labels = "%Y") +
labs(x = "Date", y = "Adjusted Price") +
theme_bw()

### 计算单个股票的每日和每月收益率

# 计算每日收益netflix\_daily\_returns <- netflix %>%
mutate_fun = periodReturn,   # 这指定如何处理该列               period = "daily",      # 此参数计算每日收益               col\_rename = "nflx\_returns") # 重命名列#计算每月收益netflix\_monthly\_returns <- netflix %>%
mutate_fun = periodReturn,
period = "monthly",      # 此参数计算每月收益               col\_rename = "nflx\_returns")

### 绘制Netflix的每日和每月收益图表

# 我们将使用折线图获取每日收益

ggplot(aes(x = date, y = nflx_returns)) +
geom_line() +
theme_classic() +

netflix\_daily\_returns %>%
ggplot(aes(x = nflx_returns)) +
geom_histogram(binwidth = 0.015) +
theme_classic() +

# 绘制Netflix的月度收益图表。 使用条形图
ggplot(aes(x = date, y = nflx_returns)) +
geom_bar(stat = "identity") +
theme_classic() +

### 计算Netflix股票的累计收益

mutate(cr = cumprod(1 + nflx_returns)) %>%      # 使用cumprod函数

ggplot(aes(x = date, y = cumulative_returns)) +
geom_line() +
theme_classic() +

R语言ARMA GARCH COPULA模型拟合股票收益率时间序列和模拟可视化

01

02

03

04

ggplot(aes(x = date, y = cumulative_returns)) +
geom_line() +
theme_classic() +

## 多只股票

### 下载多只股票的股票市场数据。

#将我们的股票代码设置为变量
tickers <- c("FB", "AMZN", "AAPL", "NFLX", "GOOG")
# 下载股价数据
multpl\_stocks <- tq\_get(tickers,

### 绘制多只股票的股价图

multpl_stocks %>%  ggplot(aes(x = date, y = adjusted,

facet\_wrap(~symbol, scales = "free\_y") +  # facet_wrap用于制作不同的页面
theme_classic() +

# 计算多只股票的收益

#计算多只股票的每日收益
mutate_fun = periodReturn,
period = 'daily',
col_rename = 'returns')#计算多只股票的月收益
mutate_fun = periodReturn,
period = 'monthly',
col_rename = 'returns')

### 绘制多只股票的收益图表

multpl\_stock\_daily_returns %>%
ggplot(aes(x = date, y = returns)) +
geom_line() +
geom_hline(yintercept = 0) +

multpl\_stock\_monthly_returns %>%
ggplot(aes(x = date, y = return
scale\_fill\_brewer(palette = "Set1",   # 我们会给他们不同的颜色，而不是黑色

### 计算多只股票的累计收益

multpl\_stock\_monthly_returns %>%
mutate(returns e_returns = cr - 1) %>%
ggplot(aes(x = date, y = cumulative_returns, color = symbol)) +
geom_line() +
labs(x = "Date"

# 统计数据

### 计算单个股票的均值，标准差

# 计算平均值

.$\[1$\] %>%  mean(na.rm = TRUE)
nflx\_monthly\_mean_ret <- netfl turns) %>%  .$\[1$\] %>%  mean(na.rm = TRUE)
# 计算标准差
nflx\_daily\_sd_ret <- netflirns) %>%  .$\[1$\] %>%  sd()
nflx\_monthly\_sd\_ret <- netflix\_rns) %>%  .$\[1$\] %>%  sd()
nflx_stat
## # A tibble: 2 x 3##   period     mean     sd
##   <chr>     <dbl>  <dbl>
## 1 Daily   0.00240 0.0337## 2 Monthly 0.0535  0.176

netflix  %>%
summarise(Monthly\_Mean\_Returns = mean(nflx_returns),
MOnthly\_Standard\_Deviation = sd(nflx_returns)
## # A tibble: 10 x 3##     year Monthly\_Mean\_Returns MOnthly\_Standard\_Deviation
##    <dbl>                <dbl>                      <dbl>
##  1  2009              0.0566                      0.0987##  2  2010              0.110                       0.142
##  3  2011             -0.0492                      0.209
##  4  2012              0.0562                      0.289
##  5  2013              0.137                       0.216
##  6  2014              0.00248                     0.140
##  7  2015              0.0827                      0.148
##  8  2016              0.0138                      0.126
##  9  2017              0.0401                      0.0815## 10  2018              0.243                       0.233

netflix\_monthly\_returns %>%
mutate(year = rns, Standard_Deviation, keyistic)) +
geom_bar(stat = "identity", position = "dodge") +
scale\_y\_continuous(b ) +
theme_bw() +

### 计算多只股票的均值，标准差

group_by(symbol) %>%  summarise(mean = mean(returns),
sd = sd(returns))
## # A tibble: 5 x 3##   symbol     mean     sd
##   <chr>     <dbl>  <dbl>
## 1 AAPL   0.00100  0.0153## 2 AMZN   0.00153  0.0183## 3 FB     0.00162  0.0202## 4 GOOG   0.000962 0.0141## 5 NFLX   0.00282  0.0300
group_by(symbol) %>%  summarise(mean = mean(returns),
sd = sd(returns))
## # A tibble: 5 x 3##   symbol   mean     sd
##   <chr>   <dbl>  <dbl>
## 1 AAPL   0.0213 0.0725## 2 AMZN   0.0320 0.0800## 3 FB     0.0339 0.0900## 4 GOOG   0.0198 0.0568## 5 NFLX   0.0614 0.157

%>%  group_by(symbol, year) %>%  summarise(mean = mean(returns),
sd = sd(returns))
## # A tibble: 30 x 4## # Groups:   symbol $?$
##    symbol  year      mean     sd
##    <chr>  <dbl>     <dbl>  <dbl>
##  1 AAPL    2013  0.0210   0.0954##  2 AAPL    2014  0.0373   0.0723##  3 AAPL    2015 -0.000736 0.0629##  4 AAPL    2016  0.0125   0.0752##  5 AAPL    2017  0.0352   0.0616##  6 AAPL    2018  0.0288   0.0557##  7 AMZN    2013  0.0391   0.0660##  8 AMZN    2014 -0.0184   0.0706##  9 AMZN    2015  0.0706   0.0931## 10 AMZN    2016  0.0114   0.0761## # ... with 20 more rows

multpl\_stock\_monthly_returns %>%
mutate(year = year(date)) %>%
group_by(symbol, yea s = seq(-0.1,0.4,0.02),
labels = scales::percent) +
scale\_x\_continuous(breaks = seq(2009,2018,1)) +
labs(x = "Year", y = Stocks") +
ggtitle

multpl\_stock\_monthly_returns %>%
mutate(year = year(date)) %>%
ggplot(aes(x = year, y = sd, fill = symbol)) +
geom_bar(stat = "identity", position = "dodge", width = 0.7) +
scale\_y\_continuous(breaks = seq(-0.1,0.4,0.02),
labels = scales::p
scale\_fill\_brewer(palette = "Set1",

### 计算多只股票的协方差和相关性

#计算协方差
tk_xts(silent = TRUE) %>%
cov()
##               AAPL        AMZN          FB         GOOG          NFLX
## AAPL  5.254736e-03 0.001488462 0.000699818 0.0007420307 -1.528193e-05## AMZN  1.488462e-03 0.006399439 0.001418561 0.0028531565  4.754894e-03## FB    6.998180e-04 0.001418561 0.008091594 0.0013566480  3.458228e-03## GOOG  7.420307e-04 0.002853157 0.001356648 0.0032287790  3.529245e-03## NFLX -1.528193e-05 0.004754894 0.003458228 0.0035292451  2.464202e-02

# 计算相关系数
%>%
tk_xts(silent = TRUE) %>%
cor()
##              AAPL      AMZN        FB      GOOG         NFLX
## AAPL  1.000000000 0.2566795 0.1073230 0.1801471 -0.001342964## AMZN  0.256679539 1.0000000 0.1971334 0.6276759  0.378644485## FB    0.107322952 0.1971334 1.0000000 0.2654184  0.244905437## GOOG  0.180147089 0.6276759 0.2654184 1.0000000  0.395662114## NFLX -0.001342964 0.3786445 0.2449054 0.3956621  1.000000000

## corrplot 0.84 loaded
cor() %>%  corrplot()

|
2天前
|

【R语言实战】聚类分析及可视化
【R语言实战】聚类分析及可视化
13 2
|
2天前
|

R语言逻辑回归logistic模型ROC曲线可视化分析2例：麻醉剂用量影响、汽车购买行为2
R语言逻辑回归logistic模型ROC曲线可视化分析2例：麻醉剂用量影响、汽车购买行为
19 2
|
2天前
|
Web App开发 数据可视化 数据挖掘

10 1
|
2天前
|

17 3
|
2天前

17 3
|
2天前
|

16 0
|
2天前
|

R语言Stan贝叶斯回归置信区间后验分布可视化模型检验|附数据代码
R语言Stan贝叶斯回归置信区间后验分布可视化模型检验|附数据代码
18 1
|
2天前
|

R语言分类回归分析考研热现象分析与考研意愿价值变现
R语言分类回归分析考研热现象分析与考研意愿价值变现
17 2
|
2天前
|

R语言广义线性混合模型GLMMs在生态学中应用可视化2实例合集|附数据代码2
R语言广义线性混合模型GLMMs在生态学中应用可视化2实例合集|附数据代码
24 2
|
2天前
|

R语言广义线性混合模型GLMMs在生态学中应用可视化2实例合集|附数据代码1
R语言广义线性混合模型GLMMs在生态学中应用可视化2实例合集|附数据代码
23 2