Это в некоторой степени дополнительный / дополнительный вопрос к вопросу, который я задаю здесь , но вопрос в другом.
Я хотел бы знать, где я ошибаюсь с моим кодом вС уважением к отображению над вложенными столбиками. Данные могут быть получены с помощью:
library(tidyquant)
library(lubridate)
tickers <- c("GIS", "KR", "MKC", "SJM", "EL", "HRL", "HSY", "K",
"KMB", "MDLZ", "MNST", "PEP", "PG", "PM", "SYY", "TAP", "TSN", "WBA", "WMT",
"MMM", "ABMD", "ACN", "AMD", "AES", "AON", "ANTM", "APA", "CSCO", "CMS", "KO", "GRMN", "GPS",
"JEC", "SJM", "JPM", "JNPR", "KSU", "KEYS", "KIM", "NBL", "NEM", "NWL", "NFLX", "NEE", "NOC", "TMO", "TXN", "TWTR")
data <- tq_get(tickers,
get = "stock.prices", # Collect the stock price data from 2010 - 2015
from = "2010-01-01",
to = "2015-01-01") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted, # Convert the data from daily prices to monthly prices
mutate_fun = periodReturn,
period = "monthly",
type = "arithmetic")
df_monthly <- data %>%
mutate(year = year(date)) %>%
group_by(symbol, year) %>% # I group_by and nest the data in order to create the event data which remains fixed over the monthly periods
nest() %>%
mutate( # Here I randomly create the dates
release_date = paste(year,
str_pad(ceiling(runif(row_number(), min = 1, max = 12)), 2, pad = "0"), # Create the months 1 - 12 months
str_pad(ceiling(runif(row_number(), min = 1, max = 27)), 2, pad = "0"), # Create the days - I choose 27 days in a month since later I set the days to the end of month day
sep = "-"),
score = runif(row_number(), min = 0, max = 1), # Randomly generate some scoring function
release_date = as.Date(release_date),
release_date = ceiling_date(release_date, "month") - days(1) # This gives the end of month date
) %>%
unnest() %>% # unnest to expand the yearly release_date and score to the monthly data
ungroup() %>%
mutate_if(is.integer, as.numeric) %>%
arrange(release_date)
Часть, на которой я застрял, является этой частью:
d <- df_monthly %>%
group_by(release_date) %>%
nest() %>%
map(~data %>%
mutate(ntile_score = ntile(score, 2))
)
также не работает:
df_monthly %>%
group_by(release_date) %>%
nest() %>%
map(~data %>%
mutate(ntile_score = ~ntile(.x$score, 2))
)
ЧтоЯ хотел бы сделать, чтобы отобразить вложенный data
и вычислить ntiles
. Я пробую несколько разных методов, но не могу заставить его работать.