Skip to content

How to modify a S4 function (in the package NADA)

6 messages · Massimo Bressan, Martin Morgan, Sarah Goslee

#
I want to get access to the code of an S4 method in order to possibly modify
a function to accomplish my particular needs: in my case the function in is
cenfit() from the package NADA 

Now, given my reproducible example:

my.ex<-structure(list(TEC = c(0.21, 0.077, 0.06, 0.033, 0.014, 0.0072), LR =
c(0L, 0L, 1L, 0L, 1L, 0L)), .Names = c("TEC", "LR"), class = "data.frame",
row.names = c(NA, -6L))

and the following few lines of code:

library(?NADA)

attach(my.ex)

cenfit(TEC,LR)

giving this output:

  n      n.cen         median       mean      sd 
6.00     2.00              0.033           0.058       0.083

I would like to add one more result to the above, namely ?sum?, very simply
computed as the product of ?n? times ?mean?

Do you think that is possible or convenient?

I?ve been reading that editing a S4 methods needs particular attention as by
using showMethods("cenfit") and then getMethod("cenfit"); in fact, I?ve been
trying that but without much success (i.e. understanding of the cumbersome
output)

can anyone give me some help on how is probably better to proceed in order
to get my new result? 

different alternative solutions or hints or advices are also more than
welcomed 
(I?m pretty new on R and specially on the field of function handling and
customizing)

all the best

max




--
View this message in context: http://r.789695.n4.nabble.com/How-to-modify-a-S4-function-in-the-package-NADA-tp4649586.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi,

Your reproducible example isn't reproducible for me, as I get an error message.

But regardless, the easiest thing to do is write your own wrapper,
something like:

mycenfit <- function(x, y) {
  result <- cenfit(x, y)
  c(result, sum = result$n * result$mean)
}

No need to change anything in the package itself. Incidentally, using
attach() is generally a really bad idea. with() is much safer.

Sarah
On Thu, Nov 15, 2012 at 7:00 AM, maxbre <mbressan at arpa.veneto.it> wrote:
--
Sarah Goslee
http://www.functionaldiversity.org
#
sorry sarah

you are right, my fault, this is the correct reproducible example
(a misinterpretation of LR type occurred in previous reproducible example)

my.ex<-structure(list(TEC = c(0.21, 0.077, 0.06, 0.033, 0.014, 0.0072
), LR = c(FALSE, FALSE, TRUE, FALSE, TRUE, FALSE)), .Names = c("TEC", 
"LR"), row.names = c(NA, -6L), class = "data.frame")

but, nevertheless,  still some problems exists because...

str(my.ex)
library(NADA)

with(my.ex,
     cenfit(TEC,LR)
     )

mycenfit <- function(x, y) {
  result <- cenfit(x, y)
  c(result, sum = result$n * result$mean)
}

with(my.ex, 
           mycenfit(TEC,LR)
           )

giving me the following error message

Error in result$n : $ operator not defined for this S4 class

again pointing to the peculiar structure of S4 class (at least I think to
understand)

thank you for your reply

max



--
View this message in context: http://r.789695.n4.nabble.com/How-to-modify-a-S4-function-in-the-package-NADA-tp4649586p4649598.html
Sent from the R help mailing list archive at Nabble.com.
#
On 11/15/2012 06:10 AM, Sarah Goslee wrote:
actually, cenfit() returns an object

 > xx = cenfit(my.ex$TEC, as.logical(my.ex$LR))

and what you're seeing is the result of the object's 'show' method

 > xx
          n      n.cen     median       mean         sd
6.00000000 2.00000000 0.03300000 0.05836667 0.08350178

You can see the body of the show method with

     selectMethod(show, class(xx))

and methods that are available to work on xx with

     showMethods(class=class(xx), where=search())

Sarah is right that you'd likely want to write your own function, using a 
combination of available methods, e.g.,

censtats <- function(x) {
     s = summary(x)
     c(n = nrow(s), n.cen = nrow(s) - sum(s$n.event), median = median(x),
       mean = mean(x)[["mean"]], sd = sd(x))
}

Martin

  
    
#
Thanks for the clarification, Martin. Since I couldn't reproduce the
example, I didn't get to see what was actually being returned (and
was, admittedly, too lazy to dig through the help).
+ c(0L, 0L, 1L, 0L, 1L, 0L)), .Names = c("TEC", "LR"), class = "data.frame",
+ row.names = c(NA, -6L))
Error in function (classes, fdef, mtable)  :
  unable to find an inherited method for function ?cenfit? for
signature ?"numeric", "integer", "missing"?

Not that I'm worried about it not working, but it's a good idea to
test reproducible examples on a clean session to ensure the best
possible answers.

Sarah
On Thu, Nov 15, 2012 at 9:32 AM, Martin Morgan <mtmorgan at fhcrc.org> wrote:
--
Sarah Goslee
http://www.functionaldiversity.org
#
Ok, this is my finally (hopefully) clean session

my.ex<-structure(list(TEC = c(0.21, 0.077, 0.06, 0.033, 0.014, 0.0072), LR =
c(FALSE, FALSE, TRUE, FALSE, TRUE, FALSE)), .Names = c("TEC","LR"),
row.names = c(NA, -6L), class = "data.frame")

library("NADA")

xx<-with(my.ex,
         cenfit(TEC,LR)
        )

xx

mycenfit <- function(x) {
  s = summary(x)
  c(n = nrow(s), n.cen = nrow(s) - sum(s$n.event), median = median(x),
    mean = mean(x)[["mean"]], sd = sd(x), sum=mean(x)[["mean"]]*length(x))
}

mycenfit(xx)


thank you so much for your help
(I still have much to fully understand S4 methods, R objects and functions)

max



--
View this message in context: http://r.789695.n4.nabble.com/How-to-modify-a-S4-function-in-the-package-NADA-tp4649586p4649613.html
Sent from the R help mailing list archive at Nabble.com.