I have two data-sets. both are xts objects.
> dput(head(all_data[,2:3]))
structure(c(0.00108166576527857, 0.00324149108589955, 0, 0, 0.00484652665589658,
0.00267952840300101, 0.00606980273141122, 0.00301659125188536,
0.00526315789473686, -0.00149588631264019, 0, -0.00299625468164799
), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", "POSIXt"
), .indexTZ = "UTC", tclass = c("POSIXct", "POSIXt"), tzone = "UTC", index = structure(c(1453716060,
1453716120, 1453716180, 1453716240, 1453716300, 1453716360), tzone = "UTC", tclass = c("POSIXct",
"POSIXt")), .Dim = c(6L, 2L), .Dimnames = list(NULL, c("ClosePrice_AGL.1",
"ClosePrice_AMC")))
> dput(head(all_data[,1]))
structure(c(0.00108166576527857, 0.00324149108589955, 0, 0, 0.00484652665589658,
0.00267952840300101), class = c("xts", "zoo"), .indexCLASS = c("POSIXct",
"POSIXt"), .indexTZ = "UTC", tclass = c("POSIXct", "POSIXt"), tzone = "UTC", index = structure(c(1453716060,
1453716120, 1453716180, 1453716240, 1453716300, 1453716360), tzone = "UTC", tclass = c("POSIXct",
"POSIXt")), .Dim = c(6L, 1L), .Dimnames = list(NULL, "ClosePrice_AGL"))
> dput(head(mydata_train[,1:3]))
structure(c(-0.00155763239875384, -0.0279251170046803, -0.00225324987963404,
-0.000479333950998528, 0.0042195179257094, -0.00163456299477571,
-0.00526315789473697, -0.0222222222222221, -0.00431818181818178,
-0.00218475886131686, 0.00217864923747269, -0.00217391304347825,
-0.00651612903225807, -0.0221442950840964, -0.00385177314384377,
0.00333333333333319, -0.00365448504983379, -0.0160053351117039
), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", "POSIXt"
), tclass = c("POSIXct", "POSIXt"), tzone = "", index = structure(c(1527255180,
1527256080, 1527256260, 1527256440, 1527256800, 1527256980), tclass = c("POSIXct",
"POSIXt")), .Dim = c(6L, 3L), .Dimnames = list(NULL, c("ACBFF.Close",
"APHQF.Close", "WDDMF.Close")))
> dput(head(mydata_train[,4]))
structure(c(0.00429610046265694, -0.00789733464955589, -0.00165837479270303,
-0.00299003322259139, 0.00333222259246901, -0.00199269345732311
), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", "POSIXt"
), tclass = c("POSIXct", "POSIXt"), tzone = "", index = structure(c(1527255180,
1527256080, 1527256260, 1527256440, 1527256800, 1527256980), tclass = c("POSIXct",
"POSIXt")), .Dim = c(6L, 1L), .Dimnames = list(NULL, "MJ.Close"))
>
and i am running spIndexTrack
from:
library(sparseIndexTracking)
test <- spIndexTrack(all_data[,2:3] , all_data[,1], lambda = 1e-7, u = 0.5, measure = 'ete')
test <- spIndexTrack(mydata_train[,1:3] , mydata_train[,4], lambda = 1e-7, u = 0.5, measure = 'ete')
The second function gives:
w
ACBFF.Close 0.47083543
APHQF.Close 0.42967200
WDDMF.Close 0.09949257
the first:
Error in if (abs(a + 1) < 1e-06) { :
missing value where TRUE/FALSE needed
I have no NAs
all_data <- all_data[complete.cases(all_data),]
any(is.na(all_data) == TRUE)
and all my data is numeric.
storage.mode(my_data) <- "numeric"
I can run a regression through with no errors:
lm(all_data[,1] ~ all_data[,2:3])
str:
> str(head(all_data[,2:3]))
An ‘xts’ object on 2016-01-25 10:01:00/2016-01-25 10:06:00 containing:
Data: num [1:6, 1:2] 0.00108 0.00324 0 0 0.00485 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:2] "ClosePrice_AGL.1" "ClosePrice_AMC"
Indexed by objects of class: [POSIXct,POSIXt] TZ: UTC
xts Attributes:
NULL
> str(head(all_data[,1]))
An ‘xts’ object on 2016-01-25 10:01:00/2016-01-25 10:06:00 containing:
Data: num [1:6, 1] 0.00108 0.00324 0 0 0.00485 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr "ClosePrice_AGL"
Indexed by objects of class: [POSIXct,POSIXt] TZ: UTC
xts Attributes:
NULL
> str(head(mydata_train[,1:3]))
An ‘xts’ object on 2018-05-25 14:33:00/2018-05-25 15:03:00 containing:
Data: num [1:6, 1:3] -0.001558 -0.027925 -0.002253 -0.000479 0.00422 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:3] "ACBFF.Close" "APHQF.Close" "WDDMF.Close"
Indexed by objects of class: [POSIXct,POSIXt] TZ:
xts Attributes:
NULL
> str(head(mydata_train[,4]))
An ‘xts’ object on 2018-05-25 14:33:00/2018-05-25 15:03:00 containing:
Data: num [1:6, 1] 0.0043 -0.0079 -0.00166 -0.00299 0.00333 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr "MJ.Close"
Indexed by objects of class: [POSIXct,POSIXt] TZ:
xts Attributes:
NULL
If anyone want's to run the full working example with online google/yahoo data you can with:
library(sparseIndexTracking)
library(xts)
library(gquote)
library(PerformanceAnalytics)
#######################################
############ SET PARAMETERS #########
#######################################
# Data
minute_interval <- 3
n_periods <- 10000
#######################################
############ GET DATA #########
#######################################
# pull yahoo / google data for the portfolio (2 stocks)
mydata <- merge(getIntradayPrice('ACBFF', period=n_periods, interval = minute_interval),
getIntradayPrice('APHQF', period=n_periods, interval = minute_interval),
getIntradayPrice('WDDMF', period=n_periods, interval = minute_interval),
getIntradayPrice('MJ', period=n_periods, interval = minute_interval),
getIntradayPrice('HMLSF', period=n_periods, interval = minute_interval)
)
#select just closing prices
mydata <- mydata[,c(1,6, 11, 16)]
# remove NA values
mydata <- mydata[complete.cases(mydata),]
# replace all with returns of the two series - can use 'log' or 'discrete'
mydata <- Return.calculate(mydata, method = 'discrete')
# remove NA values again
mydata <- mydata[complete.cases(mydata),]
## split set into first 50% training data second 50% test data
mydata_train <- mydata[1:floor(nrow(mydata) * 0.5),]
mydata_test <- mydata[floor(nrow(mydata) * 0.5 +1):nrow(mydata),]
# remove NA values again
mydata_train <- mydata_train[complete.cases(mydata_train),]
# Generate weights see : https://cran.r-project.org/web/packages/sparseIndexTracking/vignettes/SparseIndexTracking-vignette.pdf
w_ete <- spIndexTrack(mydata_train[,1:3] , mydata_train[,4], lambda = 1e-7, u = 1.5, measure = 'ete')
w_ete
i'm stuck. if anyone can help. Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire