Re: [R] Derivative of a Function Expression
Rory Winston wrote: I am currently (for pedagogical purposes) writing a simple numerical analysis library in R. I have come unstuck when writing a simple Newton-Raphson implementation, that looks like this: f - function(x) { 2*cos(x)^2 + 3*sin(x) + 0.5 } root - newton(f, tol=0.0001, N=20, a=1) My issue is calculating the symbolic derivative of f() inside the newton() function. If it's pedagogical, maybe returning to basics could help. What is f'(x)? It's the limit of (f(x + h) - f(x)) / h when h tends to zero. So, do it numerically: take a sufficiently small h and compute the limit. h must be small enough that h^2 f''(x) is much smaller than h f'(x), but big enough that f(x+h) is not f(x) numerical.derivative - function(f, x, h = 0.0001) { # test something (f(x + h) - f(x)) / h } Ex: numerical.derivative(cos, pi) = 5e-05 # should be -sin(pi) = 0 numerical.derivative(sin, pi) = -1# ok numerical.derivative(exp, 0) = 1.5 # close enough numerical.derivative(sqrt, 0) = 100 # should be Inf Alberto Monteiro __ 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] Excel (off-topic, sort of)
Chris wrote: Typically, people in the R community are not used to the spreadsheet paradigm and need some time to be able to take advantage of automatic recalculation, (...) Do you know what's in my wish list? I wish spreadsheets and computer languages had gone one step further. I mean, it's nice to define Cell X to be equal to Cell Y + 10, and then when we change Cell Y, magically we see Cell X change. But why can't it be the reverse? Why can't I change Cell X _and see the change in Cell Y_? Maybe I'll write a letter to Santa Claus [there are people who write to congressman; they must have more faith than me]. I wish a language where I can write a = b + 10 and then when I write a = 20 the language automatically assigns b = 10. There's a way to simulate this in any computer language, or even in Excel: instead of variables or cells, we have structures with value and a flag. The flag dictates if it's input, undefined or calculated. And then there's a list of relations. So the program/language/spreadsheed loops through the list of relations, detects whenever we can infer a new calculated value, and calculates it, until there's nothing else to do or a contradiction is found. Alberto Monteiro __ 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] It is possible to use a Shell command inside a R script?
Ronaldo Reis Junior wrote: It is possible to use a shell command inside a R script? I'm write a R script and I like to put somes shell commands inside to R. Somethink like: convert fig01.png fig01.xpm or sed ..., etc. It is possible? How? ?system BTW, I found that using things directly in R is _much_ slower than creating a batch file and then running it. For example, I had a directory with misnamed mp3 files, and I wanted to use R to rename and copy them to another directory. I tried to use file.copy, but it took too much time. Writing a batch file and then running it was much faster. Alberto Monteiro __ 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] It is possible to use a Shell command inside a R script?
Gabor Grothendieck wrote: What OS was that on? I suppose you are asking me :-) BTW, I found that using things directly in R is _much_ slower than creating a batch file and then running it. For example, I had a directory with misnamed mp3 files, and I wanted to use R to rename and copy them to another directory. I tried to use file.copy, but it took too much time. Writing a batch file and then running it was much faster. I was Windows, of course. In Linux, I would use a one-line script with find, mv, sed, etc. If you want to test, do this: # create subdiretory source_dir with 800 files # (for simplicity's sake, all files with decent names: no # spaces, no non-ascii characters, etc) # create empty subdiretory dest1_dir # create empty subdiretory dest2_dir # arr - list.files(source_dir) t0 - Sys.time() # 1st test: slow for (x in arr) file.copy(paste(source_dir/, x, sep=), dest1_dir) t1 - Sys.time() t1 - t0 # 2nd test: fast sink(batch_file.bat) for (x in arr) cat(copy source_dir\\, x, dest2_dir\\\n, sep=) sink() system(batch_file.bat) t2 - Sys.time() t2 - t1 Hmmm Interesting... I did this test with 47 files, and I got: t1 - t0 Time difference of 11.439 secs t2 - t1 Time difference of 11.969 secs Oops... Alberto Monteiro __ 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] Seasonality
I have a time series x = f(t), where t is taken for each month. What is the best function to detect if _x_ has a seasonal variation? If there is such seasonal effect, what is the best function to estimate it? Function arima has a seasonal parameter, but I guess this is too complex to be useful. Alberto Monteiro __ 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] Source inside source
Is there a way to know where is the source, so as to make a source call inside another source smarter? As an example: file1.R is in directory /files/dir1/ file2.R is in directory /files/dir1/dir2/ In file1.R, there is this line: source(dir2/file2.R) So, if I setwd to /files/dir1/, and then I call source(file1.R), it will run correctly. However, if I setwd to /files, then call source(dir1/file1.R), it will give an error when trying to source file2.R Alberto Monteiro __ 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] system: Linux vs. Windows differences
[I tried to send this messages two days ago, but I guess I mistyped the To: address...] Why system is different in Linux and Windows? Both in R 2.4.1, but in Windows there is an option: system(something, wait = FALSE) while on Linux (Fedora Core 4), there is no such option? Alberto Monteiro __ 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] Me again, about the horrible documentation of tcltk
[sorry for the previous mis-typed message... my mouse is playing evil tricks against me [*]] Philippe Grosjean wrote: For those who are interested, I just cook a little tcltkHelp() function to ease access to the Tcl/Tk documentation under Windows. This is on the Wiki discussion of the TkCommands help page at: http://wiki.r-project.org/rwiki/doku.php?id=rdoc:tcltk:tkcommands Best, The help is Windows-only, isn't it? I use Windows (at work and eventually at home) and Linux (at home). Maybe this is off-topic (it's more a bug of tcl/tk then of the tcltk R library), but this tk_getOpenFile opens a nice window in Windows, but a mutilated window in Linux, that does not show any file information except filename. That's why I wanted to know if there was a way to improve the function call - I think there isn't. Alberto Monteiro [*] I am neither paranoid nor animistic! __ 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] Me again, about the horrible documentation of tcltk
-- Original Message --- From: Philippe Grosjean [EMAIL PROTECTED] To: Duncan Murdoch [EMAIL PROTECTED] Cc: r-help@stat.math.ethz.ch, Mike Meredith [EMAIL PROTECTED] Sent: Fri, 06 Jul 2007 12:57:17 +0200 Subject: Re: [R] Me again, about the horrible documentation of tcltk For those who are interested, I just cook a little tcltkHelp() function to ease access to the Tcl/Tk documentation under Windows. This is on the Wiki discussion of the TkCommands help page at: http://wiki.r-project.org/rwiki/doku.php?id=rdoc:tcltk:tkcommands Best, Philippe Grosjean ..°})) ) ) ) ) ) ( ( ( ( (Prof. Philippe Grosjean ) ) ) ) ) ( ( ( ( (Numerical Ecology of Aquatic Systems ) ) ) ) ) Mons-Hainaut University, Belgium ( ( ( ( ( .. Duncan Murdoch wrote: On 06/07/2007 3:51 AM, Mike Meredith wrote: I think it would help if the tcl/tk manuals were added to the RGui Help menu. Why google when they are on your hard drive already? I'd say they are too specialized for that. There are dozens of topics that are as important as this, but a GUI menu with more than a few entries is just unwieldy. We do have a text reference to the help files in the ?tcltk topic. Duncan Murdoch Cheers, Mike Mike Prager wrote: Alberto Monteiro [EMAIL PROTECTED] wrote: How on Earth can I know what are the arguments of any of the functions of the tcl/tk package? [...] My impression is that you as supposed to look in tck/tk manuals. For example, Googling on tk tck getopenfile pointed to this Web page: http://www.tcl.tk/man/tcl8.5/TkCmd/getOpenFile.htm Hope that helps. -- Mike Prager, NOAA, Beaufort, NC * Opinions expressed are personal and not represented otherwise. * Any use of tradenames does not constitute a NOAA endorsement. __ 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-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-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. --- 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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] Recursion in R ...
Ted Harding wrote: So I slickly wrote a recursive definition: Nnk-function(n,k){ if(n==1) {return(k)} else { R-0; for(r in (1:k)) R-(R+Nnk(n-1,k-r+1)) # ,depth)) } return(R) } You are aware that this is equivalent to: Nnk1 - function(n, k) { prod(1:(n+k-1)) / prod(1:n) / prod(1:(k-1)) } aren't you? ON THAT BASIS: I hereby claim the all-time record for inefficient programming in R. Challengers are invited to strut their stuff ... No, I don't think I can bet you unintentionally, even though my second computer program that I ever wrote in life had to be aborted, because it consumed all the resources of the computer. Alberto Monteiro __ 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] Me again, about the horrible documentation of tcltk
How on Earth can I know what are the arguments of any of the functions of the tcl/tk package? I tried hard to find, using all search engines available, looking deep into keywords of R, python's tkinter and tcl/tk, but nowhere I found anything remotely similar to a help. For example, what are the possible arguments to tkgetOpenFile? I know that this works: library(tcltk) filename - tclvalue(tkgetOpenFile( filetypes={{Porn Files} {.jpg}} {{All files} {*}})) if (filename != ) cat(Selected file:, filename, \n) but, besides filetypes, what are the other arguments to tkgetOpenFile? I would like to force the files to be sorted by time, with most recent files coming first (and no, the purpose is not to use for porn files). Alberto Monteiro __ 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] Me again, about the horrible documentation of tcltk
James MacDonald wrote: 3.) Type tkgetOpenFile at R prompt. tkgetOpenFile function (...) tcl(tk_getOpenFile, ...) environment: namespace:tcltk 4.) Google tk_getOpenFile. 5.) http://www.tcl.tk/man/tcl8.5/TkCmd/getOpenFile.htm Thanks, you (all who helped) are so nice. I even incorporated your (MacDonald's) suggestion in the R-Wiki, at... http://wiki.r-project.org/rwiki/doku.php?id=rdoc:tcltk:tkcommands Alberto Monteiro __ 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] help again
Stefan Grosse wrote: This makes it much easier to help! (how do your numbers look like e.g.) You don't need to send the whole dataset but a few lines would be nice plus what commands you've done to receive those errors... In fact, I noticed that, most of the time, when I reduce the problematic code to a minimum code that reproduces the bug, it's much easier _for me_ to spot (and fix) the error :-) Alberto Monteiro __ 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] Fat tails
I have a process that generates distributions that look like normal, but with fat tails. I would like to model them as if they were a mix of two normals (with zero mean). Is there any function that recovers the standard deviations and the probability based on a sample? I would like to generate a sample like this: r.mydist - function(n, prob, mean1, sd1=1, mean2, sd2=1) { # aesthetical stuff if (missing(mean1) missing(mean2)) mean1 - 0 if (missing(mean1)) mean1 - mean2 if (missing(mean2)) mean2 - mean1 # now, let's work # test is true if we must use the first normal (mean1, sd1) test - runif(n) prob # create the return value rval - 0 rval[n] - 0 # generate the first normal when appropriate if (any(test)) rval[test] - rnorm(sum(test), mean1, sd1) # generate the second normal when appropriate if (any(!test)) rval[!test] - rnorm(sum(!test), mean2, sd2) # game over return(rval) } # test x - r.mydist(1000, 0.1, 0, 1, 0, 5) hist(x) if (require(fBasics)) cat(the sample kurtosis is, kurtosis(x), \n) Now, with this sample x, how can I get back sd1, sd2 and prob? Alberto Monteiro __ 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] compare 2 vectors
Romain Francois wrote: There is also a pretty useful operator %w/o% in the help page of %in%. see : ?`%in%` a - c(1,2,3,4,5,6,7,8,9) b - c(3,10,20,5,6) b %w/o% a [1] 10 20 I don't like the example. It's not obvious, in the expression... x[!x %in% y] ... that this is the same as x[!(x %in% y)] and not x[(!x) %in% y] Alberto Monteiro __ 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] Latex \ell symbol in plotmath
Prof Brian Ripley wrote: It is Unicode character U+2113, and so on UTF-8 R systems you may well be able to enter it as \u2113 and get it plotted on-screen in a suitable font. But we'd need to know a lot more about your system to advise on how exactly to do so. I must be sleeping, but I can't think about a program that lists all Unicode characters. A stupid and dirty solution would be: cat(u 31 = \u31\n) cat(u 32 = \u32\n) ... How can I vectorize this? Alberto Monteiro __ 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] Inverse of encodeString
What is the inverse of encodeString? For example, \u1 is some Unicode symbol. If I do s - encodeString(\u1) then s will be the string \001. But anything I do with s, will not return the Unicode that corresponds to \u1: cat(s, \n) # prints \001 cat(\u1, \n) # prints y with umlaut Alberto Monteiro __ 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] Latex \ell symbol in plotmath
Prof Brian Ripley wrote: I must be sleeping, but I can't think about a program that lists all Unicode characters. A stupid and dirty solution would be: You realize there are millions of them? (2^21, in theory.) :-) Yes, but in Windows there are only 256... cat(u 31 = \u31\n) cat(u 32 = \u32\n) ... How can I vectorize this? ?intToUtf8 E.g. to look at a range around U+2113, cat(intToUtf8(0x2110L+0:9, TRUE), \n) This does not work in R 2.4.1 for Windows. 0x2110L returns an error, and intToUtf8 returns an error on anything except the simplest calls: intToUtf8(33) # error. Argument x must be an integer vector intToUtf8(33:35) # ok intToUtf8(40 + 0:9) # error. Argument x must be an integer vector Alberto Monteiro __ 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] Latex \ell symbol in plotmath
hadley wickham wrote: intToUtf8(33) # error. Argument x must be an integer vector intToUtf8(33:35) # ok intToUtf8(40 + 0:9) # error. Argument x must be an integer vector Well you need to give it integers! intToUtf8(33L) intToUtf8(40L + 0:9) As I wrote before, 33L or 40L return an error in R 2.4.1 for Windows. Alberto Monteiro __ 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] Abstract plot
I want to make a plot, but instead of showing _numerical_ values, I would like to show _symbolic_ values. For example, I want to plot a function y = a x + b, where x varies between Xmin and Xmax. I would like the plot to show, in the x-axis, the strings Xmin and Xmax, instead of their numeric values. Is it possible? Alberto Monteiro __ 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] Excel calling R functions
Horace Tso wrote: Is it possible to have Excel call a R function. If not, how about making Excel send off a command to call a R script and then read the result back into Excel. I know, I know, this should belong to some Excel forum, but i just try my luck here. You can always put R in loop, checking each second or minute for the _existence_ of a file. As soon as this file is create, run an R function, and output a file. Then, do the same in Excel: write a file (I _think_ this is possible with macros), enter into a loop waiting for the other file to be created, and procceed after that. Slow, but better than to use Excel alone :-) Alberto Monteiro __ 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] Running an R script without running R
Thomas Adams wrote: Below I have two scripts: (1) cpc2fgroup (2) R.cpc.6_10day.outlook.batch Bash shell script (1) calls the R script (2). It works with R CMD BATH r-script I know how to do this (I do it inside make...) I guess I did not make myself clear. I want to write some GUI applications that will not be used by me (think of the target user as people with the knowledge of a 7-year-old kid). I want this application to have one icon in the Desktop, and it will be launched just by clicking in the icon. In some other message, I read that Rscript might be what I need - but I have to test it. Alberto Monteiro (who does not have any special affinity with the letter I, despite this 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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] R2 always increases as variables are added?
Paul Lynch wrote: I don't think it makes sense to compare models with and without an intercept term. (Also, I don't know what the point of using a model without an intercept term would be, but that is probably just my ignorance.) Suppose that you are 100% sure that the intercept term is zero, or so insignifantly small as not to matter. For example, if you are measuring the density of some material, and you determine a lot of pairs (mass, volume), you know that mass = density * volume, with intercept zero. Alberto Monteiro __ 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] Inverse gamma
Patrick Wang wrote: assume I need to generate X from inverse gamma with parameter (k, beta). should I generate from Y from gamma(-k, beta), then take X=1/Y? Check the Borg of All Wisdom... http://en.wikipedia.org/wiki/Inverse-gamma_distribution Generate Y from gamma(k, 1/beta) (using... rgamma(n = number.of.points, shape = k, scale = 1/beta) ... or ... rgamma(n = number.of.points, shape = k, rate = beta) ) and take X = 1/Y (unless your beta is not the rate parameter...) Alberto Monteiro __ 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] Is it possible to control R with S+?
Bos, Roger wrote: The new version of S+ (version 8.0) is supposed to be able to read R code unmodified. Insightful is supposedly spending a ton of time making S+ able to use all R code without modification. It must be taking them longer than expected because I though it was supposed to be released by the second quarter. I have a S+ license and I haven't heard any announcement from Insightful. Anyone have more of an update? How is S+ dealing with the _ feature? I have some R code that I recklessly wrote with the _, and there is a nonzero probability that my company will install S+ in some computers. Alberto Monteiro __ 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] Testing for existence inside a function
Talbot Katz wrote: I'm having trouble testing for existence of an object inside a function. No, you are having trouble testing for existence of an object _before_ the function is called :-) Suppose I have a function: f-function(x){ ... } and I call it with argument y: f(y) I'd like to check inside the function whether argument y exists. This can't be done, because the error happens before f is called. Try this: f - function(x) x + 1 f(y.does.not.exist) y.does.not.exist The error message is (almost) the same, and it happens when parsing the line. There's no way to change f to change this. Alberto Monteiro __ 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] Testing for existence inside a function
Duncan Murdoch wrote: Try this: f - function(x) x + 1 f(y.does.not.exist) y.does.not.exist The error message is (almost) the same, and it happens when parsing the line. There's no way to change f to change this. That description is true in some languages, but not in R. R doesn't check that args to functions are valid until it needs to use them. For example: f - function(y) 1 # doesn't care if y exists f(y.does.not.exist) [1] 1 Ok, I guess R optimizes every call to f, ignoring its arguments unless needed. f - function(y) 1 # doesn't care if y exists g - function() cat(g was called\n) f(g()) [1] 1 # g was not called Another test: f1 - function(x, y) if (x == 0) y else 1 f1(1, y.does.not.exist) f1(1, g()) The y-argument is never called. So maybe it _might_ be possible to test if y exists inside the function... Alberto Monteiro __ 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] Help with map
I have just learned how to play with map, but something weird (or not) is happening. Suppose I want to draw a map of two countries (that have disconnected components), like Argentina and Brazil. If I command: library(maps) library(mapdata) map(worldHires, c(Argentina, Brazil)) It works fine. However, if I want to _colour_ the interior: map(worldHires, c(Argentina, Brazil), c(cyan, green), fill=T) Then the colors will be assigned to the islands (Marajo in Brazil's North and Tierra del Fuego in Argentina's South) and there will be a recycling. Is there any way to associate one color to each country? Alberto Monteiro __ 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] Query about finding correlations
Lalitha Viswanath wrote: We are trying to find out, which of A or B cause C i.e. We are hypothesising that C is the effect and either A or B, not both is the cause. (...) I would greatly appreciate any inputs on the best statistcal approach to tackle this problem. I am thinking that we can find correlation coefficients between A and C, and between B and C, but I am not sure this answers the question. Also we do not know whether the correlation between them is linear or non linear. If the causation (not the correlation) is not linear, then the correlation (which is linear, always) may not be the best indicator. Take, as an extreme case, this: A - (-50:50) + 100 * rnorm(101) B - abs((-50):50) + 10 * rnorm(101) C - A^2 / 50 + rnorm(101) cor(A, C) cor(B, C) A is obviously the cause of C, but B (in some cases) is better correlated to C than A to C. Alberto Monteiro __ 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] importing excel-file
Hans-Peter wrote: I can't call read.xls while Excel is opening the xls file. R crashes and must be aborted. Could not reproduce here (Win2000, Excel 2003, R 2.4.1 and 2.5.0.alpha). Who knows... This is Windows XP, Excel 2003, R 2.4.1. but I cannot think of anything that could cause such a problem: xlsReadWrite works on the plain file (Excel is not even needed), doesn't lock the file and, if there were problems, handles eventual exceptions before leaving the DLL. If you have a reproducible test case I would be very interested in it. Maybe the problem is with Windows XP. I know that it locks some files, even when I open for reading. For example, if I open 'test.xls' file, then I can't issue the DOS command 'copy test.xls new_test.xls'. ...but the following should make you happy too: test9 - read.xls( filename, sheet = sheet name, rowNames = FALSE, dateTimeAs = isodatetime ) I added colClasses = double, and now it works - but I lose all strings, that become NAs (they don't matter - maybe if they matter, I just have to call read.xls twice). Without colClasses = double, the numbers become meaningless stuff. OTOH, the dates aren't rendered as dates in either case, but using colClasses=double, they are something that be made into dates, with as.POSIXlt(1899-12-30, GMT) + test9[100:120,1] * 86400 Alberto Monteiro __ 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] importing excel-file
Hans-Peter wrote: I added colClasses = double, and now it works - but I lose all strings, that become NAs (they don't matter - maybe if they matter, I just have to call read.xls twice). Without colClasses = double, the numbers become meaningless stuff. If a scalar colClasses argument is given, it will be recycled for all columns (see help). Thus a character value will be coerced to double and becomes NA. I don't understand though what is the matter with teh numbers becoming meaningless stuff. I guess that's because Excel in Portuguese uses comma to separate the integer to the fractional part. With: test9 - read.xls( filename, sheet = sheet name, rowNames = FALSE ) # (dateTimeAs argument default, i.e. numeric) dateTimeToStr( test9$NumberColumn ) you should get valid numbers (and dates). With dateTimeAs=isodatetime there could be localization issues (i.e. the date time formatting of the cell is not recognized). But I'd need a test file to say for sure. I sent you a file in private. But now I can't reproduce the error. Somehow, the localization works fine with the test.xls file, but it doesn't work with the important file. OTOH, I guess I found a new problem. The attached file (in private, R-help will filter it out) includes two spreadsheets. They are exactly identical, as I tried to copy and paste them. test1 - read.xls(filename, sheet = 1, rowNames = FALSE) # works fine test2 - read.xls(filename, sheet = 2, rowNames = FALSE) # returns NULL Alberto Monteiro -- Regards, Hans-Peter --- 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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] importing excel-file
Please ignore the last test case. I thought I had saved it, but I hadn't, sheet = 2 was empty. Now it works. Alberto Monteiro __ 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] importing excel-file
Hans-Peter wrote: Method 2: This method uses library xlsReadWrite. You must know the index of the spreadsheet that you want to load: plan6 - read.xls(filename, sheet = 6, colClasses=double) it works with the sheet name too! You can write: plan6 - read.xls(filename, sheet = sheet name, colClasses=double) Ok, it works. This works in most cases. Huu? (the package is supposed to work in *all* cases...!). Yes, but I did not test *all* cases to make such a strong assertion O:-) Do you use the newest version (v1.3.1 or v1.3.2)? No, I was using 1.1.1 If there are any bugs/issues, please report them to me and they - most likely - will get fixed. Ok - I will do it. Thanks. Alberto Monteiro __ 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] convergence
Ted Harding wrote: There are various ways round this, but a 'for' loop with a fixed number of iterations is not usully one of them! The simplest is to use while(). A possibly strategy is Y.old - initial.Y while(TRUE){ Y - compute.Y(Y.old, ...) if(abs(Y - Y.old) small.number) break Y.old - Y } This will loop indefinitely until the convergence criterion abs(Y - Y.old) small.number is met, and then stop. I guess some precaution must be taken to prevent that the loop runs forever. Those algorithms that must optimize something, but run the risk of running forever, sound like the chess playing engine: we know that a deterministic solution exists (there is a finite number of chess positions), but it's not practical to check all of them. I read somewhere that computer loop problems are treated as if the computer was playing chess agains Murphy: it tries hard to solve the problem, but sometimes he must give up a path and backtrack to a less optimum but faster solution. Do I make any sense? Alberto Monteiro __ 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] importing excel-file
Hans-Peter wrote: This works in most cases. Huu? (the package is supposed to work in *all* cases...!). Do you use the newest version (v1.3.1 or v1.3.2)? If there are any bugs/issues, please report them to me and they - most likely - will get fixed. Here are the problems I noticed. I can't call read.xls while Excel is opening the xls file. R crashes and must be aborted. I have a spreadsheet where in the first line A1..ZZ1 there are the names of some products. However, A1 is empty, but A8..A311 store dates. The other fields, like B8..B311, store numbers. So, if I do: test1 - read.xls(filename, sheet = sheet name) I will lose the first column: test1[1,1] will be B2. Also, the numbers aren't recovered as numbers, but as strings, in _local_ format (which the evil geniuses of M$ decided should be represented with commas) (BTW: what are those Levels that appear when I type test[x,y]?) If I do: test2 - read.xls(filename, sheet = sheet name, colClasses = double) I will get the numbers correctly, but I will still lose the A8..A311 column. If I do: test3 - read.xls(filename, sheet = sheet name, colNames = FALSE) I will get A8..A311 (but these are not numbers, and I can't convert them to the dates. Probably they represent the number of days since 1900-01-01, take a day or two, because Excel programmers were stupid and didn't know that 1900 was not a leap year). Finally, if I do: test4 - read.xls(filename, sheet = sheet name, colNames = FALSE, colClasses = double) I get A8..A311 as something remotely similar to dates (I can even display them as dates: as.POSIXlt(1899-12-30, GMT) + test4[8:17, 1] * 86400 will return a vector of dates!), but then I will lose the meaning of the columns, because test4[,1] is no longer the list of the product names. So it seems that none of the four possibilities is entirely satisfactory. Alberto Monteiro __ 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] erratic behavior of match()?
Bernhard Klingenberg wrote: Thank you! Is floating point arithmetic also the reason why 1 %% 0.1 gives the surprising answer 0.1 Floating point arithmetic is one of the most Evil things computer science imposed against the mathematicians; any reasonable person, back when computer languages were developed, would use integer and rational numbers by default, only using floating point approximations when the integers became too big or when forced to use irrational numbers. But I don't think we can now repair the damage that was done so long ago. Alberto Monteiro __ 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] importing excel-file
Corinna Schmitt wrote: It is a quite stupid question but please help me. I am very confuced. I am able to import normal txt ant mat-files to R but unable to import .xls-file I've tried two ways to import excel files, but none of them seems perfect. Method 1: This method uses library RODBC. The way to import excel files is this: channel - odbcConnectExcel(myfile.xls) tables - sqlTables(channel) # list the names of the spreadsheets name1 - tables[1, TABLE_NAME] # get the name of the 1st spreadsheet plan1 - sqlFetch(channel, name1) # this _should_ work, but it doesn't # The reason is that somehow the names of the sheets are altered plan1 - sqlFetch(channel, sheet name) # this works # but you must type the exact name of the sheet # the next line works, no matter what is name1 (taken from tables) plan1 - sqlQuery(channel, sprintf(select * from [%s], name1)) odbcClose(channel) # close it This is not perfect. Some (most?) of the numerical fields in the spreadsheet are translated to NA and become meaningless. Method 2: This method uses library xlsReadWrite. You must know the index of the spreadsheet that you want to load: plan6 - read.xls(filename, sheet = 6, colClasses=double) This works in most cases. I do not understand the online help. Can please anyone send me the corresponding command lines? help(help) # :-) The .xls-file is attached. No, it's not. In my file we use commas for the decimal format (example: 0,712), changes might be needed. I *think* this is an internal flag. If the numbers are numbers, then this should be no problem. An excel spreadsheet in any language is portable to other languages, even when the evil geniuses of M$ decided to localize function names so that, in Portuguese, we have SENO instead of SIN and RAIZ instead of SQRT. Alberto Monteiro __ 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] importing excel-file
Gabor Csardi wrote: There is also a read.xls command in package gdata, it seems that it uses a perl script called 'xls2csv'. I've have no idea how good this is, never tried it. Btw, xlsReadWrite is Windows-only, so you can use it only if you use windows. Ok, but who would be insane enough to use Excel in Linux or Mac? :-) Alberto Monteiro __ 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] importing excel-file
John C Frain wrote: To avoid complications, save your file as comma separated and use one of the instructions for reading delimited files. If you are using a comma as a decimal point you are probably using ; as a separator. If this is so use read.csv2. Please see the help files for read.table. I think the problem is that we _can't_ alter or write the excel file, or it would be unpractical to do it (say, this xls file is generated by someone else once a day, and we must use it). I usually save the excel file to plain text, and then read it - but this is not the best solution when the file keeps changing. Alberto Monteiro __ 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] Find zeros of analytic functions
Robin Hankin wrote: If iterative methods are appropriate, it's perhaps worth pointing out that Newton-Rapheson works nicely for complex functions. Hmmm... I think there are many cases where Newton-Raphson diverges for complex functions, like those that generate beautiful fractals. Alberto Monteiro __ 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] installing new packages
Bill Shipley wrote: I have just installed the newest version of R (2.4.1) for Windows XP. I can no longer install new packages. When trying to connect to a server (I have tried several) I get the following message: Bacause of the braindead way Windows XP is installed in my machine, the only thing that works is this: (a) outside R, I visit the CRAN site or mirror (the brazilian mirror is faster for me; YKMV) (b) I download the .zip package from the mirror and save it to some known directory (c) using RGui, I use the option to install the package from a local zip The worse that can (and does) happen is that normally the package I want requires other packages, this is easily solved with new downloads-and-installs. Alberto Monteiro __ [EMAIL PROTECTED] 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] Referencing function name from within function
Jim Holtman wrote: myfunction - function(){ + # some calculations + # now get my name + .caller - sys.call() + cat(paste(as.character(.caller[[length(.caller)]]),needs 'xyz'\n)) + } myfunction() myfunction needs 'xyz' I like this! It's even possible to know _which_ function called myfunction and caused the 'disaster' (for bug tracking): myfunction - function() { # some calculations # oops, something wrong here call.stack - sys.calls() n - length(call.stack) cat(as.character(call.stack[[n]]), crashed, called from , as.character(call.stack[[n-1]]), \n) } evil.function - function() myfunction() evil.function() How can I get the _arguments_ to the calls? as.character strips them :-/ Alberto Monteiro __ 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] faster computation of cumulative multinomial distribution
Theo Borm wrote: I have a hunch that it won't work as my p vector typically contains values like: p-c(0.99, 0.005, 0.003, 0.001, 0.0005, 0.0003, 0.0001, 0.5, 0.3, 0.2). So you should expect that only 1/5 will be in the low-prob hole in the average? Or less. And normally I'll have 30-60 holes, with the remainder hole containing 98% of the events. I think the problem here is not with the low probability associated to the total of the holes, but with the different probabilities associated to the different low-probability holes. Hmmm That's right. No way to do a Monte Carlo here! Yes. Its back to square one for me. Maybe I should get a bigger computer. I heard that IBM is selling some quite fancy stuff http://www.top500.org/list/2006/11/100 Would probably be quite a challenge to coerce R to run on such a machine ;-) I don't think a bigger computer will solve it, after all, you might be dealing with probabilities close to 1:(every atom in the Universe) Hmmm... Maybe you can break the problem into several (manageable) parts. For example, you can divide the problem into this: (a) given N shots, how many will hit the lowest-prob hole (the 0.2)? This is easy: it's the binomial distribution. There are three cases to be considered here: those that hit zero times (and this will carry a zero forever), those that hit exactly once, and those that hit two or more. Unless the two or more is not negligible compared to the exactly once, we can treat it as two. The next holes could be done considering the independence of the conditional probabilities. For example, hitting exactly once every hole would be k - length(p) prod(dbinom(1, N - (0:(k-2)), p[k - (0:(k-2))])) BTW, this is exactly dmultinom(c(N-k+1, rep(1, k-1)), prob=p) What I am missing here? Alberto Monteiro __ 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] (Newbie)Basic Basic global vs. local variables
Jim Holtman wrote: The following will work, but I would suggest that you redesign your functions so that they do not use 'globals'; It is not nice for functions to have side-effects. Globals are Evil, but, like goto, sometimes they can make a big program become simpler. I don't see a nice alternative to globals when I have a huge database where a lot of different functions read and write. But maybe this is ideology/religion/politics... Alberto Monteiro __ 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] Wikibooks
Philippe Grosjean wrote: As other have pointed out, the main reason for the lack of success of the R Wiki is that the mailing lists, particularly R-Help, are sooo successful. However, I continue to consider that the mailing list is suboptimal in two cases: (1) when text is not enough to express the idea, and (2) for frequent questions that would certainly deserve a good compilation on a wiki page and a redirection to it everytime the question is asked. I think there's one case where the mailing list is non-optimal: finding examples. This is where a wiki would be great. Say I don't know (and I can't understand the help) how to use the rnorm function. If I do RSiteSearch(rnorm), I will get too much useless information. OTOH, an ideal wikipedia would have a page http://www.r-wiki.org/rnorm, where I could find examples, learn the theory, browse the source code, and have links to similar functions. OK, maybe that's too much, I would be happy just to have some examples :-) Also, RSiteSearching is dangerous, because if someone replies in an ignorant or malicous way (let's be creative: someone asks how can I open the file CONFIG.SYS, and an evil person replies with file.remove(CONFIG.SYS)), then this wrong answer may be accessed by newbies. A wikipedia _may_ have wrong answers, but these are (hopefully) ephemeral. BTW, is it too hard to include the wiki in RSiteSearch? Alberto Monteiro __ 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] Regarding Vista
John Kane wrote: As a somewhat desperate workaround try installing R on a USB and see if you can run if from there. I have 2.4.1 on a USB and it seems to work fine albeit a bit more slowly than from the hard drive. I love this quote, in http://zoonek2.free.fr/UNIX/48_R/02.html, from Barry Rowlingson: At some point the complexity of installing things like this for Windows will cross the complexity of installing Linux... (PS excepting live-Linux installs like Knoppix) Maybe we have reached this point :-) Alberto Monteiro __ 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] Wikibooks
Duncan Murdoch wrote: But the wiki doesn't offer a way to ask questions. I'd be just as happy to answer questions there as here, but there are none there to answer (and the advice there is to ask questions here). I don't know how to organize a wiki to make it easy to ask and answer questions. It's a reasonably good way to collect reference information, but it's not very well suited to QA. The way to ask questions in the Wiki is to micro-vandalize it :-))) Since anyone can edit, if I don't know how to use some function, I can _create_ this page and fill it with my doubts - in the hope that someone will then fix it latter. Example: rnorm This is a very weird function, because things like rnorm(0.975) should return 1.96, but returns numeric(0) And then someone would either rename the page to qnorm, or write a new rnorm page. Alberto Monteiro __ 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] Wikibooks
Romain Francois wrote: Say I don't know (and I can't understand the help) how to use the rnorm function. If I do RSiteSearch(rnorm), I will get too much useless information. OTOH, an ideal wikipedia would have a page http://www.r-wiki.org/rnorm, where I could find examples, learn the theory, browse the source code, and have links to similar functions. OK, maybe that's too much, I would be happy just to have some examples :-) Do you mean something like (it fullfills basically all your requirements) : R rnorm # get the code R ?rnorm # get the help page This works when there's a decent documentation for the function. The functions in the tcltk package, for example, are horribly undocumented, and asking for help only loops to a general help about all (and none) of the functions. The wiki already has a similar thing, for example for rnorm, you can go to: http://wiki.r-project.org/rwiki/doku.php?id=rdoc:stats:Normal I didn't like the way it worked. I searched for rnorm and Norm, and I got a list of pages. Even for this trivial example, I have no idea how I could find anything using the Search. There has been (recently and less recently) some discussions on the r-sig-wiki list about why sometimes you get ~~RDOC~~ instead of the documentation page, it is still a work in progress. The only tricky bit is how do I know that I have to go to stats:normal, well you can ask that to R, for example using that small function : wikiHelp - function( ... , sarcasm = TRUE ){ if( length(hp - help(...) ) 0 ){ hp - tail( strsplit(hp[1], /)[[1]], 3 ) wikiPage - sprintf(http://wiki.r-project.org/rwiki/doku.php?id=rdoc:%s:%s;, hp[1], hp[3]) cat(the following wiki page will be displayed in your browser:, wikiPage, Please feel free to add information if you have some, , sep = \n) if( sarcasm) cat( except if you are an evil person\n) browseURL(wikiPage)} else print( hp ) } Nice code :-) R wikiHelp( rnorm ) ~~RDOC~~ # what is this? R wikiHelp( tkWidgets ) No documentation for 'tkWidgets' in specified packages and libraries: you could try 'help.search(tkWidgets)' R wikiHelp( seq ) Here it worked as expected. The wiki has its own search engine already, so you can go there and use it. I guess you can search for search there and get info on how to search. :-) Or I could ask help(help) to learn how help works :-P Alberto Monteiro __ 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] math-operations and the R-Wiki
Dimitris Rizopoulos wrote: 513 %/% 100 513 %% 100 Now this is a great opportunity to improve the R-Wiki. What about a Pascal page in the R-Wiki, where a list of Pascal-to-R translations would be available? Alberto Monteiro __ 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] Wikibooks
Duncan Murdoch wrote: This works when there's a decent documentation for the function. The functions in the tcltk package, for example, are horribly undocumented, and asking for help only loops to a general help about all (and none) of the functions. I don't remember if you've said which platform you're working on, but if you're on Windows, the TCL/TK documentation is available to you. It's in RHOME/Tcl/doc. This is mentioned in the ?tcltk R help topic. I always find it easier to get the help from the Internet, even using Google (search for tcl/tk grid, for example) than with the internal documentation... I believe most Unix-like systems with TCL/TK support installed would have the same documentation available, but I don't know where. That documentation assumes you're using a TCL interpreter rather than R, so the syntax is all wrong, but there's a mechanical translation from it to R syntax which is described in the ?TkCommands R help topic. So these functions may be horribly documented, but they're not horribly undocumented. :-)) Ok, maybe I should shut up complaining and actually _do_ something useful, like going into the R-Wiki and _writing_ everything I learned about R in the past 6 months... Alberto Monteiro __ 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] Wikibooks
Duncan Murdoch wrote: This works when there's a decent documentation for the function. The functions in the tcltk package, for example, are horribly undocumented, and asking for help only loops to a general help about all (and none) of the functions. I don't remember if you've said which platform you're working on, but if you're on Windows, the TCL/TK documentation is available to you. It's in RHOME/Tcl/doc. This is mentioned in the ?tcltk R help topic. I always find it easier to get the help from the Internet, even using Google (search for tcl/tk grid, for example) than with the internal documentation... I believe most Unix-like systems with TCL/TK support installed would have the same documentation available, but I don't know where. That documentation assumes you're using a TCL interpreter rather than R, so the syntax is all wrong, but there's a mechanical translation from it to R syntax which is described in the ?TkCommands R help topic. So these functions may be horribly documented, but they're not horribly undocumented. :-)) Ok, maybe I should shut up complaining and actually _do_ something useful, like going into the R-Wiki and _writing_ everything I learned about R in the past 6 months... Alberto Monteiro __ 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] RWiki, tcltk and plot
I think I - almost - got the knack for GUI programming using the tcltk library. Maybe I will update the RWiki with this: # # # library(tcltk) # # Create some matrix - nothing about tcltk here # matrix - cbind(rnorm(100), rpois(100, lambda=10), runif(100), rt(100, df=2), rt(100, df=4)) colnames(matrix) - c(Normal, Poisson (lambda=10), U(0,1), Student t (nu=2), Student t (nu=4)) # # Now comes the interesting part # tt - tktoplevel() tkwm.title(tt, A bunch of distributions) dist.widget - NULL plot.widget - NULL for (i in 1:length(colnames(matrix))) { dist.widget[[i]] - tklabel(tt, text=(colnames(matrix))[i]) plot.widget[[i]] - local({ n - i tkbutton(tt, text=PLOT, command=function() plot(matrix[,n])) }) tkgrid(dist.widget[[i]], row=i-1) tkgrid(plot.widget[[i]], row=i-1, column=1) } # # Game over - click and watch !!! # ### # My question: is there any way to integrate the plot part into a tcltk window? Alberto Monteiro __ 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] Wikibooks
Deepayan Sarkar wrote: I was just looking at this page, and it makes me curious: what gives anyone the right to take someone else's mailing list post and include that in a Wiki? Thinks there were posted to public mailing lists are freely copied and distributed. It's a scary thought; I may have posted things in 10 or 12 years ago that might cause me problems today, but I was pretty aware that I was posting to the whole world. Alberto Monteiro __ 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] RWiki, tcltk and plot
Dirk Eddelbuettel wrote: My question: is there any way to integrate the plot part into a tcltk window? Are you aware of the tkrplot package on CRAN ? No. Alberto Monteiro __ 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] faster computation of cumulative multinomial distribution
Theo Borm wrote: I have a series of /unequal/ probabilities [p1,p2,...,pk], describing mutually exclusive events, and a remainder class with a probability p0=1-p1-p2--pk, and need to calculate, for a given number of trials t=k, the combined probability that each of the classes 1...k contains at least 1 event (the remainder class may be empty). To me this reaks of a sum of multinomial distributions, and indeed, I can readily calculate the correct answer for small figures t,k using a small R program. The multinomial distribution is a sum of independent cathegorial distributions, so there's no such thing as a sum of multinomial distributions - unless the sum of multinomial distributions is also a multinomial distribution. Yikes. I think I am saying too much. Just look here: http://en.wikipedia.org/wiki/Multinomial_distribution However, in my typical experiment, k ranges from ~20-60 and t from ~40-100, and having to calculate these for about 6e9 experiments, a quick calculation on the back of a napkin shows me that I will not be able to complete these calculations before the expected end of the universe. But what do you want to calculate? The probabilities? I already figured out that in this particular case I may use reciprocal probabilities, and if I do this I get an equation with only 2^k terms, which would shorten my computations to a few decades. Isn't there a faster (numerical approximation?) way to do this? I don't know what you want to do, but maybe this is the case where a Monte Carlo Simulation would give a fast and accurate result. Let's say you want to estimate some function of the outcome of this multinomial. Just to fix ideas: # p - the probabilities p - c(1, 2, 3, 2, 4, 4, 2, 1, 2, 3, 1, 2, 3, 4, 3, 2, 1, 2) # normalize so that sum(p) = 1 p - p / sum(p) # get k k - length(p) # number of trials, say 100 t - 100 # x is the simulation for each trial. x[1] is the number of # times we got the thing with probability p[1], etc # # x - rmultinom(1, k, p) # simulates 1 experiment # # f is the function that you will use to evaluate x # f - function(x) { return(sum(x * 1.05^-(0:(length(x)-1 # anything can come here } # # A Monte Carlo simulation will generate a lot of xs and # get a distribution of f(x) # # Simulate 1 xs x - rmultinom(1, k, p) # takes almost zero time # Evaluate 1 f(x). apply(matrix, 1 (rows) or 2 (coluns), function) f.dist - apply(x, 2, f) # analyse it hist(f.dist) # etc R has dbinom /and/ pbinom functions, but unfortunately only a dmultinom and no pmultinom function... perhaps because there is no (known) faster way? There's a rmultinom Alberto Monteiro __ 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] tcltk, tclRequire and Tktable help
I know almost nothing about the tcltk library, and the documentation seems very poor. What's the meaning of this error, and is there any way to fix it? I'm running R 2.4.1 in a Windows XP machine where I have almost no privileges (but at home I am the Evil Overlord of a Linux machine...) library(tcltk) tclRequire(Tktable) # [1] FALSE # Warning message: # Tcl package 'Tktable' not found in: tclRequire(Tktable) Alberto Monteiro __ 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] tcltk crashing R after the (ab)use of tkwait
Running this: library(tcltk) tt - tktoplevel() done - tclVar(0) but - tkbutton(tt, text=OK, command=function() tclvalue(done) - 1) tkpack(but) tkwait.variable(done) works as fine as long as I click the OK. However, if I close the window (by clicking in the X), R enters into an infinite loop and there's no way of returning except by closing the R window. Why? What am I doing wrong? Alberto Monteiro __ 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] tcltk crashing R after the (ab)use of tkwait
Dieter Menne wrote: library(tcltk) tt - tktoplevel() done - tclVar(0) but - tkbutton(tt, text=OK, command=function() tclvalue(done) - 1) tkpack(but) tkwait.variable(done) works as fine as long as I click the OK. However, if I close the window (by clicking in the X), R enters into an infinite loop and there's no way of returning except by closing the R window. Works for me with R-2.4.1 on Windows 2000. So better tell us about the details of your operating system. R 2.4.1 on Windows XP. Should they work differently? Alberto Monteiro __ 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] Wikibooks
As a big fan of Wikipedia, it's frustrating to see how little there is about R in the correlated project, the Wikibooks: http://en.wikibooks.org/wiki/R_Programming Alberto Monteiro __ 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] Help with grep (and similar functions)
This works: grep(([A-Za-z]*) , Aaaa 3 x 0 Bbbb) # 1 This also works: grep(([A-Za-z]*) , Aaaa 3 x 0 Bbbb, value=T) # Aaaa 3 x 0 Bbbb However, I want a grep that returns the _matched_ pattern, which, in this case, would be Aaaa. How can I do it? Alberto Monteiro __ 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] Help with grep (and similar functions)
Gabor Grothendieck wrote: Try this: library(gsubfn) pat - ([[:upper:]][[:lower:]]*) s - Aaaa 3 x 0 Bbbb strapply(s, pat, backref =-1)[[1]] Ok, I think I got it. For example, if I want to retrieve Aaaa and Bbbb (which could be any strings) or the numbers 3 and 0, I would do this: library(gsubfn) pat - ^([a-zA-Z ]+) ([1-9]*[0-9]) x ([1-9]*[0-9]) ([a-zA-Z ]+)$ s - My team 3 x 0 the team from Outer Space x - strapply(s, pat, c)[[1]] Then x[1] is s, x[2] is My team, x[3] is 3, x[4] is 0 and x[5] is the team from Outer Space. Thanks. Alberto Monteiro __ 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] Large matrix into a vector
A Ezhil wrote: I have a matrix HR(9x27). I would like to make a single vector with elements: t(HR[,1]) followed by t(HR[,2]) and then t(HR[,3] ... etc. Is there any neat way of converting this matrix into a vector rather doing something like c(t(HR[,1]), t(HR[,2]), t(HR[,3]) ..)? It might be simpler than you thought... HR - matrix(1:(9*27), nrow=9) # just to create a 9x27 matrix c(HR) # oops, here it is! Alberto Monteiro __ 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] Large matrix into a vector
Prof Brian Ripley wrote: We have already seen three solutions. I don't like to see the use of c() for its side effects. In this case Marc's as.vector seems to me to be self-explanatory, and that is a virtue in programming that is too often undervalued. I agree; but for our enlightnment, what are the side effects of c()? Alberto Monteiro __ 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] Reading config data from text files
Pete Cap wrote: I'm writing a tcl/tk gui tool to wrap around RMySQL for some co-workers. Good luck; I find the documentation on the library(tcltk) very poor, lacking examples for most of the functions. Alberto Monteiro __ 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] Get home directory and simple I/O
Is there any generic function that gets the home directory? This should return /home/user in Linux and x:/Documents and Settings/user (or whatever) in Windows XP. Another (unrelated) question: what is the _simplest_ way to read and write R variables to/from files such that they are stored in a human-readable but R-like form? For example, if (say), x is a vector defined as x - c(1, 2, 3), can I write (and read) x as a file with just one line, namely: c(1, 2, 3) ? Alberto Monteiro __ 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] Binary information convert into hexadecimal
Corinna Schmitt wrote: Given is binary information and should be translated into integer and afterwards into hexadecimal: 0 0 If you have a _string_ of 0s and 1s, and you want to convert it to an integer, I think the best way should be: (1) convert to a vector of 0s and 1s (2) convert to integer (3) an integer to hex conversion is trivial (sprinf(%x, n)) (1): as.integer(unlist(strsplit(0100101, NULL))) will do the job: strsplit breaks the string, unlist transforms the resulting list into a vector of (length-one) strings, and as.integer converts the strings into integers (0 or 1). (2): x - c(1, 1, 0, 1, 0) n - sum(x * 2^seq(length(x)-1, 0, -1)) x is the vector of 0s and 1s from (1) above. length(x) is its length. seq(length(x) - 1, 0, -1) will be the vector of integers (in this case) 4, 3, 2, 1, 0. 2^seq(length(x)-1, 0, -1) will be the powers of two 16, 8, 4, 2, 1. x * 2^... will form the terms of the polynomial sum will compute 2^(n-1) x[0] + 2^(n-2) x[1] + ... + 2 * x[n-1] + x[n] Alberto Monteiro __ 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] Get home directory and simple I/O
Prof Brian Ripley wrote: But the request was for a *generic* solution. On Windows there might not be anything corresponding to a home directory (and the rw-FAQ discusses the concept and how R resolves this). The best answer I know of is path.expand(~). Thanks, this is the only solution that works for Windows XP, and it will probably work for Linux. Also, in Windows, there are variables homedrive and homepath, that I could combine to form the path (probably this is what path.expand does :-)) Sys.getenv(homepath) Sys.getenv(homedrive) Alberto Monteiro __ 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] Over-writing functions from other packages? What is a good strategy??
Gabor Grothendieck wrote: Could you call yours Edges? Is it a good idea to have two different functions, whose name differs by case sensitivity? I believe this is a shortcut to Chaos :-) IMHO, case sensitivity should _only_ be used for aesthetical purposes, else it may result in errors that are very difficult to find. Of course, YKMV.[*] Alberto Monteiro [*] I always use S.I. units :-) __ 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] newbie upgrade from 2.4.0 to 2.4.1 question
Mark Fisher wrote: I am new to R. I installed version 2.4.0 some time ago and I find that some packages I want to use require 2.4.1. I am using Windows XP. Do I need to uninstall R first before running the setup file for 2.4.1 or does the setup file do the right thing? You don't have to uninstall. It does the right thing. But it will not delete the older 2.4.0 version from the Desktop, and all other places where Windows XP places programs. Alberto Monteiro __ 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] XML - can create but can't save
What's wrong with this? library(XML) tt - xmlHashTree() head - addNode(xmlNode(head), character(), tt) test - addNode(xmlNode(test, attrs=c(pi=4)), head, tt) tt # ok saveXML(tt, file=test.xml) # error The error (in Portuguese) is: Erro em saveXML(tt, file = test.xml) : nenhum método aplicável para saveXML which could translate to Error in saveXML(tt, file = test.xml) : no method applicable to saveXML Alberto Monteiro __ 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] XML - can create but can't save
I wrote: library(XML) tt - xmlHashTree() head - addNode(xmlNode(head), character(), tt) test - addNode(xmlNode(test, attrs=c(pi=4)), head, tt) tt # ok saveXML(tt, file=test.xml) # error I found a way to circumvent this error, by replacing the saveXML line with: sink(test.xml) print(tt) sink() Alberto Monteiro __ 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] Bad points in regression
I have a question, maybe it's better to explain by example: alpha - 0.3 beta - 0.4 sigma - 0.5 err - rnorm(100) err[15] - 5; err[25] - -4; err[50] - 10 x - 1:100 y - alpha + beta * x + sigma * err ll - lm(y ~ x) plot(ll) Now, the graphs clearly show that 15, 25 and 50 are the indexes of the bad points. How can I retrieve this information from ll? Alberto Monteiro __ 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] Bad points in regression
Ted Harding wrote: alpha - 0.3 beta - 0.4 sigma - 0.5 err - rnorm(100) err[15] - 5; err[25] - -4; err[50] - 10 x - 1:100 y - alpha + beta * x + sigma * err ll - lm(y ~ x) plot(ll) ll is the output of a linear model fiited by lm(), and so has several components (see ?lm in the section Value), one of which is residuals (which can be abbreviated to res). So, in the case of your example, which(abs(ll$res)2) 15 25 50 extracts the information you want (and the 2 was inspired by looking at the residuals plot from your plot(ll)). Ok, but how can I grab those points _in general_? What is the criterium that plot used to mark those points as bad points? names(ll) gives: [1] coefficients residuals effects rank [5] fitted.values assignqrdf.residual [9] xlevels call terms model None of them include information about those bad points. Alberto Monteiro __ 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] abs(U) 0 where U is a vector?
Benjamin Dickgiesser wrote: I am looking for a way to compare if every element of a vector is 0. i.e. while(abs(U) 0) { .. } Notice that abs(U) [and, in general, most functions that are defined on scalars, like sin, cos, exp, ...], when U is a vector, operates on the _elements_ of U, so abs(U) is just a vector of non-negative elements. Likewise, (U 0) [and, in general, most relations that are defined on scalars, like (U != 0), (U == 0), (U = 1 U = 2)], when U is a vector, operates on the _elements_ of U, so (U 0) is just a vector of logical values. So, you must take some operation that will check if all components of a vector of logical (boolean) elements are non-zero. The most obvious solution is to _multiply_ those logical elements, because FALSE will become zero, and zero * anything = zero, so if any component of U is = 0, you get zero: prod(U 0) But this is not the most elegant solution, because there is a function to check if all [and another to check if any] component of a vector of booleans are [is] true: it's all(V) [resp. any(V)]. So: all(U 0) Sanity check: U - c(-1, 1, 2) all(U 0) # FALSE U[1] - 3 all(U 0) # TRUE Alberto Monteiro __ 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] How to create a list that grows automatically
Young-Jin Lee asked: I would like to know if there is a way to create a list or an array (or anything) which grows automatically as more elements are put into it. ??? I think this is the default behaviour of R arrays: x - vector(length=0) # create a vector of zero length x[1] - 2 x[10] - 3 x[length(x) + 1] - 4 x # 2 NA NA ... 3 4 What I want to find is something equivalent to an ArrayList object of Java language. In Java, I can do the following thing: // Java code ArrayList myArray = new ArrayList(); myArray.add(object1); myArray.add(object2); // End of java code myArray - vector(length=0) myArray - c(myArray, object1) myArray - c(myArray, object2) myArray # array with 2 strings Alberto Monteiro __ 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] Error distribution question
Cristina wrote: I was wondering if somebody could offer me some advice on which error distribution would be appropriate for the type of data I have. I'm studying what continuous predictor variables such as grooming received, rank, etc. affect the amount of grooming given. This response variable is continuous with many zeros, and so positively skewed. This kind of variable is very common in prospecting (oil, mining) industries, and also in medical research. It's neither continuous nor discrete, because of the weight on zero. Basically, it is a combination of _two_ variables: X: a Bernoulli trial, such that p(X = 0) = 1 - p (failure) and p(X = 1) = p (success) Y: the continous variable that represents numerically the success So, we have the final variable as X * Y. For example, if you are going do model the economic value of a possible oil field, a potential gold mine, or a new experimental drug, you must assing a non-zero (1-p) to the possibility that the oil field has no economic value, the mine has no gold, or the new drug has so many collateral effects that no g*vernment in the world (except maybe ... name here your favourite) will allow it. Then you have to estimate the return in the case of success. Alberto Monteiro __ 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] Generic distributions
Matthias Kohl wrote: in distr you can do: library(distr) N - Norm(mean = 1, sd = 2) p(N)(0.5) r(N)(100) !!! not: p(N, 0.5) or r(N, 100) !!! A detailed description of package distr is given in package distrDoc. library(distrDoc) vignette(distr) Thanks!!! This is almost perfect. It even has (some) arithmetics!!! z1 - Norm(mean = 1, sd= 0.6) z2 - Norm(mean = 2, sd= 0.8) z1+z2 Distribution Object of Class: Norm mean : 3 sd : 1 Warning message: arithmetics on distributions are understood as operations on r.v.'s see 'distrARITH()'; for switching off this warning see '?distroptions' in: print(object) Alberto Monteiro __ 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] How to open more windows to make more graphs at once!
Ted Harding wrote: Creating more than one graphic windows is, as far as I know, not possible in R. But, as to whether/to what extent X or equivalent is available for MS Windows, that is another question on which I have no expertise. X11() seems to work for Windows XP. Alberto Monteiro __ 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] Is there a quick way to count the number of times each element in a vector appears?
Dylan Arena wrote: I'm writing a function that calculates the probability of different outcomes of dice rolls (e.g., the sum of the highest three rolls of five six-sided dice). You know there are simpler ways to do this, don't you? Alberto Monteiro __ 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] Package RODBC
I have some questions about the RODBC package. library(RODBC) # required for those who want to repeat these lines 1st, I noticed that the following sequence does not work: channel - odbcConnextExcel(test.xls) tables - sqlTables(channel) name1 - tables[1, TABLE_NAME] # this should be the name plan1 - sqlFetch(channel, name1) # bang! odbcClose(channel) However, I can circumvent this with: channel - odbcConnextExcel(test.xls) tables - sqlTables(channel) name1 - tables[1, TABLE_NAME] # this should be the name plan1 - sqlQuery(channel, sprintf(select * from [%s], name1)) # ok odbcClose(channel) 2nd, it seems that only pure strings (which are not links to strings) and numerical values are correctly fetched or selected. Is this a bug? 3rd, when do something like plan1[,1] a weird message about Levels appear. What is that? Alberto Monteiro __ 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] Generate random numbers up to one
Ted Harding wrote: And, specifically (to take just 2 RVs X and Y), while U = X/(X+Y) and V = Y/(A+Y) are two RVs which summ to 1, the distribution of U is not the same as the distribution of X conditional on (X+Y = 1). This question appeared in October 2006, and the answer was the Dirichlet distribution with parameters (1,1,1...1): http://en.wikipedia.org/wiki/Dirichlet_distribution It's the distribution of uniform U1, U2, ... Un with the restriction that U1 + U2 + ... + Un = 1. Alberto Monteiro __ 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] Generic distributions
Is there any class that generalizes distributions? For example, I could say x - generic_distribution(normal, list(mean=1, sigma=0.5)) and then use it like rgeneric_distribution(100, x) to get a sample of 100, or pgeneric_distribution(0.5, x) to get the pdf at (x = 0.5). In the openbugs/winbugs package, that uses a language that looks like R/S, we can do things like x ~ dnorm(mu, tau), forget that x is a normal with mean mu and variance 1/tau, and then treat it generically. Alberto Monteiro PS: this is noise... but due to spam invasion, anything that increases the nonspam/spam ratio should be welcome :-) __ 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] plot(): I want to display dates on X-axis.
Sarthi M. wrote: I want to display dates on my x-axis of the plot. Dates are a problem. There's a standard for dates, but it seems that most users and software didn't catch up :-/ The variable dat is a data frame. The first column has numeric values and second column has date. e.g. dat [,1]dat[,2] [1,]300 20060101 [2,]257 20060102 [3,]320 20060103 [4,]311 20060104 [5,]297 20060105 [6,]454 20060106 [7,]360 20060107 [8,]307 20060108 the command I am performing is:: plot(x=dat[1], y=as.character(dat[2])) Hmmm... When I needed something similar, I did this day: y - dat[,1] # because in a (xy) plot, x is the scale and y the data years - floor(dat[,2] / 1) months - floor(dat[,2] / 100) %% 100 days - dat[,2] %% 1 x - ISOdate(years, months, days) plot(x, y) Kindly suggest some method by which I can perform my task of displaying the first column values on y-axis against dates on x-axis. Of course, you can combine all that into one line, but readability will be blown up: plot(ISOdate(floor(dat[,2] / 1), floor(dat[,2] / 100) %% 100, dat[,2] %% 1), dat[,1]) Alberto Monteiro __ 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] 0 * NA = NA
Is there any way to force 0 * NA to be 0 instead of NA? For example, suppose I have a vector with some valid values, while other values are NA. If I matrix-pre-multiply this by a weight row vector, whose weights that correspond to the NAs are zero, the outcome will still be NA: x - c(1, NA, 1) wt - c(2, 0, 1) wt %*% x # NA Alberto Monteiro __ 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] 0 * NA = NA
Ted Harding wrote: Is there any way to force 0 * NA to be 0 instead of NA? No (AFAIK), and it is pretty reasonable to define it this way. If you want to treat the NAs as zeros, use x[is.na(x)] - 0 Doing it in precisely that way would have the problem that it would not give you NA when it should. For example: x - c(1, NA, 1) wt - c(2, 1, 1) Then, after x[is.na(x)] - 0, the result of x %*% wt should be NA, but your method would give 3. That's precisely my thought - since you may have read my thoughts, it's time to recalibrate my alluminium helmet. But I also thought about something else. What is the meaning of NA? NA is a _missing_ value, and is.infinite(NA) returns FALSE [OTOH, is.finite(NA) returns FALSE too - this is weird]. A missing value times zero is zero. OTOH, 1/NA is NA, so NA could mean Inf. Maybe binary logic can't adequately handle such ideas :-/ if (NA == 0) is NA, then is.finite(NA) should be NA too... if (NA == 0) 2 else 3 # gives an error This is why I suggested a method which tests for corresponding elements of x = NA and y = 0, since what Alberto Monteiro wanted was 0*NA = 0, when that combination occures. I.e. %*NA% - function(x,y){ X-x;X[(is.na(x))(y==0)]-0; Y-y;Y[(is.na(y))(x==0)]-0; return(X%*%Y) } This method is fine. I had already done something similar Of course, the problem begins to grow if we want, for example, to use elementary matrices to transform a matrix. The 2x2 matrix that switches two lines, rbind(c(0,1), c(1,0)) will not switch a matrix with NAs: switch - rbind(c(0,1), c(1,0)) testmatrix - rbind(c(1,2,3,4), c(5,6,7,8)) switch %*% testmatrix # ok testmatrix[2,2] - NA switch %*% testmatrix # not ok But I digress... Alberto Monteiro __ 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] random uniform sample of points on an ellipsoid (e.g. WG
Russell Senior wrote: This is essentially the approach I (and my local helpers) have taken. With garden-variety calculus, we have derived a function t(z) that maps equal area over the oblate ellipsoid. We select t from a uniform distribution and then find the corresponding z dimension. The expression is quite hairy, but apparently analytically correct (I haven't found any errors yet), if possibly unstable numerically. Hmmm... I have just thought about something. The ellipsoid is almost a sphere, so every formula for the sphere should map into a formula for the ellipsoid that can be expressed as a power series in the eccentricity or the oblateness. Since these parameters are small, most of these series will converge very fast, and it might be possible to do most transformations with them. So, you could have a fast and accurate series that would generate the random latitudes. The derivative with respect to z of area on the ellipsoid at a plane of latitude intersecting at a height z above the equator, where the ellipsoid has been scaled such that a is the polar radius and the equatorial radius is 1, is: dA/dz = 2 * pi * f where f = sqrt((z^2 * (1 - a^2))/a^4 + 1) You find the function t(z) such that dA/dt is constant. Then you select from a uniform distribution and then find the value of z that corresponds. Yikes! I can't read a formula in ascii notation :-P Let's parametrize the ellipsoid by the corresponding sphere, using coordinates lats (latitude of the corresponding sphere) and lon. So: x = a cos(lats) cos(lon) y = a cos(lats) sin(lon) z = b sin(lats) But then the surface are between lats and lats + dl will require a little bit of calculus... Russell Senior ``I have nine fingers; you have ten.'' So do Frodo, Sauron and brazilian president Lula :-) Alberto Monteiro __ 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] random uniform sample of points on an ellipsoid (e.g. WG
R Heberto Ghezzo, Dr wrote: I do not know if I am completely out of it but . . . if x,y,z is a point in a sphere and [u,v,w]'A[u,v,w] = 1 is the equation of an ellipsoid and A = T'T (cholesky) then T.[x,y,z] should be a point in the ellipsoid ? isn't it? Yes, it's a point _on_ the ellipsoid, but it's not uniformly distributed over the ellipsoid. The easy part is generating points on the ellipsoid, the hard part is generating them uniformly. For example, imagine a very flat ellipsoid, so flat that it's almost a disk. Then A is something like the diagonal matrix diag(c(1, 1, 0.0001)), T (let's call it Chol) is diag(c(1,1,0.01)), and a plot of Chol.[x,y,z] will look like this: v - cbind(rnorm(1000), rnorm(1000), rnorm(1000)) # there may be a better way to write the expression below: v - v / sqrt(v[,1]^2 + v[,2]^2 + v[,3]^2) # now v is uniformly distributed over the sphere Chol - diag(c(1, 1, 0.01)) ep - v %*% t(Chol) plot(ep[,1], ep[,2]) with a clear trend to generate points closer to the equator then in the polar regions, against the assumption that they should be uniformly distributed over the surface of the ellipsoid. Alberto Monteiro __ 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] get more than get
This must be a stupid question, but is there any extension of get? For example: x - 10 get(x) # gives me 10 get(x^2) # gives me an error Alberto Monteiro __ 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] matrix manipulations
Anup Menon Nandialath wrote: I have a basic question with R. I'm generating a set of random variables and then combining them using the cbind statement. The code for that is given below. for (i in 1:100) { y - rpois(i,lambda=10) X0 - seq(1,1,length=i) X1 - rnorm(i,mean=5,sd=10) X2 - rnorm(i,mean=17,sd=12) X3 - rnorm(i,mean=3, sd=24) ind - rep(1:5,20) } data100 - cbind(y,X0,X1,X2,X3,ind) First, why the loop? For i in 1:99, this code is a waste of computer time. The code should be: i - 100 y - rpois(i,lambda=10) X0 - seq(1,1,length=i) X1 - rnorm(i,mean=5,sd=10) X2 - rnorm(i,mean=17,sd=12) X3 - rnorm(i,mean=3, sd=24) ind - rep(1:5,20) data100 - cbind(y,X0,X1,X2,X3,ind) but when i look at the data100 table, the y values now take the observation count. The y values should be the same as y. y is a (random) array of integers. Alberto Monteiro __ 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] What is a expression good for?
I mean, I can generate a expression, for example, with: z - expression(x+y) But then how can I _use_ it? Is it possible to retrieve information from it, for example, that z is a sum, its first argument is x (or expression(x)) and its second argument is y? Alberto Monteiro __ 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] ts; decompose; plot and title
Is there any way to give a decent title after I plot something generated by decompose? For example: # generate something with period 12 x - rnorm(600) + sin(2 * pi * (1:600) / 12) # transform to a monthy time series y - ts(x, frequency=12, start=c(1950,1)) # decompose z - decompose(y) # plot plot(z) Now, the title is the ugly Decomposition of additive time series. How can do this with a decent title, like Analysis of UFO abductions? Alberto Monteiro __ 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] eigenvalue ordering
Kaustubh Patil wrote: Is it possible to get unordered eigenvalues and eigenvectors of a symmetric matrix in R? Yes, see help(eigen). If you are strict about the unordered part, do a sample(set, size) to randomize the eigenvalues. Alberto Monteiro __ 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] eigenvalue ordering
Peter Dalgaard wrote: Is it possible to get unordered eigenvalues and eigenvectors of a symmetric matrix in R? Yes, see help(eigen). Er, where do you see anything about (un)order? As far as I know, there's no natural ordering of eigenvalues and eigenvalue algorithms generally find them in either increasing or decreasing order (or closest to specified value). eigen orders the values. From help(eigen): values: a vector containing the p eigenvalues of 'x', sorted in _decreasing_ order, according to 'Mod(values)' in the asymmetric case when they might be complex (even for real matrices). For real asymmetric matrices the vector will be complex only if complex conjugate pairs of eigenvalues are detected. So, if you are strict about getting unordered eigenvalues, you must shuffle them :-) Alberto Monteiro __ 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] using integrate in a function definition
theo borm wrote: jjj-function(www) {2*integrate(dnorm,0,www)$value} kkk-function(www) {2*(pnorm(www)-0.5)} xxx-seq(0:5) yyy-jjj(xxx) zzz-kkk(xxx) produces no errors, but: yyy [1] 0.6826895 zzz [1] 0.6826895 0.9544997 0.9973002 0.367 0.994 1.000 Why is this? Is this some R language feature that I've completely missed? Yes. Some functions work on vectors (and matrices), so when you give a vector, it returns a vector. This is true for most common functions (sin, cos), arithmetic operations (with the caveat that different dimensions for the arguments may cause unexpected outcomes) and some internal functions (dnorm, pnorm). So, if you write sin(0:10) or dnorm((-3):3), you get a vector. Some other functions don't, and this is the case with integrate. For example: fff - function(x) x integrate(fff, 0, 1) # ok integrate(fff, 0, 1:5) # will integrate from 0 to 1 and ignore 2:5 'plot' will probably fall into some code that uses this vector-in-vector-out hypothesis, and then fail when the size of x differs from the size of y. Alberto Monteiro PS: fff - function(x) 1 integrate(fff, 0, 1) # error. why? __ 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] pdf with an exact size
Is it possible to create a pdf output file with an (as nearly as possible) exact size? For example, if I want to draw in an A4 paper (210 x 297 mm) a square of 100 x 100 mm, how can I do it? FWIW, about 6 months ago I learned here how to create an exact png image. For example, if I want a 500 x 500 black square in a 1000 x 1000 white png, occupying the center of the png, the procedure is this: png(image.png, width=1000, height=1000, bg=white) par(mar=c(0,0,0,0)) # reset margins plot(0, xlim=c(0, 999), ylim=c(0, 999), col=white) par(usr=c(0, 999, 0, 999)) points(c(250, 250, 749, 749, 250), c(250, 749, 749, 250, 250), type=l, col=black) dev.off() However, I don't know how do this with a pdf monstr... oops... file. Alberto Monteiro __ 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] pdf with an exact size
I must be stupid and/or crazy... I figured out the solution a few minutes after I asked :-/ Is it possible to create a pdf output file with an (as nearly as possible) exact size? Yes: pdf(a4.pdf, width=210, height=297, bg=white, paper=a4) par(mar=c(0,0,0,0)) # reset margins plot(0, xlim=c(0, 210), ylim=c(0, 297), col=white) par(usr=c(0, 210, 0, 297)) points(c(100, 100, 200, 200, 100), c(100, 200, 200, 100, 100, 100), type=l, col=black) dev.off() Alberto Monteiro __ 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 tcl/tk
This must be dumbest question ever asked, but... When I ask help.search(tcltk), I get a reference to tcltk-package. When I ask help(tcltk-package), I get rtfm(s) in 'R_HOME/Tcl/doc'. But then when I ask help.search(R_HOME), I get nothing. Is something missing here in those help pages? Alberto Monteiro __ 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 tcl/tk
Gabor Grothendieck wrote: But then when I ask help.search(R_HOME), I get nothing. Is something missing here in those help pages? Note that R_HOME corresponds to the R.home() path So I guess there _is_ something missing in those help pages, after all! Instead of R_HOME, the help page should mention R.home() evil grin Alberto Monteiro __ 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] Worm distribution :-)
I don't know if anyone has heard this tale, but it runs more or like this way: A biologist was studing a (semi-spherical) cave where bats lives. He fell asleep in the cave, and he woke up in the middle of the night. Half-dreaming, he thought that he was outside, because glow-worms were living in the walls, and they looked like stars. However, he noticed that, unlike a real sky, these stars had no _pattern_: there were no recognized images like The Cross, a Scorpion, The Hunter, etc. When he woke up, he conjectured that the reason we _can_ see patterns in the real sky is that the stars are randomly distributed, while the glow-worms tried to keep a distance to each other. My question: what is the best way to generate a glow-worm-like distribution? I imagine that using a Latin Hypercube would leave too many holes in the (x,y) plane. Alberto Monteiro __ 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] I really don't understand functions in R :-)
An example: n - 3 f - function(x) x^n f(2) # [1] 8 n - 2 f(2) # [1] 4 f # function(x) x^n Ok, I know this is trivial, because function f is foverer bound to the variable n. But how can I _fix_ n when I define _f_, so that changing _n_ won't change the function f? Alberto Monteiro __ 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.