How does plot function find regsubsets in R? - r

I'm confused about the mechanics of R (crossover from python). For example, I find best subsets with regsubsets (this is a class from a library called "leaps") and then plot below:
regfit_full = regsubsets(Something~.,data = db)
plot(regfit_full, scale="r2")
How does plot know how to deal with my instance of regsubsets class? Does plot have first look for a plot method in regsubsets first tells it how? And if this is the case, this second part confuses me. In order to look up documentation I do ?plot.regsubsets. If plot is a method of regsubsets, why is it not ?regsubsets.plot or something?
Thanks for the help

plot() is a generic function in R which dispatches to the "correct" version based on the class() of the first parameter. All the "special" methods for plot can be found with methods(plot).
You can learn more about is in the Advanced R OO field guide

Related

R: Finding help for classes created by packages

It often seems to be the case that R packages contain multiple functions that create an object of some class, specified by the package, with generic or non-generic methods that apply to all objects of that class. Although it is generally easy to find out about the functions in a package, I have not found any equally straightforward way to find a precise description of the class itself for S3 classes. I think this is at least partly intentional. Class definitions may be regarded as the sort of internal workings that, on one hand, the user should not have to think about, and on the other, may be changeable by the package creator, who wants people not to rely on them.
However, I find that I sometimes want to create additional objects of the same class that work with the package functions that are methods for that class. And it is not always easy to deduce what features an object must have in order to be usable by package functions that do various things to objects of that class, especially as instances created by different functions may or may not all have exactly the same structure.
The example with which I am currently wrestling are forecast objects created by various functions of the forecast package. The forecast package provides a large number of functions that take forecast objects as inputs. This blog post by Rob Hyndman describes a function to do cross validation and requires an object of class forecast as an argument The tsCV function documentation says it takes a "forecastFunction" as an argument, which must return an object of class forecast and have a univariate time series as its first object (of forecasts, one assumes) and have an argument h giving the horizon. Well, that sounds easy enough. But then in Hyndman’s associated textbook, section 3.6, we are told that forecast objects contain information about the forecasting method, the data, the point forecasts, prediction intervals, residuals, and fitted values. That’s a lot of things, and I am not sure if they are all mandatory or if some are optional, or required only if you intend to use certain methods. And I don’t know anything about mandatory internal structure of the class.
Finally, I particularly want to know if the new fable package, intended as a forecast package replacement, uses the same forecast class mechanism and require the same internal structure., or if not, how they are different. I have not been able to find, in fpp3 or elsewhere, anything that either describes a change or contains a comparable description of objects of class forecast.
I’m going to be embarrassed if there is some simple function,
you_should_know_this_dummy(package = “forecast”, class = “forecast”),
that returns a detailed description of the class. But I have looked for such a function every way I could think of and not found it.
O.K., my bad. I was trying so hard to find a way of locating the help file for the class description (which I don't think exists) that I overlooked the existence of a pretty good description of the class forecast under the function forecast() in the manual for the package forecast. Here it is:
An object of class "forecast" is a list usually containing at least the following elements:
model A list containing information about the fitted model
method The name of the forecasting method as a character string
mean Point forecasts as a time series
lower Lower limits for prediction intervals
upper Upper limits for prediction intervals
level The confidence values associated with the prediction intervals
x The original time series (either object itself or the time series used to create the model stored as object).
residuals Residuals from the fitted model. For models with additive errors, the residuals will be x minus the fitted values.
fitted Fitted values (one-step forecasts)
This still leaves some questions unanswered, like the format for the model information argument model, and for the x argument with multivariate models. But I am hoping that these are similar to those handed to or returned by, e.g., lm(). I think this gives me enough to get started and to hope for informative errors.
I still don't know if the fable package also uses objects of class forecast. The forecast package documents the forecast() function as a generic. The fable package does not document the generic, though it has a very similar list of functions that look like methods, e.g., forecast.whatever. If I figure out the answer, I'll post it here.
I am also looking for a number of other package that provide time series forecast of particular types. I'm hoping that they provide output similar enough that I can use the forecast/fable functions for display, cross-validation, and so forth. We'll see.

Using {gtsummary} to display confidence intervals for survey.design object?

{gtsummary} has the tbl_svysummary() function for producing summary statistics tables from survey.design objects created by the {survey} package. The {gtsummary} website provides an example of how to add confidence intervals for tbl_summary(), by defining custom functions for calculating CIs which are then passed to the statistics = argument in tbl_summary().
However, the documentation for tbl_svysummary() noted that "Unlike tbl_summary(), it is not possible to pass a custom function." I'm using a survey.design object since I'm applying weighting to my data, but I really like the output of {gtsummary}, so it would be great if I could find a way to add confidence intervals as I need to show these for reporting.
Any suggestions on how to achieve this, or is it not possible?
I am sorry to report that it is currently not possible. The way one would go about it using the add_stat() function (example here How to generate effect size [90%CI] in the summary table using R package “gtsummary”?). But that function has not yet been generalized to work with tbl_svysummary() objects.
I had never considered generalizing it until now, so thank you very much for your question. I opened a GitHub Issue to track implementation progress. You can subscribe to the issue to be notified when it is complete.
https://github.com/ddsjoberg/gtsummary/issues/688
Happy Programming!

"vars" argument in plot function in Julia?

I'm looking at the Julia package "DifferentialEquations.jl", a differential equation solver package in Julia. In the tutorial page , there is an example solving the Lorenz equations and plotting the Lorenz butterfly. To plot the Lorenz butterfly, the command used is
plot(sol, vars=(1,2,3))
I understand roughly that the "vars" specifies the data used as the 3 axes. However, I never found this "vars" in the documentation of "plot", or of "DifferentialEquations.jl". What is it logically? Seems the function "plot" has many keys but "vars" is not one of them.
Any idea where I could find it?
For example, if I try:
t=[0.0:0.1:2*pi;]
points = [sin(t), cos(t)]
plot(points, vars=(1,2))
it doesn't work.
It is defined in DifferentialEquations.jl - the package has used what we call a "recipe" to overload Plots' plot function to take a new keyword if the object passed to plot is a Solution. You can find the DiffEq-specific plot keywords documented here: https://diffeq.sciml.ai/stable/basics/plot/

How to retrieve R function script from a specific package?

I realize there are generic functions like plot, predict for a list of packages. I am wondering how could I get the R script of these generic functions for a specific package, like the lme4::predict. I tried lme4::predict, but it comes with error:
> lme4::predict
Error: 'predict' is not an exported object from 'namespace:lme4'
Since you state that my suggestion above was helpful I will tell you my process. I used my own co-authored package called pacman. This package was developed because we had a hard time remembering all the obscurely named functions to get information on and work with add on packages.
I used this to figure out what you wanted:
library(pacman)
p_funs(lme4, all=TRUE)
I set all = TRUE as predict is a method for a specific class (like print, summary and plot). Generally, these methods are not exported so p_funs won't return them unless you set all = TRUE. Then I scrolled down to the p section and found only a single predict method: predict.merMod
Next I realized it wasn't exported so :: won't show me the stuff and extra colon power is needed, hence: lme4:::predict.merMod
As pointed out by David and rawr above, some functions can be suborn little snippets (methods etc.), thus methods and getAnywhere are helpful.
Here is an example of that:
library(tm)
dissimilarity #The good stuff is hid
methods(dissimilarity) #I want the good stuff
getAnywhere("dissimilarity.DocumentTermMatrix")
Small end note
And of course you don't need pacman to look at functions for packages, it's what I use and helpful but it just wraps base R things. Use THE SOURCE to figure out exactly what.

Get "hidden" information in plot() and summary()

I am using the ABC package in R which computes several statistics that can be plotted. Using
plot( the results as matrix from another function in the package ) or
summary( the results as matrix from another function in the package )
several plots/statistics are displayed.
I am interested to get the maximum value of one of the graphs that is displayed. However, the values of the plotted graphs are not returned or used in the input matrix.
How can I get them, or how can I see what function was applied to construct the graph?
Generally, to view the code for S3 methods, you would type the generic method, followed by a dot. followed by the S3 class. For example, to view the code dispatched on a glm object by the method summary:
summary.glm
Same rule applies for the abc package, however it seems that the authors have not exported their methods into the namespace. Thus, you have to specify the abc package namespace. Try:
abc:::summary.abc
abc:::plot.abc
abc:::hist.abc
for summary(), plot(), hist(), respectively.
As Joshua notes, the str() function is helpful to view how data is stored in an object. For example, after running example(abc) to generate the examples from the abc vignette, the object lin2 is produced, which is of class abc. Trying str(lin2) shows how the data is stored. Then if you wanted to see the adjusted values for lin2, you could try lin2$adj.values.

Resources