Re: [R] conditional assignments and calculations
Michael Lachmann lachmann at eva.mpg.de writes: : : Hello! : : I am using the TeXmacs interface to R. (Though I encountered a similar : problem when using Sweave) : In doing calculations I often ecounter this scenario: I'll have some : calculations in my file: : -- : A=read.lots.of.data() : : B=huge.calculation.on(A) : : C=another.calculation.on(B) : -- : Now, if A has already been read, I don't need to re-read it. If B has : already been calculated, I don't need to recalculate it. But I would : like to be able to just press 'enter' on each of them. : : So, I would like R to somehow figure out dependencies (a bit like in : Makefiles) : : I implemented something like this with the following functions: : -- : touch=function(x) {attr(x,last.updated)=Sys.time();x} : : last.updated=function(a) { :if( length(attr(a,last.updated)) == 0 ) { : Sys.time() :} else { :attr(a,last.updated) :} : } : : depends-=function(a,value,...) { :args=list(...) :if( length(attr(a,last.updated)) == 0 ) { : a - value : a -touch(a) :} else { : lu=(sapply(args,function(x) last.updated(x)-last.updated(a) 0 )) : if( sum(lu)0 ) { : a - value : a -touch(a) : } :} :a : } : : Then I can implement what I wanted above as follows: : -- : if( !exists(A) ) { A=read.lots.of.data(); A=touch(A) } : : depends(B,A)=huge.calculation.on(A) : # this means the assignment 'B=huge.calculation.on(A)' is : # done only if A has been updated more recently than B. : : depends(C,B)=another.calculation.on(B) : # dito for C more recent than B. : -- : And now I can carelessly press 'enter' on these expression that might : otherwise take hours to compute. Each variable has a datestamp of the : last time it was updated, and I do each calculation conditional on : whether certain variables have been recently changed. I can also save : A,B,C to a file,later load them, and the calculations will not be redone. : : But this solution is quite ugly, because of several problems: : : 1. To call 'depends(A,B)=f(B)' the first time, A has to already exist, : otherwise I get an error (before I enter the depends- function.) The technique used to implement mulitple return values shown in http://tolstoy.newcastle.edu.au/R/help/04/06/1406.html could be adapted to this problem. Using that technique the code would look like this: depends[A,B] - f(B) and A would not have to pre-exist. You define a structure with a class of depends, say: depends - structure(NA, class = depends) and then define the [-.depends action on that structure in an analogous way to what was done there. : 2. I would also like to have a convenient way to do : if( !exists(A) ) { A=read.lots.of.data(); A=touch(A) } : maybe something like: : depends(A)-read.lots.of.data() : But that doesn't work, because of 1. : or : A %set% read.lots.data() : But that doesn't work, because I haven't figured out a way for a : function to change one of its variables. : (Maybe I could do A=A %set read.lots.of.data(), but that is really ugly...) Is this what you want? R f - function(x,v) assign(as.character(substitute(x)), v, parent.frame()) R x # x does not exist Error: Object x not found R f(x,3) R x # now it does [1] 3 - can be used if the eval.parent is a third way (see #3 below). : 3. It would be nice to be able to do touch(A) instead of A=touch(A) touch - function(x) eval.parent(substitute(attr(x,last.updated)-Sys.time())) x - 3 touch(x) : : 4. If I modify A without calling 'A=touch(A)', then B will not be : updated next time I call 'depends(B,A)=huge.calculation.on(A)'. So it : would be nice to have the variable's 'last updated' time updated : automatically. (Though then it is a bit problematic to decide what the : 'last updated' time should be for variables loaded from a file...) If its done in a function you could use on.exit to ensure that it gets updated when leaving the function. : : 5. The whole thing is rather cludgy. But I haven't found a good way to : implement it. : : Suggestions? : : Thanks, : : Michael : __ [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
[R] creating new varFunc classes in nlme .. error: Don't know how to get coefficients for .. object
Hello. I am trying my hand at modifying the varFunc class varExp, but I must be missing a step. All I want to do right now is make a working copy of varExp, call it varExp2, and then later change it. coef.varExp2, coef-.varExp2, and Initialize.varExp2 all seem to work properly after I construct them. I can successfully use the commands: v2 - varExp2(form = ~age|Sex,fixed = c(Female=0)) v2 - Initialize(v2, Orthodont) But, after this when I type v2 at the prompt, I get the message: Error in coef.varFunc(x, uncons = FALSE, allCoef = TRUE) : Don't know how to get coefficients for varExp2 object Im not sure what to do. Im sure it is a simple fix or statement I need to enter. Can anyone offer suggestions? Do I have to use the command varFunc(varExp2) at some point? As background, I created VarExp2 by using: varExp2 - function (value = numeric(0), ... [and the rest of the VarExp function] ...) At the end of the function I had to change the statement: c(varExp,... to c(varExp2... Then I used: setMethod(Initialize,varExp2, function (object, data, ...) { form - formula(object) ... [and the rest of the Initialize.varExp function] ...) I did the same with the coef and coef- functions. Im not sure why coef, coef-, and Initialize seem to work (they produce the same output and attributes as varExp), but still I get the error message. Would it have anything to do with the warning I get when I create the coef.varExp2 and other functions: In the method signature for function coef-, class varExp2 has no current definition in: matchSignature(signature, fdef, where) Any help would be greatly appreciated. Do I somehow need to tell nlme where to find my new functions? John __ [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
Re: [R] Reading multiple files into R
On Sun, 3 Oct 2004, Vikas Rawal wrote: Thanks Kevin and Roger. This gave me the clue and was a great help. I have been trying it out. There is some problem in the code that still needs to be figured out. For the first 9 files, paste(wb-0, i, vc.dbf, sep=) works fine. But as you rightly guessed, I have more files. So when I use paste(wb-, formatC(i, width=2, flag=0), vc.dbf, sep=), dbf.read does not work. formatC works find if I use it in cat(paste.). It displays the file names correctly. But when I use it in dbf.read, it gives the following error. *** res[[i]] - maptools:::dbf.read(paste(wb-, format(i, width=2,flag=0), vc.dbf, sep=)) Error in maptools:::dbf.read(paste(wb-, format(i, width=2,flag=0), vc.dbf, sep=)) Error in maptools:::dbf.read(paste(wb-, format(i, width = 2, flag = 0), : unable to open DBF file *** Of course, the data files are all right. I can read them individually. What do you think could be the problem? Look for the difference between: for(i in 1:12) cat(paste(wb-, format(i, width=2,flag=0),vc.dbf, + sep=), \n) and for(i in 1:12) cat(paste(wb-, formatC(i, width=2,flag=0),vc.dbf, + sep=), \n) You need formatC(), not format(). sprintf() is: for(i in 1:12) cat(paste(wb-, sprintf(fmt=%0.2d, i),vc.dbf, + sep=), \n) for the same as formatC(). On the rbind question: Now I have a vector of lists res. I would like to append all these components into one single dataframe. I tried the following: rbind(for (i in 1:17) res[[i]]) - distvc But this will not work. It works if I individually specify all the res components. this works: xx - list(df1=data.frame(x=rnorm(10), y=rnorm(10), f=rep(A, 10)), + df2=data.frame(x=rnorm(10), y=rnorm(10), f=rep(B, 10))) xxx - NULL for(i in 1:length(xx)) xxx - rbind(xxx, xx[[i]]) for() loops are not a bad thing if you are not repeating the operation (like reading in data) very frequently, and seem to me easier to debug than more sophisticated constructions. This for() loop will run slower as xxx grows, because it needs to re-allocate memory each time round. I would be tempted for many and large xx[[i]] to pre-allocate the combined data frame and just slot in the rows for each list component, if I knew that the numbers and classes og the columns were identical. But rbind() is cleaner, even though it will be slower - again, if you only need this a few times, the time hit is compensated for by simplicity. Vikas Kevin Bartz wrote: Roger Bivand wrote: On Fri, 1 Oct 2004, Vikas Rawal wrote: I want to read data from a number of files into R. Reading individual files one by one requires writing enormous amount of code that will look something like the following. maptools:::dbf.read(wb-01vc.dbf)-dist1 maptools:::dbf.read(wb-02vc.dbf)-dist2 maptools:::dbf.read(wb-03vc.dbf)-dist3 maptools:::dbf.read(wb-04vc.dbf)-dist4 maptools:::dbf.read(wb-05vc.dbf)-dist5 maptools:::dbf.read(wb-06vc.dbf)-dist6 maptools:::dbf.read(wb-07vc.dbf)-dist7 maptools:::dbf.read(wb-08vc.dbf)-dist8 maptools:::dbf.read(wb-09vc.dbf)-dist9 * In this case, you could pre-allocate a list and: res - vector(mode=list, length=9) for (i in 1:length(res)) res[[i]] - maptools:::dbf.read(paste(wb-0, i, vc.dbf, sep=)) res - vector(mode=list, length=9) for (i in 1:length(res)) cat(paste(wb-0, i, vc.dbf, sep=), \n) wb-01vc.dbf wb-02vc.dbf wb-03vc.dbf ... gives a check on what file names are being used. For 10 to 99 preserving the 01-09, use paste(wb-, formatC(i, width=2, flag=0), vc.dbf, sep=). If the token is a character (string) that varies, you can roll out a character vector of tokens first and step along it. res - vector(mode=list, length=length(LETTERS)) for (i in 1:length(res)) cat(paste(wb-, LETTERS[i], vc.dbf, sep=), + \n) wb-Avc.dbf wb-Bvc.dbf wb-Cvc.dbf ... Is there a better way of doing this? Vikas __ [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 Good call. Here's a somewhat more R-ified version: res - lapply(paste(wb-, formatC(1:99, width=2, flag=0), vc.dbf, sep=), maptools:::dbf.read) Kevin -- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: [EMAIL PROTECTED] __ [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
[R] Re: creating new varFunc classes in nlme .. error: Don't know how to get coefficients for .. object
Ah!! After way too many hours of work Ive answered my own question. To get varExp2, a copy of varExp, to work as a new varFunc, one needs to use this sort of syntax: update.varExp2 - function (object, data, ...) { print (enter update) val - NextMethod() if (length(val) == 0) { aux - coef(val, allCoef = TRUE) if (!is.null(grps - getGroups(val))) { aux - aux[grps] } attr(val, logLik) - sum(log(attr(val, weights) - exp(-aux * getCovariate(val } val } setMethod(update, varExp2, update.varExp2) And then do the same for all methods used by varExp (see methods(class=varExp)). When writing the setMethod for coef-.varExp2, use cat(coef-.varExp2) in the statement. Or at least that worked for me. But I still have an unanswered question from the other day (see [R] two questions on nlme: error messages and nested variance). What is the best way in nlme to model the variance when the variance of the variance is not constant but is dependent on a covariate? Thanks. John __ [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
[R] Excluding data in R
I was hoping someone could help me! How do you exclude outliers from a set of data? Thanks, Laura [[alternative HTML version deleted]] __ [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
Re: [R] Excluding data in R
Hi, On Sun, 3 Oct 2004, Laura Collins wrote: How do you exclude outliers from a set of data? Your question is too vague. I'm assuming you have a data frame and already know exactly which observations are the outlier(s). If your data frame is called foo.df, and say observation 5 is an outlier, then something like: foo.df[-5, ] will exclude it. Kevin Ko-Kang Kevin Wang PhD Student Centre for Mathematics and its Applications Building 27, Room 1004 Mathematical Sciences Institute (MSI) Australian National University Canberra, ACT 0200 Australia Homepage: http://wwwmaths.anu.edu.au/~wangk/ Ph (W): +61-2-6125-2431 Ph (H): +61-2-6125-7407 Ph (M): +61-40-451-8301 __ [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
[R] If loops
Hi, I'm a complete beginner to all this so I was hoping someone could help me! What I'm trying to do is to write a function that returns the coordinates where a vector x is equal to a. So say I invent a vector x: x-c(,5,8,9,8,3). If a is a-8. I want the function to return the coordinates of x where the number 8 appears (i.e. 2 4). I know I need to set up an if loop but I'm really not sure how to do this. Any advice or clues will be much appreciated. Thanks, Laura [[alternative HTML version deleted]] __ [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
Re: [R] If loops
Laura Collins wrote: Hi, I'm a complete beginner to all this so I was hoping someone could help me! What I'm trying to do is to write a function that returns the coordinates where a vector x is equal to a. So say I invent a vector x: x-c(,5,8,9,8,3). If a is a-8. I want the function to return the coordinates of x where the number 8 appears (i.e. 2 4). I know I need to set up an if loop but I'm really not sure how to do this. Any advice or clues will be much appreciated. which(a==x) Please read An Introduction to R, the R FAQ and learn to use the help system as well as the mailing list archives (as described in the Posting Guide cited below at the end of your). Uwe Ligges Thanks, Laura [[alternative HTML version deleted]] __ [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 __ [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
RE: [R] If loops
From: Laura Collins Hi, I'm a complete beginner to all this so I was hoping someone could help me! What I'm trying to do is to write a function that returns the coordinates where a vector x is equal to a. So say I invent a vector x: x-c(,5,8,9,8,3). If a is a-8. I want the function to return the coordinates of x where the number 8 appears (i.e. 2 4). I know I need to set up an if loop but I'm really not sure how to do this. You don't if you use R (or S-PLUS): which(x %in% a) or even something like x.good - x[! x %in% a] HTH, Andy Any advice or clues will be much appreciated. Thanks, Laura __ [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
[R] help working with persp plot function
Hi, I don't understand why this is not working. Help is appreciated. I need to plot the following as a surface, but persp returns an error. tpcp_xy[1:10,] X_COORD Y_COORD TPCP 1 465459.7 175924.1 0.85 2 466145.8 324017.3 2.30 3 467720.2 372143.1 1.56 4 470293.2 348064.8 2.87 5 476566.9 205501.8 0.94 6 44.9 142561.0 1.31 7 479207.0 162919.6 3.04 8 480890.8 290641.3 2.20 9 488865.9 159201.4 2.30 10 490328.3 248049.0 2.81 Note that the X_COORD column is sorted in an increasing order. objects(2) [1] TPCPX_COORD Y_COORD persp(X_COORD, Y_COORD, TPCP) Error in persp.default(X_COORD, Y_COORD, TPCP) : increasing x and y values expected Ok so why is the function returning an error message? Thanks for your time and insights. Steve Friedman email [EMAIL PROTECTED] Department of Forestry Michigan State University __ [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
Re: [R] help working with persp plot function
Hi Steve, persp() is looking for x and y to be the coordinates on the axes rather than corresponding directly to the points. So, something like x - c(1:3) y - c(1:3) z - c(1:9) Take a look at the third example in help(persp) to clarify. For your data you might try the surface() function in the fields package - see .e.g http://www.cgd.ucar.edu/stats/Software/Fields/fields.demo.shtml Good luck! Andrew On Sun, Oct 03, 2004 at 06:08:57PM -0400, Steven K Friedman wrote: Hi, I don't understand why this is not working. Help is appreciated. I need to plot the following as a surface, but persp returns an error. tpcp_xy[1:10,] X_COORD Y_COORD TPCP 1 465459.7 175924.1 0.85 2 466145.8 324017.3 2.30 3 467720.2 372143.1 1.56 4 470293.2 348064.8 2.87 5 476566.9 205501.8 0.94 6 44.9 142561.0 1.31 7 479207.0 162919.6 3.04 8 480890.8 290641.3 2.20 9 488865.9 159201.4 2.30 10 490328.3 248049.0 2.81 Note that the X_COORD column is sorted in an increasing order. objects(2) [1] TPCPX_COORD Y_COORD persp(X_COORD, Y_COORD, TPCP) Error in persp.default(X_COORD, Y_COORD, TPCP) : increasing x and y values expected Ok so why is the function returning an error message? Thanks for your time and insights. Steve Friedman email [EMAIL PROTECTED] Department of Forestry Michigan State University __ [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 -- Andrew Robinson Ph: 208 885 7115 Department of Forest Resources Fa: 208 885 6226 University of Idaho E : [EMAIL PROTECTED] PO Box 441133W : http://www.uidaho.edu/~andrewr Moscow ID 83843 Or: http://www.biometrics.uidaho.edu No statement above necessarily represents my employer's opinion. __ [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
[R] compile R-1.9.1 with Portland fortran
Dear R-users, Has any one compiled R-1.9.1 successfully with Portland pgcc pgCC and pgf77? I can not configurate it with ./configure since it told me checking for Fortran name-mangling scheme... configure: error: cannot compile a simple Fortran program Can any one give me some advice. Thank you. My platform: Linux-2.4.26, pentium4, portland compiler 5.1 Jin __ [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
[R] Could anyone tell me how to extract pvalue from lm fitting?
Dear R people, I have a naive question: after fitting lm to a data, I can't extract the pvalue corresponding to a specific covariate in a direct way. Could anyone give me a hint? Thank you very much. Frank __ [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
[R] Off-Topic: LaTeX package listings
Hola! I ask here since I learnt from this list that the LaTeX package listings should be good for typesetting R code. I encountered one problem: \begin{lstlisting} X %*% V \end{lstlisting} in the output the * in %*% disappears! same with %/%, etc, the / disappears. Any ideas? Kjetil -- Kjetil Halvorsen. Peace is the most effective weapon of mass construction. -- Mahdi Elmandjra __ [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
Re: [R] Off-Topic: LaTeX package listings
On Sun, 2004-10-03 at 21:26, Kjetil Brinchmann Halvorsen wrote: Hola! I ask here since I learnt from this list that the LaTeX package listings should be good for typesetting R code. I encountered one problem: \begin{lstlisting} X %*% V \end{lstlisting} in the output the * in %*% disappears! same with %/%, etc, the / disappears. Any ideas? Kjetil That's because the % is a comment character in LaTeX. Thus, anything after it will be ignored. For program code, you generally want to use the 'verbatim' or 'smallverbatim' environment: \begin{verbatim} X %*% V \end{verbatim} In the verbatim environment, all characters are treated literally, rather than interpreted by any special meaning. Outside of that, say in a regular LaTeX document, you can escape the %: \% HTH, Marc Schwartz __ [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