densityplot and stripplot with mice object - r

I am using the densityplot and stripplot functions to analyze the results of a mice imputation. My data set has 30 variables. When applying densityplot and stripplot to the mice object containing the results of the imputation, I obtain a panel with the graphs for each of the variables. All the graphs are contained in only one panel (one for the densityplot and other one for the stipplot function) and to get it the size of the graphs is really very small, making almost imposible to differentiate and analyze the information of each of the graphs. Densityplot and stripplot are used as follows:
densityplot(tempData)
stripplot(tempData, pch = 20, cex = 1.2)
tempData is the mice object with the imputations. It contains more than 30 variables.
I would like to know if there is any option to control the number of graphs by panel when using densityplot and stripplot. In my case, if I split tho output of these functions in several panels, the graphs for each variable should be bigger enough to analyze it properly.
Thanks in advance

These functions follow the lattice conventions. So you should look into the documentation of that R package.
For a ggplot2 alternative to the plotting functions, see the plot_imps() function in this repo (under development!): https://github.com/amices/shinymice/blob/main/R/utils_04_imputations.R

Related

How to convert S3 objects with class roc for ggplot2?

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).

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.

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!

General questions about Principal Component Analysis (PCA) in R

I would like to produce some nice PCA plots in R. As usual, in R, there are several ways to perform a principal component analysis. I found so far 3 different ways of how to calculate your components and 3 ways of plotting them. I was wondering whether people who are familiar with these functions can give me some advise on the best combination of functions to produce the following plots:
Scores Plot
Loadings Plot
Histogram / Bar chart of the variances explained by each principal component
My research on functions and plots used for PCA in R resulted in:
Functions:
pca.xzy()
prcomp()
princomp()
dudi.pca()
Plot:
plot.pca (this one seems to belong to the function pca.xzy())
ggplot2
plot
biplot
I also found the following webpage:
http://pbil.univ-lyon1.fr/ade4/ade4-html/dudi.pca.html
And I was wondering if you can draw those circles and lines starting from each of the circle centers with one of the other functions mentioned above as the function dudi.pca from the ade4 package seems to be the most complicated one.
One question per question, please! There's psych package by William Revelle, see this and this. There's also a good tutorial here. Anyway...
for scores/loadings plot see pairs
histogram: see hist
So once again, what's your question actually? =)

Can I use shingles from lattice in ggplot2 in R

It is possible to use the shingles to define specific ranges in ggplot2. As far as i understand shingles are a way to generate groups. Can we create such shingles and use them in ggplot2 facet_grid to obtain graphs?
Following up from the comments, ggplot can't draw shingles (in the way lattice draws shingles with special indicators in the strip) and by default doesn't have a means of producing the overlapping groups.
However, I cam across this excellent PDF document which aims to produce a gpplot2 version of every figure in Depayan's excellent Lattice book (Lattice: Multivariate Data Visualization with R).
Page 31 contains a custom function fn() which replicates the behaviour of equal.count(), as far as I can tell, to provide the correct data structure to plot with overlapping shingles. The PDF contains plenty of examples of "shingles" in ggplot that you can play with.
So not sure if this answers the Q - but at least it appears one can fudge ggplot into producing plots that use the shingle concept.

Resources