Re: [Rd] [External] Re: svd() results should have a class

2022-06-23 Thread Gabriel Becker
This does make sense to me, though I admit to not feeling super strongly
about it. Particularly in the light of the precedent from qr().

It would also be "mostly" backwards compatible, as there would not be
methods for the new class for existing code to get hung up on out of the
gate. Particularly, if class(svd(...)) was c("svd", "list") to ensure
existing methods were always hit. I don't have a great sense of the
difference in end behavior between doing that and just having the class e
"svd", though.

Should we add this as a very late addition to the Bug BBQ list for further
discussion?

~G

On Thu, Jun 23, 2022 at 5:08 PM Lenth, Russell V 
wrote:

> Bob,
>
> I'm not talking about using svd as a generic method. I am talking about a
> method FOR svd results, e.g. an S3 method like foo.svd(), for which there
> already exist other methods, say foo.default and foo.qr. Currently if I
> wanted to do
>
> svdobj <- svd(x)
> foo(svdobj)
>
> it would not dispatch correctly because there is no svd class. Instead, it
> would be handled by foo.list if it exists, and it is certainly not clear
> that foo.list would do the right thing.
>
> Russ
>
> Sent from my iPad
>
> On Jun 23, 2022, at 6:53 PM, Robert Harlow  wrote:
>
> 
> Don't have a view on whether it makes sense in base R or not, but WRE
> section 7.1 may be helpful to you:
> https://cran.r-project.org/doc/manuals/R-exts.html#Adding-new-generics.
>
> It's not uncommon for packages to want to make base methods generic and
> the above link provides advice on how to do so.
>
> Bob
>
> On Thu, Jun 23, 2022 at 12:07 PM Lenth, Russell V  > wrote:
> Dear R-Devel,
>
> I noticed that if we run base::svd(x), we obtain an object of class
> "list". Shouldn't there be an "svd" class, in case someone (e.g., me) wants
> to write methods for singular value decompositions? Note that other
> matrix-decomposition routines like qr() and eigen() each return objects
> having those names.
>
> Thanks
>
> Russ Lenth
> russell-le...@uiowa.edu
>
> [[alternative HTML version deleted]]
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> [[alternative HTML version deleted]]
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] [External] Re: svd() results should have a class

2022-06-23 Thread Lenth, Russell V
Duncan et al.,

The real example would be the estimability package, in which there is a generic 
function nonest.basis and methods for classes lm, matrix, and qr. I wanted to 
add a method for class svd except there is no such class.

I agree BTW that the safe thing to do would be to have the returned object be 
of class c("svd", "list").

Best,

Russ

-Original Message-
From: Duncan Murdoch  
Sent: Thursday, June 23, 2022 7:23 PM
To: Lenth, Russell V ; Robert Harlow 

Cc: r-devel@r-project.org
Subject: Re: [Rd] [External] Re: svd() results should have a class

On 23/06/2022 8:07 p.m., Lenth, Russell V wrote:
> Bob,
> 
> I'm not talking about using svd as a generic method. I am talking 
> about a method FOR svd results, e.g. an S3 method like foo.svd(), for 
> which there already exist other methods, say foo.default and foo.qr. 
> Currently if I wanted to do
> 
>  svdobj <- svd(x)
>  foo(svdobj)
> 
> it would not dispatch correctly because there is no svd class. Instead, it 
> would be handled by foo.list if it exists, and it is certainly not clear that 
> foo.list would do the right thing.

I think this would be more convincing if you gave a real example.  You can see 
the existing methods for the "qr" and "eigen" classes using

   methods(class = "qr")
   methods(class = "eigen")

When I do it, I think I'm only seeing methods from base packages, and they are:

   kappa for qr
   solve for qr
   print for eigen

I think the default print method works fine for svd() results. 
kappa.svd would probably make sense, but would need some thought: 
calculating kappa on a matrix and using the help page for kappa to naively 
calculate it from the svd() of that matrix give different results:

   > set.seed(123)
   > X <- matrix(rnorm(25), 5,5)
   > kappa(X)
   [1] 90.71283
   > s <- svd(X)
   > s$d[1]/s$d[length(s$d)]
   [1] 62.68048

Duncan Murdoch

> Russ
> 
> Sent from my iPad
> 
> On Jun 23, 2022, at 6:53 PM, Robert Harlow  wrote:
> 
> 
> Don't have a view on whether it makes sense in base R or not, but WRE section 
> 7.1 may be helpful to you: 
> https://cran.r-project.org/doc/manuals/R-exts.html#Adding-new-generics.
> 
> It's not uncommon for packages to want to make base methods generic and the 
> above link provides advice on how to do so.
> 
> Bob
> 
> On Thu, Jun 23, 2022 at 12:07 PM Lenth, Russell V 
> mailto:russell-le...@uiowa.edu>> wrote:
> Dear R-Devel,
> 
> I noticed that if we run base::svd(x), we obtain an object of class "list". 
> Shouldn't there be an "svd" class, in case someone (e.g., me) wants to write 
> methods for singular value decompositions? Note that other 
> matrix-decomposition routines like qr() and eigen() each return objects 
> having those names.
> 
> Thanks
> 
> Russ Lenth
> russell-le...@uiowa.edu
> 
>  [[alternative HTML version deleted]]
> 
> __
> R-devel@r-project.org mailing list 
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel