[R] Dispatching on 2 arguments?
Dear List-members, I would like to experiment with dispatching on 2 arguments and have a few questions. p1 = data.frame(x=1:3, coeff=1) class(p1) = c("pm", class(p1)); I want to replace variables in a polynomial with either: another polynomial, or another variable (character) or with a specific value. 1.) Can I dispatch on 2 arguments? replace.pm.? = function(p1, p2, ...) {...} or classic: replace.pm = function(p1, p2, ...) { if(is.numeric(p2) || is.complex(p2)) return(replace.pm.numeric(p1, p2, ...)); if(is.character(p2)) return(replace.pm.character(p1, p2=p2, ...)); else ... } I will provide some realistic examples below. 2.) Advantages / Disadvantages of each method What are the advantages or disadvantages to each of these methods? I do not yet understand what should be the best design. Real example: ### Quintic p1 = toPoly.pm("x^5 - 5*K*x^3 - 5*(K^2 + K)*x^2 - 5*K^3*x - K^4 - 6*K^3 + 5*K^2 - K") # fractional powers: [works as well] r = toPoly.pm("K^(4/5) + K^(3/5) + K^(1/5)") # - we just found a root of a non-trivial quintic! # all variables/monomials got cancelled; replace.pm(p1, r, "x", pow=1) # more formal r = toPoly.pm("k^4*m^4 + k^3*m^3 + k*m") # m^5 = 1; # m = any of the 5 roots of unity of order 5; pR = p1; pR = replace.pm(pR, r, xn="x") # poly pR = replace.pm(pR, "K", xn="k", pow=5) # character pR = replace.pm(pR, 1, xn="m", pow=5) # value pR # the roots worked! [no remaining rows] # - we just found ALL 5 roots! The code is on Github (see below). Sincerely, Leonard = # very experimental code # some names & arguments may change; source("Polynomials.Helper.R") # also required, but are loaded automatically if present in wd; # source("Polynomials.Helper.Parser.R") # source("Polynomials.Helper.Format.R") ### not necessary for this Test (just loaded) # source("Polynomials.Helper.D.R") # source("Polynomials.Helper.Factorize.R") # the libraries pracma & polynom are not really required for this test either; ### Github: https://github.com/discoleo/R/blob/master/Math/Polynomials.Helper.R https://github.com/discoleo/R/blob/master/Math/Polynomials.Helper.Parser.R https://github.com/discoleo/R/blob/master/Math/Polynomials.Helper.Format.R # not necessary for this Test https://github.com/discoleo/R/blob/master/Math/Polynomials.Helper.D.R https://github.com/discoleo/R/blob/master/Math/Polynomials.Helper.Factorize.R __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Environmental oddity.
On Sun, Nov 7, 2021 at 6:05 AM Rolf Turner wrote: > > > I have two functions which appear to differ only in their environments. > They look like: > > > d1 > > function (x, mean = 0, sd = 1, log = FALSE) > > (((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd)/sd > > > > and > > > d2 > > function (x, mean = 0, sd = 1, log = FALSE) > > (((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd)/sd > > Typing "environment(d1)" gives > > > > > and typing "environment(d2)" gives > > > > > The d2() function however gives an incorrect result: > > > d1(1,0,3,TRUE) > > [1] -0.2962963 > > d2(1,0,3,TRUE) > > [1] -0.889 It can't be as simple as that. I get the same result (as your d2) with the following: d <- function (x, mean = 0, sd = 1, log = FALSE) { (((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd) / sd } d(1, 0, 3, TRUE) environment(d) environment(d) <- as.environment("package:stats") d(1, 0, 3, TRUE) > In d2() the result of the if() statement does not get divided > by the final "sd" whereas in d1() it does (which is the desired/correct > result). > > Of course the code is ridiculously kludgy (it was produced by "symbolic > differentiation"). That's not the point. I'm just curious (idly?) as > to *why* the association of the namespace:stats environment with d1() > causes it to "do the right thing". This sounds like a difference in precedence. The expression if (log) 1 else dnorm(x, mean, sd) / sd is apparently being interpreted differently as d1: (if (log) 1 else dnorm(x, mean, sd)) / sd d2: if (log) 1 else (dnorm(x, mean, sd)) / sd) It's unclear how environments could affect this, so it would be very helpful to have a reproducible example. Best, -Deepayan > Can anyone give me any insight? Ta. > > cheers, > > Rolf Turner > > -- > Honorary Research Fellow > Department of Statistics > University of Auckland > Phone: +64-9-373-7599 ext. 88276 > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Environmental oddity.
In general, the search for symbols for a function Z in a package Y will span only those namespaces that the package Y specifies. The search for symbols in a function whose parent environment is the global environment will start there, thereby opening the door to find masking versions of functions instead of the intended package function. Kind of hard to investigate your case from here. On November 6, 2021 5:35:08 PM PDT, Rolf Turner wrote: > >I have two functions which appear to differ only in their environments. >They look like: > >> d1 >> function (x, mean = 0, sd = 1, log = FALSE) >> (((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd)/sd >> > >and > >> d2 >> function (x, mean = 0, sd = 1, log = FALSE) >> (((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd)/sd > >Typing "environment(d1)" gives > >> > >and typing "environment(d2)" gives > >> > >The d2() function however gives an incorrect result: > >> d1(1,0,3,TRUE) >> [1] -0.2962963 >> d2(1,0,3,TRUE) >> [1] -0.889 > >In d2() the result of the if() statement does not get divided >by the final "sd" whereas in d1() it does (which is the desired/correct >result). > >Of course the code is ridiculously kludgy (it was produced by "symbolic >differentiation"). That's not the point. I'm just curious (idly?) as >to *why* the association of the namespace:stats environment with d1() >causes it to "do the right thing". > >Can anyone give me any insight? Ta. > >cheers, > >Rolf Turner > -- Sent from my phone. Please excuse my brevity. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Environmental oddity.
I have two functions which appear to differ only in their environments. They look like: > d1 > function (x, mean = 0, sd = 1, log = FALSE) > (((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd)/sd > and > d2 > function (x, mean = 0, sd = 1, log = FALSE) > (((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd)/sd Typing "environment(d1)" gives > and typing "environment(d2)" gives > The d2() function however gives an incorrect result: > d1(1,0,3,TRUE) > [1] -0.2962963 > d2(1,0,3,TRUE) > [1] -0.889 In d2() the result of the if() statement does not get divided by the final "sd" whereas in d1() it does (which is the desired/correct result). Of course the code is ridiculously kludgy (it was produced by "symbolic differentiation"). That's not the point. I'm just curious (idly?) as to *why* the association of the namespace:stats environment with d1() causes it to "do the right thing". Can anyone give me any insight? Ta. cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] extracting a R object from an R image
> Jeff Newmiller > on Fri, 05 Nov 2021 16:45:02 -0700 writes: > IMO you are being a bit too literal. It is absolutely possible to load the file into a dedicated environment and use the $ or [[]] extraction operator to access a specific object in that environment. > ?load > ?new.env > Or, you can attach the file, copy to a new variable, and detach. (see examples in ?load) Exactly: In the eyes of most R experts, it is the only useful use of attach(): attach(".RData") ls.str(2) # -> get alevel-1 str(.) of the objects in your image > On November 5, 2021 3:26:49 PM PDT, Bert Gunter wrote: >> You can't. You can only save and load whole .RData files. You can, of >> course, save and load separate R objects in separate files. But note >> in ?save.image: >> >> "For saving single R objects, saveRDS() is mostly preferable to >> save(), notably because of the functional nature of readRDS(), as >> opposed to load(). " >> >> You may wish to search on "data serialization" (e.g. on Wikipedia) or >> similar to better understand the underlying ideas. >> >> Bert Gunter >> >> "The trouble with having an open mind is that people keep coming along >> and sticking things into it." >> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) >> >> Bert Gunter >> >> "The trouble with having an open mind is that people keep coming along >> and sticking things into it." >> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) >> >> >> On Fri, Nov 5, 2021 at 3:01 PM Bogdan Tanasa wrote: >>> >>> Dear all, >>> >>> I saved my work in a Rimage that contains multiple objects ; >>> >>> the objects were generated with Monocle3 : >>> >>> https://cole-trapnell-lab.github.io/monocle3/docs/starting/ >>> >>> one object is called CDS. >>> >>> How shall I extract this object CDS (that has a complex structure) from the >>> R image ? >>> >>> thank you, >>> >>> Bogdan >>> >>> [[alternative HTML version deleted]] >>> >>> __ >>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > -- > Sent from my phone. Please excuse my brevity. > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[ESS] How to stop lots of carriage returns with company-quickhelp?
Actually I'm not sure if they are carriage returns, or just returns, but in my emacs, when I enter an R command, then the company mode popups always put in a few extra lines with each letter I type. For example, if I type "library", since each new letter causes a new popup, by the time I've typed the word I have 12 new blank lines, like this: > previous_command > > > > > > > > > > > > > library( I'm not sure if this is an issue with the R console, or with company-quickhelp mode, but it's annoying. I can't find any information about company-quickhelp causing carriage returns, so it must be something in the way that the R console is working in emacs. I could easily stop this by not using the company-quickhelp mode, but then of course I miss out on the popups. I'm using GNU Emacs 27.2 in Linux, and according to emacs info, ESS version 18.10.3snapshot. Thank you very much. Alasdair -- https://numbersandshapes.net [[alternative HTML version deleted]] __ ESS-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/ess-help
Re: [R] bootstrap confidence intervals
Hello, Às 01:36 de 06/11/21, David Winsemius escreveu: On 11/5/21 1:16 PM, varin sacha via R-help wrote: Dear R-experts, Here is a toy example. How can I get the bootstrap confidence intervals working ? Many thanks for your help library(DescTools) library(boot) A=c(488,437,500,449,364) dat<-data.frame(A) med<-function(d,i) { temp<-d[i,] # shouldn't this be HodgesLehmann(temp) # ??? # makes no sense to extract a bootstrap sample and then return a value calculated on the full dataset HodgesLehmann(A) } boot.out<-boot(data=dat,statistic=med,R=100) I would have imagined that one could simply extract the quantiles of the HodgesLehmann at the appropriate tail probabilities: quantile(boot.out$t, c(0.025, 0.975)) 2.5% 97.5% 400.5000 488.0001 It doesn't seem reasonable to have bootstrap CI's that are much tighter than the estimates on the original data: > HodgesLehmann(boot.out$t, conf.level=0.95) est lwr.ci upr.ci 449.75 444.25 453.25 # seems to be cheating > HodgesLehmann(dat$A, conf.level=0.95) est lwr.ci upr.ci 449 364 500 # Much closer to the quantiles above This cheating comes from wilcox.test, which is called by HodgesLehman to do the calculations. Below is a function calling wilcox.test directly, and the bootstrapped intervals are always equal, no matter what way they are computed. A <- c(488, 437, 500, 449, 364) dat <- data.frame(A) med <- function(d,i) { temp <- d[i, ] HodgesLehmann(temp) } med2 <- function(d, i, conf.level = 0.95){ temp <- d[i, ] wilcox.test(temp, conf.int = TRUE, conf.level = Coalesce(conf.level, 0.8), exact = FALSE)$estimate } set.seed(2021) boot.out <- boot(data = dat, statistic = med, R = 100) set.seed(2021) boot.out2 <- boot(data = dat, statistic = med2, R = 100, conf.level = 0.95) HodgesLehmann(boot.out$t) #[1] 452.75 HodgesLehmann(boot.out2$t) #[1] 452.75 HodgesLehmann(boot.out$t, conf.level = 0.95) # est lwr.ci upr.ci #452.7500 447.2500 458.7499 HodgesLehmann(boot.out2$t, conf.level = 0.95) # est lwr.ci upr.ci #452.7500 447.2500 458.7499 quantile(boot.out$t, c(0.025, 0.975)) # 2.5% 97.5% #400.5 494.0 quantile(boot.out2$t, c(0.025, 0.975)) # 2.5% 97.5% #400.5 494.0 boot.ci(boot.out, type = "all")# CI's are boot.ci(boot.out2, type = "all") # the same But the bootstrap statistic vectors t are different: identical(boot.out$t, boot.out2$t) #[1] FALSE all.equal(boot.out$t, boot.out2$t) #[1] "Mean relative difference: 8.93281e-08" I haven't time to check what is going on in wilcox.test, its source is a bit involved, with many if/else statements, maybe I'll come back to this but no promises made. Hope this helps, Rui Barradas __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.