How to convert S3 objects with class roc for ggplot2? - r

I'm planning to use patchwork to assemble several ROC curves plotted with pROC. After constructing a pROC plot list (of S3: roc objects) and attempting to use wrap_plots(plots) to assemble, I came across the following error:
Error: Only know how to add ggplots and/or grobs
AFAIK, there may be several solutions:
Coerce S3:roc objects to ggplots. It seems the function fortify does this job for S3 objects generated by precrec package but I don't know if S3:roc objects can be done in the same way. Using ggplot2::fortify I ran into
`data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class roc.
Use precrec to streamline the conversion, instead. What curtails my migration is that I want to print Youden index point and confidence intervals of the Youden index point and area under curve (AUC) on the plot. It seems only pROC package meets all my needs so I don't quite want to move on. Also I need to adjust my codes to cater parameter demands from precrec. Too much to learn and try, so tutorials and simple codes are appreciated.
Whatever, my final purpose is being able to assemble all ROC curves programmatically, with automatic annotations. The ROC curves need to show their respective Youden index point and confidence intervals of the Youden index point and area under curve (AUC) on the plot.
Drawbacks exist in the pROC package, too. The text sizes of Youden index and confidence interval values are too small for the whole plot if all ROC plots are assembled. I can adjust them by specifying par(cex=<text size>) but there's ricks that the texts may overlap with the curves or get out of bound if the texts are too marginal. pROC is not smart enough to reconcile with text sizes, curves and text positions. A smarter package to meet all of my harsh demands mentioned above will strongly push me forward to adopt a new package to draw ROC curves. Therefore, solutions vary in my scenario (but please don't recommend using a graphical vector image editor to edit these curves by hand because it's time-consuming and error-prone, and lags changing demands from different journals). All insights from all perspectives are appreciated.

Have you tried the ggroc function from pROC? It does exactly what you're asking for: it creates a ggplot2 plot (class gg) which you can then manipulate as you wish.
However I think you are being slightly confused:
Coerce S3:roc objects to ggplots. It seems the function fortify does this job for S3 objects generated by precrec package
It makes sense that the precrec package would be able to convert its own objects. However, note that it doesn't generate a ggplot2 object, but a data.frame with the coordinates of the ROC curve (which can then be used as input for ggplot2).
In pROC, this exact operation is done with the coords function, which extracts the coordinates of the ROC curve to a data.frame (and that you can then use as input for ggplot2).

Related

Plotting GAMM model results with package gratia

Is there a way to produce plots with gratia that have the y-axis on the response variable scale similar to the scale="response" visreg function? I know scale is already an option for the y-axis in gratia, but just for axis range and not transforming the variable.
Thinking of something like:
draw(mymodel, type="response")?
This is a current feature request for the package: https://github.com/gavinsimpson/gratia/issues/79
If I ever surface from creating content for two new courses this semester adding this is a top priority for me.
Currently the best I can suggest is to evaluate the smooth using evaluate_smooth(), then use mutate() to apply the inverse of the link function to the estimated value and the confidence interval, and then use the draw() method for those objects to produce the plot, with cowplot or patchwork to plot multiple plots on a single page/device.

Is there a way to use ggs_caterpillar to compare two models on the same facet, all parameters are shared?

I can produce a caterpillar plot using ggmcmc::ggs_caterpillar with two facets each one showing a model.
In this case all my parameters are shared between the models. I would like to plot them on the same facet. So I can visually compare each parameter next to each other with a small vertical offset.
Is this possible in the function or via another approach?
library(ggmcmc)
Model1 <- ggs(Model1MCMCOutput)
Model2 <- ggs(Model2MCMCOutput)
ggs_caterpillar(list(Model1, Model2))
This is not possible using this function. I looked in the internals and the plotting inside is done in a slightly convoluted way. I wrote my own function for plotting what I needed. Using geom_linerange and rotating the coordinate system rather than using geom_segment as in ggs_caterpillar.

Multiple partial dependence plots in one graph

I am using the randomForest package with the Partialplot function.
I want to make multiple partial dependence plots in one graph. My thesis promotor told me that it is possible to save them (in the environment, I did this and I got a list object with 'x' and 'y' variables in that list), but I don't know how to recall the graph after saving it.
What I want to do is:
1. Save PD plots
2. recall them
3. plot multiple PDP in one graph
Use the pdp package. Examples are given in the paper: https://journal.r-project.org/archive/2017/RJ-2017-016/RJ-2017-016.pdf.
Instead of using the partialPlot function, consider using the plotmo function in the plotmo package. This will draw plots for all variables and variable pairs on a single page. For example:
library(randomForest)
data(trees)
mod <- randomForest(Volume~., data=trees)
library(plotmo)
plotmo(mod, pmethod="partdep") # plot partial dependencies
which gives
You can specify exactly which variable and variable pairs get plotted using plotmo's all1, all2, degree1 and degree2 arguments. Additional examples are in the vignette for the plotmo package.
Perfect! Can save the plots as lattice objects and then recall. Plot together using gridArrange or CowPlot and pretty them up using ggplot. Great solution!

How to reproduce this graphical explanation (a scatter plot) of how covariance works?

I found this graphical intuitive explanation of covariance:
32 binormal points drawn from distributions with the given covariances, ordered from most negative (bluest) to most positive (reddest)
The whole material can be found at:
https://stats.stackexchange.com/questions/18058/how-would-you-explain-covariance-to-someone-who-understands-only-the-mean
I would like to recreate this sort of graphical illustration in R, but I'm not sufficiently familiar with R's plotting tools. I don't even know where to start in order to get those colored rectangles between each pair of data points, let alone make them semi-transparent.
I think this could make a very efficient teaching tool.
The cor.rect.plot function in the TeachingDemos package makes plots similar to what is shown. You can modify the code for the function to make the plot even more similar if you desire.

R: getting data (instead of plot) back from sm.density.compare

I'm doing a density compare in R using the sm package (sm.density.compare). Is there anyway I can get a mathematical description of the graph or at least a table with number of points rather than a plot back? I would like to plot the resulting graphs in a different application, but need the data to do so.
Thanks a lot for the help,
culicidae

Resources