Skip to content

Writing do and resample functions

5 messages · Erich Neuwirth, Ben Ward, John Sorkin +1 more

#
Hi,

I'm trying to take a function from a workspace download provided in a 
stats textbook book, so I have it in my workspace to use all the time.

I opened the workspace and typed the names of the two functions to get 
the code that makes them up:
-------------------------------------------------------------------------------------
 > resample
function(d, size, replace=TRUE,prob=NULL,within=NULL) {
   if (!is.null(within)) return( resample.within(d, 
within,replace=replace) )
   if (is.null(dim(d))) {
      # it's just a vector
      if (missing(size)) size=length(d)
         return( d[ sample(1:length(d),size, replace=replace, prob=prob)])
   }
   else {
      if (missing(size)) size = dim(d)[1];
      inds = sample(1:(dim(d))[1], size, replace=replace, prob=prob)
      if (is.data.frame(d) | is.matrix(d)) {
         return(d[inds,]);
      } else {
         return(d[inds]);
      }
   }
}
-------------------------------------------------------------------------------------
 > do
function(n=10){
   as.repeater(n)
}
-------------------------------------------------------------------------------------
 > as.repeater
function(n=5){
   foo = list(n=n)
   class(foo) = 'repeater'
   return(foo)
}

Then I made the functions in my workspace by choosing the name (same 
name), and then "=" and then copied and pasted the function, beginning 
with function( and ending with the final }'s.

But when I try to do the following in my workspace afterwards:

samps = do(500)* coef(lm(MIC.~1+Challenge+Cleaner+Replicate, 
data=resample(ecoli)))
sd(samps)

I get an error:
Error in do(500) * coef(lm(MIC. ~ 1 + Challenge + Cleaner + Replicate,  :
non-numeric argument to binary operator.

But in the workspace that comes with that book, I get a decent output:

sd(samps)
     (Intercept)       Challenge   CleanerGarlic  ReplicateFirst 
ReplicateFourth
       3.9455401       0.7178385       1.6830641       5.4564926       
5.4320998
ReplicateSecond  ReplicateThird
       5.3895562       5.5422622

Is there anybody out there who know a lot more about programming 
functions in R than I do, that might know why this is giving me the 
error? I don't understand why one workspace would accept the model 
formula, when the other give me the non-numeric argument to binary 
vector, the only vector that's not numerical is Replicate, but I don't 
that's what the error is talking about.

Thanks,
Ben Ward.
#
It seems that the textbook workspace you are using as a method
definition for * for class repeater. Load the workspace and try
methods(`*`)
If you get something like '*.repeater' this suspicion is confirmed
On 1/4/2011 11:36 AM, Ben Ward wrote:
#
I've just loaded the workspace and done

 > methods('*')
And got the ouptut
[1] *.difftime *.repeater

I'm not entirely sure on what this repeater and as.repeater is. From my 
reading when one want to repeat stuff for say bootstrapping, you have to 
use repeat as part of a function and put in some sort of qualification 
that end the repetitions. Coding functions is a bit beyond what is 
taught on my BSc Biology course. However, I do know that if I do the 
same in a new R session:

 > methods('*')
[1] *.difftime

*.repeater is missing. So to clarify my understanding, the * is the 
problem because it hasn't got this .repeater value attributed to it?

Thanks,
Ben Ward.
On 04/01/2011 12:30, Erich Neuwirth wrote:
#
(1) I know that \n when used in cat, e.g. cat("\n") produces a line feed (i.e. skips to the next line). Is there any escape sequence that will go to the top of the next page?
(2) I know that control L will clear the console. Is there an equivalent function or other means that can be used in R code to clear the console?

Thanks,
John  


John David Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)

Confidentiality Statement:
This email message, including any attachments, is for th...{{dropped:6}}
#
On Tue, Jan 4, 2011 at 9:48 AM, John Sorkin <jsorkin at grecc.umaryland.edu> wrote:
Its always a good idea to look through the archives before posting.  I
had posted this 5 years ago.  It uses RDCOMClient:

http://www.mail-archive.com/r-help at stat.math.ethz.ch/msg57802.html

and also posted a second version using rcom in the same thread.