Dmitriy Dmitriy - 3 months ago 12
R Question

How to hide packages intersection when method called in R

I have one problem with packages intersection.
I use arfima and forecast packages which both have identical name methods (like AIC, BIC, etc).

So, when I try to run code, I have a problem with methods mismatch - R try to use method from the last loaded package. Yes, I can call exported methods by"::", I know. The main problem connected with internal code of these methods - they use own package methods in the methods body without "::". For example, when I try to use run this code:

require(arfima);
require(forecast);

x <- rnorm(100);
fit <- arfima::arfima(x);
summary(fit);


It gives those errors:

> fit <- arfima::arfima(x);
Note: autoweed is ON. It is possible, but not likely,
that unique modes may be lost.
Beginning the fits with 2 starting values.

> summary(fit);
Error in AIC.logLik(logl) :unrealized type (29) in 'eval'


So, this code w/o loaded forecast package works well (I ran it in "clear" R session):

require(arfima);
#require(forecast);

x <- rnorm(100);
fit <- arfima::arfima(x);
summary(fit);

# Note: autoweed is ON. It is possible, but not likely,
# that unique modes may be lost.
# Beginning the fits with 2 starting values.
#
# summary(fit);
#
# Call:
#
# arfima::arfima(z = x)
#
#
# Mode 1 Coefficients:
# Estimate Std. Error Th. Std. Err. z-value Pr(>|z|)
# d.f -0.0208667 0.0770519 0.0779679 -0.27081 0.78653
# Fitted mean -0.0432115 0.0845518 NA -0.51107 0.60930
# sigma^2 estimated as 0.851957; Log-likelihood = 8.51214; AIC = -11.0243; BIC = 282.976
#
# Numerical Correlations of Coefficients:
# d.f Fitted mean
# d.f 1.00 -0.09
# Fitted mean -0.09 1.00
#
# Theoretical Correlations of Coefficients:
# d.f
# d.f 1.00
#
# Expected Fisher Information Matrix of Coefficients:
# d.f
# d.f 1.65


So, is it possible to hide package for the part or executed code? Something like this:

require(arfima);
#require(forecast);

hide(forecast);

x <- rnorm(100);
fit <- arfima::arfima(x);
summary(fit);

unhide(forecast);


or like this:

require(arfima);
#require(forecast);

used(arfima);

x <- rnorm(100);
fit <- arfima::arfima(x);
summary(fit);

unused(arfima);

42- 42-
Answer

Perhaps you should update all your package installations and retry. I don't get any error with your code using a newly installed binary copy of arfima. AIC is generic and when you look at the loaded methods you do see one for objects of class "arfima", so no use of the "::" function should be needed:

> methods(AIC)
[1] AIC.arfima*  AIC.default* AIC.logLik* 
see '?methods' for accessing help and source code

There is a detach function but it's often not enough to completely set aside a package unless its "unload" parameter is set to TRUE, and in this case may be overkill since your diagnosis appears to be incorrect. This shows that strategy to be feasible but I still suspect unnecessary:

require(arfima);
require(forecast);
detach(package:forecast,unload=TRUE)
x <- rnorm(100);
fit <- arfima::arfima(x);
summary(fit); library(forecast)

This shows that there are class specific methods for summary so there should be no errors caused by package confusion there:

> methods(summary)
 [1] summary,ANY-method             summary,DBIObject-method      
 [3] summary,quantmod-method        summary.aov                   
 [5] summary.aovlist*               summary.arfima*               
 [7] summary.Arima*                 summary.arma*                 
 [9] summary.aspell*                summary.check_packages_in_dir*
[11] summary.connection             summary.data.frame            
[13] summary.Date                   summary.default               
[15] summary.ecdf*                  summary.ets*                  
[17] summary.factor                 summary.forecast*       
snipped res

Furthermore you are incorrect in thinking there is a forecast::BIC. With forecast and arfima loaded we see only :

 methods(BIC)
[1] BIC.arfima*

(And there is no suggestion on the Index page of forecast that either AIC or BIC are defined within that package. So any object created by the forecast functions would be handled by AIC.default and would be expected to fail with BIC.