Re: [R] Discriminant of a cubic polynomial
Dear Thomas, Unfortunately, I do not know if any packages implement this functionality. Though, it is a topic that interests me. Unlike the "classic discriminant", I prefer to work with the reduced polynomial. This "discriminant" is generalizable to a superset of Chebysev polynomials (which I called Cardano-polynomials). x^3 - 3*c*x - 2*d = 0 x^5 - 5*c*x^3 + 5*c^2*x - 2*d = 0 x^7 - 7*c*x^5 + 14*c^2*x^3 - 7*c^3*x - 2*d = 0 discr = d^2 - c^n, where n = 3 for the degree 3 polynomial; The beauty of this approach is that you can solve for all roots of these types of polynomials and the formula is much simpler. But you need to compute first the reduced polynomial. The order 3 polynomials always reduce to this type (which is not valid for higher orders). By the way, the roots are as follows: # intermediary quantities p = (d + sqrt(discr))^(1/n); q = (d - sqrt(discr))^(1/n); # Roots: p + q # Base-Root m = cos(2*pi/n) + 1i * sin(2*pi/n); # root of unity p * m^c(0, seq(n-1)) + q * m^c(0, rev(seq(n-1))); # All Roots See on my GitHub page: https://github.com/discoleo/R/blob/master/Math/Polynomials.CardanGeneralisation.R Sincerely, Leonard [[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 https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] BUG: atan(1i) / 5 = NaN+Infi ?
Dear Bert, These behave like real divisions/multiplications: complex(re=Inf, im = Inf) * 5 # Inf+Infi complex(re=-Inf, im = Inf) * 5 # -Inf+Infi The real division / multiplication should be faster and also is well behaved. I was expecting R to do the real division/multiplication on a complex number. Which R actually does for these very particular cases; but not when only Im(x) is Inf. Sincerely, Leonard From: Bert Gunter Sent: Friday, September 6, 2024 1:12 AM To: Duncan Murdoch Cc: Leo Mada ; r-help@r-project.org Subject: Re: [R] BUG: atan(1i) / 5 = NaN+Infi ? Perhaps > Inf*1i [1] NaN+Infi clarifies why it is *not* a bug. (Boy, did that jog some long dusty math memories :-) ) -- Bert On Thu, Sep 5, 2024 at 2:48 PM Duncan Murdoch mailto:murdoch.dun...@gmail.com>> wrote: On 2024-09-05 4:23 p.m., Leo Mada via R-help wrote: > Dear R Users, > > Is this desired behaviour? > I presume it's a bug. > > atan(1i) > # 0+Infi > > tan(atan(1i)) > # 0+1i > > atan(1i) / 5 > # NaN+Infi There's no need to involve atan() and tan() in this: > (0+Inf*1i)/5 [1] NaN+Infi Why do you think this is a bug? Duncan Murdoch __ R-help@r-project.org<mailto: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 https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[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 https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] BUG: atan(1i) / 5 = NaN+Infi ?
Dear R Users, Is this desired behaviour? I presume it's a bug. atan(1i) # 0+Infi tan(atan(1i)) # 0+1i atan(1i) / 5 # NaN+Infi There were some changes in handling of complex numbers. But it looks like a bug. Sincerely, Leonard [[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 https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Tools to modify highlighted areas in pdf documents?
Dear Ivan, Thank you very much for the hint. I have started to test it. - it offers more colours and types of highlighting than MS Edge; - it seems to have better word-boundary detection than MS Edge (but I haven't tested all the cases yet); There are some nit-picks: - I wish it had a better default color-pallet; - the vertical positioning continues to be sometimes sub-optimal: editing manually the coordinates may be still useful; Returning to R: 1. Some of the pdf-packages could implement some of the annotation-functionality as well. 2. It would be useful to be able to export the annotations and import/merge them in another document. I have spotted errors in various articles; such a functionality would be handy, if a new version of those articles gets published. Sincerely, Leonard From: Ivan Krylov Sent: Sunday, June 2, 2024 8:02 PM To: Leo Mada via R-help Cc: Leo Mada Subject: Re: [R] Tools to modify highlighted areas in pdf documents? � Sat, 1 Jun 2024 16:16:23 +0000 Leo Mada via R-help �: > When highlighting pdf-documents with Microsoft Edge, the bounding box > is sometimes misplaced, and quite ugly so. It also lacks the ability > to draw lines or arrows. > > On the other hand, I did not get used to Acrobat Reader: it usually > involves much more effort to add specific highlights. Lines can be > drawn, but are NOT straight! Sorry for answering a different question, but have you considered using a different PDF viewer + annotation application? Okular <https://eu01.z.antigena.com/l/iZmB5crNT773HTSUSt2S6McW6mdP5phHyzgXromFHINsN6Uo6BMnSdZK0kmSK~aLduXw-YpIAWy-DV9bac-5U3grBsLdYxuX7aMmbxQjKSLGCqTyJG54WQ2W7oaR2NEPTWiFotbkB4_eQTzHI3L-cAgOgsS4exJ4ie4BHLt > is free and available on Windows (including from outside Microsoft store). Its annotation features include all kinds of highlights, arrows and lines, both straight and arbitrarily-shaped, quickly available from the "annotations" panel. -- Best regards, Ivan [[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.
Re: [R] R code for overlapping variables -- count
Correcting a small glitch - see new code. From: Leo Mada Sent: Sunday, June 2, 2024 8:34 PM To: Shadee Ashtari Cc: r-help@r-project.org Subject: [R] R code for overlapping variables -- count Dear Shadee, If you have a data.frame with the following columns: n = 100; # population size x = data.frame( Sex = sample(c("M","F"), n, T), Country = sample(c("AA", "BB", "US"), n, T), Income = as.factor(sample(1:3, n, T)) ) # Dummy variable ONE = rep(1, nrow(x)) # corrected r = aggregate(ONE ~ Sex + Income + Country, length, data = x) r = r[, c("Country", "Income", "Sex", "ONE")] names(r)[4] = "Count" print(r) It is possible to write more simple code, if you need only the particular combination of variables (which you specified in your mail). But this is the more general approach. Note: you may want to use "sum" instead of "length", e.g. if you have a column specifying the number of individuals in that category. Hope this helps, Leonard [[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] R code for overlapping variables -- count
Dear Shadee, If you have a data.frame with the following columns: n = 100; # population size x = data.frame( Sex = sample(c("M","F"), n, T), Country = sample(c("AA", "BB", "US"), n, T), Income = as.factor(sample(1:3, n, T)) ) # Dummy variable ONE = rep(1, nrow(x)) r = aggregate(ONE ~ Sex + Income + Country, length, data = x) r = r[, c("Country", "Income", "Sex")] print(r) It is possible to write more simple code, if you need only the particular combination of variables (which you specified in your mail). But this is the more general approach. Note: you may want to use "sum" instead of "length", e.g. if you have a column specifying the number of individuals in that category. Hope this helps, Leonard [[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.
Re: [R] Tools to modify highlighted areas in pdf documents?
Dear Bert, Thank you very much for the response. I was aware of pdftools - but did not recall any such functionality. I have checked again (both pdftools, qpdf and the 3rd one): unfortunately, they do not implement such functionality. There might be other packages, which I missed. However, the functionality is feasible. I will add a few more details - maybe someone picks up the task. It is possible to edit manually the pdf-file, though it is quite cumbersome to find the right annotation. 1. One needs to edit the values both in the \QuadPoints and the \Rect in the \AP object. 2. Modifying the color is trickier: \C() encodes the color and \CA the alpha channel (= 1): but neither Acrobat, nor MIcrosoft Edge update the color. The value of the color encoded in the stream is used instead. It is possible to "trick" Edge: modify the \C color and set "\ca 1" (in the stream block) to a lower value (e.g. "\ca 0.99"). MS Edge will then accept the modified color (but Acrobat ignores it). Changing the value in the stream is the actual solution. Note: non-rectangular shapes can be specified as well. I hope that some of the referenced packages pick up this task. Sincerely, Leonard From: Bert Gunter Sent: Saturday, June 1, 2024 9:23 PM To: Leo Mada Cc: r-help@r-project.org Subject: Re: [R] Tools to modify highlighted areas in pdf documents? Search! on rseek.org<http://rseek.org>, the query "modify pdf documents in R" brought up the staplr package. A quick web search with the same query brought up the pdftools package. These were cursory efforts, so you may well find more. You will have to determine whether and to what degree any meet your needs. -- Bert On Sat, Jun 1, 2024 at 9:16 AM Leo Mada via R-help mailto:r-help@r-project.org>> wrote: Dear R-Users, Are there any packages that enable the modifications of highlighted areas / annotations in pdf documents? It seems feasible - I have explored some R code (see below). However, I would rather avoid to reinvent the wheel. The problem: When highlighting pdf-documents with Microsoft Edge, the bounding box is sometimes misplaced, and quite ugly so. It also lacks the ability to draw lines or arrows. On the other hand, I did not get used to Acrobat Reader: it usually involves much more effort to add specific highlights. Lines can be drawn, but are NOT straight! Are there tools to change the size/position of highlights? Or to add highlights and underline words? Changing position/size manually by editing the data in the pdf-document is possible. Changing the color is more trickier (somehow possible in Microsoft Edger; though the direct approach to rewrite the actual stream is better). Maybe there are some tools to do it? Some R code is below. Sincerely, Leonard # library(zip) con = file("_some_pdf_.pdf", "rb") NL = 0 # - very dirty hack; # - assumes Annotations are in the last fragment/chunk; while(TRUE) { tmp = readBin(con, "raw", 1024*128 + 515); if(length(tmp) == 0) break; x = tmp; # isNL = (x == 10) | (x == 13); isNL = (x == 13); isNL = isNL & (x[which(isNL) + 1] == 10); NL = NL + sum(isNL); } close(con) idP = which(isNL) idS = 935; # will vary with pdf and Annotations and ...; nLast = 4; # usually 2 chunks idx = idP[seq(idS, length.out = nLast)] # Check: Right position? # tmp = x[seq(idx[1] + 2, idx[1 + 2] - 1)] # intToUtf8(tmp) tmp = inflate(x[seq(idx[1] + 2, idx[nLast] - 1)]) intToUtf8(tmp$output) # Output of inflate: an Example # "/GS gs .56078434 .87058824 .97647059 rg\n # 337.298 183.836 m 364.322 183.836 l 364.322 171.83 l 337.298 171.83 l h f\n" # Note: /BBox[ 337.298 171.83 364.322 183.836] The raw pdf data: 1948 0 obj <>/C[ 0.560784 0.870588 0.976471]/CA 1/F 4/PDFIUM_HasGeneratedAP true/QuadPoints[ 337.298 186 364.322 186 337.298 174.6 364.322 174.6]/Rect[ 337.298 174.6 364.322 186]/Subtype/Highlight/Type/Annot>> endobj 1949 0 obj <>>>>>/Subtype/Form/Type/XObject>>stream xœE˱ €0 Àž)~ “ä Û™€ Ø P@ ûKˆ"Оtó²¢ß jÉC© ðT#ŠBš›zª WŸH—Ò 9(Aà š Kùäøų _ iÀŽmz dR ² endstream endobj [[alternative HTML version deleted]] __ R-help@r-project.org<mailto: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. [[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] Tools to modify highlighted areas in pdf documents?
Dear R-Users, Are there any packages that enable the modifications of highlighted areas / annotations in pdf documents? It seems feasible - I have explored some R code (see below). However, I would rather avoid to reinvent the wheel. The problem: When highlighting pdf-documents with Microsoft Edge, the bounding box is sometimes misplaced, and quite ugly so. It also lacks the ability to draw lines or arrows. On the other hand, I did not get used to Acrobat Reader: it usually involves much more effort to add specific highlights. Lines can be drawn, but are NOT straight! Are there tools to change the size/position of highlights? Or to add highlights and underline words? Changing position/size manually by editing the data in the pdf-document is possible. Changing the color is more trickier (somehow possible in Microsoft Edger; though the direct approach to rewrite the actual stream is better). Maybe there are some tools to do it? Some R code is below. Sincerely, Leonard # library(zip) con = file("_some_pdf_.pdf", "rb") NL = 0 # - very dirty hack; # - assumes Annotations are in the last fragment/chunk; while(TRUE) { tmp = readBin(con, "raw", 1024*128 + 515); if(length(tmp) == 0) break; x = tmp; # isNL = (x == 10) | (x == 13); isNL = (x == 13); isNL = isNL & (x[which(isNL) + 1] == 10); NL = NL + sum(isNL); } close(con) idP = which(isNL) idS = 935; # will vary with pdf and Annotations and ...; nLast = 4; # usually 2 chunks idx = idP[seq(idS, length.out = nLast)] # Check: Right position? # tmp = x[seq(idx[1] + 2, idx[1 + 2] - 1)] # intToUtf8(tmp) tmp = inflate(x[seq(idx[1] + 2, idx[nLast] - 1)]) intToUtf8(tmp$output) # Output of inflate: an Example # "/GS gs .56078434 .87058824 .97647059 rg\n # 337.298 183.836 m 364.322 183.836 l 364.322 171.83 l 337.298 171.83 l h f\n" # Note: /BBox[ 337.298 171.83 364.322 183.836] The raw pdf data: 1948 0 obj <>/C[ 0.560784 0.870588 0.976471]/CA 1/F 4/PDFIUM_HasGeneratedAP true/QuadPoints[ 337.298 186 364.322 186 337.298 174.6 364.322 174.6]/Rect[ 337.298 174.6 364.322 186]/Subtype/Highlight/Type/Annot>> endobj 1949 0 obj <>/Subtype/Form/Type/XObject>>stream xœE˱ €0 Àž)~“äÛ™€ØP@ûKˆ"Оtó²¢ßjÉC©ðT#ŠBš›zª WŸH—Ò9(Aà š Kùäøų_iÀŽmz dR² endstream endobj [[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.
Re: [R] Clustering Functions used by Reverse-Dependencies
Dear Ivan, Thank you very much for this interesting information. Regarding: "For well-behaved packages that declare their dependencies correctly, parsing the NAMESPACE for importFrom() and import() calls should give you the explicit imports." I did learn something new (I am not very experienced in package writing). Unfortunately, Roxygen2 as of the current version still suggests to use the pkg::fname approach: "If you are using just a few functions from another package, we recommending adding the package to the Imports: field of the DESCRIPTION file and calling the functions explicitly using ::, e.g., pkg::fun()." https://roxygen2.r-lib.org/articles/namespace.html Regarding analysing the actual code: it is good to know that CMD check has also some functionality. I will look into it, when I find some free time. tools:::.check_packages_used is a few pages of code. On the other hand, the help page for codetools::checkUsage is quite cryptic. But it's good to know at least where to look. Sincerely, Leonard From: Ivan Krylov Sent: Wednesday, February 28, 2024 10:36 AM To: Leo Mada via R-help Cc: Leo Mada Subject: Re: [R] Clustering Functions used by Reverse-Dependencies � Sat, 24 Feb 2024 03:08:26 + Leo Mada via R-help �: > Are there any tools to extract the function names called by > reverse-dependencies? For well-behaved packages that declare their dependencies correctly, parsing the NAMESPACE for importFrom() and import() calls should give you the explicit imports. (What if the package imports the whole dependency? The safe assumption is that all functions are used, but it comes with false positives. You could also walk the package code looking for function names that may belong to the imported package, but that may involve both false positives and false negatives.) For the rest of the imports and uses of weak dependencies, you'll have to walk the package code looking for the uses of the `::` operator. See how R CMD check walks the package code in functions tools:::.check_packages_used and codetools::checkUsage. A less-well-behaved package can always load a namespace during runtime and choose the functions to call depending on the phase of the moon or weather on Jupiter. For these, like for the halting problem, there's no general solution: the package could be written to say, "if Leonard's function says I'm about to call foo::bar, I won't do it, otherwise I will". -- Best regards, Ivan [[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] Clustering Functions used by Reverse-Dependencies
Dear R Users, Are there any tools to extract the function names called by reverse-dependencies? I would like to group these functions using clustering methods based on the co-occurrence in the reverse-dependencies. Utility: It may be possible to split complex packages into modules with fewer reverse-dependencies. Package pkgdepR may offer some of the functionality; but I did not have time to fully explore it: https://cran.r-project.org/web/packages/pkgdepR/index.html If such tools are not yet available, I have opened an issue on GitHub and would like to collect any ideas: https://github.com/discoleo/PackageBrowser/issues/1 There are some model packages that could be used to test the clustering: 1) Rcpp, xml: the core-functionality will always be needed; splitting into modules may not be possible/useful; 2) pracma: most reverse-dependencies are likely using only a small subset of the functions in pracma; (there was some discussion on R-package-devel about reverse dependencies a few weeks ago) 3) survival: I have no idea in which category it falls - but it has a lot of reverse-dependencies; Note: I missed some important functionality from the pkgsearch package. I have started therefore work on a new package (PackageBrowser) - although it is not yet published on CRAN: https://github.com/discoleo/PackageBrowser It does not yet process recursively the reverse-dependencies; although this does not seem a big challenge. The real challenge is to parse the code and extract function names. I did some work in the past, but never had time for a full implementation. Many thanks, Leonard [[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] Optimal use of optim?
Dear R-Users, I am interested in the optimal strategy for optim: Q: How to formulate the optimization problem? Q1: Are there benefits for abs(f(x)) vs (f(x))^2? Q2: Are there any limitations for using abs(...)? Regarding point 1: my feeling is that the gradients should be more robust with the abs(...) method; but I did not test it. Regarding point 2: Obviously, abs(f(x)) is not differentiable. This is a limitation from a mathematical point of view, but the gradient should be still computable using a finite difference method. Are there any other limitations? The question is based on the following example: solving gamma(x) = y for some given y. Function multiroot in package rootSolve fails when y < 1. The version using optim fares much better. However, I had to tweak somewhat the parameters in order to get a higher precision. This made me curious about the optimal strategy; unfortunately, I do not have much experience with optimizations. The example is also available on GitHub: https://github.com/discoleo/R/blob/master/Math/Integrals.Gamma.Inv.R Sincerely, Leonard ### Example: gamma.invN = function(x, x0, lim, ..., ndeps = 1E-5, rtol = 1E-9, gtol = 0.0001, digits = 10) { v = x; if(length(lim) == 1) lim = c(lim - 1 + gtol, lim - gtol); cntr = c(list(...), ndeps = ndeps, pgtol = rtol); # abs(): should provide greater precision than ()^2 when computing the gradients; # param ndeps: needed to improve accuracy; r = optim(x0, \(x) { abs(gamma(x) - v); }, lower = lim[1], upper =lim[2], method = "L-BFGS-B", control = cntr); if( ! is.null(digits)) print(r$par, digits); return(r$par); } ### Example Euler = 0.57721566490153286060651209008240243079; x = gamma.invN(Euler, -3.5, lim = -3) gamma(x) - Euler x = gamma.invN(Euler, -3.9, lim = -3) gamma(x) - Euler [[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.
Re: [R] Skip jumps in curve
Dear Duncan, Thank you very much for the response. I suspected that such an option has not been implemented yet. The plot was very cluttered due to those vertical lines. Fortunately, the gamma function is easy to handle. But the feature remains on my wishlist as useful more in general. Sincerely, Leonard From: Duncan Murdoch Sent: Tuesday, February 13, 2024 6:05 PM To: Leo Mada ; r-help@r-project.org Subject: Re: [R] Skip jumps in curve On 13/02/2024 10:29 a.m., Leo Mada via R-help wrote: > Dear R-Users, > > Is there a way to skip over without plotting the jumps/discontinuities in > curve()? > > I have not seen such an option, but maybe I am missing something. > > plot.gamma = function(xlim = c(-6, -1), ylim = c(-1,3), hline = NULL, n = > 1000) { > curve(gamma(x), from = xlim[1], to = xlim[2], ylim=ylim, n=n); > if( ! is.null(hline)) abline(h = hline, col = "green"); > } > > Euler = 0.57721566490153286060651209008240243079; > plot.gamma(hline = Euler) > > Adding an option to the function curve may be useful: > options = c("warn", "silent", "unconnected") > > This is part of some experiments in math; but that's another topic. For > latest version: > https://github.com/discoleo/R/blob/master/Math/Integrals.Gamma.Inv.R If you know where the discontinuities are, plot multiple times with the discontinuities as endpoints: plot.gamma = function(xlim = c(-6, -1), ylim = c(-1,3), hline = NULL, n = 1000) { start <- floor(xlim[1]):floor(xlim[2]) end <- start + 1 start[1] <- xlim[1] end[length(end)] <- xlim[2] n <- round(n/length(start)) curve(gamma(x), from = start[1], to = end[1], ylim=ylim, n=n, xlim = xlim) for (i in seq_along(start)[-1]) curve(gamma(x), from = start[i], to = end[i], add = TRUE, n) if( ! is.null(hline)) abline(h = hline, col = "green"); } Euler = 0.57721566490153286060651209008240243079; plot.gamma(hline = Euler) If you don't know where the discontinuities are, it would be much harder, because discontinuities can be hard to detect unless the jumps are really big. Duncan Murdoch [[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] Skip jumps in curve
Dear R-Users, Is there a way to skip over without plotting the jumps/discontinuities in curve()? I have not seen such an option, but maybe I am missing something. plot.gamma = function(xlim = c(-6, -1), ylim = c(-1,3), hline = NULL, n = 1000) { curve(gamma(x), from = xlim[1], to = xlim[2], ylim=ylim, n=n); if( ! is.null(hline)) abline(h = hline, col = "green"); } Euler = 0.57721566490153286060651209008240243079; plot.gamma(hline = Euler) Adding an option to the function curve may be useful: options = c("warn", "silent", "unconnected") This is part of some experiments in math; but that's another topic. For latest version: https://github.com/discoleo/R/blob/master/Math/Integrals.Gamma.Inv.R Sincerely, Leonard [[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.
Re: [R] Use of geometric mean for geochemical concentrations
Dear Rich, It depends how the data is generated. Although I am not an expert in ecology, I can explain it based on a biomedical example. Certain variables are generated geometrically (exponentially), e.g. MIC or Titer. MIC = Minimum Inhibitory Concentration for bacterial resistance Titer = dilution which still has an effect, e.g. serially diluting blood samples; Obviously, diluting the samples will generate the following concentrations: 1, 1/2, 1.4, 1/8, 1/16, ... (or the reciprocal: 1, 2, 4, 8, 16, ...) It makes no sense to compute the arithmetic mean. Results are usually reported as some quantile (median or 90%); alternatively, one computes the geometric mean. ### Ecology /Environmental Chemistry I suppose that certain chemicals may be generated/released in the environment through a non-linear process. The LLOD may also play a role, but may NOT be the main reason. If the generating process is exponential, then the arithmetic mean would strongly skew the results (also inconsistently based on season, particular year, etc - the generating processes may differ). ### Harmonic Mean Did not encounter it often: maybe because of the problematic handling of 0. I do have in the meantime a nice workaround for 0 (which also works with the geometric mean), see also (unfortunately not well documented): https://github.com/discoleo/R/blob/master/Stat/Moments.Stat.R v0 = 1; # some initial "skew" 1 /(xharm + v0) = sum( 1 / (x + v0) ) / length(x) xgeom = prod(x + v0)^(1/length(x)) - v0; I apologize for the late reply; I did not have much time to read messages during the past weeks. Sincerely, Leonard [[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] Fit NLE - was: computer algebra in R
Fit NLE - was: [R] computer algebra in R Original post: https://stat.ethz.ch/pipermail/r-help/2023-November/478619.html Dear Kornad, I think I have started to understand what you try to achieve. The problem is to fit a NLE and compute the parameters of the NL-Eq. I have included the R Help-list back in the loop, as I am not an expert in optimization. Goal: y ~ I0 + IHD * hd + ID * d; where: y = given vector of measurements; x = given vector of values; I0, IHD, ID, kd = parameters to optimize; hd = satisfies a polynomial of order 3; As d = d0 - hd, the previous formula can be written: y ~ I0 + ID * d0 + (IHD - ID) * hd; f(x, hd, kd) = 0, where f = a polynomial of order 3 in hd and order 2 in kd; d0 (and other components of the polynomial) = given constants; 1) First Approach I would back-substitute hd into the polynomial: hd = (y - I0 - ID*d0) / (IHD - ID); f(x, hd, kd) becomes then f(x, y, kd, I0, ID, IHD) = 0; - f is order 3 in y; You could fit: (y^3) ~ f(x, y, kd, I0, ID, IHD) - y^3, where you subtract the y^3 term from the function f, and add the (y^3) values as a new columng to the data.frame: data.frame(y3 = y^3, y=y, x=x) If the values of y are versy small (abs(y) << 1), then it may be wiser to fit: y ~ f(x, y^2, y^3, kd, I0, ID, IHD) - (y-term); But I am not an expert in these problems. Other R-users may be more helpful. 2.) Approach 2: Math I feel that the problem can be solved quasy-exactly as well. It is much harder with 4 parameters to optimize: - one needs to compute the 4 partial derivatives; - solve the resulting system of 4 polynomial equations; The system is polynomial; although it looks ugly and I am not inclined to do such calculations myself. I hope that you can get more useful answers from the R help-list. Sincerely, Leonard [[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] computer algebra in R
Dear Konrad, I presume that the system can be written as follows, where h0, d0, ga0, kga and kd are given: err1 = h + hd + hga - h0; err2 = d + hd - d0; err3 = ga + hga - ga0; err4 = hga - kga*h*ga; err5 = hd - kd*h*d; All error terms should be zero. Do you need (a) the symbolic solution or (b) is a numeric solution fine? I do not have any experience with yacas or caracas. But see below. ### Case (a) I did write a (very rudimentary) symbolic solver for systems of polynomial equations in pure R. It should be able to solve this one. Please let me know if this is of interest and I will provide further details. ### Case (b) Again, it should be possible to solve, see some examples: https://github.com/discoleo/R/blob/master/Math/Systems.NLE.Tutorial.md https://github.com/discoleo/R/blob/master/Math/Systems.Lambert.R I did solve much more complex ones, for a monster see: https://github.com/discoleo/R/blob/master/Math/Poly.System.S5.Ht.Formulas.Derivation.HS0.R [and the remaining Poly.System.S5.Ht... scripts] I presume that only the real roots are required (it's possible to solve systems with complex roots as well). I can provide further details as well. Sincerely, Leonard [[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.