如何在一个值上返回具有相同日期的所有行?

时间:2021-12-15 12:37:56

I'm new using R. I have the following sample dataset:

我是使用r的新用户,我有以下示例数据集:

> head(abn)
       Dates  DTM   YTM
1 2010-09-28 1133 2.965
2 2010-09-28 1834 3.613
3 2010-09-29 1132 2.994
4 2010-09-29 1833 3.595
5 2010-09-30 1131 3.026
6 2010-09-30 1832 3.590

The observations are several bond values on an observation period from 2010-2016. My data set is composed of multiple bonds with maturities between 1-15 years (260-3900 business days as depicted in the dataset). DTM stands for days to maturity and YTM for yield to maturity.

观察结果是2010-2016年观察期内的几个债券价值。我的数据集由1-15年到期的多个债券组成(如数据集中所示260-3900个工作日)。DTM代表到期天数,YTM代表到期收益率。

My goal is to construct a synthetic bond with a maturity of 5 years for each day. Therefore I need to make a regression and find the YTM value for the DTM value of 1300, which is exactly 5 years.

我的目标是每天构建一个5年到期的合成债券。因此我需要做一个回归,找到DTM值为1300的YTM值,正好是5年。

I need to get the value of the y-axis at x=1300. However I need to have this information for every date separately.

我需要得到y轴在x=1300处的值。但是我需要每个日期分别有这个信息。

I got help and the person got me this code:

我得到了帮助,那个人给了我这个密码:

library(dplyr) newval <- data.frame(DTM=1300) #predict.lm likes new values in a dataframe abn5y <- abn %>% group_by(Dates) %>% summarise(Y5=predict(lm(YTM ~ DTM), newval))

库(dplyr) newval <- data.frame(DTM=1300) # prediction。lm喜欢dataframe abn5y中的新值<- abn %>% % group_by(日期)%>% summary (Y5= forecast (lm(YTM ~ DTM), newval)))

This worked. However I loaded the next data set.

这工作。但是我加载了下一个数据集。

head(bmp))
   Dates   DTM   YTM
  <dttm> <dbl> <dbl>

1 2007-11-02 1498 4.782 2 2007-11-02 1892 4.883 3 2007-11-02 1300 4.934 4 2007-11-05 1497 4.768 5 2007-11-05 1891 4.880 6 2007-11-05 1299 4.924'

英语作文网为您收集英语作文网为您收集英语作文网为您收集英语作文网

And used the same code and got the following errors, with different attempts.

并使用了相同的代码,得到了以下错误,尝试不同。

bmp5y <- bmp %>% group_by(Dates) %>% + + summarise(Y5=predict(lm(YTM ~ DTM), newval)) Error in eval(predvars, data, env) : object 'YTM' not found

bmp5y <- bmp %>% group_by(日期)%>% + +概述(Y5=预测值(lm(YTM ~ DTM), newval))在eval(predvars, data, env)中的错误:object 'YTM'未找到。

bmp5y <- bmp %>% group_by(dates) %>% + summarise(Y5=predict(lm(ytm ~ dtm), newval)) Error in grouped_df_impl(data, unname(vars), drop) : Column dates is unknown

bmp5y <- bmp %>% group_by(日期)%>% +总结(Y5=预测值(lm(ytm ~ dtm), newval)),在grouped_df_impl(数据,unname(vars), drop):列日期未知。

bmp5y <- bmp %>% group_by(Dates) %>% + summarise(Y5=predict(lm(ytm ~ dtm), newval)) Error in summarise_impl(.data, dots) : Column Y5 must be length 1 (a summary value), not 6563 In addition: Warning message: 'newdata' had 1 row but variables found have 6563 rows

bmp5y <- bmp %>% group_by(日期)%>% + summary (Y5= prediction (lm(ytm ~ dtm), newval)汇总错误。数据,点):第Y5列的长度必须是1(总结值),而不是6563

What seems to be the problem?

有什么问题吗?

1 个解决方案

#1


2  

It is not clear from the question precisely what code and data is being used but to reconstruct it in a reproducible and verifiable manner, copy and paste the code below to a fresh R session -- it runs without any error messages for me:

这个问题并不清楚究竟使用了什么代码和数据,但为了以一种可复制和可验证的方式重构它,将下面的代码复制并粘贴到一个新的R会话中——它运行时没有任何错误消息:

Lines <- "
      Dates   DTM   YTM
1 2007-11-02 1498 4.782 
2 2007-11-02 1892 4.883 
3 2007-11-02 1300 4.934 
4 2007-11-05 1497 4.768 
5 2007-11-05 1891 4.880 
6 2007-11-05 1299 4.924"  
bmp <- read.table(text = Lines)

library(dplyr)
newval <- data.frame(DTM=1300)
bmp %>% group_by(Dates) %>% summarise(Y5=predict(lm(YTM ~ DTM), newval))

giving:

给:

# A tibble: 2 x 2
       Dates       Y5
      <fctr>    <dbl>
1 2007-11-02 4.876237
2 2007-11-05 4.863499

#1


2  

It is not clear from the question precisely what code and data is being used but to reconstruct it in a reproducible and verifiable manner, copy and paste the code below to a fresh R session -- it runs without any error messages for me:

这个问题并不清楚究竟使用了什么代码和数据,但为了以一种可复制和可验证的方式重构它,将下面的代码复制并粘贴到一个新的R会话中——它运行时没有任何错误消息:

Lines <- "
      Dates   DTM   YTM
1 2007-11-02 1498 4.782 
2 2007-11-02 1892 4.883 
3 2007-11-02 1300 4.934 
4 2007-11-05 1497 4.768 
5 2007-11-05 1891 4.880 
6 2007-11-05 1299 4.924"  
bmp <- read.table(text = Lines)

library(dplyr)
newval <- data.frame(DTM=1300)
bmp %>% group_by(Dates) %>% summarise(Y5=predict(lm(YTM ~ DTM), newval))

giving:

给:

# A tibble: 2 x 2
       Dates       Y5
      <fctr>    <dbl>
1 2007-11-02 4.876237
2 2007-11-05 4.863499