R gamlss::predict.gamlss not an exported object from 'namespace:gamlss' - r

I am experiencing some confusing behavior from gamlss in R. The documentation lists predict.gamlss and ?predict.gamlss returns the function documentation in RStudio. However, the function does not autocomplete when typing out predict.gamlss, and trying to run it returns Error: 'predict.gamlss' is not an exported object from 'namespace:gamlss'. How does that happen? Is the function deactivated somehow? There is a separate function predictAll that does work.
The documentation does state
This function is under development
I am trying to access the function because I am experiencing some confusing results with predict and predictAll.
R version is 4.0.0. gamlss version is 5.1.6.

As far as I can tell, what you describe is expected and is normal S3 method dispatching. The method predict.gamlss is called when you call predict on a object of class gamlss.
Consider the following (from the documentation of ?predict.gamlss)
data(abdom)
aa <- gamlss(y ~ cs(x^.5), data = abdom)
#[1] 371.3931
predict(aa)[610]
Looking at the class of aa:
class(aa)
#[1] "gamlss" "gam" "glm" "lm"
The function is not exported, but the S3 method is registered.
As to the difference between predictAll and predict.gamlss, you'll have to read the documentation (the two are documented together). My guess is that predictAll predict all listed in the what-argument of predict.gamlss.

Related

source code for grow function in randomForest R package

In source code of R randomForest package, I find the following code in grow.R. What's the purpose for UseMethod? Why does function grow not have function definition and just grow.default and grow.randomForest have definition? Is this something related to calling C function in the R package?
grow <- function(x, ...) UseMethod("grow")
grow.default <- function(x, ...)
stop("grow has not been implemented for this class of object")
grow.randomForest <- function(x, how.many, ...) {
y <- update(x, ntree=how.many)
combine(x, y)
}
Also, in the randomForest.R file, I only find the following code. There is randomForest.default.R file too. Why is there no randomForest.randomForest definition like function grow?
"randomForest" <-
function(x, ...)
UseMethod("randomForest")
What's the purpose for UseMethod? Why does function grow not have function definition and just grow.default and grow.randomForest have definition?
I'd suggest reading about S3 dispatch to understand the patterns you see. Advanced R has a chapter on S3. You can also see related questions here on Stack Overflow.
Is this something related to calling C function in the R package?
No.
Why is there no randomForest.randomForest definition like function grow?
This should make sense if you do the recommended reading above. S3 dispatch uses a pattern of function_name.class to call the correct version of the function (method) based on class of the input. You don't give a randomForest object as an input to the randomForest function, so there is no randomForest.randomForest method defined.
grow() does get called on randomForest objects, hence the grow.randomForest() method. Presumably the authors wanted grow() to error early if it gets called on inappropriate input, so the default for other classes is an immediate error, but they still keep dispatch flexible to work with other classes, enabling extensions of the package and nice play with other packages that may have their own grow() implementations.

R - Using patsy.dmatrices() with reticulate

I have a problem of namespace when trying to use function patsy.dmatrices() with the reticulate R package.
Here is a simple reproducible example:
patsy <- import("patsy")
# Data
dataset <- data.frame(Y=rnorm(1000,2.5,1))
# Null model
formula_null <- "I(Y-1) ~ 1"
dmat = patsy$dmatrices(formula_null, data=dataset, NA_action="drop",
return_type="dataframe")
I get the following error:
Error in py_call_impl(callable, dots$args, dots$keywords) :
AttributeError: 'NoneType' object has no attribute 'f_locals'
I think this is associated to the namespace (cf. Namespace issues when calling patsy within a function) which might be fixed by using the eval_env argument of function dmatrices() but I wasn't able to figure out how.
This is quite problematic when we want to use in R the Python statsmodels package which make uses of the patsy package for formulas.
Thanks for your help,
I'm not sure, but I think your guess about namespaces is correct, and this is an unfortunate interaction between patsy and reticulate. By default, patsy tries to look into the caller's scope to evaluate any unrecognized functions/variables in the formula (just like R formula functions do). This requires using Python's stack introspection to peek at the caller's scope. But since the caller is in a different language entirely, this almost certainly isn't going to work.
It is possible to override patsy's normal behavior of reading the caller's namespace, using the eval_env argument to dmatrices. (Docs.) Try this:
dmat = patsy$dmatrices(formula_null, data=dataset, NA_action="drop",
return_type="dataframe",
# New:
eval_env=patsy$EvalEnvironment(c())
)
The idea is that here we create an empty EvalEnvironment object, and tell patsy to use that instead of trying to read the caller's environment.
I'm not familiar with reticulate, so you might need to tweak the above to work – in Python the equivalent would be:
dmat = patsy.dmatrices(formula_null, data=dataset, NA_action="drop",
return_type="dataframe",
eval_env=patsy.EvalEnvironment([])
In particular, if reticulate doesn't convert c() into an empty list, then you'll want to find something that does. (Maybe try patsy$EvalEnvironment(list())?)

What R package is the "Error" function in?

I'm trying to call the Error() function but it says could not find function "Error". I checked the docs and Error does not seem to be a part of R base package. This is a very hard function to search for because "Error" is a very overloaded word. What package is Error() in? For context, I'm running an anova. I'm pretty sure that this isn't a user defined since I see multiple tutorials referencing it without defining.
EDIT:
Here are the tutorials:
https://datascienceplus.com/two-way-anova-with-repeated-measures/ , http://personality-project.org/r/r.guide/r.anova.html#withinone (look at usages of Error() in within sujects/repeated measures anova)
EDIT2:
Here is the model answer from the tutorial. There does not seem to be any information about how the 'Error' function is defined or where it comes from:
model <- aov(wm$iq ~ wm$condition + Error(wm$subject / wm$condition))
The Error() in this case is specifying the error term for the aov function. It's a parameter passed to the function aov() and thus is not a function on its own. I've also tried searching for Error using the package sos, which yields 0 results:
# install.packages("sos")
library(sos)
results <- findFn("Error")
filtered_results <- results[results$Function == 'Error']
nrow(filtered_results)
Output:
[1] 0
You might want to read this Cross Validated post on how to set the Error term within the aov() function.

Where did the forecast.Holtwinters go in R 3.4.3?

I'm using R Studio based on R 3.4.3. However, when I tried to call the forecast.HoltWinters function, R told me that "could not find function "forecast.HoltWinters"". Inspect the installed package (v8.2) told me that it's true, there is no forecast.HoltWinters. But the manual in https://cran.r-project.org/web/packages/forecast/ clearly stated that forecast.HoltWinters is still available.
I have also tried stats::HoldWinters, but it's working wrong. The code run fine on another computer, but it couldn't run at all on mine. Is there any solution?
Here is the code. Book2.csv has enough data to last more than 3 periods.
dltt <- read.csv("book2.csv", header = TRUE)
dltt.ts <- ts(dltt$Total, frequency=12, start=c(2014,4))
dltt.ts.hw <- HoltWinters(dltt.ts)
library(forecast)
dltt.ts.hw.fc <- forecast.HoltWinters(dltt.ts.hw) //Error as soon as I run this line
Fit a HoltWinters model using the HoltWinters function and then use forecast. Its all in the help for HoltWinters and forecast, namely "The function invokes particular _methods_ which depend on the class of the first argument". I'll copy the guts of it here:
m <- HoltWinters(co2)
forecast(m)
Note this will call the non-exported forecast.HoltWinters function, which you should never call directly using triple-colon notation as some may suggest.

Looking into the predict function in R

I am trying to understand how the SVM predict function works when using command ksvm from R package kernlab.
I tried the look into the predict function using the following commands:
methods(class="ksvm")
getAnywhere(ksvm:::predict)
However, I get the following output and not the complete predict function:
A single object matching ‘:::’ ‘ksvm’ ‘predict’ was found
It was found in the following places
package:base
namespace:base
with value
function (pkg, name)
{
pkg <- as.character(substitute(pkg))
name <- as.character(substitute(name))
get(name, envir = asNamespace(pkg), inherits = FALSE)
}
<bytecode: 0x00000000088be4f8>
<environment: namespace:base>
Warning message:
In find(x, numeric = TRUE) :
elements of 'what' after the first will be ignored
Can someone help with how to obtain the complete predict function?
Update 1:
Suggestion from misspelled worked fine on predict function for ksvm in kernlab package but doesn't seem to work on svm in e1071 package.
It throws the following error:
> getMethod("predict", "svm")
Error in getMethod("predict", "svm") :
no generic function found for 'predict'
In general, how to know which get method to use?
You were close. I was able to get the function code with getMethod("predict", "ksvm"). This answer describing S4 method dispatch was helpful. View source code for function
Per your updated question, I can get the source code for predict.svm using the ::: function. Specifically with e1071:::predict.svm. The link above also describes this in the section on S3 method dispatch.
There are at least a couple of things going on here. First is that in the former case you are dealing with S4 objects and S3 objects in the latter. The two systems have different method dispatches and different ways to view the source code. Another wrinkle is that the predict.svm function is an invisible function and can only be viewed either with ::: or getAnywhere().

Resources