Re: [Rd] feature request: optim() iteration of functions that return multiple values
I have done this using attributes: fr <- function(x) { ## Rosenbrock Banana function x1 <- x[1] x2 <- x[2] ans <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 attr(ans, "extra1") <- 1:10 attr(ans, "extra2") <- letters ans } Not sure if this works in your case though. Cheers, Ott On Sat, Aug 5, 2023 at 1:13 AM Martin Becker < martin.bec...@mx.uni-saarland.de> wrote: > For a solution that does not require any change to the original function > being optimized, the following one-liner could be used, which converts > existing functions to functions that return only the first element: > > returnFirst <- function(fun) function(...) do.call(fun,list(...))[[1]] > > Example: > > fr <- function(x) { ## Rosenbrock Banana function >x1 <- x[1] >x2 <- x[2] >ans <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 >list(ans=ans, extra1 = 1:10, extra2 = letters) > } > > fr2 <- returnFirst(fr) > tmp <- optim(c(-1.2,1), fr2) > fr(tmp$par) > > > Am 03.08.23 um 22:21 schrieb Sami Tuomivaara: > > Dear all, > > > > I have used optim a lot in contexts where it would useful to be able to > iterate function myfun that, in addition to the primary objective to be > minimized ('minimize.me'), could return other values such as alternative > metrics of the minimization, informative intermediate values from the > calculations, etc. > > > > myfun <- function() > > { > > ... > > return(list(minimize.me = minimize.me, R2 = R2, pval = pval, etc.)) > > } > > > > During the iteration, optim could utilize just the first value from the > myfun return list; all the other values calculated and returned by myfun > could be ignored by optim. > > After convergence, the other return values of myfun could be finally > extracted and appended into the optim return value (which is a list) as > additional entry e.g.: $aux <- list(R2, pval, etc.), (without 'minimize.me' > as it is already returned as $value). > > > > The usual ways for accessing optim return values, e.g., $par, $value, > etc. are not affected. Computational cost may not be prohibitive either. > Is this feasible to consider? > > > > > > [[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 > [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] hist(..., log="y")
Sorry if this topic has been discussed earlier. Currently, hist(..., log="y") fails with > hist(rexp(1000, 1), log="y") Warning messages: 1: In plot.window(xlim, ylim, "", ...) : nonfinite axis=2 limits [GScale(-inf,2.59218,..); log=TRUE] -- corrected now 2: In title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...) : "log" is not a graphical parameter 3: In axis(1, ...) : "log" is not a graphical parameter 4: In axis(2, at = yt, ...) : "log" is not a graphical parameter The same applies for log="x" > hist(rexp(1000, 1), log="x") Warning messages: 1: In plot.window(xlim, ylim, "", ...) : nonfinite axis=1 limits [GScale(-inf,0.954243,..); log=TRUE] -- corrected now 2: In title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...) : "log" is not a graphical parameter 3: In axis(1, ...) : "log" is not a graphical parameter 4: In axis(2, at = yt, ...) : "log" is not a graphical parameter This applies for the current svn version of R, and also a few recent published versions. This is unfortunate for two reasons: * the error message is not quite correct--"log" is a graphical parameter, but "hist" does not support it. * for various kinds of data it is worthwhile to make histograms in log scale. "hist" is a very nice and convenient function and support for log scale would be handy here. I also played a little with the code, and it seems to be very easy to implement. I am happy to make a patch if the team thinks it is worth pursuing. Cheers, Ott [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] R-Forge > GitHub?
in this thread, I think someone suggested I make the > >>>>> Ecdat and Ecfun packages separate projects on GitHub (though I can't > >>>>> find that suggestion now). This would not be an issue if it were all > >>>>> local without version control. With RStudio managing my interface > with > >>>>> GitHub, it now seems quite tricky. > >>>> I'm 99.999% confident that your life will be much much easier if you > >>>> keep one R package per repository. If you don't, you'll probably be > >>>> very lonely when it comes to tools etc. There are built-in 'git' > >>>> commands, but also git utility tools, for extracting a subset of > >>>> folders/files from git repository into new git repositories. You'll > >>>> still preserve the commit history. I would deal with this in the > >>>> terminal, using the 'git' client and possible some extraction tool. > >>>> > >>>> Also, while you spend time on this, have a look at the commit > >>>> authorship that I mentioned previously. It's nice to have that in > >>>> place later. > >>>> > >>>> After you got the above in place, then .travis.yml and appveyor.yml is > >>>> pretty straightforward (might even be a copy'n'paste). > >>>> > >>>> Finally, I saw you put your credentials in the URL when you cloned. I > >>>> don't think that's safe, your GitHub credentials will be stored in the > >>>> ./.git/config file. Instead, just clone with: > >>>> > >>>> git clone https://github.com/sbgraves237/Ecdat.git > >>>> > >>>> You can then configure git to cache your HTTPS credentials for a > >>>> certain time, e.g. 120 minutes, so you don't have to enter them each > >>>> time you pull/push. See > https://git-scm.com/docs/git-credential-cache > >>>> for details. That's what I tell new-comers to Git(Hub|Lab|...) to > >>>> use. Personally, I add my public SSH key to GitHub and then clone > >>>> with the ssh protocol: > >>>> > >>>> git clone g...@github.com:sbgraves237/Ecdat.git > >>>> > >>>> That way my I never have to worry entering my credentials. > >>>> > >>>> /Henrik > >>>> > >>>>> Suggestions? > >>>>> Thanks again to all who have offered suggestions so far. > This > >>>>> migration from R-Forge to GitHub seems complete except for the > automatic > >>>>> tests provided via "Travis CI". > >>>>> > >>>>> > >>>>> Spencer > >>>>> > >>>>> > >>>>> On 2019-06-28 22:25, Ott Toomet wrote: > >>>>>> Apparently your username/password are wrong. Can you clone/push > from > >>>>>> other repos? > >>>>>> > >>>>>> You do not need authorization when cloning a public repo, so even > >>>>>> incorrect credentials may work (haven't tested this though). But > for > >>>>>> push you have to have that in order. > >>>>>> > >>>>>> I suggest you create ssh keys, upload those to GH, and use ssh > >>>>>> authorization instead of https. > >>>>>> > >>>>>> Cheers, > >>>>>> Ott > >>>>>> > >>>>>> On Fri, Jun 28, 2019 at 8:18 PM Spencer Graves > >>>>>> mailto:spencer.gra...@prodsyse.com>> > wrote: > >>>>>> > >>>>>> Thanks to Duncan, Henrik and Henrik, Brian, and Gábor: > >>>>>> > >>>>>> > >>>>>> I created a local copy of the new GitHub version using > the > >>>>>> following: > >>>>>> > >>>>>> git clone > >>>>>> > https://sbgraves237:mypassw...@github.com/sbgraves237/Ecdat.git > >>>>>> > >>>>>> > >>>>>> > >>>>>> That worked in the sense that I got a local copy. > However, > >>>>>> after > >>>>>> I rolled the version number and did "git commit&quo
Re: [Rd] R-Forge > GitHub?
Apparently your username/password are wrong. Can you clone/push from other repos? You do not need authorization when cloning a public repo, so even incorrect credentials may work (haven't tested this though). But for push you have to have that in order. I suggest you create ssh keys, upload those to GH, and use ssh authorization instead of https. Cheers, Ott On Fri, Jun 28, 2019 at 8:18 PM Spencer Graves wrote: > Thanks to Duncan, Henrik and Henrik, Brian, and Gábor: > > >I created a local copy of the new GitHub version using the > following: > > git clone https://sbgraves237:mypassw...@github.com/sbgraves237/Ecdat.git > > > >That worked in the sense that I got a local copy. However, after > I rolled the version number and did "git commit" on the DESCRIPTION > files, my "git push" command generated the following: > > > remote: Invalid username or password. > fatal: Authentication failed for > 'https://sbgraves237:mypassw...@github.com/sbgraves237/Ecdat.git/' > > >What am I missing? [Note: I used my actual GitHub password in > place of "mypassword" here, and this "Authentication failed" message > reported the GitHub password I used here.] > > >Thanks, >Spencer > > > p.s. I'm doing this under macOS Mojave 10.14.5. Also, I added > ".onAttach" functions to the R-Forge versions as Brian G. Peterson > suggested. That seemed to work fine. > > > On 2019-06-28 07:13, Duncan Murdoch wrote: > > On 28/06/2019 6:26 a.m., Gábor Csárdi wrote: > > > >> Instead, you can do as Duncan suggested, and put a README in your > >> R-Forge > >> repository, that points to *your* GitHub repositor(y/ies). Then the > >> https://github.com/rforge/ecdat read only mirror will pick this up > >> and will > >> point there as well. > > > > Just for the record: that was Henrik Singmann's suggestion, I just > > agreed with it. > > > > Duncan Murdoch > > > > > [[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] Testing for vectors
Gabe, I agree that If by standard you mean commonly used/understood, though, I doubt > most R users would understand a list to be a vector. I think most people > think of atomic vectors exclusively when they hear "vector" unless they've > very specifically been trained not to do so. However, a common way to create lists is by a construct like 'vector("list", n)'. Also, 'is.vector' reports TRUE for lists (but FALSE for many other vectors). This causes quite a bit of confusion, unless everyone understands the different 'vector' concepts, embedded in R. Cheers, Ott [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Testing for vectors
Thanks, Hadley for bringing this up:-) I am teaching R and I can suggest 5 different definitions of 'vector': a) vector as a collection of homogeneous objects, indexed by [ ] (more precisely atomic vector). Sometimes you hear that in R, "everything is a vector", but this is only true for atomic objects. b) vector as a collection of objects, indexed by either [ ] and [[ ]]. This includes atomic vectors and lists. c) vector versus scalar. It pops up when teaching math and stats, and is somewhat confusing, in particular if my previous claim was that "R does not have scalars". d) vector versus matrix (or other arrays). Again, it only matters when doing matrix operations where 'vectors', i.e. objects with NULL dimension, behave their own way. e) finally, 'is.vector' has it's own understanding what constitutes a vector. Maybe there are more... I don't think there are any easy answers but both me and my students would appreciate more consistent terminology. Cheers, Ott On Sat, Jul 7, 2018 at 1:32 PM, Hadley Wickham wrote: > On Sat, Jul 7, 2018 at 1:50 PM, Gabe Becker wrote: > > Hadley, > > > >> > >> I was thinking primarily of completing the set of is.matrix() and > >> is.array(), or generally, how do you say: is `x` a 1d dimensional > >> thing? > > > > > > Can you clarify what you mean by dimensionality sense and specifically 1d > > here? > > What do we call a vector that is not an array? (or matrix) > > What do we call an object that acts 1-dimensional? (i.e. has > length(dim()) %in% c(0, 1)) ? > > > You can also have an n x 1 matrix, which technically has 2 dimensions but > > conceptually is equivalent to a 1d array and/or a vector. > > Yes. You can also have array that's n x 1 x 1. > > > Also, are you including lists in your conceptions of 1d vector here? I'm > > with Duncan here, in that i'm having trouble understanding exactly what > you > > want to do without a bit more context. > > Isn't it standard terminology that a vector is the set of atomic vectors + > list? > > Hadley > > -- > http://hadley.nz > > __ > 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
[Rd] as.character.factor and S4 object containing factor
Do I mess up something or is this a bug? If I define an S4 object that contains "factor", all the tests indicate that it is a factor but as.character.factor() complains of it being a non-factor... > setClass("Foo", contains="factor") > a <- new("Foo", factor(1:3)) > a Object of class "Foo" [1] 1 2 3 Levels: 1 2 3 > class(a) [1] "Foo" attr(,"package") [1] ".GlobalEnv" > inherits(a, "factor") [1] TRUE > is(a, "factor") [1] TRUE > as.character.factor(a) Error in as.character.factor(a) : attempting to coerce non-factor > print(a) Error in as.character.factor(x) : attempting to coerce non-factor In addition: Warning message: In print.factor(a) : Setting class(x) to NULL; result will no longer be an S4 object This means I cannot use ordinary print/summary methods... platform x86_64-pc-linux-gnu arch x86_64 os linux-gnu system x86_64, linux-gnu status major 3 minor 3.1 year 2016 month 06 day21 svn rev 70800 language R version.string R version 3.3.1 (2016-06-21) nickname Bug in Your Hair Cheers, Ott -- Ott Toomet Visiting Researcher School of Information Mary Gates Hall, Suite 310 University of Washington Seattle, WA 98195 [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] row names of 'rowsum()'
I am referring to base::rowsum(), not rowSums(). For some reason I cannot access it's help on my computer but the online documentation (R-devel version) states: Value A matrix or data frame containing the sums. There will be one row per unique value of group Period. Above, the argument 'reorder' is mentioned which orders rows to 'agree with tapply'. Cheers, Ott On Mon, Sep 19, 2016 at 8:41 AM, S Ellison wrote: > > 'rowsum()' seems to add row names to the resulting matrix, corresponding > to > > the respective 'group' values. This is very handy, but it is not > documented. > > Should the documentation mention it so it could be relied upon as part > of API? > > If you're referring to base::rowSums, the 'value' section of the help page > says > " A numeric or complex array of suitable size, or a vector if the > result is one-dimensional. For the first four functions the > 'dimnames' (or 'names' for a vector result) are taken from the > original array. " > > S Ellison > > > *** > This email and any attachments are confidential. Any u...{{dropped:23}} __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] row names of 'rowsum()'
'rowsum()' seems to add row names to the resulting matrix, corresponding to the respective 'group' values. This is very handy, but it is not documented. Should the documentation mention it so it could be relied upon as part of API? Cheers, Ott -- Ott Toomet Visiting Researcher School of Information Mary Gates Hall, Suite 310 University of Washington Seattle, WA 98195 [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] issues with dev.new avoiding RStudio plot device on unix?
Can you describe your problem a bit more? * What kind of unix system do you have? * Can you run other X11 programs? I had a similar issue, and the problem was that the computer was not set up to support X11. As a minimum, you have to install /xauth/, and potentially also other libraries if you want to install packages from source. Best, Ott On Fri, Sep 25, 2015 at 11:53 AM, Skye Bender-deMoll wrote: > Hi R-devl, > > I'm still unable to force opening an *interactive* non-Rstudio > platform-specific plot device on *unix* systems. > > dev.new() add a new argument 'noRStudioGD' in R 3.1.1. Thank you. It > works for me when using RStudio on Windows, but on the unix system it opens > a pdf device instead of an interactive device when using an interactive > RStudio session (with R_DEFAULT_DEVICE and R_INTERACIVE_DEVICE not set). > > Do other unix RStudio users see this behavior? > > It appears that the relevant line of dev.new (and in zzz.R): > >else if (nzchar(dsp) && .Platform$GUI %in% c("X11", "Tk")) > X11 >else defdev > > > but when I step through in debugger, I see that > > Browse[2]> .Platform$GUI > [1] "RStudio" > > so instead of returning X11, it returnd defdev (pdf) > > perhaps changing to > > .Platform$GUI %in% c("X11", "Tk", "RStudio") > > would work, but seems a little strange logically. > > > best, > -skye > > p.s. I wonder if instead of having a noRStudioGD=TRUE flag, it might be a > more future-proof design to have an avoid.devices='RStudioGD' argument in > case users need to induce similar behavior to avoid other current or future > devices? Probably to late now tho. > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > -- Ott Toomet Visiting Researcher School of Information Mary Gates Hall, Suite 310 University of Washington Seattle, WA 98195 [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] sequence divided by data.frame
Can anyone explain me the following behavior: > 1:2/1 [1] 1 2 -- makes sense > 1:2/matrix(1,1,1) [1] 1 2 -- makes sense > 1:2/data.frame(a=1) a 1 1 -- why is this different? Best, Ott -- Ott Toomet Visiting Researcher School of Information Mary Gates Hall, Suite 095 University of Washington Seattle, WA 98195 [[alternative HTML version deleted]] __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel