将函数应用于xts quantmod的子集。

问题描述 投票:0回答:1

我想按年计算股票价格的标准差,但每年得到的值都是一样的。

我试过用dplyr(group_by,summaryise),也试过用函数,但都不顺利,都返回同样的值67.0。

可能是传递了整个数据框而没有子集,如何解决这个问题?

library(quantmod)
library(tidyr)
library(dplyr)

#initial parameters
initialDate = as.Date('2010-01-01')
finalDate = Sys.Date()

ybeg = format(initialDate,"%Y")
yend = format(finalDate,"%Y")

ticker = "AAPL"

#getting stock prices
stock = getSymbols.yahoo(ticker, from=initialDate, auto.assign = FALSE)
stock = stock[,4] #working only with closing prices

用dplyr。

#Attempt 1 with dplyr - not working, all values by year return the same

stock = stock %>% zoo::fortify.zoo()
stock$Date = stock$Index
separate(stock, Date, c("year","month","day"), sep="-") %>% 
   group_by(year) %>%
   summarise(stdev= sd(stock[,2]))

# A tibble: 11 x 2
#   year  stdev
#   <chr> <dbl>
# 1 2010   67.0
# 2 2011   67.0
#....
#10 2019   67.0
#11 2020   67.0

还有用函数

#Attempt 2 with function - not working - returns only one value instead of multiple

#getting stock prices
stock = getSymbols.yahoo(ticker, from=initialDate, auto.assign = FALSE)
stock = stock[,4] #working only with closing prices

#subsetting
years = as.character(seq(ybeg,yend,by=1))
years

calculate_stdev = function(series,years) {
  series[years] #subsetting by years, to be equivalent as stock["2010"], stock["2011"] e.g.
  sd(series[years][,1]) #calculate stdev on closing prices of the current subset
}

yearly.stdev = calculate_stdev(stock,years)

> yearly.stdev
[1] 67.04185
r dplyr subset quantmod
1个回答
1
投票

不要在你的 "数据框架 "中再次传递数据框架的名称。summarise 函数。用变量名代替。

separate(stock, Date, c("year","month","day"), sep="-") %>% 
  group_by(year) %>% 
  summarise(stdev = sd(AAPL.Close)) # <-- here
# A tibble: 11 x 2
#   year  stdev
#   <chr> <dbl>
# 1 2010   5.37
# 2 2011   3.70
# 3 2012   9.57
# 4 2013   6.41
# 5 2014  13.4 
# 6 2015   7.68
# 7 2016   7.64
# 8 2017  14.6 
# 9 2018  20.6 
#10 2019  34.5 
#11 2020  28.7 

1
投票

我不知道 dplyr但以下是如何与 data.table

library(data.table)

# convert data.frame to data.table
setDT(stock)

# convert your Date column with content like "2020-06-17" from character to Date type
stock[,Date:=as.Date(Date)]

# calculate sd(price) grouped by year, assuming here your price column is named "price"
stock[,sd(price),year(Date)]
© www.soinside.com 2019 - 2024. All rights reserved.