Related
This is my code, it works so far, except that the legend does not appear in the plot:
plot(norm_prices_daily[,1], col = "blue",lwd=3,grid.col = NA, ylim = c(0,3))
lines(norm_prices_daily[,2],lwd=3, col = "dark green")
legend("bottomright",legend=c("Stock 1","Stock 2"),col = c("blue","dark green"))
norm_prices_daily is a zoo object with two columns and 182 rows.
Here is part of the data. Output from dput(head(norm_prices_daily[,1],20)):
structure(c(1, 0.9929401294387, 0.99644855697716, 0.9936552740678,
0.998526620668352, 1.00517207744161, 1.00230213170477, 1.00929440707496,
1.00776579745506, 1.0096505179963, 1.01809781273736, 1.02202987149226,
1.01931947115634, 1.01961413204347, 1.0207774879027, 1.01154745364837,
0.99563509229331, 1.0056447937894, 1.00477302623885, 1.00792234548049
), class = c("xts", "zoo"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", src = "yahoo", updated = structure(1600533381.35332, class = c("POSIXct",
"POSIXt")), index = structure(c(1577923200, 1578009600, 1578268800,
1578355200, 1578441600, 1578528000, 1578614400, 1578873600, 1578960000,
1579046400, 1579132800, 1579219200, 1579564800, 1579651200, 1579737600,
1579824000, 1580083200, 1580169600, 1580256000, 1580342400), tzone = "UTC", tclass = "Date"), .Dim = c(20L,
1L), .Dimnames = list(NULL, "^GSPC"))
I have tried different options for the position of the legend, such as specific x and y coordinates, because I suspected that the legend is there but just too low or something. But that does not seem to the issue.
You haven't specified the kind of line you want in the legend, but it should still print the labels. Here is a slight modification of the first example on the manual page for print.zoo to make it more comparable to yours:
x.Date <- as.Date(paste(2003, 02, c(1, 3, 7, 9, 14), sep = "-"))
set.seed(42)
x <- zoo(cbind(rnorm(5), rnorm(5, sd= 0.2)), x.Date)
plot(x[, 1], col = "blue")
lines(x[, 2], col = "dark green")
legend("bottomright", legend=c("Stock 1","Stock 2"), lty=1, col = c("blue","dark green"))
The following script plots 2 charts side by side:
require(xts)
par(mfrow=c(1,2))
XTS1 <- structure(c(12, 7, 7, 22, 24, 30, 26, 23, 27, 30), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts", "zoo"), .CLASS = structure("double", class = "CLASS"), formattable = structure(list(formatter = "formatC", format = structure(list(format = "f", digits = 2), .Names = c("format", "digits")), preproc = "percent_preproc", postproc = "percent_postproc"), .Names = c("formatter", "format", "preproc", "postproc")), index = structure(c(1413981900, 1413982800, 1413983700, 1413984600, 1413985500, 1413986400, 1413987300, 1413988200, 1413989100, 1413990000), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(10L, 1L))
XTS2 <- XTS1 ^ 0.2
plot(XTS1)
plot(XTS2)
The following script fails to plot 2 charts side by side:
require(PerformanceAnalytics)
require(xts)
par(mfrow=c(1,2))
XTS1 <- structure(c(12, 7, 7, 22, 24, 30, 26, 23, 27, 30), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts", "zoo"), .CLASS = structure("double", class = "CLASS"), formattable = structure(list(formatter = "formatC", format = structure(list(format = "f", digits = 2), .Names = c("format", "digits")), preproc = "percent_preproc", postproc = "percent_postproc"), .Names = c("formatter", "format", "preproc", "postproc")), index = structure(c(1413981900, 1413982800, 1413983700, 1413984600, 1413985500, 1413986400, 1413987300, 1413988200, 1413989100, 1413990000), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(10L, 1L))
XTS2 <- XTS1 ^ 0.2
charts.PerformanceSummary(XTS1)
charts.PerformanceSummary(XTS2)
Would anyone know how to get the latter script to plot 2 charts side by side?
I would like to avoid using another package if possible. Thanks.
chart.PerformanceSummary is really just a wrapper to several charts.
You could do this, and extend it to any number of symbols horizontally if you wish (more than 2 symbols if you wanted):
par(mfrow=c(3,2))
# First row
chart.CumReturns(XTS1, ylab = "Cumulative Return", main = "give me a title")
chart.CumReturns(XTS2, ylab = "Cumulative Return", main = "give me a title2")
# second row
chart.BarVaR(XTS1)
chart.BarVaR(XTS2)
# third row
chart.Drawdown(XTS1, main = "DD title", ylab = "Drawdown",
)
chart.Drawdown(XTS2, main = "", ylab = "Drawdown",
)
You need to add the appropriate parameters to each plot for things like colour and titles (leaving that to you), but you have the flexibility of adding any charts from the wonderful xts, quantmod, performanceAnalytics packages (and others).
I am trying to estimate overnight returns for many stocks using a for loop and store it in a dataframe with stock names as column names. The trade has raw intraday data and trade2 has cleaned intraday data. list.namess has stock names. This is my code:
require(xts)
require(highfrequency)
OvernightRet<-list()
list.namess<- list.files(pattern="*.IS Equity")
list.namess<- list.namess[2]
for(Q in 1:length(list.namess)){
trade<-readRDS(list.namess[Q])
trade<-xts(trade[,-1], order.by = trade[,1])
colnames(trade)[c(1,2)]<-c("PRICE", "SIZE")
#Unduplicating
trade2<-do.call(rbind, lapply(split(trade,"days"), mergeTradesSameTimestamp))
trade2<-trade2[,1]
fun.first= function(x) first(x)
fun.last= function(x) last(x)
A=do.call(rbind, lapply(split(trade2, "days"), FUN=fun.first))
B=do.call(rbind, lapply(split(trade2, "days"), FUN=fun.last))
OvernightRetA <- (as.numeric(A)-as.numeric(lag.xts(B)))/as.numeric(lag.xts(B))
colnames(OvernightRetA)<-list.namess[Q]
OvernightRet[[Q]]<-OvernightRetA
}
df.OvernightRet<-do.call(merge, OvernightRet)
However, it gives error, probably because of not being able to rename the OvernightRetA:
Error in `colnames<-`(`*tmp*`, value = "ACEM IS Equity.rds") :
attempt to set 'colnames' on an object with less than two dimensions
In addition: There were 50 or more warnings (use warnings() to see the first 50)
> df.OvernightRet<-do.call(merge, OvernightRet)
Error in as.data.frame(x) : argument "x" is missing, with no default
As trade and trade2 is huge and not appropriate for dput. I am posting given Open(A), Close(B) and list of names (list.namess) for reproducibility of error.
dput(head(A,10))
structure(c(231.9, 236.35, 230, 226.85, 229.05, 225.7, 226.95,
224.55, 227, 234.65), class = c("xts", "zoo"), .indexCLASS = c("POSIXct",
"POSIXt"), .indexTZ = "Asia/Calcutta", tclass = c("POSIXct",
"POSIXt"), tzone = "Asia/Calcutta", Price = 1L, index = structure(c(1459481850,
1459741066, 1459827433, 1459913867, 1460000236, 1460086630, 1460345867,
1460432285, 1460518631, 1460950628), tzone = "Asia/Calcutta", tclass = c("POSIXct",
"POSIXt")), .Dim = c(10L, 1L), .Dimnames = list(NULL, "PRICE"))
dput(head(B,10))
structure(c(235.35, 231.2, 226.1, 229.05, 226.45, 225.75, 224.55,
223.75, 231.1, 228.6), class = c("xts", "zoo"), .indexCLASS = c("POSIXct",
"POSIXt"), .indexTZ = "Asia/Calcutta", tclass = c("POSIXct",
"POSIXt"), tzone = "Asia/Calcutta", Price = 1L, index = structure(c(1459508732,
1459767943, 1459854348, 1459940748, 1460027143, 1460113538, 1460374518,
1460465873, 1460545568, 1460977541), tzone = "Asia/Calcutta", tclass = c("POSIXct",
"POSIXt")), .Dim = c(10L, 1L), .Dimnames = list(NULL, "PRICE"))
dput(list.namess) "ACEM IS Equity.rds"
Kindly help me solve this error.
I believe the problem, as the error message implies, is that you are trying to assign a column header to a single value. You can work around this by changing the line above to:
OvernightRetA <- as.data.frame(as.numeric(A)-as.numeric(lag.xts(B)))/as.numeric(lag.xts(B))
I have data like this:
View(dose_merged)
SUBJECT_Blinded PACKID SACDPDAT SACRTDAT treatment_interval SD_SDAT SD_EDAT
1 1501301 10094 2012-05-26 2012-07-23 58 2012-01-03 2013-01-02
2 1601301 10555 2012-01-03 2012-01-31 28 2012-01-03 2013-01-0
With columns types in data table:
> mapply(class, dose_merged)
$SUBJECT_Blinded
[1] "numeric"
$PACKID
[1] "numeric"
$SACDPDAT
[1] "POSIXct" "POSIXt"
$SACRTDAT
[1] "POSIXct" "POSIXt"
$treatment_interval
[1] "Interval"
attr(,"package")
[1] "lubridate"
$SD_SDAT
[1] "POSIXct" "POSIXt"
$SD_EDAT
[1] "POSIXct" "POSIXt"
I want to determine the length of intersection of intervals: interval(SACDPDAT, SACRTDAT) and interval(SD_SDAT, SD_EDAT).
I am trying this:
dose_merged[,intersect1 := aaply(dose_merged, 1, function(x){intersect(interval(x[3],x[4]), interval(x[8],x[9]))})]
But then I get error message:
Error: error while computing 'x' when choosing method for 'intersect': Error in as.POSIXct.default(start) :
do not know how to convert 'start' to class “POSIXct”
The line
intersect(interval(x[3],x[4]), interval(x[8],x[9]))})
works for specified row x.
Any ideas what I am doing wrong ?
The first two rows of dput(dose_merge):
structure(list(SUBJECT_Blinded = c(1101001, 1101001), PACKID = c(10096,
10595), SACDPDAT = structure(c(1335304800, 1325545200), class = c("POSIXct",
"POSIXt"), tzone = ""), SACRTDAT = structure(c(1340316000, 1327964400
), class = c("POSIXct", "POSIXt"), tzone = ""), treatment_interval = structure(c(58,
28), class = structure("Interval", package = "lubridate")), TS_SDAT = structure(c(NA_real_,
NA_real_), class = c("POSIXct", "POSIXt"), tzone = ""), TS_EDAT = structure(c(NA_real_,
NA_real_), class = c("POSIXct", "POSIXt"), tzone = ""), SD_SDAT = structure(c(1325545200,
1325545200), class = c("POSIXct", "POSIXt"), tzone = ""), SD_EDAT = structure(c(1357081200,
1357081200), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("SUBJECT_Blinded",
"PACKID", "SACDPDAT", "SACRTDAT", "treatment_interval", "TS_SDAT",
"TS_EDAT", "SD_SDAT", "SD_EDAT"), sorted = "SUBJECT_Blinded", class = c("data.table",
"data.frame"), row.names = c(NA, -2L), .internal.selfref = <pointer: 0x0000000002f30788>)
Hi all I want to plot a heatmap:
df ist {xts} and looking like this:
structure(c(1.3728813559322, 0.871666666666667, 0.586666666666667,
0.34, -0.31, -0.973333333333333, -1.52666666666667, -1.71333333333333,
-0.396666666666667, 0.698333333333333, 2.84666666666667, 4.68333333333333,
5.33833333333333, 5.66666666666667, 5.63666666666667, 5.69, 5.69666666666667,
5.54333333333333, 5.50833333333333, 4.335, 3.065, 2.42666666666667,
1.88666666666667, 1.47833333333333), .indexCLASS = c("POSIXct",
"POSIXt"), .indexTZ = "", tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts",
"zoo"), index = c(1364770740, 1364774340, 1364777940, 1364781540,
1364785140, 1364788740, 1364792340, 1364795940, 1364799540, 1364803140,
1364806740, 1364810340, 1364813940, 1364817540, 1364821140, 1364824740,
1364828340, 1364831940, 1364835540, 1364839140, 1364842740, 1364846340,
1364849940, 1364853540), .Dim = c(24L, 1L), .Dimnames = list(
NULL, "df.xts"))
As in the following post I want to have y-axis 24 hours - one value per hour, and y-axis the date.
Is it possible to work with the existing xts format?
ggplot2 heatmap to assign colors to breaks
I also found another example with heatmap.plus().
z = matrix(rnorm(30),nrow=5,ncol=6);
rlab = matrix(as.character(c(1:5,2:6,3:7,4:8)),nrow=5,ncol=4);
clab = matrix(as.character(c(1:6,6:1)),nrow=6,ncol=2);
colnames(rlab) = LETTERS[1:dim(rlab)[2]];
colnames(clab) = 1:dim(clab)[2];
heatmap.plus(z,ColSideColors=clab,RowSideColors=rlab);
Example is running, but I would prefer a legend, and my data look different from df - and not an xts with date.
Thanks!
This perhaps isn't an answer to the heatmap question, but this code and output will not display properly in a comment. (Noting my comments above...) Using an object named datcreated from the dput output above, but with the tclass-attribute removed, I subsetted it and:
dput(dat['2013-04-01'])
structure(c(0.698333333333333, 2.84666666666667, 4.68333333333333,
5.33833333333333, 5.66666666666667, 5.63666666666667, 5.69, 5.69666666666667,
5.54333333333333, 5.50833333333333, 4.335, 3.065, 2.42666666666667,
1.88666666666667, 1.47833333333333), .indexCLASS = c("POSIXct",
"POSIXt"), .indexTZ = "", tzone = "", class = c("xts", "zoo"), index = c(1364803140,
1364806740, 1364810340, 1364813940, 1364817540, 1364821140, 1364824740,
1364828340, 1364831940, 1364835540, 1364839140, 1364842740, 1364846340,
1364849940, 1364853540), .Dim = c(15L, 1L), .Dimnames = list(
NULL, "df.xts"))
> d2 <- dat['2013-04-01']
> tclass(d2) <- "POSIXct"
> d2 #displays with time appropriate format
df.xts
2013-04-01 00:59:00 0.6983333
2013-04-01 01:59:00 2.8466667
2013-04-01 02:59:00 4.6833333
2013-04-01 03:59:00 5.3383333
2013-04-01 04:59:00 5.6666667
2013-04-01 05:59:00 5.6366667
2013-04-01 06:59:00 5.6900000
2013-04-01 07:59:00 5.6966667
2013-04-01 08:59:00 5.5433333
2013-04-01 09:59:00 5.5083333
2013-04-01 10:59:00 4.3350000
2013-04-01 11:59:00 3.0650000
2013-04-01 12:59:00 2.4266667
2013-04-01 13:59:00 1.8866667
2013-04-01 14:59:00 1.4783333
> dput(d2)
structure(c(0.698333333333333, 2.84666666666667, 4.68333333333333,
5.33833333333333, 5.66666666666667, 5.63666666666667, 5.69, 5.69666666666667,
5.54333333333333, 5.50833333333333, 4.335, 3.065, 2.42666666666667,
1.88666666666667, 1.47833333333333), .indexCLASS = c("POSIXct",
"POSIXt"), .indexTZ = "", tzone = "", class = c("xts", "zoo"), index = structure(c(1364803140,
1364806740, 1364810340, 1364813940, 1364817540, 1364821140, 1364824740,
1364828340, 1364831940, 1364835540, 1364839140, 1364842740, 1364846340,
1364849940, 1364853540), tclass = c("POSIXct", "POSIXt")), .Dim = c(15L,
1L), .Dimnames = list(NULL, "df.xts"))
No added tclass attribute after : tclass(d2) <- "POSIXct". So I wonder if the heatmap axis problem relates to an improperly formed xts object.