Performance Analytics drawdown function not working r - r

I am trying to plot a drawdown graph of daily profits using the Performance Analytics package. I have managed to do this with the code:
cols = rainbow(ncol(pdrawdown),s=0.7, v=0.8, alpha= 0.7)
chart.Drawdown(pdrawdown, legend.loc = "bottomleft",colorset = cols,
main = "Drawdown Chart", xlab ="Date", ylab = "Drawdown")
for percentage returns. However when I use profits (which deals with much larger numbers), the graphics window doesn't even show any table, and no error or warning messages are produced.
My pdrawdown data is structured like so:
structure(list(Arbitrage = c(-410344.36040002, 43186613.0914002,
-56243745.8289002, 33212085.9369, 2685633.86650004, 52056396.8137002
), Cmdty = c(-5661740.59000004, -12816611.3327999, -15271367.5797001,
30328698.7996001, 2346206.95040001, -20111667.3121), Cnvt = c(-0.448500000005879,
-4.09389999999985, -3.76900000000023, -4.46530000000348, 9.47310000000289,
5.57809999999154), FI = c(-4959851.92789985, 51301719.2496983,
19347533.8012021, -45928596.3382014, -126566982.481699, 7039919.16710053
), IndexArb = c(465514.007300064, 712460.314099789, -1241298.64239982,
859103.288600107, -9142.46960010222, 1670160.15220016), OptVolD = c(3207463.8402,
-2645827.2004, 1917467.1194, -1645717.199, 1346643.9976, 1249267.9222
), OptVolG = c(1809178.81009999, -1247076.75579998, 1208738.92329999,
-998966.340099988, 474978.958799993, 2312496.41480001), Other = c(3121114.45319998,
-7558428.4574, 28751941.5262, -8287057.41060003, -5057308.97439997,
25541692.1845), RVG = c(107064420.2606, 41122417.5658004, -33634242.2959002,
-25318480.7920002, -9396822.53510034, 69874891.7186005), SAAsia = c(-31022426.4966,
-1381533.43030003, 21484053.8479001, 15456037.1175, 11307323.7993999,
8081090.74740009)), .Names = c("Arbitrage", "Cmdty", "Cnvt",
"FI", "IndexArb", "OptVolD", "OptVolG", "Other", "RVG", "SAAsia"
), row.names = c("2015-08-03", "2015-08-04", "2015-08-05", "2015-08-06",
"2015-08-07", "2015-08-10"), class = "data.frame")
Update: Setting a ylim value i.e.
cols = rainbow(ncol(pdrawdown),s=0.7, v=0.8, alpha= 0.7)
chart.Drawdown(pdrawdown, legend.loc = "bottomleft",colorset = cols,
main = "Drawdown Chart", xlab ="Date", ylab = "Drawdown",ylim = c(-1,0))
causes the graph to appear but it still displays effectively nonsensical data, no matter what my lower bound is for the ylim. With returns, however, this ylim term works as expected, fixing the axes of the plot.

Since no-one was able to give an answer, I will post my best solution in case anyone else runs into this problem in the future.
As far as I can tell, the issue is (as expected) simply being caused because the drawdown PnL is too large. My "hack" to fix this was to divide the datatable by a number that causes all values to be below 1. In my case this meant my y scale being in billions like so:
pdrawdown = pdrawdown/1000000000 # PnL displays in billions (set units here)
cols = rainbow(ncol(pdrawdown),s=0.7, v=0.8, alpha= 0.7)
chart.Drawdown(pdrawdown, legend.loc = "bottomleft",colorset = cols,
main = "Drawdown Chart", xlab ="Date", ylab = "Drawdown ($billions)
Dividing by any number smaller just gave a very odd looking graph with massive jumps that did not resemble the returns graph for the same data.
I suspect this is because the drawdown graph is designed for percentages and thus can only deal with numbers between 0 and -1 (maximum drawdown in percentage being, of course, -100%).

Related

labels for boxplot and barplot loop in r [duplicate]

I am trying to plot 16 boxplots, using a for loop. My problem is, that the 2nd title is plotted on the first plot, the 3rd title on the second plot and so forth.
Does anyone have a guess on, what I am doing wrong?
My code is the following:
boxplot(data$distance[data$countryname=="Sweden"]~data$alliance[data$countryname=="Sweden"],title(main = "Sweden"))
boxplot(data$distance[data$countryname=="Norway"]~data$alliance[data$countryname=="Norway"],title(main = "Norway"))
boxplot(data$distance[data$countryname=="Denmark"]~data$alliance[data$countryname=="Denmark"],title(main = "Denmark"))
boxplot(data$distance[data$countryname=="Finland"]~data$alliance[data$countryname=="Finland"],title(main = "Finland"))
boxplot(data$distance[data$countryname=="Iceland"]~data$alliance[data$countryname=="Iceland"],title(main = "Iceland"))
boxplot(data$distance[data$countryname=="Belgium"]~data$alliance[data$countryname=="Belgium"],title(main = "Belgium"))
boxplot(data$distance[data$countryname=="Netherlands"]~data$alliance[data$countryname=="Netherlands"],title(main = "Netherlands"))
boxplot(data$distance[data$countryname=="Luxembourg"]~data$alliance[data$countryname=="Luxembourg"],title(main = "Luxembourg"))
boxplot(data$distance[data$countryname=="France"]~data$alliance[data$countryname=="France"],title(main = "France"))
boxplot(data$distance[data$countryname=="Italy"]~data$alliance[data$countryname=="Italy"],title(main = "Italy"))
boxplot(data$distance[data$countryname=="Spain"]~data$alliance[data$countryname=="Spain"],title(main = "Spain"))
boxplot(data$distance[data$countryname=="Portugal"]~data$alliance[data$countryname=="Portugal"],title(main = "Portugal"))
boxplot(data$distance[data$countryname=="Germany"]~data$alliance[data$countryname=="Germany"],title(main = "Germany"))
boxplot(data$distance[data$countryname=="Austria"]~data$alliance[data$countryname=="Austria"],title(main = "Austria"))
boxplot(data$distance[data$countryname=="Ireland"]~data$alliance[data$countryname=="Ireland"],title(main = "Ireland"))
boxplot(data$distance[data$countryname=="UK"]~data$alliance[data$countryname=="UK"],title(main = "UK"))
I think this could replace all your lines and fix your problem:
for (i in data$countryname)
boxplot(distance~alliance, subset(data, countryname==i), main=i)
But that's hard to verify without a reproducible example or some of your data.frame.
Based on the documentation, you should be assigning a title to your boxplots by making explicit calls to the function title(), rather than as a parameter in the call to boxplot(). The first two calls to generate your boxplots should look something like the following:
boxplot(data$distance[data$countryname=="Sweden"]~data$alliance[data$countryname=="Sweden"])
title(main = "Sweden")
boxplot(data$distance[data$countryname=="Norway"]~data$alliance[data$countryname=="Norway"])
title(main = "Norway")

Customising the IRF plot

I am trying to customise the plot generated by:
plot(irf(VECMcoeff, n.ahead = 20, cumulative = FALSE, ortho = TRUE))
Current figure:
Not yet allowed to post figures.
Is there a way, using R's basis plotting functions, to adjust this plot? More specifically;
I would like to have plots stand-alone from each other.
Adjust x- and y-axis titles.
Adjust the main title.
Remove the '95 % Bootstrap CI, 100 runs'.
Thanks for any help!
Data:
dput(head(combined,25))
structure(c(3.12378036948822, 3.24514490963516, 2.54231015523096,
3.10758964326189, 3.26905177146087, 3.39086921629928, 3.39867627597089,
3.063339608249, 2.82158440194456, 3.00060851536641, 2.87498214357332,
2.73447964251719, 2.51961774067125, 2.43535838893541, 2.53536474393679,
2.11458263713232, 2.08443293370433, 1.70951997715485, 1.6939353104687,
1.99402766681289, 2.17851574489578, 2.02035721460859, 2.19849725222166,
2.12385225312224, 2.13870052300126, 2.53563259854902, 2.71236841778707,
2.80602806173539, 2.44978220282482, 2.22240349195674, 2.6269002941349,
2.55424892433652, 2.84227347851153, 3.00695212249206, 2.56409065301929,
2.11958065079056, 1.93021828518557, 1.91149187923047, 2.12824458610721,
1.99034383037538, 1.85993728242216, 1.78831122085649, 1.70508421574581,
1.34148894168009, 1.26428948883955, 1.53707667916106, 1.40125321322403,
1.56189928398736, 1.59267901471992, 1.29435444758231, 2.88357952825106,
3.2967949657277, 2.71315870827614, 2.88194083947586, 2.55384396254808,
2.48162552588286, 2.43461752858767, 2.60895931242784, 2.88699097436266,
3.06774759389068, 2.92820858177705, 1.9236817467793, 1.30469143981917,
1.63412478606386, 1.32569634585868, 1.66411340281953, 1.811114177636,
1.32324449480086, 0.683740288067047, 0.506428412402278, 0.244160570695116,
-0.0614637978267916, 0.11100051693192, 0.107431188637327, -0.0946163941762501,
1.56887584570782, 2.2953989716194, 2.3913948824343, 1.60366568545365,
2.14074303245166, 1.42821783272864, 1.14416900596202, 1.32550310805691,
1.06775704738626, 1.1754985484452, 1.30819594680082, 1.57801107586324,
1.57465869540119, 1.52953051921855, 1.59632502092932, 1.51164066108273,
1.74699133577352, 1.89513403376172, 1.50403737650093, 1.69077755145674,
1.51619819345532, 1.7908456553931, 1.63120428277988, 1.72264300428443,
1.91016040082409, 2.93953881174612, 0.573867521584496, 1.36693966408554,
1.33745582274447, 2.00217541671565, 1.47500074486359, 1.54892810099376,
1.52596101747453, 1.85097710190023, 1.8027452973638, 1.71255671138078,
1.78801314649281, 1.73039561596535, 1.7797925346833, 1.68662137367852,
2.10887254895115, 2.47630376444312, 2.10728662380876, 1.99939507617536,
2.1661652656972, 1.97780409080129, 2.08116163569287, 2.33934227442197,
2.38773088163046, 2.39899888596041), .Dim = c(25L, 5L), .Dimnames = list(
NULL, c("rstar.nl2", "rstar.ger2", "rstar.fr2", "rstar.sp2",
"rstar.it2")))
Somewhat general advice:
plot() is a generic function that actually calls a more specific function (called a "method") depending on what you are trying to plot (see this chapter from Hadley Wickham's Advanced R book for details). In this case, you are feeding-in an object of class "varirf" to plot(). You can see this by running, e.g.:
out <- irf(your_arguments_go_here)
class(out)
The generic function plot() is calling the method plot.varirf() because you are feeding in an object of type "varirf". To see which parameters of plot.varirf() you can control, check out that function's help page
?plot.varirf
If this doesn't provide you with sufficient control over what you want your plot to look like, then you should abandon trying to use plot.varif() and construct your desired plot manually, as in:
plot(x=my_x_vals, y=my_y_vals, main="My Title", pch=20, col="red", etc.)
In manually creating your plot, you may find it useful to see how plot.varirf plots are created so you can implement some of the same formatting. To view the source code for plot.varirf, use:
getAnywhere(plot.varirf)
Here is an example:
plot(x, plot.type = "single", names = NULL, main = "IRF to a one-standard deviation shock to APP", sub = NULL, lty = NULL, lwd = NULL, col = NULL, ylim = NULL, ylab = "Eonia", xlab = "Number of periods", mar.multi = c(0, 4, 0, 4), oma.multi = c(6, 4, 6, 4), adj.mtext = NA, padj.mtext = NA, col.mtext = NA)
where, x is your varest object,
Good luck!

Title is missing in 1st boxplot when using a for loop

I am trying to plot 16 boxplots, using a for loop. My problem is, that the 2nd title is plotted on the first plot, the 3rd title on the second plot and so forth.
Does anyone have a guess on, what I am doing wrong?
My code is the following:
boxplot(data$distance[data$countryname=="Sweden"]~data$alliance[data$countryname=="Sweden"],title(main = "Sweden"))
boxplot(data$distance[data$countryname=="Norway"]~data$alliance[data$countryname=="Norway"],title(main = "Norway"))
boxplot(data$distance[data$countryname=="Denmark"]~data$alliance[data$countryname=="Denmark"],title(main = "Denmark"))
boxplot(data$distance[data$countryname=="Finland"]~data$alliance[data$countryname=="Finland"],title(main = "Finland"))
boxplot(data$distance[data$countryname=="Iceland"]~data$alliance[data$countryname=="Iceland"],title(main = "Iceland"))
boxplot(data$distance[data$countryname=="Belgium"]~data$alliance[data$countryname=="Belgium"],title(main = "Belgium"))
boxplot(data$distance[data$countryname=="Netherlands"]~data$alliance[data$countryname=="Netherlands"],title(main = "Netherlands"))
boxplot(data$distance[data$countryname=="Luxembourg"]~data$alliance[data$countryname=="Luxembourg"],title(main = "Luxembourg"))
boxplot(data$distance[data$countryname=="France"]~data$alliance[data$countryname=="France"],title(main = "France"))
boxplot(data$distance[data$countryname=="Italy"]~data$alliance[data$countryname=="Italy"],title(main = "Italy"))
boxplot(data$distance[data$countryname=="Spain"]~data$alliance[data$countryname=="Spain"],title(main = "Spain"))
boxplot(data$distance[data$countryname=="Portugal"]~data$alliance[data$countryname=="Portugal"],title(main = "Portugal"))
boxplot(data$distance[data$countryname=="Germany"]~data$alliance[data$countryname=="Germany"],title(main = "Germany"))
boxplot(data$distance[data$countryname=="Austria"]~data$alliance[data$countryname=="Austria"],title(main = "Austria"))
boxplot(data$distance[data$countryname=="Ireland"]~data$alliance[data$countryname=="Ireland"],title(main = "Ireland"))
boxplot(data$distance[data$countryname=="UK"]~data$alliance[data$countryname=="UK"],title(main = "UK"))
I think this could replace all your lines and fix your problem:
for (i in data$countryname)
boxplot(distance~alliance, subset(data, countryname==i), main=i)
But that's hard to verify without a reproducible example or some of your data.frame.
Based on the documentation, you should be assigning a title to your boxplots by making explicit calls to the function title(), rather than as a parameter in the call to boxplot(). The first two calls to generate your boxplots should look something like the following:
boxplot(data$distance[data$countryname=="Sweden"]~data$alliance[data$countryname=="Sweden"])
title(main = "Sweden")
boxplot(data$distance[data$countryname=="Norway"]~data$alliance[data$countryname=="Norway"])
title(main = "Norway")

Calculate equation from .csv file input and plot result over barplot

I coulnd't found any post with a related subject. I actually don't know if its posible.
So I have my. csv file:
Periodo;Teorico;Real;F1;F2;F3
20140101;50;20;7;7;16
20140108;55;29;11;5;5
20140115;52;21,4;8,6;10;12
20140122;66;32;9;8;17
I asign it to a data.frame:
df<-read.csv2('d:\\xxx\\test2.csv', header = T, sep = ";")
Then I do barplot function:
bp <- barplot(t(df[,-c(1:2)]),col=c("blue", "red", "green", "yellow"),legend=colnames(df[,-c(1:2)]),args.legend = list(x="topleft"))
axis(side = 1, at = bp, labels = df$Periodo)
title(main = "Teorico = Real + F1+F2+F3", font.main = 4)
Now I must calculate the following function: (efficiency function)
((Teorico-Real)/Teorico)*100
And represent the result of the function of each row on the top of each Periodo (week).
If you could help me with the code for the function and "replotting" parts or give some guidelines or posts related to this I would be really gratefull.
Thanks
You can try:
lbls <- round(((df$Teorico - df$Real) / df$Teorico)* 100)
mtext(lbls, at=bp)
(I just used round to make it look better.)

Issues with formatting header in R prior to using plot() function

I have a data set that I've successfully read into R. It's a simple data.frame with ONE ROW of data (I'm not sure how many columns, but its in the hundreds). It was read with column headers, but no row labels. So the data set looks something like this:
df=structure(list(X500000 = 0.0958904109589041, X1500000 = 0.10958904109589, X2500000 = 0.10958904109589, X3500000 = 0.164383561643836, X4500000 = 0.136986301369863, X5500000 = 0.205479452054795, X6500000 = 0.136986301369863, X7500000 = 0.0273972602739726, X8500000 = 0.0821917808219178, X9500000 = 0.178082191780822), .Names = c("X500000", "X1500000", "X2500000", "X3500000", "X4500000", "X5500000", "X6500000", "X7500000", "X8500000", "X9500000"), class = "data.frame", row.names = 79L)
Except that it is MUCH LARGER (I don't know if it matters, but it has around 300 columns going across). I'm trying to plot it so that the X##### labels are on the x axis, and the value of each data point is plotted on the y axis (say like a scatter plot on excel or even a line graph). Doing just plot(df) gives me an extremely bizarre graph that makes no sense to me (a bunch of boxes each with a dot right in the centre and no labels?).
I have a feeling it might work if I were to transform the data frame into a vector by removing the headings and then adding x-axis labels individually afterwards and doing a plot() on the vector, but if there is a way of avoiding that it would be great....
As explained in '?plot', 'x' and 'y' must be two vectors of numerics, of same size:
df=structure(list(X500000 = 0.0958904109589041, X1500000 = 0.10958904109589, X2500000 = 0.10958904109589, X3500000 = 0.164383561643836, X4500000 = 0.136986301369863, X5500000 = 0.205479452054795, X6500000 = 0.136986301369863, X7500000 = 0.0273972602739726, X8500000 = 0.0821917808219178, X9500000 = 0.178082191780822), .Names = c("X500000", "X1500000", "X2500000", "X3500000", "X4500000", "X5500000", "X6500000", "X7500000", "X8500000", "X9500000"), class = "data.frame", row.names = 79L)
plot(x=as.numeric(substr(names(df),2,nchar(names(df)))), as.numeric(df), xlab="This is xlab", ylab="This is y")

Resources