我正在研究R代码,以实现各种美国国债的收益率曲线。以下是我的代码:(代码末尾的问题陈述)
library(quantmod)
t3mo=getSymbols("DGS3MO",src="FRED",auto.assign = FALSE)
t6mo=getSymbols("DGS6MO",src="FRED",auto.assign = FALSE)
t1yr=getSymbols("DGS1",src="FRED",auto.assign = FALSE)
t2yr=getSymbols("DGS2",src="FRED",auto.assign = FALSE)
t3yr=getSymbols("DGS3",src="FRED",auto.assign = FALSE)
t5yr=getSymbols("DGS5",src="FRED",auto.assign = FALSE)
t7yr=getSymbols("DGS7",src="FRED",auto.assign = FALSE)
t10yr=getSymbols("DGS10",src="FRED",auto.assign = FALSE)
t20yr=getSymbols("DGS20",src="FRED",auto.assign = FALSE)
t30yr=getSymbols("DGS30",src="FRED",auto.assign = FALSE)
# Combine the yield data into one object
treasury=merge(t3mo ,t6mo ,t1yr ,t2yr ,t3yr ,t5yr ,t7yr ,t10y) ,t20yr ,t30yr)
treasury[c(1:3,nrow(treasury)),]
# subset to include the yield from 1990-2013
extreme=subset(treasury, index(treasury)>="1990-01-01" & index(treasury)<="2013-12-31")
extreme=extreme[,c(1,8,10)]
extreme=na.omit(extreme)
# Identify examples of different shapes of the yield curve
extreme$sign.diff=extreme$DGS30-extreme$DGS3MO
extreme$inverted=ifelse(extreme$sign.diff==min(extreme$sign.diff),1,0)
# inverted: 30 year yield<3 month yield
inverted=subset(extreme,extreme$inverted==1)
extreme$upward=ifelse(extreme$sign.diff==max(extreme$sign.diff),1,0)
# upward normal: 30 year yield>3 month yield
upward=subset(extreme,extreme$upward==1)
extreme$abs.diff=abs(extreme$DGS30 - extreme$DGS3MO)
extreme$flat=ifelse(extreme$abs.diff==min(extreme$abs.diff),1,0)
# maybe the flat yield curve or the humpback yield curve
flat=subset(extreme,extreme$flat==1)
# compare 30-year yield to 10-year yield to find
# the flattest curves within the set of flat curves above
flat$abs.diff2=abs(flat$DGS30-flat$DGS10)
flat$flat2=ifelse(flat$abs.diff2==min(flat$abs.diff2),1,0)
flat$flat2==1
# WHY DOES THIS NOT WORK, GIVEN "flat" IS THE SAME AS THE VARIABLE "extreme"???**
flat2=subset(flat,flat$flat2==1)
class(flat); class(extreme)
is.recursive(extreme)
is.recursive(flat)
问题在于:extreme
是一个xts和原子(不是递归的)。 flat
是extreme
的子集。 flat
也是一个xts和atomic(不是递归的)。我可以使用extreme
运算符访问flat
和$
中的列。但是,虽然我可以使用extreme
运算符对$
进行分组,但我无法使用flat
进行分组,而R会返回错误消息:
flat $ flat2中的错误:$运算符对原子向量无效
我不知道为什么R说明这个错误。 flat$flat2==1
独立工作,但在subset()
功能中站不起作用? R会做不同的治疗吗?
答案 0 :(得分:0)
subset
使用其参数的非标准评估。您在subset
参数中重新指定了对象名称。与flat
对象的区别在于flat
是对象的名称和对象中列的名称,这就是导致问题的原因。< / p>
这是一个说明问题的简单示例:
R> x <- xts(cbind(x=1, y=2), as.Date("2016-09-27"))
R> subset(x, x$x == 1)
Error in x$x : $ operator is invalid for atomic vectors
R> subset(x, x == 1)
x y
2016-09-27 1 2
此外,这里是一个更简洁的代码版本,可以避免subset
一起使用:
library(quantmod)
e <- new.env()
symbols <- c("DGS3MO","DGS6MO","DGS1","DGS2","DGS3",
"DGS5","DGS7","DGS10","DGS20","DGS30")
getSymbols(symbols, src="FRED", env=e)
# Combine the yield data into one object
treasury <- do.call(merge, as.list(e))[,symbols]
# subset to include the yield from 1990-2013
extreme <- na.omit(treasury["1990/2013"])
# Identify examples of different shapes of the yield curve
extreme$sign.diff <- extreme$DGS30 - extreme$DGS3MO
extreme$inverted <- extreme$sign.diff == min(extreme$sign.diff)
# inverted: 30 year yield<3 month yield
inverted <- extreme[extreme$inverted == 1,]
extreme$upward <- extreme$sign.diff == max(extreme$sign.diff)
# upward normal: 30 year yield>3 month yield
upward <- extreme[extreme$upward == 1,]
extreme$abs.diff<- abs(extreme$DGS30 - extreme$DGS3MO)
extreme$flat <- extreme$abs.diff == min(extreme$abs.diff)
# maybe the flat yield curve or the humpback yield curve
flat <- extreme[extreme$flat == 1,]
# compare 30-year yield to 10-year yield to find
# the flattest curves within the set of flat curves above
flat$abs.diff2 <- abs(flat$DGS30 - flat$DGS10)
flat$flat2 <- flat$abs.diff2 == min(flat$abs.diff2)
flat2 <- flat[flat$flat2 == 1,]