#StackBounty: #r #namespaces Namespaces and generic functions in R

Bounty: 50

this question is somewhat a follow up on this question.
Consider the following example

set.seed(1)
x <- cumsum(rnorm(10))
y <- stats::arima(x, order = c(1, 0, 0))
length(stats::fitted(y))
[1] 0

So far so good: zero is returned because R does not now how to use stats::fitted on an object of class Arima.

Next in my code, I need one function from the forecast package. I do not attach the package, I just load it using the ::notation.
In my code below I will load it directly using requireNamespace.

requireNamespace("forecast", quietly = TRUE)
length(stats::fitted(y))
[1] 10

And suddenly the same command returns a different result.
I understand why this happens (and I hope I am saying it correctly): by loading the forecastpackage a new method for the generic function fitted (namely fitted.Arima) is loaded into the namespace which results in a different outcome.

For me this behavior is quite annoying: is there any way to choose on specific method for fitted?
I read this chapter but did not figure out how to circumvent this problem.

I also tried to unload the forecast package from namespace, but no success:

unloadNamespace("forecast")
length(stats::fitted(y))
[1] 10

It seems that once I load the package I cannot use the old method of fitted.
I am wondering how to handle these situations.

EDIT

As pointed out in the comments after unloadNamespace("forecast") I get that

isNamespaceLoaded("forecast")
[1] FALSE

But methods fitted still includes fitted.Arima.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.