Re: [R] R and SAS proc format
The down side to R's factor solution: The numerical values of factors are always 1 to number of levels. Thus, it can be tough and requires great care to work with studies that have both numerical values different from this and value labels. This situation is currently not well-supported by R. Regards, Ulrike P.S.: I fully agree with Frank regarding the annoyance one sometimes encounters with formats in SAS! You can add an attribute to a variable. In the sas.get function in the Hmisc package for example, when importing SAS variables that have PROC FORMAT value labels, an attribute 'sas.codes' keeps the original codes; these can be retrieved using sas.codes(variable name). This could be done outside the SAS import context also. Frank -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University Frank, are these attributes preserved when merging or subsetting a data frame? Are they used in R packages other than Hmisc and Design (e.g. in a simple table request)? If this is the case, my wishlist items 8658 and 8659 (http://bugs.r-project.org/cgi-bin/R/wishlist?id=8658;user=guest, http://bugs.r-project.org/cgi-bin/R/wishlist?id=8659;user=guest) can be closed. Otherwise, I maintain the opinion that there are workarounds but that R is not satisfactorily able to handle this type of data. Regards, Ulrike --- End of Original Message --- [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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.
[R] rpart-question regarding relation between cp and rel error
Dear useRs, I may be temporarily (I hope :-)) confused, and I hope that someone can answer this question that bugs me at the moment: In the CP table of rpart, I thought the following equation should hold: rel error = rel error(before) - (nsplit - nsplit(before)) * CP(before), where (before) always denotes the entry in the row above. While this equation holds for many rows of the CP tables I've looked at, it doesn't hold for all. For example, in the table below, 0.67182 != 0.68405 - (47-38)*0.0010616, with a difference of 0.002676 which appears larger than just numerical inaccuracy. CP nsplit rel error xerror xstd 1 0.1820909 0 1.0 1.0 0.012890 2 0.0526194 1 0.81791 0.81768 0.012062 3 0.0070390 2 0.76529 0.76529 0.011780 4 0.0043850 4 0.75121 0.77660 0.011842 5 0.0036157 5 0.74683 0.77106 0.011812 6 0.0032310 8 0.73598 0.77083 0.011810 7 0.0026541 9 0.73275 0.77083 0.011810 8 0.0025387 14 0.71936 0.76829 0.011796 9 0.0016155 16 0.71429 0.76644 0.011786 10 0.0013847 20 0.70759 0.76206 0.011761 11 0.0011539 28 0.69605 0.76621 0.011785 12 0.0010616 38 0.68405 0.76875 0.011799 13 0.0010001 47 0.67182 0.76991 0.011805 14 0.001 57 0.66144 0.77060 0.011809 Can someone explain why/when this happens? Regards, Ulrike -- View this message in context: http://www.nabble.com/rpart-question-regarding-relation-between-cp-and-rel-error-tf3356652.html#a9335690 Sent from the R help mailing list archive at Nabble.com. __ R-help@stat.math.ethz.ch 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.
Re: [R] R and SAS proc format
The down side to R's factor solution: The numerical values of factors are always 1 to number of levels. Thus, it can be tough and requires great care to work with studies that have both numerical values different from this and value labels. This situation is currently not well-supported by R. Regards, Ulrike P.S.: I fully agree with Frank regarding the annoyance one sometimes encounters with formats in SAS! lamack lamack wrote: Dear all, Is there an R equivalent to SAS's proc format? Best regards J. Lamack _ O Windows Live Spaces é seu espaço na internet com fotos (500 por mês), blog e agora com rede social http://spaces.live.com/ __ R-help@stat.math.ethz.ch 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. -- View this message in context: http://www.nabble.com/R-and-SAS-proc-format-tf3357624.html#a9340323 Sent from the R help mailing list archive at Nabble.com. __ R-help@stat.math.ethz.ch 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.
[R] [R-pkgs] relaimpo: CRAN package for relative importance in linear regression
Dear useRs, the R-package relaimpo is now mature enough to be advertised on this list: Version 1.1 is now on CRAN. relaimpo allows to assess relative importance of predictors in linear regression using different metrics, the most important of which is lmg, which partitions R-squared based on averaging of orders of regressors (like hier.part). Since relaimpo makes use of the specifics of linear regression, it is much faster than hier.part and also offers bootstrap intervals for all metrics. (However, relaimpo does not replace hier.part, since hier.part covers more goodness-of-fit measures and also generalized linear regression.) For non-US users (restriction due to a potential patent restriction on pmvd), relaimpo is also available in a non-US version that additionally includes the metric pmvd, an alternative way of partitioning R-squared based on weighted averages over orders of regressors. This version can be downloaded from my website. User feedback is very welcome! Regards, Ulrike ** Prof. Dr. Ulrike Grömping Fachbereich II TFH Berlin Luxemburger Str. 10 13353 Berlin mail: [EMAIL PROTECTED] www: www.tfh-berlin.de/~groemp/ ** [[alternative HTML version deleted]] ___ R-packages mailing list [EMAIL PROTECTED] https://stat.ethz.ch/mailman/listinfo/r-packages __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Reading a list of mixed type values
Dear helpeRs, I have a column of various different entry types denoting missing value codes for a data frame. Elements can be numbers (e.g. 9), combinations of numbers e.g. in the form c(0,9,99), or NULL (for no valid missing data code). I used codes-read.table(paste (path2, MissingCodes.csv,sep=),header=FALSE,as.is=T) codes - as.list(codes$V1) to read the codes and transform them into a list, which unfortunately is a list of character entries: codes - as.list(c(NULL, 9, 999, c(0,9), c(999,99))) ### reproducible mini version of what I get I do not know how to transform these to the numeric versions I need. With codesnum-lapply(codes,function(obj){if (!is.na(as.double(obj))) as.double(obj) else { if (obj==NULL) NULL else obj)}}) I can handle everything but the c(0,9) type characters. I have without success tried various ways to get these into numeric versions (c(0,9)). Any suggestions ? There might even be something quite simple around? Thanks and regards, Ulrike __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] hierarchical partitioning
Dear Meike, a variable with negative entry for J in IJ (I cannot become negative) explains more in the joint model than it can explain when alone in the model. You can find discussions of such phenomena e.g. under the key word suppression or suppressor variables. And by the way, as you are using hier.part, you might also be interested in the package relaimpo (linear model with R-squared only, but more different metrics for partitioning). Regards, Ulrike ** Prof. Dr. Ulrike Grömping Fachbereich II TFH Berlin Luxemburger Str. 10 13353 Berlin www: www.tfh-berlin.de/~groemp/ __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] R CMD check: problems possibly from mapply?
Dear expeRts, I am trying to wrap up a package utilities (for my internal use). After adding a function datNAtreat that uses mapply, R CMD check gives WARNINGs for S3 generic/method consistency, checking replacement functions and checking foreign function calls, all of which are accompanied by the following error message: Error in .try_quietly : Error: unable to load R code in package 'utilities' Execution halted Additionally, I get an ERROR for missing documentation entries (which I don't understand, because this function like all others is documented). R CMD check works without problems when omitting this function. The function itself also works when running the code outside the package. I have now stripped it down (omitted error controls etc., it still works), and can only guess that the R CMD check problems have to do with mapply (with which I am not all that familiar). I include the code (not reproducible) in case someone has an idea what I can do to make this function packageable. (The function as.factora is defined elsewhere in the package and takes the arguments x (a column of a data frame), na.level and na.show (a logical).): ## datNAtreat - function(x, na.levels=NULL, na.show=FALSE) { if (!is.data.frame(x)) x - as.data.frame(x) names - colnames(x) if (is.null(names)) names - paste(x,1:ncol(x),sep=) x - as.data.frame(mapply(as.factora, x, na.level=na.levels, MoreArgs=list(na.show = na.show), SIMPLIFY=FALSE)) colnames(x) - names return(x) } Any suggestions are appreciated, as I have no idea where else to look. With kind regards, Ulrike __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] R CMD check: problems possibly from mapply?
Sorry, just pressing the send button brought the solution :-) The problem has nothing to do with any programming issues, but rather with confusion about directories to use R CMD from. Regards, Ulrike -- Forwarded Message --- From: Ulrike Grömping [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Sent: Tue, 14 Mar 2006 15:25:05 +0100 Subject: R CMD check: problems possibly from mapply? Dear expeRts, I am trying to wrap up a package utilities (for my internal use). After adding a function datNAtreat that uses mapply, R CMD check gives WARNINGs for S3 generic/method consistency, checking replacement functions and checking foreign function calls, all of which are accompanied by the following error message: Error in .try_quietly : Error: unable to load R code in package 'utilities' Execution halted Additionally, I get an ERROR for missing documentation entries (which I don't understand, because this function like all others is documented). R CMD check works without problems when omitting this function. The function itself also works when running the code outside the package. I have now stripped it down (omitted error controls etc., it still works), and can only guess that the R CMD check problems have to do with mapply (with which I am not all that familiar). I include the code (not reproducible) in case someone has an idea what I can do to make this function packageable. (The function as.factora is defined elsewhere in the package and takes the arguments x (a column of a data frame), na.level and na.show (a logical).): ## datNAtreat - function(x, na.levels=NULL, na.show=FALSE) { if (!is.data.frame(x)) x - as.data.frame(x) names - colnames(x) if (is.null(names)) names - paste(x,1:ncol(x),sep=) x - as.data.frame(mapply(as.factora, x, na.level=na.levels, MoreArgs=list(na.show = na.show), SIMPLIFY=FALSE)) colnames(x) - names return(x) } Any suggestions are appreciated, as I have no idea where else to look. With kind regards, Ulrike --- End of Forwarded Message --- ** Prof. Dr. Ulrike Grömping Fachbereich II TFH Berlin Luxemburger Str. 10 13353 Berlin mail: [EMAIL PROTECTED] www: www.tfh-berlin.de/~groemp/ __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] fractional factorial design in R
Roberto Furlan wrote: Hi, i need to create a fractional factorial design sufficient to estimate the main effects. The factors may have any number of levels, let's say any number from 2 to 6. I've tried to use the library conf.design , but i cannot figure out how to write the code. For example, what is the code for a design with 5 factors (2x3x3x5x2) and only main effects not confounded? thanks in advance! Roberto Furlan University of Turin, Italy Hi Roberto, unfortunately, fractional factorial designs typically require all factors to have the same number of levels. Hence, your 2x3x3x5x2 example is not a simple special case of a fractional factorial design. There are some special plans for mixed level designs, but the conf.design function requires all factors to have the same number of levels, as you can also find in its help: p: The common number of levels for each factor. Must be a prime number. ffDesMatrix from package BHH2 is even worse, since it requires all factors to have 2 levels: k: numeric. The number of 2-levels design factors in the designs. (Note that designs with two level-factors only can accommodate a few four- level factors, by combining two main effects plus their interaction for the three degrees of freedom of the 4-level factor. For doing this, I would recommend to have someone plan the design who really knows what he/she is doing.) Regards, Ulrike __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] R: fractional factorial design in R
I think that there is an understandable wish to have the simple orthogonal plans (and be it only for non-experts to be able to analyse the results themselves). For mixed levels, there is e.g. the L36 that should be able to accomodate plans like 2x2x2x3x3x3. Unfortunately, R is not very strong in this arena. If I had more time, I would think about writing a package on comfortably designing experiments supported e.g. by the catalogues of Chen, J., Sun, D.X., and Wu, C.F.J. (1993). (A catalogue of two-level and three-level fractional factorial designs with small runs. International Statistical Review 61, 131-145.) Such a package should also provide the analysis facilities for any design generated with it, once it has been enriched with observed data. (This is a bit different from the typical R spirit, where users are often required to be experts themselves.) If anyone is planning a project like this or wants to make a diploma student work on it I would be interested in contributing. For the moment, if you want to implement main effects plans of the orthogonal sort (e.g. a Taguchi-plan like the L36) you have to use books or tables published on the internet, if you don't want to use expensive software like SPSS - not very comfortable, but possible. For example, you can find the L36 - which would be able to accomodate your 2x2x2x3x3x3 - in http://www.itl.nist.gov/div898/handbook/pri/section3/pri33a.htm. With kind regards, Ulrike In general, a main effects design need not be orthogonal -- the main effects merely need to be estimable. The trick is to estimate them with good efficiency, etc. I think you need to consult a local statistician for help to understand what these statistical concepts mean. In your example you could cross the 2^(3-1) with the 3^(3-1) to produce an orthogonal design to estimate main effects. But of course that's 72 runs, which I don't think you would consider small. As a previous poster commented, there are orthogonal mixed level arrays (Addleman, Kempthorne Youden -designs are a couple of phrases to try googling on) which stem from the 1960's. I doubt that, in general, they would satisfy your needs. I have not used the AlgDesign package myself. I suggest you direct questions about it to the author/maintainer, Bob Wheeler. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA The business of the statistician is to catalyze the scientific learning process. - George E. P. Box -Original Message- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of statistical.model at googlemail.com Sent: Monday, January 23, 2006 12:20 PM To: Berton Gunter; statistical.model at googlemail.com; r-help at stat.math.ethz.ch Subject: [R] R: fractional factorial design in R Yes, you're right. For, say, a 3 x 5 design, one can do this in as few as 7 runs -- but only in general by some version of one-factor-at-a-time (OFAT) designs, which are inefficient. It is easy, via, say model.matrix() to write a general function to produce these. But I think it's a bad idea; more efiicient algorithmic designs are better, IMO, which is why I suggested AlgDesign. You and others are free to disagree, of course. Hi Bert, thanks for your suggestion. However, let us say that i need a 2x2x2x3x3x3 design, which should not be too hard. I've loaded AlgDesign, and i am aware now that gen.factorial allows me to create a full desing. But how to create a main-effects-only factorial design (orthogonal)? I am still not able to produce what i need. The function model.matrix.formula is not very clear... :( Could you please indicate which syntax should i use? I'd really appreciate your help. Thanks in advance, Roberto Furlan University of Turin, Italy La mia Cartella di Posta in Arrivo è protetta con SPAMfighter 188 messaggi contenenti spam sono stati bloccati con successo. Scarica gratuitamente SPAMfighter! __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Time delay function or plot animation
Tobias, thanks to you and others, who made the same suggestion. This is exactly what I was looking for, Sys.sleep(0.1) does the job much more quietly than my previous solution ... Regards, Ulrike -- Original Message --- From: Tobias Verbeke [EMAIL PROTECTED] To: Ulrike Grömping [EMAIL PROTECTED] Cc: r-help@stat.math.ethz.ch Sent: Mon, 12 Dec 2005 21:14:26 +0100 Subject: Re: [R] Time delay function or plot animation Ulrike Grömping wrote: is it possible to specify a time delay for plotting the points in a curve? I would like to make the plotting process slow enough to show the development of the graph, and therefore I am looking either for the possibility within the plot function to specify a plotting speed or (if that doesn't exist) for a function like pause or wait that allows to specify a time delay until the next statement is executed. I have searched help and mailing list archives, but I don't seem to look for the right keywords. My current solution is very crude: I add the following to an existing plot: for (i in 1:steps) { lines(x[i:(i+1)], y[i:(i+1)]) #calculations with the sole purpose of generating a time delay zoeger-1 for (j in 1:85000) {zoeger-zoeger*j} } Is there a better way to achieve this? See ?Sys.sleep HTH, Tobias Regards, Ulrike __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting- guide.html --- End of Original Message --- __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Time delay function or plot animation
Dear R-help crowd, is it possible to specify a time delay for plotting the points in a curve? I would like to make the plotting process slow enough to show the development of the graph, and therefore I am looking either for the possibility within the plot function to specify a plotting speed or (if that doesn't exist) for a function like pause or wait that allows to specify a time delay until the next statement is executed. I have searched help and mailing list archives, but I don't seem to look for the right keywords. My current solution is very crude: I add the following to an existing plot: for (i in 1:steps) { lines(x[i:(i+1)], y[i:(i+1)]) #calculations with the sole purpose of generating a time delay zoeger-1 for (j in 1:85000) {zoeger-zoeger*j} } Is there a better way to achieve this? Regards, Ulrike __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Method for $
Does that mean, then, that the help page's x$name just accidentally coincides with the arguments x and name that the $-function requires in S4? Regards, Ulrike -- Original Message --- From: Prof Brian Ripley [EMAIL PROTECTED] To: Ulrike Grömping [EMAIL PROTECTED] Cc: Duncan Temple Lang [EMAIL PROTECTED], r-help@stat.math.ethz.ch Sent: Tue, 22 Nov 2005 08:39:32 + (GMT) Subject: Re: [R] Method for $ On Fri, 18 Nov 2005, Ulrike Grömping wrote: From: Duncan Temple Lang [EMAIL PROTECTED] [...] For the record, the problem you are experiencing is that you are not using the same argument names in your method as the function $ uses. Hence there is a mismatch. Use setMethod($, signature(x=myclass), function(x, name){ slot(x, name) } ) i.e. rather than slotname as the second argument, use name as that is what $ uses. Thanks very much for clarifying that. I just wasn't aware of the argument names of $. It's obvious from the help file, once you know what to look for, but ... `For the record', this is rather misleading. $ is a primitive function in R, and does _not_ have argument names (it uses only positional matching like all primitive functions). To check this, try xx - list(a=1, b=2) `$`(y=xx, zz=b) which is perfectly valid R and works just like xx$b. Hence there is no reason for the help page to specify argument names (and it does in fact explain that they are not used). However, the S4 model needs argument names, and so when setting S4 methods for primitive functions one needs to use the argument names which it _assumes_. Sometimes the only way to find those out is to read the code. They are in src/library/methods/R/BasicFunsList.R which contains $ = function(x, name) { name - as.character(substitute(name)) standardGeneric($) } S3 methods for primitives do not need to match argument names, fortunately as Duncan TL has just added an S3 method with the (mis-matching) arguments args(`$.DLLInfo`) function (x, i, ...) Since there are no argument names for the generic, the QC tools do not check such cases. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595 --- End of Original Message --- [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Method for $
Dear R experts, I have defined a class myclass and would like the slots to be extractable not only by @ but also by $. I now try to write a method for $ that simply executes the request [EMAIL PROTECTED], whenever someone calls object$slotname for any object of class myclass. I don't manage to find out how I can provide this function with slotname, so that one and the same function works for any arbitrary slotname a user might choose. I have tried setMethod($, signature(x=myclass), function(x,slotname){ [EMAIL PROTECTED] } ) This produced the error message: In method for function $: expanding the signature to include omitted arguments in definition: name = missing Error in rematchDefinition(definition, fdef, mnames, fnames, signature) : methods can add arguments to the generic only if '...' is an argument to the generic My searches for a solution of this problem have not been successful. Can someone help? Thanks and regards, Ulrike Grömping, Berlin __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Method for $
Uwe, thank thank you very much for your suggestion. This does in fact work, if I define $.myclass-function(x,slotname){slot(x,slotname)} which solves my immediate need. However, the setMethod version setMethod($, signature(x=myclass), function(x,slotname){ slot(x,slotname) } ) still produces the same error message as before (cf. below). If anyone has a solution for this, I would appreciate it. Uwe, my best wishes also to Dortmund and Lena, and by the way, it's your book that got me started with understanding (part of) the programming aspects of R relatively fast. Regards, Ulrike Grömping -- Original Message --- From: Uwe Ligges [EMAIL PROTECTED] To: Ulrike Grömping [EMAIL PROTECTED] Cc: r-help@stat.math.ethz.ch Sent: Fri, 18 Nov 2005 17:45:40 +0100 Subject: Re: [R] Method for $ Ulrike Grömping wrote: Dear R experts, I have defined a class myclass and would like the slots to be extractable not only by @ but also by $. I now try to write a method for $ that simply executes the request [EMAIL PROTECTED], whenever someone calls object$slotname for any object of class myclass. I don't manage to find out how I can provide this function with slotname, so that one and the same function works for any arbitrary slotname a user might choose. I have tried setMethod($, signature(x=myclass), function(x,slotname){ [EMAIL PROTECTED] } ) Ulrike, what about (untested!): slot(x, slotname) Best wishes from Dortmund and Lena, Uwe Ligges This produced the error message: In method for function $: expanding the signature to include omitted arguments in definition: name = missing Error in rematchDefinition(definition, fdef, mnames, fnames, signature) : methods can add arguments to the generic only if '...' is an argument to the generic My searches for a solution of this problem have not been successful. Can someone help? Thanks and regards, Ulrike Grömping, Berlin __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting- guide.html --- End of Original Message --- __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Method for $
I have had that thought myself as well, but don't see any harm in my particular application. I describe in general terms what I'm trying to do: I have defined a class for the output of a function that does various calculations. The reason for this is that I want methods for plotting and printing for the output object. On the other hand, the output object does include various sub-objects the access of which is useful for users (basically, the object was a list of several vectors and numbers, before I decided that I need a class, and in the class definition, I have defined the elements of the list as slots). Maybe I could (and should?) have defined the class with just one slot that contains the list, which would make it behave like I want it immediately. Regards, Ulrike Grömping -- Original Message --- From: Martin Maechler [EMAIL PROTECTED] To: Ulrike Grömping [EMAIL PROTECTED] Cc: r-help@stat.math.ethz.ch Sent: Fri, 18 Nov 2005 18:29:27 +0100 Subject: Re: [R] Method for $ Ulrike == Ulrike Grömping [EMAIL PROTECTED] on Fri, 18 Nov 2005 15:37:58 +0100 writes: Ulrike Dear R experts, Ulrike I have defined a class myclass and would like the Ulrike slots to be extractable not only by @ but also by $. hmm, I know this is not what you've asked for, but why would you want to do that? To me, it seems a pretty harmful idea that only leads to confusion and does not provide sensible functionality. When S4 classes where created, using '@' instead of '@' with a *different* meaning was very much on purpose and I think it's a bit too ambitious to try being smarter than John Chambers, the creator of the S4 object system, on this. Martin Maechler, ETH Zurich Ulrike I now try to write a method for $ that Ulrike simply executes the request [EMAIL PROTECTED], whenever someone calls Ulrike object$slotname for any object of class myclass. Ulrike .. Ulrike .. --- End of Original Message --- [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Method for $
-- Original Message --- From: Duncan Temple Lang [EMAIL PROTECTED] To: Ulrike Grömping [EMAIL PROTECTED] Cc: Uwe Ligges [EMAIL PROTECTED], r-help@stat.math.ethz.ch Sent: Fri, 18 Nov 2005 10:26:51 -0800 Subject: Re: [R] Method for $ -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Ulrike Grömping wrote: Uwe, thank thank you very much for your suggestion. This does in fact work, if I define $.myclass-function(x,slotname){slot(x,slotname)} which solves my immediate need. However, the setMethod version setMethod($, signature(x=myclass), function(x,slotname){ slot(x,slotname) } ) still produces the same error message as before (cf. below). If anyone has a solution for this, I would appreciate it. For the record, the problem you are experiencing is that you are not using the same argument names in your method as the function $ uses. Hence there is a mismatch. Use setMethod($, signature(x=myclass), function(x, name){ slot(x, name) } ) i.e. rather than slotname as the second argument, use name as that is what $ uses. Thanks very much for clarifying that. I just wasn't aware of the argument names of $. It's obvious from the help file, once you know what to look for, but ... But, as Martin pointed out, this is not an advisable thing to do. And if you want to use an S4 class to house sub-elements, either explicitly specify their types with setClass('myclass', representation( a = integer, b = matrix, ) or if you want them to be arbitary objects, then think about why you are using S4. I have in fact used a class definition like the one you suggest, i.e. have switched from a list to a class by defining an object type for each of my list elements. As a consequence, the former list elements were no longer accessible via $, which is what I want to recover. Uwe, my best wishes also to Dortmund and Lena, and by the way, it's your book that got me started with understanding (part of) the programming aspects of R relatively fast. Regards, Ulrike Grömping -- Original Message --- From: Uwe Ligges [EMAIL PROTECTED] To: Ulrike Grömping [EMAIL PROTECTED] Cc: r-help@stat.math.ethz.ch Sent: Fri, 18 Nov 2005 17:45:40 +0100 Subject: Re: [R] Method for $ Ulrike Grömping wrote: Dear R experts, I have defined a class myclass and would like the slots to be extractable not only by @ but also by $. I now try to write a method for $ that simply executes the request [EMAIL PROTECTED], whenever someone calls object$slotname for any object of class myclass. I don't manage to find out how I can provide this function with slotname, so that one and the same function works for any arbitrary slotname a user might choose. I have tried setMethod($, signature(x=myclass), function(x,slotname){ [EMAIL PROTECTED] } ) Ulrike, what about (untested!): slot(x, slotname) Best wishes from Dortmund and Lena, Uwe Ligges This produced the error message: In method for function $: expanding the signature to include omitted arguments in definition: name = missing Error in rematchDefinition(definition, fdef, mnames, fnames, signature) : methods can add arguments to the generic only if '...' is an argument to the generic My searches for a solution of this problem have not been successful. Can someone help? Thanks and regards, Ulrike Grömping, Berlin __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting- guide.html --- End of Original Message --- __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html - -- Duncan Temple Lang [EMAIL PROTECTED] Department of Statistics work: (530) 752-4782 371 Kerr Hall fax: (530) 752-7099 One Shields Ave. University of California at Davis Davis, CA 95616, USA -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.2 (Darwin) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFDfhzr9p/Jzwa2QP4RAnoPAKCAeNsDpbAC52kfJnmaBdLeGPUTgwCfTqBF Ffg0eMA//XcfzY3kjBqGJ8c= =iO7y -END PGP SIGNATURE- --- End of Original Message --- [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Method for $
Berton, thanks for your code, it does work on my problem. However, I do want the $ do work the extraction, and it does if I do it right (see answer from Duncan Temple Lang). Regards, Ulrike -- Original Message --- From: Berton Gunter [EMAIL PROTECTED] To: 'Uwe Ligges' [EMAIL PROTECTED], 'Ulrike Grömping' [EMAIL PROTECTED] Cc: r-help@stat.math.ethz.ch Sent: Fri, 18 Nov 2005 09:42:12 -0800 Subject: RE: [R] Method for $ I believe that a recommended S4 convention is simply to write methods for slot extraction with the same name as the slot. Thus, for an object,x of class 'myclass' slotname(x) would give [EMAIL PROTECTED] This is a more natural interface than either @ or $ (and I am not sure that methods for $ can be defined due to S3/S4 class consistency details). Obviously, it is trivial, but a bit tedious, to manually write such methods for any class you care to. However, I wrote a small generator function that automatically generates the slot extraction methods for any class if you wish to do things this way. I just run this right after I've define a new class,'myclass', to generate slot access methods by: slotget('myclass') The code is below. I would appreciate feedback on any errors or suggestions for improvement. Cheers, -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA slotGet-function(x,where=topenv()) #automatically generates accessor and replacement methods for class x { if(!isClass(x,where=where))stop(paste('Class',x,'not found')) slots-slotNames(getClass(x,where=where)) for(slt in slots){ ## slot accessor fun-function(object)NULL if(!isGeneric(slt,where=where)){ if(exists(slt,where=where,mode=function)) fun-get(slt,pos=where,mode=function) else body(fun)-substitute(standardGeneric(slt)) setGeneric(slt,fun,where=where) } if(!existsMethod(slt,x,where=where)){ body(fun)-substitute([EMAIL PROTECTED]) setMethod(slt,x,fun,where=where) } } invisible() } -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Uwe Ligges Sent: Friday, November 18, 2005 8:46 AM To: Ulrike Grömping Cc: r-help@stat.math.ethz.ch Subject: Re: [R] Method for $ Ulrike Grömping wrote: Dear R experts, I have defined a class myclass and would like the slots to be extractable not only by @ but also by $. I now try to write a method for $ that simply executes the request [EMAIL PROTECTED], whenever someone calls object$slotname for any object of class myclass. I don't manage to find out how I can provide this function with slotname, so that one and the same function works for any arbitrary slotname a user might choose. I have tried setMethod($, signature(x=myclass), function(x,slotname){ [EMAIL PROTECTED] } ) Ulrike, what about (untested!): slot(x, slotname) Best wishes from Dortmund and Lena, Uwe Ligges This produced the error message: In method for function $: expanding the signature to include omitted arguments in definition: name = missing Error in rematchDefinition(definition, fdef, mnames, fnames, signature) : methods can add arguments to the generic only if '...' is an argument to the generic My searches for a solution of this problem have not been successful. Can someone help? Thanks and regards, Ulrike Grömping, Berlin __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html --- End of Original Message --- __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html