this =
structure(c(-0.012, -0.028, -0.044, -0.033, -0.039, -0.042), .Dim = c(3L, 2L),
.Dimnames = list(NULL, c("one", "two")), index = structure(
c(1313643600, 1313730000, 1313816400), tzone = "", tclass = "Date"),
.indexCLASS = "Date", .indexTZ = "", class = c("xts", "zoo"))
m1=last(this$one) - last(this$two)
m2=first(last(this$one,n=2)) - first(last(this$two,n=2))
m1 > 0 #returns a TRUE OR FALSE
m1 > m2 #breaks
Run Code Online (Sandbox Code Playgroud)
我知道我可以coredata用来提取然后比较.我不确定这是不是错误.比较工作似乎不一致,甚至数学运算符在xts对象上工作得很好但是将xts与另一个xts进行比较失败.
我有两个 data.frames,我想将它们附加在一起。一个 data.frame 保存过去的数据,另一个 data.frame 保存未来的预测数据。例如:
past <- structure(list(Period = c("2010-01-01", "2010-02-01", "2010-03-01",
"2010-04-01", "2010-05-01", "2010-06-01"), Count = c(3379221317,
3379221317, 3791458246, 4182424930, 4891432401, 5116360744)),
.Names = c("Period", "Count"), row.names = c(NA, 6L), class = "data.frame")
future <- structure(list(Period = structure(c(15522, 15553, 15584, 15614,
15645, 15675), class = "Date"), Count = c(11051746713.9419, 11385578654.4160,
10864084232.2216, 11336164255.3271, 11121729107.9691, 12321341701.3780)),
.Names = c("Period", "Count"), row.names = c(NA, 6L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
我使用过rbind(past,future),由于某种原因, data.framePeriod中的条目future全部被重命名。看起来像:
> rbind(past,future)
Period Count
1 …Run Code Online (Sandbox Code Playgroud) 我想从使用getSymbols获得的时间序列中提取日期,但是当我使用index/index.xts函数时,返回的日期似乎是提前一天.我无法理解为什么在以下代码中发生此行为.
但是,预期的行为是获取与原始时间序列中的对象相对应的Date对象列表.
以下是代码,请注意时间序列SPY的最后日期是2012年8月24日,但索引(SPY)调用的最后一个值是2012年8月23日:
getSymbols("SPY")
tail(SPY)
SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
2012-08-17 142.23 142.30 141.86 142.18 90813700 142.18
2012-08-20 141.98 142.22 141.59 142.19 78255700 142.19
2012-08-21 142.54 143.09 141.45 141.76 105581100 141.76
2012-08-22 141.40 142.05 141.07 141.82 132999200 141.82
2012-08-23 141.47 141.48 140.44 140.66 111406800 140.66
2012-08-24 140.31 141.83 140.22 141.51 99431500 141.51
tail(index(SPY))
[1] "2012-08-16" "2012-08-19" "2012-08-20" "2012-08-21" "2012-08-22" "2012-08-23"
tail(index.xts(SPY))
[1] "2012-08-16" "2012-08-19" "2012-08-20" "2012-08-21" "2012-08-22" "2012-08-23"
Run Code Online (Sandbox Code Playgroud)
感谢所有能回复我帖子的人.
有关会话的其他信息
>sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: i386-pc-mingw32/i386 (32-bit)
locale: …Run Code Online (Sandbox Code Playgroud) 我正在尝试为许多资产计算一个滚动窗口(移动1天)协方差矩阵.
说我的df看起来像这样:
df <- data.frame(x = c(1.5,2.3,4.7,3,8.4), y =c(5.3,2.4,8.4,1.3,2.5),z=c(2.5,1.3,6.5,4.3,2.8),u=c(1.1,2.5,4.3,2.5,6.3))
Run Code Online (Sandbox Code Playgroud)
我希望输出看起来如下:
cov(df[1:3,]) :
x y z u
x 2.773333 3.666667 4.053333 2.613333
y 3.666667 9.003333 7.846667 2.776667
z 4.053333 7.846667 7.413333 3.413333
u 2.613333 2.776667 3.413333 2.573333
cov(df[2:4,]) :
x y z u
x 1.523333 4.283333 3.053333 1.23
y 4.283333 14.603333 7.253333 3.93
z 3.053333 7.253333 6.813333 2.22
u 1.230000 3.930000 2.220000 1.08
cov(df[3:5,]) :
x y z u
x 7.6233333 -0.5466667 -3.008333 5.1633333
y -0.5466667 14.4433333 5.941667 0.9233333
z -3.0083333 …Run Code Online (Sandbox Code Playgroud) 在SAS中有一种创建库的方法(使用LIBNAME).这很有用,因为当我们必须进行长数据处理时,我们不会始终更改数据集名称.因此,如果我们想要再次使用数据集而不更改名称,我们可以放入库中.因此,即使数据集名称相同,但由于它们位于不同的库中,我们可以一起处理它们.
我的问题是R中是否有任何可以创建库(或R中的单独文件夹)的选项,以便我们可以在那里保存数据?
这是一个例子:
假设我有一个数据集"dat1".我总结了var1中dat1 var1和var2中的变量.
proc summary data=dat1 nway missing;
var var1 var2;
class var3;
output out=tmp.dat1 (drop = _freq_ _type_) sum = ;
run;
Run Code Online (Sandbox Code Playgroud)
然后我将dat1与dat2合并,这是另一个数据集.dat1和dat2都有公共变量var3,我合并了它.我再次创建了新的数据集dat1.
proc sql;
create table dat1 as
select a.*,b.*
from dat1 a left join tmp.dat2 b
on a.var3=b.var3;
quit;
Run Code Online (Sandbox Code Playgroud)
现在,我再次在合并后总结数据集dat1,以检查var1和var 2的值在合并之前和之后是否保持不变.
proc summary data=dat1 nway missing;
var var1 var2;
class var3;
output out=tmp1.dat1 (drop = _freq_ _type_) sum = ;
run;
Run Code Online (Sandbox Code Playgroud)
R中的等效代码将是
dat3<-ddply(dat1,.(var3),summarise,var1=sum(var1,na.rm=TRUE),var2=sum(var2,na.rm=TRUE))
dat1<-sqldf("select a.*,b.* from dat1 a left join dat2 b on a.var3=b.var3")
dat4<-ddply(dat1,.(var3),summarise,var1=sum(var1,na.rm=TRUE),var2=sum(var2,na.rm=TRUE)) …Run Code Online (Sandbox Code Playgroud) 一个简单的问题:我知道如何xts从帮助中分配时间序列的年份,月份和日期:x['2000-05/2001']依此类推.
但是,如何在一天中的几个小时内对我的数据进行子集化?我想在07:00 am到06:00 pm之间获取所有数据.即,我想在营业时间内提取数据 - 与当天无关(我稍后会照顾周末).帮助有一个表单示例:
.parseISO8601('T08:30/T15:00')
Run Code Online (Sandbox Code Playgroud)
但这在我的情况下不起作用.有人有线索吗?
这是输出:
library(tseries) # for adf.test function
adf.test(data)
Augmented Dickey-Fuller Test
data: data
Dickey-Fuller = 11.1451, Lag order = 16, p-value = 0.99
alternative hypothesis: stationary
Warning message:
In adf.test(spread.princomp) : p-value greater than printed p-value
adf.test(coredata(data))
Augmented Dickey-Fuller Test
data: coredata(data)
Dickey-Fuller = -4.031, Lag order = 16, p-value = 0.01
alternative hypothesis: stationary
Warning message:
In adf.test(coredata(spread.princomp)) :
p-value smaller than printed p-value
Run Code Online (Sandbox Code Playgroud)
基础数据是数字向量.人们似乎成功地使用xts应用adf.test,所以我不确定我做错了什么.请告诉我我能提供的其他信息.
我想用R公司的颜色做一个图表.这意味着所有图表的背景应为浅蓝色,但绘图区域应为白色.我正在寻找答案,并发现绘制一个矩形完成工作(差不多).然而,绘图区域现在是白色的,图形不再可见.这甚至可能吗?
getSymbols('SPY', from='1998-01-01', to='2011-07-31', adjust=T)
GRAPH_BLUE<-rgb(43/255, 71/255,153/255)
GRAPH_ORANGE<-rgb(243/255, 112/255, 33/255)
GRAPH_BACKGROUND<-rgb(180/255, 226/255, 244/255)
par(bg=GRAPH_BACKGROUND)
colorPlottingBackground<-function(PlottingBackgroundColor = "white"){
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col ="white")
}
plot.xts(SPY, col=GRAPH_BLUE)
colorPlottingBackground()
Run Code Online (Sandbox Code Playgroud) 我在使用时不断收到此错误:ES calculation produces unreliable result (inverse risk) for column: 1消息DEoptim。也许我忽略了一些事情,所以我需要一些帮助来解决这个问题。我在网上搜索过但似乎找不到答案。
我有一个xts名为 的对象RETS,包含 127 行和 4 列,其中有日志返回:
library("quantmod")
library("PerformanceAnalytics")
library("DEoptim")
e <- new.env()
getSymbols("SPY;QCOR;CLNT;SRNE", from="2007-06-30", to="2007-12-31", env=e)
# combine the adjusted close values in one xts object
dataset1 <- do.call(merge, eapply(e, Ad))
# calculate returns
RETS <- na.omit(CalculateReturns(dataset1, method="log"))
# objective function
optRR.gt3 <- function(x, ret) {
retu <- ret %*% x
obj <- -CVaR(as.ts(-retu))/CVaR(as.ts(retu))
obj <- ifelse(obj>0,-obj,obj)
weight.penalty <- 100*(1-sum(x))^2
small.weight.penalty <- …Run Code Online (Sandbox Code Playgroud)