zoo

Why is there no apply.hourly in R with xts/zoo?

走远了吗. 提交于 2019-11-30 05:25:11
I want to aggregate data by hourly mean. Daily is very easy: apply.daily(X2,mean) Why is there no function for hourly? I tried hr.means <- aggregate(X2, format(X2["timestamp"],"%Y-%m-%d %H")) and got always error with trim argument. Is there an easier function similar to apply.daily? What if I want to aggregate the mean of 5 minutes. Data are values per minute: "timestamp", value "2012-04-09 05:03:00",2 "2012-04-09 05:04:00",4 "2012-04-09 05:05:00",5 "2012-04-09 05:06:00",0 "2012-04-09 05:07:00",0 "2012-04-09 05:08:00",3 "2012-04-09 05:09:00",0 "2012-04-09 05:10:00",1 I am using xts and zoo.

R: Filling missing dates in a time series?

安稳与你 提交于 2019-11-30 04:51:06
I have a zoo time series with missing days. In order to fill it and have a continuous series I do... I generate a chron date-time sequence from start to end. I merge my series with this one. I use na.locf to substitute NAs with las obsservation. I remove the syntetic chron sequence. Can I do same easier? Maybe with some index function related to the frequency? It's slightly easier if you use a "empty" zoo object with an index. > x <- zoo(1:10,Sys.Date()-10:1)[c(1,3,5,7,10)] > empty <- zoo(order.by=seq.Date(head(index(x),1),tail(index(x),1),by="days")) > na.locf(merge(x,empty)) 2010-08-14 2010

How to analyse irregular time-series in R

这一生的挚爱 提交于 2019-11-30 00:38:06
I have a zoo time series in R: d <- structure(c(50912, 50912, 50912, 50912, 50913, 50913, 50914, 50914, 50914, 50915, 50915, 50915, 50916, 50916, 50916, 50917, 50917, 50917, 50918, 50918, 2293.8, 2302.64, 2310.5, 2324.02, 2312.25, 2323.93, 2323.83, 2338.67, 2323.1, 2320.77, 2329.73, 2319.63, 2330.86, 2323.38, 2322.92, 2317.71, 2322.76, 2286.64, 2294.83, 2305.06, 55.9, 62.8, 66.4, 71.9, 59.8, 65.7, 61.9, 67.9, 38.5, 36.7, 43.2, 30.3, 42.4, 33.5, 48.8, 52.7, 61.2, 30, 41.7, 50, 8.6, 9.7, 10.3, 11.1, 9.2, 10.1, 9.6, 10.4, 5.9, 5.6, 6.6, 4.7, 6.5, 5.2, 7.5, 8.1, 9.5, 4.6, 6.4, 7.7, 9

Dividing each row of an xts or zoo time series object by a fixed row

心已入冬 提交于 2019-11-29 23:55:23
问题 I am trying to divide an xts object which holds a number of time series (columns; with a common date column (index). I want to divide each column by its value at a specified date (say '2010-09-30'). This is so as to re-scale the entire object with values of 1 in each column at that date (a common re-basing task). Had it been an ordinary matrix, A , and the row I wanted to rebase to was say A[6,] , I could just do t(t(A)/A[6,]) and that works. But, trying to manipulate the xts object and its

Compute a rolling weighted sum by group

[亡魂溺海] 提交于 2019-11-29 18:10:01
I have a working example of a weighted sum of transactions over the last 12 hours. Now, I've added an account column and would like to compute this weighted sum separately by group. The code will run as written below. Uncomment the line starting with # account to add the account column to df . How can I modify the second to last line of code such that it computes rollapplyr separately on each account ? library(zoo) library(tidyverse) Create the example data: set.seed(123) randomDates <- function(N, st="2017-01-01 00:00:00", et="2017-02-01 23:59:59") { st <- as.POSIXct(st, tz = "UTC") et <- as

sum over past window-size dates per group

▼魔方 西西 提交于 2019-11-29 15:41:13
The problem is similar to How do I do a conditional sum which only looks between certain date criteria but slightly different and the answer from that does not fit into current problem. The main difference is that the date column based on each group may not necessarily be complete (i.e., certain date may be missing) Input: input <- read.table(text=" 2017-04-01 A 1 2017-04-02 B 2 2017-04-02 B 2 2017-04-02 C 2 2017-04-02 A 2 2017-04-03 C 3 2017-04-04 A 4 2017-04-05 B 5 2017-04-06 C 6 2017-04-07 A 7 2017-04-08 B 8 2017-04-09 C 9") colnames(input) <- c("Date","Group","Score") Rule: for each group

Does rollapply() allow an array of results from call to function?

偶尔善良 提交于 2019-11-29 15:27:17
# Loading packages require(forecast) require(quantmod) # Loading OHLC xts object getSymbols('SPY', from = '1950-01-01') # Selecting weekly Close prices x <- Cl(to.weekly(SPY)) # ARIMA(p,d,q) estimation and forecasting function a.ari.fun <- function(x) { a.ari <- auto.arima(x = x, d = 1, max.p = 50, max.q = 50, max.P = 50, max.Q = 50, ic = 'aic', approximation = TRUE) fore <- forecast.Arima(object = a.ari, h = 4, level = c(.9)) supp <- tail(fore$lower, 1) rest <- tail(fore$upper, 1) return(c(supp, rest)) } # Roll apply ARIMA(p,d,q) in rolling window rollapplyr(data = tail(x, 800), width = 750,

Calculate rolling correlation using rollapply

╄→尐↘猪︶ㄣ 提交于 2019-11-29 13:13:38
问题 I have zoo object with 10000+ rows. > head(tt) A B 2007-01-04 0.005945924 0.0021167475 2007-01-05 -0.004201991 -0.0080020024 2007-01-08 0.001740897 0.0045804104 2007-01-09 0.000000000 -0.0008163931 2007-01-10 -0.004503531 0.0032615812 2007-01-11 -0.005841138 0.0043863282 I have tried variations of the following line, but to no avail. rollapply(tt, 21, function(x) cor(x[,1],x[,2])) Every entry gave correlation of 1, looks like it's picking up the 1 off the diagonal of the correlation matrix.

Zookeeper伪分布式配置

北慕城南 提交于 2019-11-29 12:15:07
解压文件 tar -zxvf zookeeper-3.4.10.tar.gz mv zookeeper-3.4.10 /opt/zookeeper/zookeeper3.4 修改配置文件 cd /opt/zookeeper/zookeeper3.4/ cp conf/zoo_sample.cfg conf/zoo_1.cfg vi conf/zoo_1.cfg dataDir=/opt/zookeeper/zookeeper3.4/data/zk1 clientPort=2181 server.1=localhost:2888:3888 server.2=localhost:2889:3889 server.3=localhost:2890:3890 cat conf/zoo_1.cfg > conf/zoo_2.cfg vi conf/zoo_2.cfg dataDir=/opt/zookeeper/zookeeper3.4/data/zk2 clientPort=2182 server.1=localhost:2888:3888 server.2=localhost:2889:3889 server.3=localhost:2890:3890 cat conf/zoo_1.cfg > conf/zoo_3.cfg vi conf/zoo_3.cfg dataDir=/opt

Rolling regression xts object in R

你说的曾经没有我的故事 提交于 2019-11-29 12:10:01
I am attempting to perform a rolling 100 day regression on an xts object and return the t statistic of the slope coefficient for all dates. I have an xts object, prices: > tail(prices) DBC EEM EFA GLD HYG IEF IWM IYR MDY TLT 2012-11-02 27.14 41.60 53.69 162.60 92.41 107.62 81.19 64.50 179.99 122.26 2012-11-05 27.37 41.80 53.56 163.23 92.26 107.88 81.73 64.02 181.10 122.95 2012-11-06 27.86 42.13 54.07 166.30 92.40 107.39 82.34 64.16 182.69 121.79 2012-11-07 27.34 41.44 53.26 166.49 91.85 108.29 80.34 63.84 178.90 124.00 2012-11-08 27.38 40.92 52.78 167.99 91.55 108.77 79.21 63.19 176.37 125.84