I need to see the code for the summary function which is used in mirt package to see the factor loading matrix.
I have tried edit(summary) which in return giving me this
function (object, ...)
standardGeneric("summary")
which is not very helpful for me. How can I see the code for summary?
summary is a generic function. So you need to see the class-specific method for whatever type of object you're trying to summary-ize. E.g., see summary.lm (for lm objects). You don't specify the object class you're working with, so it's impossible to say what specific summary function you need to look at.
UPDATE: These are s4 generics, which is a bit more complicated than summary.lm, which is s3. Some quick googling reveals that you can see the relevant s4 methods on the package's Github page. The contents of summary will depend on what specific class you're looking at (and there appear to be four classes in the package):
Exploratory
Mixed
Confirmatory
MultipleGroup
This question and answer will also be helpful for addressing these kinds of questions in general in the future.
Related
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.
I am currently translating some MATLAB scripts to R for Multivariate Data Analysis. Currently I am trying to generate the same data as the Coeffs.Linear and Coeffs.Const part of the fitdiscr function in MATLAB.
The code being used is:
fitcdiscr(data, groups, 'DiscrimType', 'linear');
The data consists of 3 groups.
Unfortunately the R function seems to do the LDA only for two LDs and MATLAB seems to always compare all groups in all constellations. Does anybody have an idea how I could obtain that data?
I suspect you mean information on the implementation of various MATLAB function, which would be doc <functionname> (doc fitcdiscr would yield this documentation page on fitcdscr) to get the documentation, and edit <functionname> to get the implementation, if it is not obscured by The MathWorks. If those two do not give you enough information, I'm afraid you're out of luck, since not all TMW codes are available non-obscured.
fitcdiscr is non-obscured, although very brief; it's just a wrapper for some other functions. Keep doing edit <functionname> and doc <functionname> and see how deep the rabbit hole takes you.
NB: there's no built-in function called fitdiscr, but the syntax you describe is that of fitcdiscr (note the c), so I used that as examples. If the actual function being called is named fitdiscr, it's custom-made and you'll have to spit through its file by edit fitdiscr and hope for the best.
I have a question pertaining to the R presentation by José A. Sánchez-Espigares and Jordi Ocaña entitled "An R implementation of bootstrap procedures for mixed models".
See: http://www.r-project.org/conferences/useR-2009/slides/SanchezEspigares+Ocana.pdf
On slide 22 on the examples they use the function bootstrap() (in the slide it is: sleep.boot=bootstrap(model,B=1000) ).
The only package they reference is lme4 but that package does not contain the bootstrap() function and I get:
Error: could not find function "bootstrap"
Does anybody know what package they are using here?
I am 99% sure that lme4 never had a bootstrap() function, and that it in fact came from code written by the presenters (I have looked for that code on-line before but have never found it). The built-in bootMer function tries to do most of what this presentation describes, but doesn't do everything (e.g. it doesn't include an implementation of a Wild bootstrap).
If you want to use bootMer without specifying a function FUN, you might take a look at confint(.,method="boot") ... it looks like the summary function that the authors used might have been something like
function(x) c(fixef(x),getME(x,"theta"),sigmaREML=sigma(x))
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.
I'm trying to evaluate the residual normality for three objects: one lm() object, one nnet() and one randomForest(). For lm() using the code:
> qqnorm(rstudent(lmodel)); qqline(rstudent(lmodel))"
it worked fine. But for nnet() and RandomForest() I had no success:
> Error in UseMethod("rstudent") :
no applicable method for 'rstudent' applied to an object of class "nnet"
Does somebody have some suggestion to deal with this issue?
rstudent is a so called generic function, this means that rstudent will call a different function for different input objects. In computer science this is known as polymorphism, i.e. rstudent is a polymorphic function. For example, rstudent(lm()) will trigger the function rstudent.lm.
These specific implementation of the generic function need to be written for every object type the function supports, and are not automagically generated. The error you get indicates that there is no specific implementation of rstudent for the output of your neural net function, i.e. No rstudent.nnet.
The solution is to write this specific implementation yourself, or propose its implementation to the package maintainer.