Data di fine settimana errata utilizzando la funzione "a.-weekly" nel pacchetto "XTS"
Domanda
Ho un problema davvero strano ... sto usando il to.weekly
e to.period
funzione per convertire un giorno xts
obiettare ai dati settimanali. Nella maggior parte dei casi, ottengo la data di fine settimana come venerdì (day.of.week
La funzione tornerà 5) (ad esempio "2010-01-08"
, "2011-02-11"
), ma ci sono alcuni casi in cui ottengo qualcosa di diverso dal venerdì (sabato/domenica/giovedì/ecc.)
Ho provato to.weekly
e to.period(x, period = 'weeks')
ed entrambi restituiscono lo stesso problema.
Perché sta succedendo? C'è un lavoro per questo ??
Grazie!!
Modifica: esempio di seguito
test.dates <- as.Date(c("2010-04-27","2010-04-28","2010-04-29","2010-04-30","2010-05-03","2010-05-04","2010-05-05","2010-05-06","2010-05-07","2010-05-10","2010-05-11","2010-05-12","2010-05-13","2010-05-14","2010-05-17","2010-05-18","2010-05-19","2010-05-20","2010-05-21","2010-05-22","2010-05-24","2010-05-25","2010-05-26","2010-05-27","2010-05-28","2010-06-01","2010-06-02","2010-06-03","2010-06-04"))
test.data <- rnorm(length(test.dates),mean=1,sd=2)
test.xts <- xts(x=test.data,order.by=test.dates)
#Function that takes in a vector of zoo/xts objects (e.g. "2010-01-08") and returns the day of the week for each
dayofweek <- function(x) {
placeholder <- vector("list",length=length(x))
names(placeholder) <- x
for(i in 1:length(x)) {placeholder[[i]] <- month.day.year(x[i])}
placeholder2 <- rep(NA,times=length(x))
for(i in 1:length(x)) {placeholder2[i] <- day.of.week(placeholder[[i]][[1]],placeholder[[i]][[2]],placeholder[[i]][[3]])}
return(placeholder2)}
Questo restituisce le data che non sono venerdì: time(to.weekly(test.xts))[dayofweek(time(to.weekly(test.xts))) != 5]
Soluzione
Hai 2 problemi con il tuo esempio:
- Tuo
dayofweek
La funzione è un po 'ingombrante e probabilmente errata nei suoi risultati. - Le tue date di esempio mancano alcune date, come 05-23-2010.
Ecco una versione pulita del tuo codice:
library(xts)
test.dates <- as.Date(c("2010-04-27","2010-04-28","2010-04-29","2010-04-30","2010-05-03","2010-05-04","2010-05-05","2010-05-06","2010-05-07","2010-05-10","2010-05-11","2010-05-12","2010-05-13","2010-05-14","2010-05-17","2010-05-18","2010-05-19","2010-05-20","2010-05-21","2010-05-22","2010-05-24","2010-05-25","2010-05-26","2010-05-27","2010-05-28","2010-06-01","2010-06-02","2010-06-03","2010-06-04"))
test.data <- rnorm(length(test.dates),mean=1,sd=2)
test.xts <- xts(x=test.data,order.by=test.dates)
test.weekly <- to.weekly(test.xts)
library(lubridate)
test.weekly[wday(test.weekly, label = TRUE, abbr = TRUE) != "Fri"]
L'unico risultato di questa funzione è
test.xts.Open test.xts.High test.xts.Low test.xts.Close
2010-05-22 -1.705749 1.273982 -2.084203 -1.502611
Il problema ovviamente è che questa settimana termina 05-23-2010
, ma quella data non è presente nelle serie temporali. Perciò, to.weekly
usa la data più vicina al punto finale, che è 05-22-2010
. Questa è la fonte del tuo problema.
Ecco un esempio migliore, che non rivela alcun problema con il to.weekly
funzione.
library(lubridate); library(xts)
test.dates <- seq(as.Date("1900-01-01"),as.Date("2011-10-01"),by='days')
test.dates <- test.dates[wday(test.dates)!=1 & wday(test.dates)!=7] #Remove weekends
test.data <- rnorm(length(test.dates),mean=1,sd=2)
test.xts <- xts(x=test.data,order.by=test.dates)
test.weekly <- to.weekly(test.xts)
test.weekly[wday(test.weekly, label = TRUE, abbr = TRUE) != "Fri"]