[R] Method dispatch
Martin Morgan
mtmorgan at fhcrc.org
Tue Mar 2 01:14:05 CET 2010
On 03/01/2010 03:35 PM, Zhang,Yanwei wrote:
> Dear Martin,
>
> Thanks a lot for your reply. I do have a namespace and it does seem
> to
be a namespace problem. But this time, it's getting even stranger:
>
> My package name is ChainLadder:
>
>> library(ChainLadder)
>
>> sessionInfo()
> R version 2.10.1 (2009-12-14)
> i386-pc-mingw32
>
> locale:
> [1] LC_COLLATE=English_United States.1252
> [2] LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252
> [4] LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
>
> attached base packages:
> [1] splines stats graphics grDevices utils datasets methods
> [8] base
>
> other attached packages:
> [1] ChainLadder_0.1.3-3 systemfit_1.1-4 lmtest_0.9-26
> [4] zoo_1.6-2 car_1.2-16 Matrix_0.999375-33
> [7] lattice_0.17-26 Hmisc_3.7-0 survival_2.35-7
>
> loaded via a namespace (and not attached):
> [1] cluster_1.12.1 grid_2.10.1 tools_2.10.1
>
>
> Then I called the main function to create an object "uni2" of class "MultiChainLadder".
>
>> uni2 <- MultiChainLadder(list(GenIns),
> + fit.method="OLS",
> + extrap=TRUE,
> + model="MCL")
>>
>> class(uni2)
> [1] "MultiChainLadder"
> attr(,"package")
> [1] "ChainLadder"
>
> The summary function defined for this class "MultiChainLadder" works as desired:
>
>> showMethods(summary)
> Function: summary (package base)
> object="ANY"
> object="MultiChainLadder"
> object="sparseMatrix"
>
>> summary(uni2)
> $`Summary Statistics for the Input Triangle`
> Latest Dev.To.Date Ultimate IBNR S.E CV
> 2 5339085 0.98258397 5433719 94633.81 75535.04 0.7981823
> 3 4909315 0.91271120 5378826 469511.29 121698.56 0.2592026
> 4 4588268 0.86605315 5297906 709637.82 133548.85 0.1881930
> 5 3873311 0.79727292 4858200 984888.64 261406.45 0.2654173
>
>
> However, problems come when I call the "show" function defined for this class, which is essentially the same as the "summary" function, since the default "summary" for class "ANY" is called, not my new method:
>
>> getMethod(show,signature="MultiChainLadder")
> Method Definition:
>
> function (object)
> {
> summary(object)
> }
> <environment: namespace:ChainLadder>
>
> Signatures:
> object
> target "MultiChainLadder"
> defined "MultiChainLadder"
>> show(uni2)
> Length Class Mode
> 1 MultiChainLadder S4
>
>
> I can't figure out why the call of "summary" within "show" failed.
> And
what is even stranger is that although the showMethods(summary)
indicates a method is defined for "MultiChainLadder", I could not find
the "summary" function in the namespace "ChainLadder":
Do you importFrom(Matrix, summary) ? I think it is a variant of
https://stat.ethz.ch/pipermail/r-help/2010-February/229523.html
also the S3method(summary, MultiChainLoader) and friend don't make sense
(to me) from what you've described.
Also, you've switched stories in mid stream -- talking first about
summary then show; my advice would be to simplify, e.g., with toy
packages, so that you can more effectively trouble-shoot -- sometimes it
is not such a good thing to have a lot of fish in the barrel.
Martin
>
>> ChainLadder::summary
> Error: 'summary' is not an exported object from 'namespace:ChainLadder'
>> objects(2)
> [1] "ABC" "as.triangle" "auto"
> [4] "BootChainLadder" "chainladder" "coef"
> [7] "cum2incr" "fitted" "fitted.values"
> [10] "GenIns" "GenInsLong" "getLatestCumulative"
> [13] "incr2cum" "Join2Fits" "JoinFitMse"
> [16] "liab" "M3IR5" "MackChainLadder"
> [19] "MCLincurred" "MCLpaid" "Mortgage"
> [22] "Mse" "MultiChainLadder" "MunichChainLadder"
> [25] "plot" "predict" "qincurred"
> [28] "qpaid" "RAA" "residCov"
> [31] "residuals" "rstandard" "vcov"
>
>
>
> I know this is a namespace issue, but I do not know how to fix this. The following is a part of the namespace file, but you can see that I did export the "summary" method in "exportMethods". What did I miss here? Thanks a lot for your help.
>
>
>
> export(MackChainLadder, MunichChainLadder, BootChainLadder, MultiChainLadder)
> export(Join2Fits, JoinFitMse, Mse, residCov)
>
> importFrom(stats, quantile, predict, coef, vcov,
> residuals, fitted, fitted.values, rstandard)
> importFrom(methods, show, coerce)
> importFrom(graphics, plot)
>
> #Classes
> exportClasses(triangles, MultiChainLadder, MultiChainLadderFit,
> MCLFit, GMCLFit, MultiChainLadderMse)
>
> #Methods
> S3method(plot, MackChainLadder)
> S3method(plot, MunichChainLadder)
>
> S3method(summary, MunichChainLadder)
> S3method(summary, BootChainLadder)
>
> exportMethods(predict, Mse, summary, show, coerce,
> "[", "$", "[[", names, coef, vcov, residCov,
> residuals, rstandard, fitted, fitted.values, plot)
>
>
>
>
>
> Wayne (Yanwei) Zhang
> Statistical Research
>> CNA
>
> -----Original Message-----
> From: Martin Morgan [mailto:mtmorgan at fhcrc.org]
> Sent: Monday, March 01, 2010 5:10 PM
> To: Zhang,Yanwei
> Cc: r-help at r-project.org
> Subject: Re: [R] Method dispatch
>
> On 03/01/2010 01:31 PM, Zhang,Yanwei wrote:
>> Dear all,
>>
>> In a package, I defined a method for "summary" using
> setMethod(summary, signature="abc") for my class "abc", but when the
> package is loaded, the function "summary(x)" where x is of class "abc"
> seems to have called the default summary function for "ANY" class.
> Shouldn't it call the method I have defined? How could I get around with
> that? Thanks.
>
> Hi Wayne -- It's hard to tell from what you've written, but this and
> your earlier question on 'S4 issues' sounds like a NAMESPACE problem.
> Are you using a name space? If so, provide additional detail. Also,
> verify that you are only loading your package and not another, e.g., one
> with a 'summary' generic that is being found before yours.
>
> Probably your best bet is to simplify as much as possible -- I have a
> single file in 'pkgA/R', with
>
> setClass("A", "numeric")
> setMethod("summary", "A", function(object, ...) "A")
>
>
>> library(pkgA)
>> summary(new("A"))
> [1] "A"
>> sessionInfo()
> R version 2.10.1 Patched (2010-02-23 r51168)
> x86_64-unknown-linux-gnu
>
> locale:
> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
> [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
> [5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8
> [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
> [9] LC_ADDRESS=C LC_TELEPHONE=C
> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base
>
> other attached packages:
> [1] pkgA_1.0
>
> Martin
>
>
>>
>>
>> Wayne (Yanwei) Zhang
>> Statistical Research
>> CNA
>>
>>
>>
>>
>>
>> NOTICE: This e-mail message, including any attachments and appended messages, is for the sole use of the intended recipients and may contain confidential and legally privileged information.
>> If you are not the intended recipient, any review, dissemination, distribution, copying, storage or other use of all or any portion of this message is strictly prohibited.
>> If you received this message in error, please immediately notify the sender by reply e-mail and delete this message in its entirety.
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
--
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109
Location: Arnold Building M1 B861
Phone: (206) 667-2793
More information about the R-help
mailing list