[Rd] Design choice of plot.design for formulas
Dear all, Today I figured out that the formula interface of plot.design is kind of counter intuitive. Suppose the following setting ddf <- expand.grid(a=factor(1:3), b=factor(1:3)) ddf$y <- rnorm(9) plot.design(y ~ a + b, data=ddf) which does what it should do, basically printing the means for the respective levels of the factors. I had to learn that the function does not care at all whether I specify a variable at the LHS or the RHS of the formula. Thus, the following commands are all equivalent plot.design(~ y + a + b, data=ddf) plot.design(a ~ y + b, data=ddf) plot.design(b ~ y + a, data=ddf) A closer look into the code revealed that the function basically looks whether a variable is numeric or a factor. All factors are supposed to be stratification factors, while all numerical variables are supposed to be responses. While the former assumption makes sense, the latter is misleading in conjunction with the formula interface: ddf$z <- sample(3, 9, TRUE) plot.design(y ~ a + z, data=ddf) In my reading that should produce a plot where a and z are regarded as stratification factors, while y is the response. Instead the function regards y and z as responses. So my question: is there a particular reason why the formatting of a variable in a data frame (factor vs. numerical) takes precedence over the specification in the formula interface of plot.design? Is it the case that one cannot specify multiple responses otherwise? In this case, I was wondering whether an approach like in lattice where one can specify multiple responses would be useful: ddf$y.new <- rnorm(9) lattice:::xyplot(y + y.new ~ a, data = ddf, pch = 15) Thanks for your feedback. Kind regards, -Thorn __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Converting expression to a function
Hi John, Here's a somewhat streamlined version of the code: Form2resfun <- function(f, params) { stopifnot(inherits(f, "formula"), length(f) == 3) # Create function body body <- substitute( crossprod(rhs - lhs), list(lhs = f[[2]], rhs = f[[3]]) ) # Create argument list free_params <- setdiff(all.vars(f), names(params)) missing_args <- setNames(rep(list(bquote()), length(free_params)), free_params) args <- as.pairlist(c(missing_args, params)) eval(call("function", args, body)) } # a test tfn <- Form2resfun(y ~ b1 / (1 + b2 * exp(-1 * b3 * t)), c(b1 = 1, b2 = 1, b3 = 1)) tfn y <- c(5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.443, 38.558, 50.156, 62.948, 75.995, 91.972) tfn(y, seq_along(y)) It basically works by generating the call to "function", so you get back a regular function (although I haven't made sure it has the environment that you might expect). A couple of tricks: * bquote() generates a call that represents a missing value * using as.pairlist to make sure that the arugments argument to function is a pairlist - as far as I know this is the only case where you need to care about the difference between lists and pairlists You may also want to chat with Randy Pruim, who seems to be working on similar stuff. Hadley On Sun, Mar 18, 2012 at 12:01 PM, John C Nash wrote: > Previously, I've posted queries about this, and thanks to postings and > messages in > response have recently had some success, to the extent that there is now a > package called > nlmrt on the R-forge project https://r-forge.r-project.org/R/?group_id=395 > for solving > nonlinear least squares problems that include small or zero residual problems > via a > Marquardt method using a call that mirrors the nls() function. nls() > specifically warns > against zero residual problems. > > However, I would still like to be able to convert expressions with example > vectors of > parameters to functions that optim() and related functions can use. The code > below gets > "almost" there, but > > 1) Can the code be improved / cleaned up? > > 2) Can the eval() of the output of the Form2resfun be avoided? > > 3) Can the extraction of the parameter names be embedded in the function > rather than put > separately? > > Off-list responses are likely best at this stage, while the tedious details > are sorted > out. I will post a summary in a couple of weeks of the results. > Collaborations re: this > and the larger package welcome, as there is considerable testing and tuning > to do, but > preliminary experience is encouraging. > > John Nash > > > # - code block --- > rm(list=ls()) # clear workspace > Form2resfun <- function(f, p ) { > cat("In Form2resfun\n") > xx <- all.vars(f) > fp <- match(names(p), xx) # Problem in matching the names of params > xx2 <- c(xx[fp], xx[-fp]) > ff <- vector("list", length(xx2)) > names(ff) <- xx2 > sf<-as.character(f) > if ((length(sf)!=3) && (sf[1]!="~")) stop("Bad model formula > expression") > lhs<-sf[2] # NOTE ORDER formula with ~ puts ~, lhs, rhs > rhs<-sf[3] > # And build the residual at the parameters > resexp<-paste(rhs,"-",lhs, collapse=" ") > fnexp<-paste("crossprod(",resexp,")", sep="") > ff[[length(ff) + 1]] <- parse(text=fnexp) > # want crossprod(resexp) > myfn<-as.function(ff, parent.frame()) > } > # a test > y<-c(5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.443, > 38.558, 50.156, 62.948, 75.995, 91.972) # for testing > t<-1:length(y) # for testing > f<- y ~ b1/(1+b2*exp(-1*b3*t)) > p<-c(b1=1, b2=1, b3=1) > b<-p > npar<-length(b) > for (i in 1:npar){ > bbit<-paste(names(b)[[i]],"<-",b[[i]]) > eval(parse(text=bbit)) > } > tfn<-Form2resfun(f, b) > ans<-eval(tfn(t=t,y=y, b)) > print(ans) > # - end code block --- > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/ __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Problem with table
R version 2.14.0, started with --vanilla > table(c(1,2,3,4,NA), exclude=2, useNA='ifany') 134 1112 This came from a local user who wanted to remove one particular response from some tables, but also wants to have NA always reported for data checking purposes. I don't think the above is what anyone would want. PS. This is on a background of our local desires, which is to have the default action of the table command be to report NA, if present. (It's one of the only commands that we globally override at Mayo.) The user had added only the exclude=2 argument, and the useNA value is our default. The above makes this harder to do without rewriting the command wholesale, which is ok (we've done it before at various times in R and Splus) but we would avoid it if possible. Please no wars about whether this is the "right" decison or not, we've done it for 10+ years and quite firmly believe the extra robustness gained by having NA appear is worth the maintainance bother, correctness being paramount in medical research. We're not trying to convert anyone else, just get feedback on the best way to approach this. Terry T. __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] merge bug fix in R 2.15.0
I would like to add a vote for keeping blank suffixes in merge(), as I routinely use this functionality. An example use case: # using R 2.14.1 # d1 is some data that I've been working on for a while d1 <- data.frame(a=letters[1:10], b=1:10) # d2 is some new data from a collaborator. I want to add one of these # columns to d1, and also check that the existing columns are consistent d2 <- data.frame(a=letters[1:10], b=1:10, c=101:110) # use blank suffix to avoid changing the column names of my # original data frame d3 <- merge(d1, d2, by="a", suffixes=c("", ".new")) all(d3$b == d3$b.new) # if this is FALSE, time to email collaborator d3$b.new <- NULL In real usage d1 would have many more columns than d2, so adding suffixes to d1 would be tedious to undo after the merge. Stephanie Gogarten Research Scientist, Biostatistics University of Washington On 3/19/12 4:00 AM, r-devel-requ...@r-project.org wrote: Message: 12 Date: Sun, 18 Mar 2012 15:48:30 -0400 From: Steve Lianoglou To: Uwe Ligges Cc: Matthew Dowle,r-devel@r-project.org Subject: Re: [Rd] merge bug fix in R 2.15.0 Message-ID: Content-Type: text/plain; charset=ISO-8859-1 Hi Uwe, 2012/3/17 Uwe Ligges: > > > On 15.03.2012 22:48, Matthew Dowle wrote: >> >> >> Anyone? >> >>> Is it intended that the first suffix can no longer be blank? Seems to be >>> caused by a bug fix to merge in R 2.15.0. > > > > Right, the user is now protected against confusing himself by using names > that were not unique before the merge. ... now I'm confused:-) If the user explicitly asks for a NULL/0/empty/whatever suffix, they're not really going to be confusing themselves, right? I actually feel like I do this often, where "this" is explicitly asking to not add a suffix to one group of columns ... I do confuse myself every and now and again, but not in this context, yet. I can see that*this* confusing case is now handled w/ this change (which wasn't before): ## I'm using R-devel compiled back in November, 2011 (r57571) R> d1<- data.frame(a=letters[1:10], b=rnorm(10), b.x=tail(letters, 10)) R> d2<- data.frame(a=letters[1:10], b=101:110) R> merge(d1, d2, by='a', suffixes=c('.x', '.y')) a b.x b.x b.y 1 a -1.52250626 q 101 2 b -0.99865341 r 102 ... ## Let's call this "exhibit A" But if I do this: R> merge(d1, d2, by='a', suffixes=c("", ".y")) I totally expect: a b b.x b.y 1 a -1.52250626 q 101 2 b -0.99865341 r 102 ## Let's call this "exhibit B" ... and not (using R-2.15.0 beta) (exhibit B): Error in merge.data.frame(d1, d2, by = "a", suffixes = c("", ".y")) : there is already a column named 'b' I can take a crack at a patch to keep the "rescue user from surprises" example outlined in "exhibit A," but also letting user accomplish "exhibit B" if there is a consensus of agreement on this particular world view. -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology ?| Memorial Sloan-Kettering Cancer Center ?| Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] bzip2'ed data under data/
Hi, R CMD check PACKAGE_VERSION_tar.gz gives warning: Files not of a type allowed in a ‘data’ directory: ‘tser1.csv.bz2’ ‘tser2.csv.bz2’ Please use e.g. ‘inst/extdata’ for non-R data files which I didn't expect, based on section 1.1.5 (Data in packages) of the Writing R Extensions manual: Tables (`.tab', `.txt', or `.csv' files) can be compressed by `gzip', `bzip2' or `xz', optionally with additional extension `.gz', `.bz2' or `.xz'. However, such files can only be used with R 2.10.0 or later, and so the package should have an appropriate `Depends' entry in its DESCRIPTION file. In this case, I have a Depends: R (>= 2.13.0), and the package was built with R version 2.15.0 beta (2012-03-16 r58769), Platform: x86_64-pc-linux-gnu (64-bit), so I don't understand the warning. Cheers, -- Seb __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] diff(time) vs. difftime?
I just encountered another RTFM problem: With diff(as.POSIXct(...), ...) I was unable to control the units of the results. Examples: > (d.d <- diff(as.POSIXct(c('2012-12-12', '2012-12-13' Time difference of 1 days > (d.h <- diff(as.POSIXct(c('2012-12-12 08:00', '2012-12-12 09:00' Time difference of 1 hours > (d.m <- diff(as.POSIXct(c('2012-12-12 08:00', '2012-12-12 08:01' Time difference of 1 mins > (d.s <- diff(as.POSIXct(c('2012-12-12 08:00:00', '2012-12-12 08:00:01' Time difference of 1 secs > as.numeric(d.d) [1] 1 > as.numeric(d.s) [1] 1 methods('diff') identified the following: [1] diff.Datediff.default diff.POSIXt diff.ts diff.zoo* In looking at the help pages for each of these functions, I found no mention of "difftime" or any other way to control the units. methods('-') [1] -.Date -.POSIXt -.yearmon* -.yearqtr* ?"-.Date" didn't seem helpful on this, either. ?"-.POSIXt" contained a link to "difftime", but I didn't see that until after I read the code for "-.POSIXt" and found "difftime". I humbly beseech ye to consider adding a difftime example to all these help pages. Thanks, Spencer p.s. In case there is any doubt, I very much appreciate all the work that the R Core team has invested in making R what it is today. -- Spencer Graves, PE, PhD President and Chief Technology Officer Structure Inspection and Monitoring, Inc. 751 Emerson Ct. San José, CA 95126 ph: 408-655-4567 web: www.structuremonitoring.com __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] diff(time) vs. difftime?
I've just uncovered an infelicity in as.difftime: > as.difftime(diff(as.POSIXct(c('2012-12-12', '2012-12-13'))), units='hours') Time difference of 1 days Is this a bug in the code or in my understanding? Thanks again for all your hard work in making R the great product it is and in answering so many questions. Best Wishes, Spencer > sessionInfo() R version 2.14.1 (2011-12-22) Platform: i386-pc-mingw32/i386 (32-bit) 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] stats graphics grDevices utils datasets methods base other attached packages: [1] zoo_1.7-7 RODBC_1.3-4WriteXLS_2.1.0 sos_1.3-1 brew_1.0-6 loaded via a namespace (and not attached): [1] grid_2.14.1lattice_0.20-0 tools_2.14.1 I just encountered another RTFM problem: With diff(as.POSIXct(...), ...) I was unable to control the units of the results. Examples: (d.d <- diff(as.POSIXct(c('2012-12-12', '2012-12-13' Time difference of 1 days (d.h <- diff(as.POSIXct(c('2012-12-12 08:00', '2012-12-12 09:00' Time difference of 1 hours (d.m <- diff(as.POSIXct(c('2012-12-12 08:00', '2012-12-12 08:01' Time difference of 1 mins (d.s <- diff(as.POSIXct(c('2012-12-12 08:00:00', '2012-12-12 08:00:01' Time difference of 1 secs as.numeric(d.d) [1] 1 as.numeric(d.s) [1] 1 methods('diff') identified the following: [1] diff.Datediff.default diff.POSIXt diff.ts diff.zoo* In looking at the help pages for each of these functions, I found no mention of "difftime" or any other way to control the units. methods('-') [1] -.Date -.POSIXt -.yearmon* -.yearqtr* ?"-.Date" didn't seem helpful on this, either. ?"-.POSIXt" contained a link to "difftime", but I didn't see that until after I read the code for "-.POSIXt" and found "difftime". I humbly beseech ye to consider adding a difftime example to all these help pages. Thanks, Spencer p.s. In case there is any doubt, I very much appreciate all the work that the R Core team has invested in making R what it is today. -- Spencer Graves, PE, PhD President and Chief Technology Officer Structure Inspection and Monitoring, Inc. 751 Emerson Ct. San José, CA 95126 ph: 408-655-4567 web: www.structuremonitoring.com __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] issue with Rd2pdf and \Sexpr in Rd files
Hello, Sorry to repeat myself, but I was wondering if anyone had taken a look at this. Because of this problem, reference manuals are not being created for many Bioconductor packages (any package where there is a \Sexpr in an .Rd file). Thanks in advance--we appreciate your help very much. Dan On Wed, Mar 14, 2012 at 1:13 PM, Dan Tenenbaum wrote: > Hi, > > The following command: > R CMD Rd2pdf --no-preview --output=./tmp.pdf --title=test genefu-package.Rd > run against this file: > https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/genefu/man/genefu-package.Rd > (username: readonly; password: readonly) > > produces a very verbose error (see below) > with R version 2.15.0 alpha (2012-03-07 r58622). > > The .Rd file has these lines in it: > > Version: \tab \Sexpr{packageDescription("genefu")$Version}\cr > Date: \tab \Sexpr{packageDescription("genefu")$Date}\cr > > If I take these lines out, or take out the \Sexpr part, the Rd2pdf > command will complete successfully. > > Is there some other step I need to run to evaluate the \Sexpr tags > before running Rd2pdf, or is there an issue that needs to be fixed? > > Thanks, > Dan > > Error output: > > Converting Rd files to LaTeX ... > genefu-package.Rd > Creating pdf output from LaTeX ... > Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, : > Running 'texi2dvi' on 'Rd2.tex' failed. > Messages: > /usr/bin/texi2dvi: pdflatex exited with bad status, quitting. > /usr/bin/texi2dvi: see Rd2.log for errors. > Output: > This is pdfTeX, Version 3.1415926-2.3-1.40.12 (TeX Live 2011) > restricted \write18 enabled. > entering extended mode > (/Users/dtenenba/dev/bioc_devel/genefu/man/.Rd2pdf62869/Rd2.tex > LaTeX2e <2011/06/27> > Babel and hyphenation patterns for english, dumylang, nohyphenation, > ge > rman-x-2011-07-01, ngerman-x-2011-07-01, afrikaans, ancientgreek, ibycus, > arabi > c, armenian, basque, bulgarian, catalan, pinyin, coptic, croatian, czech, > danis > h, dutch, ukenglish, usenglishmax, esperanto, estonian, ethiopic, farsi, > finnis > h, french, galician, german, ngerman, swissgerman, monogreek, greek, > hungarian, > icelandic, assamese, bengali, gujarati, hindi, kannada, malayalam, marathi, > or > iya, panjabi, tamil, telugu, indonesian, interlingua, irish, italian, > kurmanji, > lao, latin, latvian, lithuanian, mongolian, mongolianlmc, bokmal, nynorsk, > pol > ish, portuguese, romanian, russian, sanskrit, serbian, serbianc, slovak, > sloven > ian, spanish, swedish, turkish, turkmen, ukrainian, uppersorbian, welsh, > loaded > . > (/usr/local/texlive/2011/texmf-dist/tex/latex/base/book.cls > Document Class: book 2007/10/19 v1.4h Standard LaTeX document class > (/usr/local/texlive/2011/texmf-dist/tex/latex/base/bk10.clo)) > (/Library/Frameworks/R.framework/Resources/share/texmf/tex/latex/Rd.sty > (/usr/local/texlive/2011/texmf-dist/tex/latex/base/ifthen.sty) > (/usr/local/texlive/2011/texmf-dist/tex/latex/tools/longtable.sty) > (/usr/local/texlive/2011/texmf-dist/tex/latex/tools/bm.sty) > (/usr/local/texlive/2011/texmf-dist/tex/latex/base/alltt.sty) > (/usr/local/texlive/2011/texmf-dist/tex/latex/tools/verbatim.sty) > (/usr/local/texlive/2011/texmf-dist/tex/latex/url/url.sty) NOT loading ae > (/usr/local/texlive/2011/texmf-dist/tex/latex/base/fontenc.sty > (/usr/local/texlive/2011/texmf-dist/tex/latex/base/t1enc.def)) > (/usr/local/texlive/2011/texmf-dist/tex/latex/psnfss/times.sty) > NOT loading lmodern > (/usr/local/texlive/2011/texmf-dist/tex/latex/inconsolata/inconsolata.sty > (/usr/local/texlive/2011/texmf-dist/tex/latex/base/textcomp.sty > (/usr/local/texlive/2011/texmf-dist/tex/latex/base/ts1enc.def)) > (/usr/local/texlive/2011/texmf-dist/tex/latex/graphics/keyval.sty)) > (/usr/local/texlive/2011/texmf-dist/tex/latex/graphics/color.sty > (/usr/local/texlive/2011/texmf-dist/tex/latex/latexconfig/color.cfg) > (/usr/local/texlive/2011/texmf-dist/tex/latex/pdftex-def/pdftex.def > (/usr/local/texlive/2011/texmf-dist/tex/generic/oberdiek/infwarerr.sty) > (/usr/local/texlive/2011/texmf-dist/tex/generic/oberdiek/ltxcmds.sty))) > (/usr/local/texlive/2011/texmf-dist/tex/latex/hyperref/hyperref.sty > (/usr/local/texlive/2011/texmf-dist/tex/generic/oberdiek/hobsub-hyperref.sty > (/usr/local/texlive/2011/texmf-dist/tex/generic/oberdiek/hobsub-generic.sty)) > (/usr/local/texlive/2011/texmf-dist/tex/generic/ifxetex/ifxetex.sty) > (/usr/local/texlive/2011/texmf-dist/tex/latex/oberdiek/kvoptions.sty) > (/usr/local/texlive/2011/texmf-dist/tex/latex/hyperref/pd1enc.def) > (/usr/local/texlive/2011/texmf-dist/tex/latex/latexconfig/hyperref.cfg)) > > Package hyperref Message: Driver (autodetected): hpdftex. > > (/usr/local/texlive/2011/texmf-dist/tex/latex/hyperref/hpdftex.def > (/usr/local/texlive/2011/texmf-dist/tex/latex/oberdiek/rerunfilecheck.sty)) > > Package hyperref Warning: Option `hyperindex' has already been used, > (hyperref) setting the option has no effect on input line 356. > > > Package hyp