我开始对成对股票(对交易)进行一些分析,这是我为生成图表而编写的函数(pairs.report - 下面列出).
我需要在一个图中绘制三条不同的线.我列出的功能完成了我想要的功能,但如果我想在x轴(时间线)中进行精细定制,则需要一些工作.实际上,它只打印x轴上的年份(10年的数据)或月份(6个月的数据),没有刻度的格式.
如果我使用xts对象,即如果我使用
plot(xts-object-with-date-asset1-asset2, ...)
Run Code Online (Sandbox Code Playgroud)
代替
plot(date, asset2, ...)
Run Code Online (Sandbox Code Playgroud)
我得到一个格式很好的x轴(连同网格和方框),但随后使用点(),text(),lines()等函数添加到图中失败.我想points.xts()和text.xts()不会很快出来.
我想要xts对象的便利,但我还需要对我的情节进行精细控制.那么我的工作流程应该是什么样的呢?我是否必须坚持使用基本图形并手动完成所有自定义操作?或者有什么方法可以让xts为我工作?
我知道格子和ggplot2,但我现在不想使用它们.这是我提到的功能(欢迎任何批评/改进代码的建议) -
library(xts)
pairs.report <- function(asset1, asset2, dataset) {
#create data structures
attach(dataset)
datasetlm <- lm(formula = asset1 ~ asset2 + 0, data = dataset)
beta = coef(datasetlm)[1]
#add extra space to right margin of plot within frame
par(mar=c(5, 4, 4, 4) + 0.1)
# Plot first set of data and draw its axis
ylim <- c(min(asset2,asset1), max(asset2,asset1))
plot(date,
asset2,
axes=T,
ylim=ylim,
xlab="Timeline",
ylab="asset2 and asset1 equity", …Run Code Online (Sandbox Code Playgroud) 我有以下格式的xts
a b c d e f ......
2011-01-03 11.40 NA 23.12 0.23 123.11 NA ......
2011-01-04 11.49 NA 23.15 1.11 111.11 NA ......
2011-01-05 NA NA 23.11 1.23 142.32 NA ......
2011-01-06 11.64 NA 39.01 NA 124.21 NA ......
2011-01-07 13.84 NA 12.12 1.53 152.12 NA ......
Run Code Online (Sandbox Code Playgroud)
是否有一个函数可以应用于生成新的xts或data.frame缺少仅包含NA的列?
带有NA的列的位置不是静态的,因此无法按名称或位置删除这些列
我正在尝试运行auto.arima一些xts数据,但收到以下错误:
library(quantmod)
library(forecast)
getSymbols('^GSPC',from='2000-01-01')
auto.arima(GSPC$GSPC.Close)
Error in dimnames(cd) <- list(as.character(index(x)), colnames(x)) :
'dimnames' applied to non-array
Run Code Online (Sandbox Code Playgroud)
我发现如果我
close <- as.ts(GSPC$GSPC.Close)
Run Code Online (Sandbox Code Playgroud)
然后auto.arima不会返回错误.但后来我丢失了与该xts对象相关的日期信息.有没有办法将数据保持为xts仍然运行该功能?
我注意到,例如acf(GSPC$GPSC.Close),pacf()不要给出错误.
我有一个带有少量事件指标的xts对象.我需要通过某些事件将其拆分,以便从给定事件到下一个事件的所有条目都将保存在同一个事件中xts,最终创建一个xts对象列表,每个对象都包含事件作为最后一个条目而没有其他事件相同的类型.
一个例子:
ts = as.Date(Sys.Date()-99:0)
e1 = numeric(100);e1[10*1:10]=1
e2 = numeric(100);e2[15*1:6]=1
y = 1:100 # just a sample content
xs = as.xts(cbind(e1,e2,y),order.by=ts)
ee = e1*e2==1 # the event in which both e1 and e2 are 1, should happen at 30,60,90
# here should be splitting function that gets xs and ee as parameters
# and should return a list of 4 xts: the first with the entries 1 through 30,
# the second with entries 31 …Run Code Online (Sandbox Code Playgroud) 我很确定这很简单,但似乎卡住了...我有两个xts向量已合并在一起,其中包含数值和NA.
我想获得每个索引周期的rowSums,但保持NA值.
以下是可重复的示例
set.seed(120)
dd <- xts(rnorm(100),Sys.Date()-c(100:1))
dd1 <- ifelse(dd<(-0.5),dd*-1,NA)
dd2 <- ifelse((dd^2)>0.5,dd,NA)
mm <- merge(dd1,dd2)
mm$m <- rowSums(mm,na.rm=TRUE)
tail(mm,10)
dd1 dd2 m
2013-08-02 NA NA 0.000000
2013-08-03 NA NA 0.000000
2013-08-04 NA NA 0.000000
2013-08-05 1.2542692 -1.2542692 0.000000
2013-08-06 NA 1.3325804 1.332580
2013-08-07 NA 0.7726740 0.772674
2013-08-08 0.8158402 -0.8158402 0.000000
2013-08-09 NA 1.2292919 1.229292
2013-08-10 NA NA 0.000000
2013-08-11 NA 0.9334900 0.933490
Run Code Online (Sandbox Code Playgroud)
在2013年8月10日的上述例子中,我希望它能NA代替0,而2013年8月2日至4日也是如此.
有关在相关场所获得NAs的优雅方式的任何建议吗?
我曾经getSymbols获得股票数据,它返回的内容如下:
> require(quantmod)
> getSymbols(AAPL)
> head(AAPL)
AAPL.Open AAPL.High AAPL.Low AAPL.Close
2007-01-03 86.29 86.58 81.90 83.80
2007-01-04 84.05 85.95 83.82 85.66
2007-01-05 85.77 86.20 84.40 85.05
2007-01-08 85.96 86.53 85.28 85.47
2007-01-09 86.45 92.98 85.15 92.57
2007-01-10 94.75 97.80 93.45 97.00
> str(AAPL)
An ‘xts’ object on 2007-01-03/2015-02-23 containing:
Data: num [1:2049, 1:6] 86.3 84 85.8 86 86.5 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:6] "AAPL.Open" "AAPL.High" "AAPL.Low" "AAPL.Close" ...
Indexed by …Run Code Online (Sandbox Code Playgroud) 我在xts对象中有多个列,我想在第一列中找到一定数量以上的百分比,在第一列或第二列中高于某个数字的百分比,前三列中任何一列中的百分比超过一定数量等
我目前正在手动执行此操作,如下所示:
library(xts)
set.seed(69)
x = xts( cbind( v.1 = runif(20)*100, v.2 = runif(20)*100, v.3 = runif(20)*100, v.4 = runif(20)*100), Sys.Date()-20:1 )
c(
mean( x$v.1 > 50),
mean( x$v.1 > 50 | x$v.2 > 50) ,
mean( x$v.1 > 50 | x$v.2 > 50 | x$v.3 > 50) ,
mean( x$v.1 > 50 | x$v.2 > 50 | x$v.3 > 50 | x$v.4 > 50)
)
Run Code Online (Sandbox Code Playgroud)
这给出了这个示例输出:
[1] 0.50 0.70 0.80 0.95
Run Code Online (Sandbox Code Playgroud)
但现在我要推广到任意数量的列,而不仅仅是v.1对v.4.所以我正在寻找一个像这样的单一函数:
this_is_mean( x, c('v.1','v.2','v.3','v.4'), …Run Code Online (Sandbox Code Playgroud) 我创建了一个虚拟时间序列xts对象,其日期为2-09-2015,缺少数据:
library(xts)
library(ggplot2)
library(scales)
set.seed(123)
seq <- seq(as.POSIXct("2015-09-01"),as.POSIXct("2015-09-02"), by = "1 hour")
ob1 <- xts(rnorm(length(seq),150,5),seq)
seq2 <- seq(as.POSIXct("2015-09-03"),as.POSIXct("2015-09-05"), by = "1 hour")
ob2 <- xts(rnorm(length(seq2),170,5),seq2)
final_ob <- rbind(ob1,ob2)
plot(final_ob)
# with ggplot
df <- data.frame(time = index(final_ob), val = coredata(final_ob) )
ggplot(df, aes(time, val)) + geom_line()+ scale_x_datetime(labels = date_format("%Y-%m-%d"))
Run Code Online (Sandbox Code Playgroud)
红色矩形部分表示缺少数据的日期.如何在主图中显示当天缺少数据?
我想我应该用不同的颜色显示这个缺失的数据.但是,我不知道如何处理数据以反映主图中缺少的数据行为.
我有一个包含大量缺失数据点的月度数据的时间序列,设置为NA.我想简单地从数据中减去年度周期,忽略丢失的条目.似乎分解函数无法处理丢失的数据点,但我在其他地方看到过建议使用季节性包.但是我也遇到了NA的问题.
以下是使用内置数据集的问题的最小可重现示例...
library(seasonal)
# set range to missing NA in Co2 dataset
c2<-co2
c2[c2>330 & c2<350]=NA
seas(c2,na.action=na.omit)
Error in na.omit.ts(x) : time series contains internal NAs
Run Code Online (Sandbox Code Playgroud)
是的我知道!这就是为什么我要你省略它们!我们试试这个:
seas(c2,na.action=na.x13)
Error: X-13 run failed
Errors:
- Adding MV1981.Apr exceeds the number of regression effects
allowed in the model (80).
Run Code Online (Sandbox Code Playgroud)
嗯,有趣,不知道这意味着什么,好吧,请排除NA:
seas(c2,na.action=na.exclude)
Error in na.omit.ts(x) : time series contains internal NAs
Run Code Online (Sandbox Code Playgroud)
这没多大帮助!并且好的措施
decompose(c2)
Error in na.omit.ts(x) : time series contains internal NAs
Run Code Online (Sandbox Code Playgroud)
我在以下内容:
R version 3.4.4 (2018-03-15) -- "Someone to Lean On"
Copyright …Run Code Online (Sandbox Code Playgroud) 我有以下格式的 xts df:
structure(c("May 2022", "Jun 2022", "Jul 2022", "Aug 2022", "Sep 2022",
"Oct 2022", "Nov 2022", "Dec 2022", " 3035.199", " 5500.000",
"11568.750", " 2510.000", " 6999.999", "21792.149", " 9750.000",
" 5624.999", " 2250.000", " 4136.975", " 6525.500", " 2771.875",
" 4637.500", "16273.499", " 6000.000", " 4494.649", " 2500.000",
" 0.000", " 3029.000", " 2803.500", " 0.000", "14481.250",
" 4374.998", " 4062.498", " 0.000", " 3075.000", " 6939.249",
" 1500.000", " 4183.157", " 5769.000", " 3559.500", " 3250.000"
), …Run Code Online (Sandbox Code Playgroud)