Plotting GAMM model results with package gratia - plot

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.

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

densityplot and stripplot with mice object

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

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!

changing default colours when using the plot function of the R package mixtools

I have a plotting problem with curves when using mixtools
Using the following R code
require(mixtools)
x <- c(rnorm(10000,8,2),rnorm(10000,18,5))
xMix <- normalmixEM(x, lambda=NULL, mu=NULL, sigma=NULL)
plot(xMix, which = 2, nclass=25)
I get a nice histogram, with the 2 normal curves estimated from the model superimposed.
The problem is with the default colours (i.e. red and green), which I need to change for a publication to be black and grey.
One way I thought to doing this was first to produce the histogram
hist(xMix$x, freq=FALSE, nclass=25)
and then add the lines using the "curve" function.
....... but I lost my way, and couldn't solve it
I would be grateful for any pointers or the actual solution
thanks
PS. Note that there is an alternative work-around to this problem using ggplot:
Any suggestions for how I can plot mixEM type data using ggplot2
but for various reasons I need to keep using the base graphics
You can also edit the colours directly using the col2 argument in the mixtools plotting function
For example
plot(xMix, which = 2, nclass=25, col2=c("dimgrey","black"))
giving the problem a bit more thought, I managed to rephrase the problem and ask the question in a much more direct way
Using user-defined functions within "curve" function in R graphics
this delivered two nice solutions of how to use the "curve" function to draw normal distributions produced by the mixture modelling.
the overall answer therefore is to use the "hist" function to draw a histogram of the raw data, then the "curve" function (incorporating the sdnorm function) to draw each normal distribution. This gives total control of the colours (and potentially any other graphic parameter).
And not to forget - this is where I got the code for the sdnorm function - and other useful insights
Any suggestions for how I can plot mixEM type data using ggplot2
Thanks as always to StackOverflow and the contributors who provide such helpful advice.

Resources