Re: [R] Opening package manual from within R
On Tue, 23 Aug 2011, Tyler Rinker wrote: Simple question but searching rseek did not yield the results I wanted. Question: Is there a way to open a help manual for a package from within R. For instance I would like to type a function in r for the tm package and R would open that PDF as seen here: http://cran.r-project.org/web/packages/tm/tm.pdf -The vignette function exists for vignettes [vignette(package.name)] so I assume the same exists for manuals. You assume wrong. Vignettes PDFs are installed as part of the package (and often take minutes to regenerate): the PDF version of the help pages (what you seem to call 'the package manual') is not (in general). In many cases what other people (including the author, e.g. me for RODBC) call the 'package manual' is a PDF in the doc directory (which may or may not be a vignette). The assumption is that people will use search facilities or the hints given by the help titles in help(package=tm) or browse the HTML version of the same information (e.g. via help.start). But you can (provided you have pdflatex etc in your path) generate the PDF version of the help pages by R CMD Rd2pdf /path/to/installed/package It will even open it in a browser for you (unless you use --no-preview). You could easily encapsulate this in a function by e.g. showPDFmanual - function(package, lib.loc=NULL) { path - find.package(package, lib.loc) system(paste(shQuote(file.path(R.home(bin), R)), CMD, Rd2pdf, shQuote(path))) } Alternatively *for packages on CRAN only* you can access the version on CRAN by browseURL. -I do not want library(help=package.name) as this is not detailed enough. I am running R 2.14.0 beta on a windows 7 machine Reproducible code does not seem appropriate in this case. But accurate 'at a minimum' information (and no HTML) does. There is no such version as '2.14.0 beta', and will not be for a couple of months. If you are running a beta version of R it is old, so please update to a released or patched version. (Also, any version calling itself '2.14.0 Under development' is old and needs updating: the current R-devel displays no version number.) [[alternative HTML version deleted]] __ R-help@r-project.org 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. -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@r-project.org 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] Efficient way to Calculate the squared distances for a set of vectors to a fixed vector
I am pretty new to R. So this may be an easy question for most of you. I would like to calculate the squared distances of a large set (let's say 2) of vectors (let's say dimension of 5) to a fixed vector. Say I have a data frame MY_VECTORS with 2 rows and 5 columns, and one 5x1 vector y. I would like to efficiently calculate the squared distances between each of the 2 vectors in MY_VECTORS and y. The squared distance between two vectors x and y can be calculated: distance - crossprod(x-y) Without looping, what is the efficient code to achieve this? Thanks. __ R-help@r-project.org 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 message: object of type 'closure' is not subsettable
I think I have found my problem, but I dont know how to correct it. I have found an old post saying that it might be a problem if the starting values are evaluated at Inf (see link here http://r.789695.n4.nabble.com/Help-about-nlminb-function-td3089048.html) But how can I run nlminb without the starting values being evaluated at Inf.? What I more specifically want to do, is to set C and D equal to 0 for the starting values, but to have it evaluated for Inf for the rest (C and D equal to what the function says). Does anyone have a suggestion on how to do this? Is the solution to do loops instead of vectorisation? Please help me Thanks in advance! -- View this message in context: http://r.789695.n4.nabble.com/Error-message-object-of-type-closure-is-not-subsettable-tp3752886p3764513.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] Efficient way to Calculate the squared distances for a set of vectors to a fixed vector
Let's say your fixed vector is x, and y is the list of vectors that you want to create the squared distance to x with, then: x-c(1:5) y-list() y[[1]]-sample(c(1:5),5) y[[2]]-sample(c(1:5),5) y[[3]]-sample(c(1:5),5) y distances-lapply(y,function(a,b) crossprod(a-b), b=x) #lapply goes over the list elements of y and executes the function for each of these list elements #the current list element of y is plugged in for a and x is plugged in for b and the crossproduct is formed distances-unlist(distances) HTH, Daniel Wei Wu wrote: I am pretty new to R. So this may be an easy question for most of you. I would like to calculate the squared distances of a large set (let's say 2) of vectors (let's say dimension of 5) to a fixed vector. Say I have a data frame MY_VECTORS with 2 rows and 5 columns, and one 5x1 vector y. I would like to efficiently calculate the squared distances between each of the 2 vectors in MY_VECTORS and y. The squared distance between two vectors x and y can be calculated: distance - crossprod(x-y) Without looping, what is the efficient code to achieve this? Thanks. __ R-help@r-project.org 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. -- View this message in context: http://r.789695.n4.nabble.com/Efficient-way-to-Calculate-the-squared-distances-for-a-set-of-vectors-to-a-fixed-vector-tp3764589p3764616.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] Efficient way to Calculate the squared distances for a set ofvectors to a fixed vector
You could do something like this: # data nrows - 2L ncols - 5L myVec - array(rnorm(nrows * ncols), dim = c(nrows, ncols)) y - rnorm(ncols) temp - t(myVec) - y result - colSums(temp * temp) # check all.equal(as.numeric(crossprod(myVec[1L, ] - y)), result[1L]) #... (And don't use a data.frame, but a matrix.) regards, Enrico -Ursprüngliche Nachricht- Von: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Im Auftrag von Wei Wu Gesendet: Mittwoch, 24. August 2011 07:18 An: r-help@r-project.org Betreff: [R] Efficient way to Calculate the squared distances for a set ofvectors to a fixed vector I am pretty new to R. So this may be an easy question for most of you. I would like to calculate the squared distances of a large set (let's say 2) of vectors (let's say dimension of 5) to a fixed vector. Say I have a data frame MY_VECTORS with 2 rows and 5 columns, and one 5x1 vector y. I would like to efficiently calculate the squared distances between each of the 2 vectors in MY_VECTORS and y. The squared distance between two vectors x and y can be calculated: distance - crossprod(x-y) Without looping, what is the efficient code to achieve this? Thanks. __ R-help@r-project.org 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@r-project.org 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] Easier ways to create .Rd files?
On 24/08/11 01:55, Jonathan Greenberg wrote: R-helpers: Are there any ways to auto-generate R-friendly (e.g. will pass a compilation check) .Rd files given a set of .R code? How about GUIs that help properly format the .Rd files? Thanks! I want a basic set of .Rd files that I can update as I go, but as with most things my documentation typically lags behind my coding by a few days. --j See inlinedocs package on CRAN. There are several examples included. Best, Philippe Grosjean __ R-help@r-project.org 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] Efficient way to Calculate the squared distances for a set ofvectors to a fixed vector
Hi Wei Wu, What about: x - matrix(rnorm(2*5),ncol=5) y - rnorm(5) distances - rowSums((x-y)**2) Cheers, Tsjerk On Wed, Aug 24, 2011 at 8:43 AM, Enrico Schumann enricoschum...@yahoo.de wrote: You could do something like this: # data nrows - 2L ncols - 5L myVec - array(rnorm(nrows * ncols), dim = c(nrows, ncols)) y - rnorm(ncols) temp - t(myVec) - y result - colSums(temp * temp) # check all.equal(as.numeric(crossprod(myVec[1L, ] - y)), result[1L]) #... (And don't use a data.frame, but a matrix.) regards, Enrico -Ursprüngliche Nachricht- Von: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Im Auftrag von Wei Wu Gesendet: Mittwoch, 24. August 2011 07:18 An: r-help@r-project.org Betreff: [R] Efficient way to Calculate the squared distances for a set ofvectors to a fixed vector I am pretty new to R. So this may be an easy question for most of you. I would like to calculate the squared distances of a large set (let's say 2) of vectors (let's say dimension of 5) to a fixed vector. Say I have a data frame MY_VECTORS with 2 rows and 5 columns, and one 5x1 vector y. I would like to efficiently calculate the squared distances between each of the 2 vectors in MY_VECTORS and y. The squared distance between two vectors x and y can be calculated: distance - crossprod(x-y) Without looping, what is the efficient code to achieve this? Thanks. __ R-help@r-project.org 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@r-project.org 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. -- Tsjerk A. Wassenaar, Ph.D. post-doctoral researcher Molecular Dynamics Group * Groningen Institute for Biomolecular Research and Biotechnology * Zernike Institute for Advanced Materials University of Groningen The Netherlands __ R-help@r-project.org 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] Autocorrelation using library(tseries)
Dear R list I am trying to understand the auto-correlation concept. Auto-correlation is the self-correlation of random variable X with a certain time lag of say t. The article http://www.mit.tut.fi/MIT-3010/luentokalvot/lk10-11/MDA_lecture16_11.pdf; (Page no. 9 and 10) gives the methodology as under. Suppose you have a time series observations as say X = c(44,41,46,49,49,50,40,44,49,41) # For autocorrelation with time lag of 1 we define A = c(41,46,49,49,50,40,44,49,41) # first element of X not considered B = c(44,41,46,49,49,50,40,44,49) # Last element of X not considered cor(A,B) [1] -0.02581234 However, if I try the acf command using library tseries I get acf(X, 1) Autocorrelations of series âXâ, by lag     0     1  1.000 -0.019 So by usual correlation command (where same random variable X is converted into two series with a time lag of 1), I obtain auto-correlation as -0.02581234 and by acf command I get auto-correlation = -0.019 (for time lag of 1). I am not able to figure out where I am going wrong or is it my understanding of auto-correlation procedure is wrong? Will be grateful if someone guides . Vincy [[alternative HTML version deleted]] __ R-help@r-project.org 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] Passing a large amount of parameters to a function
Hello, I have a function with a long list of parameters (of different types, numeric and string) myFunc -function(p1, p2, p3, p4, p5...etc) { do.something(p1,p2,) } I want to loop over this to provide a different set of parameters to the list every time. for (ii in 1:N) { myFunc(p1(ii), p2(ii),etc) } I would like to simplify the notation and use some kind of structure, maybe a list or dataframe. However when I try, for example, the following: params = data.frame(cbind(p1=c(1,2,3), p2=5, p3=c(3,2,1))) myFunc -function(params) { attach(params) do.something(p1,p2,) detach(params) } for (ii in 1:N) { myFunc(params[ii,]) } do.something fails because p1, p2,... are now factors, and i have to as.numeric() or as.char() each parameter before applying mathematical operators (such as simple multiplication). This defeats the purpose of simplifying the code. Is there any decent, simple way of doing this? Can I unfactor a row of a data.frame? or a list? (BTW, as.numeric() won't work since some of the parameters are strings, same goes for stringsAsFactors in data.frame) Thanks, Eran [[alternative HTML version deleted]] __ R-help@r-project.org 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] Passing a large amount of parameters to a function
You could use something like the following: paramsList - list(p1 = 1:10, p2 = -5:5, p3 = 5, p4 = 8) params - unlist(as.relistable(paramsList)) myFunc - function (params) { params - relist(params, skeleton = paramsList) p1 - params$p1 p2 - params$p2 p3 - params$p3 p4 - params$p4 ... } For another example have a look at the online help file of relist(). I hope it helps. Best, Dimitris On 8/24/2011 9:28 AM, Eran Eidinger wrote: Hello, I have a function with a long list of parameters (of different types, numeric and string) myFunc-function(p1, p2, p3, p4, p5...etc) { do.something(p1,p2,) } I want to loop over this to provide a different set of parameters to the list every time. for (ii in 1:N) { myFunc(p1(ii), p2(ii),etc) } I would like to simplify the notation and use some kind of structure, maybe a list or dataframe. However when I try, for example, the following: params = data.frame(cbind(p1=c(1,2,3), p2=5, p3=c(3,2,1))) myFunc-function(params) { attach(params) do.something(p1,p2,) detach(params) } for (ii in 1:N) { myFunc(params[ii,]) } do.something fails because p1, p2,... are now factors, and i have to as.numeric() or as.char() each parameter before applying mathematical operators (such as simple multiplication). This defeats the purpose of simplifying the code. Is there any decent, simple way of doing this? Can I unfactor a row of a data.frame? or a list? (BTW, as.numeric() won't work since some of the parameters are strings, same goes for stringsAsFactors in data.frame) Thanks, Eran [[alternative HTML version deleted]] __ R-help@r-project.org 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. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org 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] Efficient way to Calculate the squared distances for a set ofvectors to a fixed vector
Yes, sorry, so the distance is colSums((t(x)-y)**2) (I knew that) :S Tsjerk On Wed, Aug 24, 2011 at 9:19 AM, Enrico Schumann enricoschum...@yahoo.de wrote: R will subtract the vector columnwise from the matrix (so the vectors need be the columns). x - matrix(0, nrow = 10L, ncol = 5L) y - 1:5 x - y [,1] [,2] [,3] [,4] [,5] [1,] -1 -1 -1 -1 -1 [2,] -2 -2 -2 -2 -2 [3,] -3 -3 -3 -3 -3 [4,] -4 -4 -4 -4 -4 [5,] -5 -5 -5 -5 -5 [6,] -1 -1 -1 -1 -1 [7,] -2 -2 -2 -2 -2 [8,] -3 -3 -3 -3 -3 [9,] -4 -4 -4 -4 -4 [10,] -5 -5 -5 -5 -5 -Ursprüngliche Nachricht- Von: Tsjerk Wassenaar [mailto:tsje...@gmail.com] Gesendet: Mittwoch, 24. August 2011 09:02 An: Enrico Schumann Cc: Wei Wu; r-help@r-project.org Betreff: Re: [R] Efficient way to Calculate the squared distances for a set ofvectors to a fixed vector Hi Wei Wu, What about: x - matrix(rnorm(2*5),ncol=5) y - rnorm(5) distances - rowSums((x-y)**2) Cheers, Tsjerk On Wed, Aug 24, 2011 at 8:43 AM, Enrico Schumann enricoschum...@yahoo.de wrote: You could do something like this: # data nrows - 2L ncols - 5L myVec - array(rnorm(nrows * ncols), dim = c(nrows, ncols)) y - rnorm(ncols) temp - t(myVec) - y result - colSums(temp * temp) # check all.equal(as.numeric(crossprod(myVec[1L, ] - y)), result[1L]) #... (And don't use a data.frame, but a matrix.) regards, Enrico -Ursprüngliche Nachricht- Von: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Im Auftrag von Wei Wu Gesendet: Mittwoch, 24. August 2011 07:18 An: r-help@r-project.org Betreff: [R] Efficient way to Calculate the squared distances for a set ofvectors to a fixed vector I am pretty new to R. So this may be an easy question for most of you. I would like to calculate the squared distances of a large set (let's say 2) of vectors (let's say dimension of 5) to a fixed vector. Say I have a data frame MY_VECTORS with 2 rows and 5 columns, and one 5x1 vector y. I would like to efficiently calculate the squared distances between each of the 2 vectors in MY_VECTORS and y. The squared distance between two vectors x and y can be calculated: distance - crossprod(x-y) Without looping, what is the efficient code to achieve this? Thanks. __ R-help@r-project.org 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@r-project.org 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. -- Tsjerk A. Wassenaar, Ph.D. post-doctoral researcher Molecular Dynamics Group * Groningen Institute for Biomolecular Research and Biotechnology * Zernike Institute for Advanced Materials University of Groningen The Netherlands -- Tsjerk A. Wassenaar, Ph.D. post-doctoral researcher Molecular Dynamics Group * Groningen Institute for Biomolecular Research and Biotechnology * Zernike Institute for Advanced Materials University of Groningen The Netherlands __ R-help@r-project.org 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: evaluation nested too deeply: infinite recursion / options(expressions=)?
I think you have change options(expression = default_value) you can input the command options()in Rconsole And check the value of the expression. and change the value to 100. Hope you over this problem. -- View this message in context: http://r.789695.n4.nabble.com/Error-evaluation-nested-too-deeply-infinite-recursion-options-expressions-tp3344168p3764732.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] setMethods/setGeneric problem when R CMD CHECK'ing a package
R-helpers: I'm trying to build a package, but I'm a bit new to the whole S3/S4 methods concept. I'm trying to add a new definition of the zoo function as.yearmon, but I'm getting the following error when it gets to this point during a package install: *** R CMD INSTALL STARStools * installing to library ‘/Library/Frameworks/R.framework/Versions/2.13/Resources/library’ WARNING: omitting pointless dependence on 'R' without a version requirement * installing *source* package ‘STARStools’ ... ** R ** inst ** preparing package for lazy loading Loading required package: sp raster version 1.9-5 (28-July-2011) Geospatial Data Abstraction Library extensions to R successfully loaded Loaded GDAL runtime: GDAL 1.8.0, released 2011/01/12 Path to GDAL shared files: /Library/Frameworks/GDAL.framework/Versions/1.8/Resources/gdal Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009 Path to PROJ.4 shared files: (autodetected) Attaching package: 'zoo' The following object(s) are masked from 'package:base': as.Date Creating a new generic function for as.Date in STARStools Creating a new generic function for as.list in STARStools Error in setGeneric(f, where = where) : must supply a function skeleton, explicitly or via an existing function Error : unable to load R code in package 'STARStools' ERROR: lazy loading failed for package ‘STARStools’ * removing ‘/Library/Frameworks/R.framework/Versions/2.13/Resources/library/STARStools’ * restoring previous ‘/Library/Frameworks/R.framework/Versions/2.13/Resources/library/STARStools’ *** # Note that these require statements do not appear in the code, but appear in the DESCRIPTION file: require(sp) require(zoo) # Here are the class definitions (filename AAAclassdefinitions.R): setClass(SpatialPointsDataFrameList,representation(list=list),contains=c(SpatialPointsDataFrame)) setClass(SpatialPointsDataFrameListZoo,contains=c(SpatialPointsDataFrameList)) # And here is where it is getting hung up. filename as.yearmon.SpatialPointsDataFrameListZoo_SpatialPointsDataFrameListZoo.R setMethod(as.yearmon, signature(x = SpatialPointsDataFrameListZoo), as.yearmon.SpatialPointsDataFrameListZoo ) # Filename as.yearmon.SpatialPointsDataFrameListZoo.R as.yearmon.SpatialPointsDataFrameListZoo=function(x,...) { newlist=mapply(zoo:::as.yearmon,x@list,MoreArgs=list(...),simplify=FALSE) x@list=newlist return(x) } Thoughts? --j -- Jonathan A. Greenberg, PhD Assistant Project Scientist Center for Spatial Technologies and Remote Sensing (CSTARS) Department of Land, Air and Water Resources University of California, Davis One Shields Avenue Davis, CA 95616 Phone: 415-763-5476 AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307 __ R-help@r-project.org 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] Easier ways to create .Rd files?
Thanks Kevin! Got started with roxygen tonight! Cheers! On Tue, Aug 23, 2011 at 6:28 PM, Kevin Wright kw.s...@gmail.com wrote: I like the roxygen2 package for combining code and documentation. If you use Emacs + ESS, it will even create much of the roxygen code for you (and auto-revise it if you change the function arguments). Kevin Wright On Tue, Aug 23, 2011 at 6:55 PM, Jonathan Greenberg greenb...@ucdavis.edu wrote: R-helpers: Are there any ways to auto-generate R-friendly (e.g. will pass a compilation check) .Rd files given a set of .R code? How about GUIs that help properly format the .Rd files? Thanks! I want a basic set of .Rd files that I can update as I go, but as with most things my documentation typically lags behind my coding by a few days. --j -- Jonathan A. Greenberg, PhD Assistant Project Scientist Center for Spatial Technologies and Remote Sensing (CSTARS) Department of Land, Air and Water Resources University of California, Davis One Shields Avenue Davis, CA 95616 Phone: 415-763-5476 AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307 __ R-help@r-project.org 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. -- Jonathan A. Greenberg, PhD Assistant Project Scientist Center for Spatial Technologies and Remote Sensing (CSTARS) Department of Land, Air and Water Resources University of California, Davis One Shields Avenue Davis, CA 95616 Phone: 415-763-5476 AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307 __ R-help@r-project.org 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 into vector with vertex names
Hi Gabor, Thanks. I will try to figure out the solution you suggest. I found out about melt() from a discussion forum; it seems to me that melt()$value is similar to c(), and when I modified the script as below it 'seems' to be running faster. Anyway in the end I only needed to use a smaller network so the computation went ok. y_s - melt(y_s)[c(upper.tri(y_s)),] #new code y_s - melt(y_s)[melt(upper.tri(y_s))$value,] #old code The reason I needed three columns was for later statistical analysis for which such a format was necessary. Best regards, Joe. On Thu, Aug 18, 2011 at 12:47 PM, Gábor Csárdi csa...@rmki.kfki.hu wrote: Joe, what is melt() supposed to do here? What's wrong with the simple solution of creating a data.frame first, and then filling it with values through a loop? Actually, keeping the matrix is just as good, indexing is just as fast, and takes the same amount of memory as your three column matrix, doesn't it? Gabor On Fri, Aug 5, 2011 at 10:40 AM, joe j joe.st...@gmail.com wrote: Using Igraph, I create shortest paths, then convert the matrix into three column vectors - vertex1, vertex2, shortestpath - as the code below shows. #code for generating shortest path matrix and creating a 3 columns from an igraph graph object y y_s-shortest.paths(y, weights = NULL) y_s - melt(y_s)[melt(upper.tri(y_s))$value,] #Step 2: this is where the trouble with memory occurs y_s[,1] - V(y)$name[y_s[,1]] y_s[,2] - V(y)$name[y_s[,2]] names(y_s)-c(vertex1, vertex2, shortestpath) However I am looking for an alternative way of doing this becase at the second step I run into a fight with my machine's memory. I know I can create vectors using as.vector(), c(), etc, but I am not able to create the two other columns with vertex names. Best regards, Joe. __ R-help@r-project.org 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. -- Gabor Csardi csa...@rmki.kfki.hu MTA KFKI RMKI __ R-help@r-project.org 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] setMethods/setGeneric problem when R CMD CHECK'ing a package
On 24.08.2011 10:30, Jonathan Greenberg wrote: R-helpers: I'm trying to build a package, but I'm a bit new to the whole S3/S4 methods concept. I'm trying to add a new definition of the zoo function as.yearmon, but I'm getting the following error when it gets to this point during a package install: Thhis seesm more appropiate for the R-devel rather than the R-help list. Anyway, see below. *** R CMD INSTALL STARStools * installing to library ‘/Library/Frameworks/R.framework/Versions/2.13/Resources/library’ WARNING: omitting pointless dependence on 'R' without a version requirement * installing *source* package ‘STARStools’ ... ** R ** inst ** preparing package for lazy loading Loading required package: sp raster version 1.9-5 (28-July-2011) Geospatial Data Abstraction Library extensions to R successfully loaded Loaded GDAL runtime: GDAL 1.8.0, released 2011/01/12 Path to GDAL shared files: /Library/Frameworks/GDAL.framework/Versions/1.8/Resources/gdal Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009 Path to PROJ.4 shared files: (autodetected) Attaching package: 'zoo' The following object(s) are masked from 'package:base': as.Date Creating a new generic function for as.Date in STARStools Creating a new generic function for as.list in STARStools Error in setGeneric(f, where = where) : must supply a function skeleton, explicitly or via an existing function Error : unable to load R code in package 'STARStools' ERROR: lazy loading failed for package ‘STARStools’ * removing ‘/Library/Frameworks/R.framework/Versions/2.13/Resources/library/STARStools’ * restoring previous ‘/Library/Frameworks/R.framework/Versions/2.13/Resources/library/STARStools’ *** # Note that these require statements do not appear in the code, but appear in the DESCRIPTION file: require(sp) require(zoo) ??? Not valid in the DESCRIPTION file. Either use this in your code or import from the corresponding Namespaces which you will have to do anyway for the next R release. # Here are the class definitions (filename AAAclassdefinitions.R): setClass(SpatialPointsDataFrameList,representation(list=list),contains=c(SpatialPointsDataFrame)) setClass(SpatialPointsDataFrameListZoo,contains=c(SpatialPointsDataFrameList)) # And here is where it is getting hung up. filename as.yearmon.SpatialPointsDataFrameListZoo_SpatialPointsDataFrameListZoo.R setMethod(as.yearmon, signature(x = SpatialPointsDataFrameListZoo), as.yearmon.SpatialPointsDataFrameListZoo ) You try to register the method pointing to a function definition that does not exist at this point (since you define it below). Just move the definition up. Uwe Ligges # Filename as.yearmon.SpatialPointsDataFrameListZoo.R as.yearmon.SpatialPointsDataFrameListZoo=function(x,...) { newlist=mapply(zoo:::as.yearmon,x@list,MoreArgs=list(...),simplify=FALSE) x@list=newlist return(x) } Thoughts? --j __ R-help@r-project.org 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] Autocorrelation using library(tseries)
Your understanding is wrong. For a start, there is no function acf() in package tseries: it is in stats. And the autocorrelation at lag one is not the correlation omitting the first and last values: it uses the mean and variance estimated from the whole series and divisor n. Have you looked at the reference given on ?acf ? As the help says (This contains the exact definitions used.) Neither the R help pages nor R-help are intended as tutorials in statistics. On Wed, 24 Aug 2011, Vincy Pyne wrote: Dear R list I am trying to understand the auto-correlation concept. Auto-correlation is the self-correlation of random variable X with a certain time lag of say t. The article http://www.mit.tut.fi/MIT-3010/luentokalvot/lk10-11/MDA_lecture16_11.pdf; (Page no. 9 and 10) gives the methodology as under. But that is not the definitive reference, and no, it doesn't (and what it does give is not the conventional definition in the time series literature). Suppose you have a time series observations as say X = c(44,41,46,49,49,50,40,44,49,41) # For autocorrelation with time lag of 1 we define A = c(41,46,49,49,50,40,44,49,41)?? # first element of X not considered B = c(44,41,46,49,49,50,40,44,49) # Last element of X not considered cor(A,B) [1] -0.02581234 However, if I try the acf command using library tseries I get acf(X, 1) Autocorrelations of series ???X???, by lag 0?? 1 ??1.000 -0.019 So by usual correlation command (where same random variable X is converted into two series with a time lag of 1), I obtain auto-correlation as -0.02581234 and by acf command I get auto-correlation = -0.019 (for time lag of 1). I am not able to figure out where I am going wrong or is it my understanding of auto-correlation procedure is wrong? Will be grateful if someone guides . Vincy [[alternative HTML version deleted]] -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@r-project.org 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] Efficient way to Calculate the squared distances for a set ofvectors to a fixed vector
On Wed, 24 Aug 2011, Tsjerk Wassenaar wrote: Yes, sorry, so the distance is colSums((t(x)-y)**2) (I knew that) :S Did you know that ** is deprecated (and almost undocumented), so your readers can hardly be expected to understand that? Please use the documented operator ^ . Tsjerk On Wed, Aug 24, 2011 at 9:19 AM, Enrico Schumann enricoschum...@yahoo.de wrote: R will subtract the vector columnwise from the matrix (so the vectors need be the columns). x - matrix(0, nrow = 10L, ncol = 5L) y - 1:5 x - y [,1] [,2] [,3] [,4] [,5] [1,] -1 -1 -1 -1 -1 [2,] -2 -2 -2 -2 -2 [3,] -3 -3 -3 -3 -3 [4,] -4 -4 -4 -4 -4 [5,] -5 -5 -5 -5 -5 [6,] -1 -1 -1 -1 -1 [7,] -2 -2 -2 -2 -2 [8,] -3 -3 -3 -3 -3 [9,] -4 -4 -4 -4 -4 [10,] -5 -5 -5 -5 -5 -Ursprüngliche Nachricht- Von: Tsjerk Wassenaar [mailto:tsje...@gmail.com] Gesendet: Mittwoch, 24. August 2011 09:02 An: Enrico Schumann Cc: Wei Wu; r-help@r-project.org Betreff: Re: [R] Efficient way to Calculate the squared distances for a set ofvectors to a fixed vector Hi Wei Wu, What about: x - matrix(rnorm(2*5),ncol=5) y - rnorm(5) distances - rowSums((x-y)**2) Cheers, Tsjerk On Wed, Aug 24, 2011 at 8:43 AM, Enrico Schumann enricoschum...@yahoo.de wrote: You could do something like this: # data nrows - 2L ncols - 5L myVec - array(rnorm(nrows * ncols), dim = c(nrows, ncols)) y - rnorm(ncols) temp - t(myVec) - y result - colSums(temp * temp) # check all.equal(as.numeric(crossprod(myVec[1L, ] - y)), result[1L]) #... (And don't use a data.frame, but a matrix.) regards, Enrico -Ursprüngliche Nachricht- Von: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Im Auftrag von Wei Wu Gesendet: Mittwoch, 24. August 2011 07:18 An: r-help@r-project.org Betreff: [R] Efficient way to Calculate the squared distances for a set ofvectors to a fixed vector I am pretty new to R. So this may be an easy question for most of you. I would like to calculate the squared distances of a large set (let's say 2) of vectors (let's say dimension of 5) to a fixed vector. Say I have a data frame MY_VECTORS with 2 rows and 5 columns, and one 5x1 vector y. I would like to efficiently calculate the squared distances between each of the 2 vectors in MY_VECTORS and y. The squared distance between two vectors x and y can be calculated: distance - crossprod(x-y) Without looping, what is the efficient code to achieve this? Thanks. __ R-help@r-project.org 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@r-project.org 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. -- Tsjerk A. Wassenaar, Ph.D. post-doctoral researcher Molecular Dynamics Group * Groningen Institute for Biomolecular Research and Biotechnology * Zernike Institute for Advanced Materials University of Groningen The Netherlands -- Tsjerk A. Wassenaar, Ph.D. post-doctoral researcher Molecular Dynamics Group * Groningen Institute for Biomolecular Research and Biotechnology * Zernike Institute for Advanced Materials University of Groningen The Netherlands __ R-help@r-project.org 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. -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595__ R-help@r-project.org 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] Bug or feature? sum(c(a, b, c)) != (a + b + c)
Thomas Lumley wrote: So it looks as though sum(c(...)) is somwhow independent of the order of its arguments, which implies that the summation it does is not quite the addition corresponding to +. I now feel out of my depth, so hope someone else knows/can find the truth about this! sum() uses a long double accumulator. BTW, summing numbers is (surprisingly?) actually a quite interesting problem. See, for example, the paper ‘A Comparison Of Methods For Accurate Summation’ by John Michael McNamee for some nice example. The paper is available online at http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.103.344rep=rep1type=pdf -- Karl Ove Hufthammer __ R-help@r-project.org 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] spectral analysis
Is there anything in R similar to spectogram command in matlab? As I do not use matlab I can not know what spectogram does. What is wrong with fft or spectrum functions? Regards Petr On Tue, Aug 23, 2011 at 10:20 AM, Petr PIKAL petr.pi...@precheza.cz wrote: Hi Hi all, I am trying to do spectral analysis of a time series data. But I am not sure how to do it. Can anyone tell me if there are any package I can use to do the analysis using fast fourier transform? Maybe ?fft or ?spectrum Regards Petr Thanks in advance. Cassie [[alternative HTML version deleted]] __ R-help@r-project.org 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@r-project.org 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] setMethods/setGeneric problem when R CMD CHECK'ing a package
On 24.08.2011 11:09, Jonathan Greenberg wrote: Hmm, so I moved the function call to the same file as the methods call, and placed it above the method in the file -- but I'm getting the same error. Is there something odd about as.yearmon in the zoo package that it might not be getting defined as a generic function? If so, how would I go about creating a generic? setGeneric(as.yearmon) doesn't seem to cut it. I haven't looked at the details but just commented on the obvious glitches in your code. Now that I tested with R-2.13.1 patched, zoo 1.7-4, sp 0.9-84, it just works for me: setClass(SpatialPointsDataFrameList,representation(list=list),contains=c(SpatialPointsDataFrame)) setClass(SpatialPointsDataFrameListZoo,contains=c(SpatialPointsDataFrameList)) #setGeneric(as.yearmon) as.yearmon.SpatialPointsDataFrameListZoo=function(x,...) { newlist=mapply(zoo:::as.yearmon,x@list,MoreArgs=list(...),simplify=FALSE) x@list=newlist return(x) } setMethod(as.yearmon, signature(x = SpatialPointsDataFrameListZoo), as.yearmon.SpatialPointsDataFrameListZoo ) works for me (at least, it is installable and passes checks, if I add Depends: methods, sp, zoo into the DESCRIPTION file (hence without using a NAMESPACE yet). Uwe Ligges Here's the new file contents: as.yearmon.SpatialPointsDataFrameListZoo=function(x,...) { newlist=mapply(zoo:::as.yearmon,x@list,MoreArgs=list(...),simplify=FALSE) x@list=newlist return(x) } setMethod(as.yearmon, signature(x = SpatialPointsDataFrameListZoo), as.yearmon.SpatialPointsDataFrameListZoo ) --j 2011/8/24 Uwe Liggeslig...@statistik.tu-dortmund.de: On 24.08.2011 10:30, Jonathan Greenberg wrote: R-helpers: I'm trying to build a package, but I'm a bit new to the whole S3/S4 methods concept. I'm trying to add a new definition of the zoo function as.yearmon, but I'm getting the following error when it gets to this point during a package install: Thhis seesm more appropiate for the R-devel rather than the R-help list. Anyway, see below. *** R CMD INSTALL STARStools * installing to library ‘/Library/Frameworks/R.framework/Versions/2.13/Resources/library’ WARNING: omitting pointless dependence on 'R' without a version requirement * installing *source* package ‘STARStools’ ... ** R ** inst ** preparing package for lazy loading Loading required package: sp raster version 1.9-5 (28-July-2011) Geospatial Data Abstraction Library extensions to R successfully loaded Loaded GDAL runtime: GDAL 1.8.0, released 2011/01/12 Path to GDAL shared files: /Library/Frameworks/GDAL.framework/Versions/1.8/Resources/gdal Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009 Path to PROJ.4 shared files: (autodetected) Attaching package: 'zoo' The following object(s) are masked from 'package:base': as.Date Creating a new generic function for as.Date in STARStools Creating a new generic function for as.list in STARStools Error in setGeneric(f, where = where) : must supply a function skeleton, explicitly or via an existing function Error : unable to load R code in package 'STARStools' ERROR: lazy loading failed for package ‘STARStools’ * removing ‘/Library/Frameworks/R.framework/Versions/2.13/Resources/library/STARStools’ * restoring previous ‘/Library/Frameworks/R.framework/Versions/2.13/Resources/library/STARStools’ *** # Note that these require statements do not appear in the code, but appear in the DESCRIPTION file: require(sp) require(zoo) ??? Not valid in the DESCRIPTION file. Either use this in your code or import from the corresponding Namespaces which you will have to do anyway for the next R release. # Here are the class definitions (filename AAAclassdefinitions.R): setClass(SpatialPointsDataFrameList,representation(list=list),contains=c(SpatialPointsDataFrame)) setClass(SpatialPointsDataFrameListZoo,contains=c(SpatialPointsDataFrameList)) # And here is where it is getting hung up. filename as.yearmon.SpatialPointsDataFrameListZoo_SpatialPointsDataFrameListZoo.R setMethod(as.yearmon, signature(x = SpatialPointsDataFrameListZoo), as.yearmon.SpatialPointsDataFrameListZoo ) You try to register the method pointing to a function definition that does not exist at this point (since you define it below). Just move the definition up. Uwe Ligges # Filename as.yearmon.SpatialPointsDataFrameListZoo.R as.yearmon.SpatialPointsDataFrameListZoo=function(x,...) { newlist=mapply(zoo:::as.yearmon,x@list,MoreArgs=list(...),simplify=FALSE) x@list=newlist return(x) } Thoughts? --j __ R-help@r-project.org 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] boxplot from mean and SD data
Dear all, I have a dataset of 3 categorical factors, the mean and the standard deviation of each value. I want to use these values to plot a boxplot, grouped by each of the 3 categorical factors (24 boxplots in total). I don't have a clue on how to do the boxplot from mean and SD data already calculated. Thanks in advance [[alternative HTML version deleted]] __ R-help@r-project.org 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] Odp: boxplot from mean and SD data
Hi Dear all, I have a dataset of 3 categorical factors, the mean and the standard deviation of each value. I want to use these values to plot a boxplot, grouped by each of the 3 categorical factors (24 boxplots in total). I don't have a clue on how to do the boxplot from mean and SD data already calculated. I am afraid that you can not do it. Only if you you accept assumption that distribution within each factor is normal and therefore median equals mean. You can than also use 25 an 75 percentile for box construction. see ?boxplot and ?bxp for internals of plotting function. One possibility also is to generate values by mean and sd parameters to rnorm, although I do not see the reason for doing it. Regards Petr Thanks in advance [[alternative HTML version deleted]] __ R-help@r-project.org 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@r-project.org 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] obtaining p-values for lm.ridge() coefficients (package 'MASS')
Dear Michael Thank you for your pointers. On Tue, Aug 23, 2011 at 4:05 PM, Michael Friendly frien...@yorku.ca wrote: First, you should be using rms::ols, as Design is old. Good to know. I've always wondered why Design and rms, in many cases, were providing similar functions and (upon cursory inspection) identical documentation. Second, penalty in ols() is not the same as the ridge constant in lm.ridge, but rather a penalty on the log likelihood. The documentation for ols refers to ?lrm for the description. I see. At the same time ?rms::ols contains: The penalized maximum likelihood estimate (penalized least squares or ridge estimate) of beta is (X'X + P)^{-1} X'Y., while ?rms::lrm defines P as penalty \times diag(pf) \times penalty.matrix \times diag(pf), where pf is the vector of square roots of penalty factors computed from penalty. This is suspiciously similar to the definition of the classical Ridge Regression beta estimates (X'X + kI)^{-1} X'Y, where k is the lambda constant and I the identity matrix. Hence I was wondering if rms::ols could be forced to use 'P = kI', and thus compute the classical Ridge estimates? I tried to hack rms::ols to ensure that it passed 'kI' to lm.pfit(..., penalty.matrix = ...), but I get strange results so I must be missing something. (See attached.) Third, other ridge estimators (e.g., ElemStatLearn::simple.ridge also give slightly different results for the same data due to differences in the way scaling is handled. And ?ElemStatLearn::prostate points to several other 'ridge'-related functions. However, similar to MASS::lm.ridge, simple.ridge() roughly stops at estimating the betas, hence my question: is the purpose of Ridge Regression to stabilize the coefficients (when the design matrix is collinear) and use the results to infer the relative importance of the coefficients on the response, while keeping the unpenalized model for prediction purposes? Or am I missing something? Thank you Liviu __ R-help@r-project.org 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] obtaining p-values for lm.ridge() coefficients (package 'MASS')
The attachment seems to have been dropped, so I'm pasting the code below. Sorry for that Liviu On Wed, Aug 24, 2011 at 1:44 PM, Liviu Andronic landronim...@gmail.com wrote: Second, penalty in ols() is not the same as the ridge constant in lm.ridge, but rather a penalty on the log likelihood. The documentation for ols refers to ?lrm for the description. I see. At the same time ?rms::ols contains: The penalized maximum likelihood estimate (penalized least squares or ridge estimate) of beta is (X'X + P)^{-1} X'Y., while ?rms::lrm defines P as penalty \times diag(pf) \times penalty.matrix \times diag(pf), where pf is the vector of square roots of penalty factors computed from penalty. This is suspiciously similar to the definition of the classical Ridge Regression beta estimates (X'X + kI)^{-1} X'Y, where k is the lambda constant and I the identity matrix. Hence I was wondering if rms::ols could be forced to use 'P = kI', and thus compute the classical Ridge estimates? I tried to hack rms::ols to ensure that it passed 'kI' to lm.pfit(..., penalty.matrix = ...), but I get strange results so I must be missing something. (See attached.) ols.ridge - function (formula, data, weights, subset, na.action = na.delete, method = qr, model = FALSE, x = FALSE, y = FALSE, se.fit = FALSE, linear.predictors = TRUE, penalty = 0, penalty.matrix, tol = 1e-07, sigma = NULL, var.penalty = c(simple, sandwich), ridge=TRUE, ...) { call - match.call() var.penalty - match.arg(var.penalty) m - match.call(expand = FALSE) mc - match(c(formula, data, subset, weights, na.action), names(m), 0) m - m[c(1, mc)] m$na.action - na.action m$drop.unused.levels - TRUE m[[1]] - as.name(model.frame) if(ridge) lambda - penalty if (length(attr(terms(formula), term.labels))) { dul - .Options$drop.unused.levels if (!length(dul) || dul) { on.exit(options(drop.unused.levels = dul)) options(drop.unused.levels = FALSE) } X - Design(eval.parent(m)) options(drop.unused.levels = dul) atrx - attributes(X) atr - atrx$Design nact - atrx$na.action Terms - atrx$terms assig - DesignAssign(atr, 1, Terms) penpres - FALSE if (!missing(penalty) any(unlist(penalty) != 0)) penpres - TRUE if (!missing(penalty.matrix) any(penalty.matrix != 0)) penpres - TRUE if (penpres missing(var.penalty)) warning(default for var.penalty has changed to \simple\) if (method == model.frame) return(X) scale - as.character(formula[2]) attr(Terms, formula) - formula weights - model.extract(X, weights) if (length(weights) penpres) stop(may not specify penalty with weights) Y - model.extract(X, response) n - length(Y) if (model) m - X X - model.matrix(Terms, X) if (length(atr$colnames)) dimnames(X)[[2]] - c(Intercept, atr$colnames) else dimnames(X)[[2]] - c(Intercept, dimnames(X)[[2]][-1]) if (method == model.matrix) return(X) } else { if (length(weights)) stop(weights not implemented when no covariables are present) assig - NULL yy - attr(terms(formula), variables)[1] Y - eval(yy, sys.parent(2)) nmiss - sum(is.na(Y)) if (nmiss == 0) nmiss - NULL else names(nmiss) - as.character(yy) Y - Y[!is.na(Y)] yest - mean(Y) coef - yest n - length(Y) if (!length(sigma)) sigma - sqrt(sum((Y - yest)^2)/(n - 1)) cov - matrix(sigma * sigma/n, nrow = 1, ncol = 1, dimnames = list(Intercept, Intercept)) fit - list(coefficients = coef, var = cov, non.slopes = 1, fail = FALSE, residuals = Y - yest, df.residual = n - 1, intercept = TRUE) if (linear.predictors) { fit$linear.predictors - rep(yest, n) names(fit$linear.predictors) - names(Y) } if (model) fit$model - m if (x) fit$x - matrix(1, ncol = 1, nrow = n, dimnames = list(NULL, Intercept)) if (y) fit$y - Y fit$fitFunction - c(ols, lm) oldClass(fit) - c(ols, rms, lm) return(fit) } if (!penpres) { fit - if (length(weights)) lm.wfit(X, Y, weights, method = method, ...) else lm.fit(X, Y, method = method, ...) cov.unscaled - chol2inv(fit$qr$qr) r - fit$residuals yhat - Y - r if (length(weights)) { sse - sum(weights * r^2) m - sum(weights * yhat/sum(weights)) ssr - sum(weights * (yhat - m)^2) r2 - ssr/(ssr + sse) if (!length(sigma)) sigma - sqrt(sse/fit$df.residual)
Re: [R] setMethods/setGeneric problem when R CMD CHECK'ing a package
Hmm, so I moved the function call to the same file as the methods call, and placed it above the method in the file -- but I'm getting the same error. Is there something odd about as.yearmon in the zoo package that it might not be getting defined as a generic function? If so, how would I go about creating a generic? setGeneric(as.yearmon) doesn't seem to cut it. Here's the new file contents: as.yearmon.SpatialPointsDataFrameListZoo=function(x,...) { newlist=mapply(zoo:::as.yearmon,x@list,MoreArgs=list(...),simplify=FALSE) x@list=newlist return(x) } setMethod(as.yearmon, signature(x = SpatialPointsDataFrameListZoo), as.yearmon.SpatialPointsDataFrameListZoo ) --j 2011/8/24 Uwe Ligges lig...@statistik.tu-dortmund.de: On 24.08.2011 10:30, Jonathan Greenberg wrote: R-helpers: I'm trying to build a package, but I'm a bit new to the whole S3/S4 methods concept. I'm trying to add a new definition of the zoo function as.yearmon, but I'm getting the following error when it gets to this point during a package install: Thhis seesm more appropiate for the R-devel rather than the R-help list. Anyway, see below. *** R CMD INSTALL STARStools * installing to library ‘/Library/Frameworks/R.framework/Versions/2.13/Resources/library’ WARNING: omitting pointless dependence on 'R' without a version requirement * installing *source* package ‘STARStools’ ... ** R ** inst ** preparing package for lazy loading Loading required package: sp raster version 1.9-5 (28-July-2011) Geospatial Data Abstraction Library extensions to R successfully loaded Loaded GDAL runtime: GDAL 1.8.0, released 2011/01/12 Path to GDAL shared files: /Library/Frameworks/GDAL.framework/Versions/1.8/Resources/gdal Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009 Path to PROJ.4 shared files: (autodetected) Attaching package: 'zoo' The following object(s) are masked from 'package:base': as.Date Creating a new generic function for as.Date in STARStools Creating a new generic function for as.list in STARStools Error in setGeneric(f, where = where) : must supply a function skeleton, explicitly or via an existing function Error : unable to load R code in package 'STARStools' ERROR: lazy loading failed for package ‘STARStools’ * removing ‘/Library/Frameworks/R.framework/Versions/2.13/Resources/library/STARStools’ * restoring previous ‘/Library/Frameworks/R.framework/Versions/2.13/Resources/library/STARStools’ *** # Note that these require statements do not appear in the code, but appear in the DESCRIPTION file: require(sp) require(zoo) ??? Not valid in the DESCRIPTION file. Either use this in your code or import from the corresponding Namespaces which you will have to do anyway for the next R release. # Here are the class definitions (filename AAAclassdefinitions.R): setClass(SpatialPointsDataFrameList,representation(list=list),contains=c(SpatialPointsDataFrame)) setClass(SpatialPointsDataFrameListZoo,contains=c(SpatialPointsDataFrameList)) # And here is where it is getting hung up. filename as.yearmon.SpatialPointsDataFrameListZoo_SpatialPointsDataFrameListZoo.R setMethod(as.yearmon, signature(x = SpatialPointsDataFrameListZoo), as.yearmon.SpatialPointsDataFrameListZoo ) You try to register the method pointing to a function definition that does not exist at this point (since you define it below). Just move the definition up. Uwe Ligges # Filename as.yearmon.SpatialPointsDataFrameListZoo.R as.yearmon.SpatialPointsDataFrameListZoo=function(x,...) { newlist=mapply(zoo:::as.yearmon,x@list,MoreArgs=list(...),simplify=FALSE) x@list=newlist return(x) } Thoughts? --j -- Jonathan A. Greenberg, PhD Assistant Project Scientist Center for Spatial Technologies and Remote Sensing (CSTARS) Department of Land, Air and Water Resources University of California, Davis One Shields Avenue Davis, CA 95616 Phone: 415-763-5476 AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307 __ R-help@r-project.org 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] identify connected cells in a cellular automaton
Hello, I’m working on a simulation using a cellular automata. The scenario is: Each cell represent a land plot. Each plot can have 3 different states: -The owner is a private individual (P) -The owner is a company ( C) -The land is abandoned/not used (A) The first year the cellular automaton is composed with one cell C in the middle. The rest of the cells are in P state. Each year a random number of cells P become A. The company buys the land A connected to C cells. The expansion process occurs only around the C cells. When the company buy a new cell its budget decrease of 10 cu (currency unit). When the amount of connected cells is bigger than 3 the each new cells connected start to generate earnings. The process occurs until the budget is equal to 0. I wish now introduce a new rule: each year the company start to buy available land connected to it’s own land then purchase land (cells) not connected according to its budget. The problem is now to calculate the total earning of the company since with the new rule some disconnected plot will be created. I need to calculate the earning of each disconnected land ( since plot with less than three cells connected will not generate earnings). With the first simulation calculate the total earnings was easy since every C cells was connected ( I only had to calculate the area own by the company). I’m looking for a way to identify the number of connected cells for each parcel of company land. Any suggestion? This is the code of the first simulation: # starting population m - 10 # nbr of rows n - 10 # nbr of cols x - rep(0, m*n)# cells dim(x) - c(m,n)# m rows n cols # company land is in the middle x[floor((m+1)/2),floor((n+1)/2)] - company # image(x, col=c(white,green)) span - 10 # time span in periods p_avail - 1/10 # probability of land becoming available company_area - sum(x==company) # initial company area newly_owned - function(t){ # newly owned area in year t company_area[[t]] - company_area[[t-1]]} earning - function(t){ # earning in year t ifelse(critical_size company_area[[t]], cells_earn*(company_area[[t-1]] - critical_size),0)} revenue - 0# for t=1 net_earned - 0 # for t=1 for(t in 2:span){ # some cells become available r01 - ifelse(x == private, matrix((runif(x,0,1)),nrow=m,ncol=n), x) x - ifelse(r01 = p_avail, available, x) # buy available lands around the company land x - ifelse( 1 == neighbors(x,state=company,wdist=wdist) x==available, company, x) company_area[[t]] - sum(x==company) revenue[[t]] - earning(t) expansion_cost - newly_owned(t)*land_cost # the cost of buying new land net_earned[[t]] - revenue[[t]] - expansion_cost budget[[t]] - budget[[t-1]] + net_earned[[t]] if (budget[[t]]=0) break # white = private owned land, blue = available, green = company image(x, col=c(white,blue,green)) } -- View this message in context: http://r.789695.n4.nabble.com/identify-connected-cells-in-a-cellular-automaton-tp3764851p3764851.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] gofCopula() function
Hi all, I'm trying to use gofCopula() function in copula package. But I'm always (trying different data sets) getting the same p value for Cramer-von Mises statistic. I'm using: g-gumbelCopula(1.653, dim = 2)gofCopula(g, x, N = 1000, method = itau,simulation = pb) c-claytonCopula(1.306, dim = 2)gofCopula(c, x, N = 1000, method = itau,simulation = pb) The p values are always 0.0004995005. I tried different data sets, but it was the same. Can anyone tell me what is wrong? Best regards Deniz R-help@r-project.org 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. [[alternative HTML version deleted]] __ R-help@r-project.org 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] hide row and column-names in cat()
Dear list, to prepare data as input for a stand-alone groundwater model I use the cat() function to print data. I want to print a table with print() or cat() but without the row- and column-names. Is this possible? Here a small example my.df - rbind(c(Nr,FraSand,FraSilt,FraClay,pH), c(, (kg.kg-1),(kg.kg-1), (kg.kg-1), (-)), c(1, 19, 60, 21, 7.1), c(2, 35, 53, 11, 7.7)) print(my.df, quote=FALSE) [,1] [,2] [,3] [,4] [,5] [1,] Nr FraSand FraSilt FraClay pH [2,] (kg.kg-1) (kg.kg-1) (kg.kg-1) (-) [3,] 1 19 60 21 7.1 [4,] 2 35 53 11 7.7 What I want to have is this: Nr FraSand FraSilt FraClay pH (kg.kg-1) (kg.kg-1) (kg.kg-1) (-) 1 19 60 21 7.1 2 35 53 11 7.7 Thank you. TIM __ R-help@r-project.org 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] hide row and column-names in cat()
try function write.matrix() from package MASS, e.g., library(MASS) write.matrix(my.df) I hope it helps. Best, Dimitris On 8/24/2011 1:46 PM, Tim Haering wrote: Dear list, to prepare data as input for a stand-alone groundwater model I use the cat() function to print data. I want to print a table with print() or cat() but without the row- and column-names. Is this possible? Here a small example my.df- rbind(c(Nr,FraSand,FraSilt,FraClay,pH), c(, (kg.kg-1),(kg.kg-1), (kg.kg-1), (-)), c(1, 19, 60, 21, 7.1), c(2, 35, 53, 11, 7.7)) print(my.df, quote=FALSE) [,1] [,2] [,3] [,4] [,5] [1,] Nr FraSand FraSilt FraClay pH [2,] (kg.kg-1) (kg.kg-1) (kg.kg-1) (-) [3,] 11960217.1 [4,] 23553117.7 What I want to have is this: Nr FraSand FraSilt FraClay pH (kg.kg-1) (kg.kg-1) (kg.kg-1) (-) 11960217.1 23553117.7 Thank you. TIM __ R-help@r-project.org 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. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/ __ R-help@r-project.org 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] How to use mvrnorm?
Dear R community! I would like to simulate distributions based on a polynomial model. It consists of 96 values and I want to randomly select new normaly distributed values around the modeled values. Furthermore, each value should be correlated with the previous one. Is it correct to therefore use mvrnorm? How do I define sigma? Thank you very much for your help. All the best, Marianne -- View this message in context: http://r.789695.n4.nabble.com/How-to-use-mvrnorm-tp3765225p3765225.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] Scatter plots, linear regression in ggplot2
Hi, Based on some modification that I did to the R Cookbook Graphs Scatterplots code, link:http://wiki.stdout.org/rcookbook/Graphs/Scatterplots%20(ggplot2) I have some questions and I will appreciate a help: - How do I change the legend title? - How can I change the for each linear regression its color and linetype? - How can I add for both the linear regression lines their equations and Rseq inside or below the plot? Code: set.seed(955) df - data.frame(cond = rep(c('A', 'B'), each=10), xvar = 1:20 + rnorm(20,sd=3), yvar = 1:20 + rnorm(20,sd=3)) ggplot(df, aes(x=xvar, y=yvar, shape=cond)) + scale_shape_manual(values=c(1,2)) + geom_smooth(method = lm, se=FALSE) + theme_bw()+ geom_point(size = 5)+ opts(title = Xvar vs. Yvar, plot.title = theme_text(face=bold, size=16), axis.text.x = theme_text(angle=90), axis.title.x = theme_text(face=bold, size=12), axis.title.y = theme_text(face=bold, size=12, angle=90), panel.grid.major = theme_blank(), # switch off major gridlines panel.grid.minor = theme_blank() ) Thanks a lot in advance -- View this message in context: http://r.789695.n4.nabble.com/Scatter-plots-linear-regression-in-ggplot2-tp3765080p3765080.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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 (stats) newcomer.... help!
Hi all, I hope that i've posted this in the correct place. if not, please accept my apologies (where should this go?) I have carried out experimental removal of bivalves at 2 intertidal shores. Bivalves were removed by raking of surface sediments. I wish compare the biomass values of for a total of 8 species between the 2 shores My 3 treatments are: Undisturbed Controls (Cont), Procedural Controls (Proc) and Experimetnally Fished (Fished). As Fished and Proc have both experienced disturbance, I set the model using 2 factors as follows: ControlsProcedural Fished Raked 0 1 1 Fished 0 0 1 As a newcomer to R ( stats!), I am unsure as to how to proceed. i am currently adopting the approach model-glm(biomass~Shore*Raked*Species+Shore*Fished*Species) And then run post-hoc adjusted pairwise comparisons between signifcant terms. Does this look OK to you guys? Many, many thanks Chris -- View this message in context: http://r.789695.n4.nabble.com/R-stats-newcomer-help-tp3764819p3764819.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] Change color in forest.rma (metafor)
My script is the following: library(metafor) yi-c(-0.1, 0.2, 0.3, 0.4) sei-c(0.4, 0.2, 0.6, 0.1) vi-sei^2 studi-c(A, B, C, D) eventi.c-c(10, 5, 7, 6) n.c-c(11, 34, 25, 20) eventi.a-c(2, 7, 6, 5) n.a-c(11, 35, 25, 15) dfs-rma(yi, vi, method=DL) dfs windows(height=6, width=10, pointsize=10) windowsFonts(B=windowsFont(Bookman Old Style)) forest.rma(dfs, slab=studi, xlim=c(-15, 10), ilab=cbind(eventi.c, n.c, eventi.a, n.a), ilab.xpos=c(-9.5, -8, -6, -4.5), cex=1.2, at=c(-2, -1, 0, 1, 2), family=B, xlab=Hazard Ratio (log scale), mlab=Random Effects Model, efac=5, col=red, border=red) text(-10, -1.3, paste(Heterogeneity: I-squared=, paste(paste(round(dfs$I2, 2), %, sep=), paste(p, round(dfs$QEp, 4), sep==), sep=, ), sep=), font=4, cex=1.2, family=B) op-par(cex=1.2, font=2, family=B, oma=c(0.5, 0.5, 0.5, 0.5), mar=c(0.5, 0.5, 0.5, 0.5)) text(x=c(-9.5, -8, -6, -4.5), 6, c(Events, N, Events, N), cex=1.2 ) text(c(-8.7, -5.5, 8), 6.5, c(S, A, Log)) text(-15, 6, Trials, pos=4) text(10, 6, Hazard Ratio [95% CI], pos=2) par(op) Even if I have specified col=red, border=red, color of squares and diamond rests black! Why? Thanks, Paola -- View this message in context: http://r.789695.n4.nabble.com/Change-color-in-forest-rma-metafor-tp3765090p3765090.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] library REEMtree = Error in estRE[toString(uniqueID[i]), 1] : incorrect number of dimensions
Hi List, I'm having this problem when trying to use the PREDICT function. Here is a way to reproduce the error library(REEMtree) data(simpleREEMdata) REEMresult-REEMtree(Y~D+t+X, data=simpleREEMdata, random=~1|ID/D) predict(REEMresult, simpleREEMdata, id = simpleREEMdata$ID/simpleREEMdata$D, EstimateRandomEffects=TRUE) As far as I understand the problem is that I'm not being able to specify correctly the number of groups created in the random structure of the model (~1|ID/D). Does anybody knows how to FIX this ? __ R-help@r-project.org 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 (stats) newcomer.... help!
geigercounter120 geigercounter120 at yahoo.co.uk writes: Hi all, I hope that i've posted this in the correct place. if not, please accept my apologies (where should this go?) I have carried out experimental removal of bivalves at 2 intertidal shores. Bivalves were removed by raking of surface sediments. I wish compare the biomass values of for a total of 8 species between the 2 shores My 3 treatments are: Undisturbed Controls (Cont), Procedural Controls (Proc) and Experimetnally Fished (Fished). As Fished and Proc have both experienced disturbance, I set the model using 2 factors as follows: ControlsProcedural Fished Raked 0 1 1 Fished0 0 1 As a newcomer to R ( stats!), I am unsure as to how to proceed. i am currently adopting the approach model-glm(biomass~Shore*Raked*Species+Shore*Fished*Species) And then run post-hoc adjusted pairwise comparisons between signifcant terms. The best thing would be if you (1) found a local expert to help you with the analysis or (2) sat down and read a book-length treatment of stats in R: check out the 'books' section on www.r-project.org (Dalgaard's and Faraway's books seem particularly appropriate) or the contributed documentation page, *or* read Gotelli and Ellison's _Primer of Statistics for Ecologists_ (not in R, but very clear). Learning R and statistics at the same time with only the built-in R documentation is likely to be very frustrating ... There is also an r-sig-ecology mailing list. Your approach doesn't seem completely crazy (you don't need glm() if your data are normally distributed, lm() will work a little bit better), but I would want to know a lot more about the experimental design: how many data points do you have? Are there experimental blocks in the data?If you really want to treat each species as a separate comparison, it might be more straightforward to use the lmList() command from the nlme package to run a biomass~Shore*(Raked+Fished) comparison for each species ... __ R-help@r-project.org 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] Scatter plots, linear regression in ggplot2
Hi, Have you looked at the documentation at http://had.co.nz/ggplot2/ ? You will find the answers to your questions there. On Wed, Aug 24, 2011 at 7:46 AM, ashz a...@walla.co.il wrote: Hi, Based on some modification that I did to the R Cookbook Graphs Scatterplots code, link:http://wiki.stdout.org/rcookbook/Graphs/Scatterplots%20(ggplot2) I have some questions and I will appreciate a help: - How do I change the legend title? See the scales documentation on the website. - How can I change the for each linear regression its color and linetype? See the geom_smooth documentation on the website. - How can I add for both the linear regression lines their equations and Rseq inside or below the plot? see ?annotate Best, Ista Code: set.seed(955) df - data.frame(cond = rep(c('A', 'B'), each=10), xvar = 1:20 + rnorm(20,sd=3), yvar = 1:20 + rnorm(20,sd=3)) ggplot(df, aes(x=xvar, y=yvar, shape=cond)) + scale_shape_manual(values=c(1,2)) + geom_smooth(method = lm, se=FALSE) + theme_bw()+ geom_point(size = 5)+ opts(title = Xvar vs. Yvar, plot.title = theme_text(face=bold, size=16), axis.text.x = theme_text(angle=90), axis.title.x = theme_text(face=bold, size=12), axis.title.y = theme_text(face=bold, size=12, angle=90), panel.grid.major = theme_blank(), # switch off major gridlines panel.grid.minor = theme_blank() ) Thanks a lot in advance -- View this message in context: http://r.789695.n4.nabble.com/Scatter-plots-linear-regression-in-ggplot2-tp3765080p3765080.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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. -- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org __ R-help@r-project.org 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] Odp: boxplot from mean and SD data
On Aug 24, 2011, at 13:18 , Petr PIKAL wrote: Hi Dear all, I have a dataset of 3 categorical factors, the mean and the standard deviation of each value. I want to use these values to plot a boxplot, grouped by each of the 3 categorical factors (24 boxplots in total). I don't have a clue on how to do the boxplot from mean and SD data already calculated. I am afraid that you can not do it. Or, you can't do it without modifying the definition of a boxplot. There are variations in which the box is mean +/- 2SD and the whiskers show the range, but I consider that a doubleplusungood idea -- box plots should be box plots! Only if you you accept assumption that distribution within each factor is normal and therefore median equals mean. You can than also use 25 an 75 percentile for box construction. see ?boxplot and ?bxp for internals of plotting function. One possibility also is to generate values by mean and sd parameters to rnorm, although I do not see the reason for doing it. Regards Petr Thanks in advance [[alternative HTML version deleted]] __ R-help@r-project.org 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@r-project.org 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. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com Døden skal tape! --- Nordahl Grieg __ R-help@r-project.org 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] debugging functions in R
Hi, I am not sure if this is the right list to ask this question (though I did not find a more appropriate one). I've started using R a month ago, and small scripts work fine. However, when I start writing more complex code, it gets messy. 1. Is there any way to debug normally, with breakpoints? 2. I am using the Eclipse plugin (StatET), and tried JGR(). Is there an IDE that enables breakpoints? 3. Is there an equivalent to include in other programming languages? So many functions in one file are very messy. I would like to break it to several files. 4. Any way to create a local context of variables inside a function? Otherwise I have to be careful to give different names inside functions, to those in the workspace. I should point that I am a long time Matlab user and am probably expecting some things that don't necessarily exist in R... I know it's a lot, if there is a more appropriate forum to ask these, please point me in that direction. Thanks, Eran. * * [[alternative HTML version deleted]] __ R-help@r-project.org 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] Change color in forest.rma (metafor)
Am 24.08.2011 07:50, schrieb Paola Tellaroli: My script is the following: library(metafor) yi-c(-0.1, 0.2, 0.3, 0.4) sei-c(0.4, 0.2, 0.6, 0.1) vi-sei^2 studi-c(A, B, C, D) eventi.c-c(10, 5, 7, 6) n.c-c(11, 34, 25, 20) eventi.a-c(2, 7, 6, 5) n.a-c(11, 35, 25, 15) dfs-rma(yi, vi, method=DL) dfs windows(height=6, width=10, pointsize=10) windowsFonts(B=windowsFont(Bookman Old Style)) forest.rma(dfs, slab=studi, xlim=c(-15, 10), ilab=cbind(eventi.c, n.c, eventi.a, n.a), ilab.xpos=c(-9.5, -8, -6, -4.5), cex=1.2, at=c(-2, -1, 0, 1, 2), family=B, xlab=Hazard Ratio (log scale), mlab=Random Effects Model, efac=5, col=red, border=red) text(-10, -1.3, paste(Heterogeneity: I-squared=, paste(paste(round(dfs$I2, 2), %, sep=), paste(p, round(dfs$QEp, 4), sep==), sep=, ), sep=), font=4, cex=1.2, family=B) op-par(cex=1.2, font=2, family=B, oma=c(0.5, 0.5, 0.5, 0.5), mar=c(0.5, 0.5, 0.5, 0.5)) text(x=c(-9.5, -8, -6, -4.5), 6, c(Events, N, Events, N), cex=1.2 ) text(c(-8.7, -5.5, 8), 6.5, c(S, A, Log)) text(-15, 6, Trials, pos=4) text(10, 6, Hazard Ratio [95% CI], pos=2) par(op) Even if I have specified col=red, border=red, color of squares and diamond rests black! Why? As far as I know, col and border do only affect the fitted values (diamonds), i.e. the FEM/REM estimators (see ?forest.rma: col: character string specifying the name of a color to use for _the fitted_ values (‘darkgray’ by default).) Furthermore, I had a quick look at the source code and it might be a bug. If I replace in line 2770 the line cex * efac), col = black, ...) with cex * efac), col = col, ...) you can at least specify your own colour. Changing the border color seems a bit more tricky... However, Wolfgang Viechbauer (the package author) is always a very responsive and helpful person and I suggest you better wait for his answer. Bernd __ R-help@r-project.org 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] silently testing for data from another package for .Rd examples
In an .Rd example for a package, I want to use data from another package, but avoid loading the entire package and avoid errors/warnings if that other package is not available. If I don't care about loading the other package, I can just do: if (require(ElemStatLearn, quietly=TRUE)) { data(prostate) # rest of example } I'd rather just be able to do something like: if (data(prostate, package=ElemStatLearn)) { # rest of example } but it appears that data() doesn't return anything useful (like FALSE or NULL) in case the named data set doesn't exist, or the package cannot be found. Below are some test cases in a fresh R 2.13.1 session. Is there someway I can incorporate such a data example silently without errors or warnings if the package doesn't exist, as is the case with require()? data(prostate, package=ElemStatLearn) dd - data(prostate, package=ElemStatLearn) dd [1] prostate dd2 - data(x, package=ElemStatLearn) Warning message: In data(x, package = ElemStatLearn) : data set 'x' not found dd2 [1] x dd2 - data(x, package=ElemStatLearn, verbose=FALSE) Warning message: In data(x, package = ElemStatLearn, verbose = FALSE) : data set 'x' not found dd3 - data(z, package=foobar) Error in find.package(package, lib.loc, verbose = verbose) : there is no package called 'foobar' dd3 Error: object 'dd3' not found try() doesn't seem to help here: ddtry - try(data(z, package=foobar)) Error in find.package(package, lib.loc, verbose = verbose) : there is no package called 'foobar' ddtry [1] Error in find.package(package, lib.loc, verbose = verbose) : \n there is no package called 'foobar'\n attr(,class) [1] try-error -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 4700 Keele StreetWeb: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA __ R-help@r-project.org 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] debugging functions in R
On Wed, Aug 24, 2011 at 4:20 PM, Eran Eidinger e...@taykey.com wrote: Hi, I am not sure if this is the right list to ask this question (though I did not find a more appropriate one). I've started using R a month ago, and small scripts work fine. However, when I start writing more complex code, it gets messy. 1. Is there any way to debug normally, with breakpoints? fortune('browser') My solution when I run into mysteries like this is to put 'browser()' in the function just before or after the line of interest. The magnitude and direction of my stupidity usually become clear quickly. -- Patrick Burns R-help (February 2006) Use browser() to inspect the environment and execute the code one step at a time. Liviu 2. I am using the Eclipse plugin (StatET), and tried JGR(). Is there an IDE that enables breakpoints? 3. Is there an equivalent to include in other programming languages? So many functions in one file are very messy. I would like to break it to several files. 4. Any way to create a local context of variables inside a function? Otherwise I have to be careful to give different names inside functions, to those in the workspace. I should point that I am a long time Matlab user and am probably expecting some things that don't necessarily exist in R... I know it's a lot, if there is a more appropriate forum to ask these, please point me in that direction. Thanks, Eran. * * [[alternative HTML version deleted]] __ R-help@r-project.org 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. -- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail __ R-help@r-project.org 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] debugging functions in R
Another great tool is debugonce() wrap your function name in it and then execute your function call. debugonce(my.function) out-my.function(df) And you'll be brought into the same interactive browser. (its Vi if im not mistaken which can take a little getting used to.) Justin On Wed, Aug 24, 2011 at 7:29 AM, Liviu Andronic landronim...@gmail.comwrote: On Wed, Aug 24, 2011 at 4:20 PM, Eran Eidinger e...@taykey.com wrote: Hi, I am not sure if this is the right list to ask this question (though I did not find a more appropriate one). I've started using R a month ago, and small scripts work fine. However, when I start writing more complex code, it gets messy. 1. Is there any way to debug normally, with breakpoints? fortune('browser') My solution when I run into mysteries like this is to put 'browser()' in the function just before or after the line of interest. The magnitude and direction of my stupidity usually become clear quickly. -- Patrick Burns R-help (February 2006) Use browser() to inspect the environment and execute the code one step at a time. Liviu 2. I am using the Eclipse plugin (StatET), and tried JGR(). Is there an IDE that enables breakpoints? 3. Is there an equivalent to include in other programming languages? So many functions in one file are very messy. I would like to break it to several files. 4. Any way to create a local context of variables inside a function? Otherwise I have to be careful to give different names inside functions, to those in the workspace. I should point that I am a long time Matlab user and am probably expecting some things that don't necessarily exist in R... I know it's a lot, if there is a more appropriate forum to ask these, please point me in that direction. Thanks, Eran. * * [[alternative HTML version deleted]] __ R-help@r-project.org 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. -- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail __ R-help@r-project.org 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. [[alternative HTML version deleted]] __ R-help@r-project.org 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] THX-- How to use 'switch' with strings containing spaces?
Richard, Thanks for your observation and tip. My apologies that the 'expr' seemed undefined. That was intentional on my part as I only wanted to show the form of the non-working code. Let me be clearer by updating the code with what I actually type at the command line. The code below does not work (error message included). x - c(Choice 1, Choice 2, Choice 3) switch(Choice 2, x[1]=My first choice, x[2]=My 2nd choice, x[3]=My 3rd choice) Error: unexpected '=' in switch(Choice 2, x[1]= On an earlier reply, David Winsemius suggested using the 'match' function instead. Perhaps that is the way to go, bypassing 'switch' altogether. But I would like to know why the code above does not work. Again, many thanks, Mauricio From: Richard M. Heiberger r...@temple.edu Cc: r-help@r-project.org r-help@r-project.org Sent: Tuesday, August 16, 2011 6:59 PM Subject: Re: [R] How to use 'switch' with strings containing spaces? The problem is that your argument expr is undefined. This works switch(Choice 2, Choice 1=My first choice, Choice 2=My 2nd choice, Choice 3=My 3rd choice) [1] My 2nd choice x - Choice 2 switch(x, Choice 1=My first choice, Choice 2=My 2nd choice, Choice 3=My 3rd choice) [1] My 2nd choice On Tue, Aug 16, 2011 at 4:53 PM, Mauricio Cornejo mauriciocorn...@yahoo.com wrote: Hi, Does anyone know if the alternatives in the 'switch' function can be specified as strings containing spaces? Neither of the two approaches below works. switch(expr, Choice 1=My first choice, Choice 2=My 2nd choice, Choice 3=My 3rd choice) x - c(Choice 1, Choice 2, Choice 3) switch(expr, x[1]=My first choice, x[2]=My 2nd choice, x[3]=My 3rd choice) If the intended functionality can be achieved by a different function(s), I'd most appreciate such a suggestion as well. Many thanks, Mauricio [[alternative HTML version deleted]] __ R-help@r-project.org 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. [[alternative HTML version deleted]] __ R-help@r-project.org 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] debugging functions in R
Wow, thanks Justin and Liviu, DebugOnce and browser. great! Eran On Wed, Aug 24, 2011 at 5:34 PM, Justin Haynes jto...@gmail.com wrote: Another great tool is debugonce() wrap your function name in it and then execute your function call. debugonce(my.function) out-my.function(df) And you'll be brought into the same interactive browser. (its Vi if im not mistaken which can take a little getting used to.) Justin On Wed, Aug 24, 2011 at 7:29 AM, Liviu Andronic landronim...@gmail.comwrote: On Wed, Aug 24, 2011 at 4:20 PM, Eran Eidinger e...@taykey.com wrote: Hi, I am not sure if this is the right list to ask this question (though I did not find a more appropriate one). I've started using R a month ago, and small scripts work fine. However, when I start writing more complex code, it gets messy. 1. Is there any way to debug normally, with breakpoints? fortune('browser') My solution when I run into mysteries like this is to put 'browser()' in the function just before or after the line of interest. The magnitude and direction of my stupidity usually become clear quickly. -- Patrick Burns R-help (February 2006) Use browser() to inspect the environment and execute the code one step at a time. Liviu 2. I am using the Eclipse plugin (StatET), and tried JGR(). Is there an IDE that enables breakpoints? 3. Is there an equivalent to include in other programming languages? So many functions in one file are very messy. I would like to break it to several files. 4. Any way to create a local context of variables inside a function? Otherwise I have to be careful to give different names inside functions, to those in the workspace. I should point that I am a long time Matlab user and am probably expecting some things that don't necessarily exist in R... I know it's a lot, if there is a more appropriate forum to ask these, please point me in that direction. Thanks, Eran. * * [[alternative HTML version deleted]] __ R-help@r-project.org 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. -- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail __ R-help@r-project.org 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. [[alternative HTML version deleted]] __ R-help@r-project.org 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] debugging functions in R
Also check out the 'debug' package On Wed, Aug 24, 2011 at 10:59 AM, Eran Eidinger e...@taykey.com wrote: Wow, thanks Justin and Liviu, DebugOnce and browser. great! Eran On Wed, Aug 24, 2011 at 5:34 PM, Justin Haynes jto...@gmail.com wrote: Another great tool is debugonce() wrap your function name in it and then execute your function call. debugonce(my.function) out-my.function(df) And you'll be brought into the same interactive browser. (its Vi if im not mistaken which can take a little getting used to.) Justin On Wed, Aug 24, 2011 at 7:29 AM, Liviu Andronic landronim...@gmail.comwrote: On Wed, Aug 24, 2011 at 4:20 PM, Eran Eidinger e...@taykey.com wrote: Hi, I am not sure if this is the right list to ask this question (though I did not find a more appropriate one). I've started using R a month ago, and small scripts work fine. However, when I start writing more complex code, it gets messy. 1. Is there any way to debug normally, with breakpoints? fortune('browser') My solution when I run into mysteries like this is to put 'browser()' in the function just before or after the line of interest. The magnitude and direction of my stupidity usually become clear quickly. -- Patrick Burns R-help (February 2006) Use browser() to inspect the environment and execute the code one step at a time. Liviu 2. I am using the Eclipse plugin (StatET), and tried JGR(). Is there an IDE that enables breakpoints? 3. Is there an equivalent to include in other programming languages? So many functions in one file are very messy. I would like to break it to several files. 4. Any way to create a local context of variables inside a function? Otherwise I have to be careful to give different names inside functions, to those in the workspace. I should point that I am a long time Matlab user and am probably expecting some things that don't necessarily exist in R... I know it's a lot, if there is a more appropriate forum to ask these, please point me in that direction. Thanks, Eran. * * [[alternative HTML version deleted]] __ R-help@r-project.org 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. -- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail __ R-help@r-project.org 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. [[alternative HTML version deleted]] __ R-help@r-project.org 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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? __ R-help@r-project.org 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] THX-- How to use 'switch' with strings containing spaces?
?switch If you read the help page, you will see that if the EXPR evaluates to a character string, then is matches on the names of the elements; 'x[1]' is not a name, it is a value. You want to probably use 'match' match(Choice 2, x) [1] 2 On Wed, Aug 24, 2011 at 10:52 AM, Mauricio Cornejo mauriciocorn...@yahoo.com wrote: Richard, Thanks for your observation and tip. My apologies that the 'expr' seemed undefined. That was intentional on my part as I only wanted to show the form of the non-working code. Let me be clearer by updating the code with what I actually type at the command line. The code below does not work (error message included). x - c(Choice 1, Choice 2, Choice 3) switch(Choice 2, x[1]=My first choice, x[2]=My 2nd choice, x[3]=My 3rd choice) Error: unexpected '=' in switch(Choice 2, x[1]= On an earlier reply, David Winsemius suggested using the 'match' function instead. Perhaps that is the way to go, bypassing 'switch' altogether. But I would like to know why the code above does not work. Again, many thanks, Mauricio From: Richard M. Heiberger r...@temple.edu Cc: r-help@r-project.org r-help@r-project.org Sent: Tuesday, August 16, 2011 6:59 PM Subject: Re: [R] How to use 'switch' with strings containing spaces? The problem is that your argument expr is undefined. This works switch(Choice 2, Choice 1=My first choice, Choice 2=My 2nd choice, Choice 3=My 3rd choice) [1] My 2nd choice x - Choice 2 switch(x, Choice 1=My first choice, Choice 2=My 2nd choice, Choice 3=My 3rd choice) [1] My 2nd choice On Tue, Aug 16, 2011 at 4:53 PM, Mauricio Cornejo mauriciocorn...@yahoo.com wrote: Hi, Does anyone know if the alternatives in the 'switch' function can be specified as strings containing spaces? Neither of the two approaches below works. switch(expr, Choice 1=My first choice, Choice 2=My 2nd choice, Choice 3=My 3rd choice) x - c(Choice 1, Choice 2, Choice 3) switch(expr, x[1]=My first choice, x[2]=My 2nd choice, x[3]=My 3rd choice) If the intended functionality can be achieved by a different function(s), I'd most appreciate such a suggestion as well. Many thanks, Mauricio [[alternative HTML version deleted]] __ R-help@r-project.org 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. [[alternative HTML version deleted]] __ R-help@r-project.org 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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? __ R-help@r-project.org 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] Function rank() for data frames (or multiple vectors)?
Hello, I'd like to rank rows of a data frame similar to what rank() does for vectors. However, ties should be broken by columns that I specify. If it is not possible to break a ties (because the row data is essentially the same), I'd like to have the same flexibility that rank() offers. Is there an elegant solution to this simple problem in R? Basically, what I need is a mixture of order() and rank(). While the former allows to specify multiple vectors, it doesn't provide the flexibility of rank() such that I can specify what happens if ties can not be broken. Thanks for your help! Best, Sebastian __ R-help@r-project.org 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] THX-- How to use 'switch' with strings containing spaces?
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Mauricio Cornejo Sent: Wednesday, August 24, 2011 7:53 AM To: Richard M. Heiberger Cc: r-help@r-project.org Subject: [R] THX-- How to use 'switch' with strings containing spaces? Richard, Thanks for your observation and tip. My apologies that the 'expr' seemed undefined. That was intentional on my part as I only wanted to show the form of the non-working code. Let me be clearer by updating the code with what I actually type at the command line. The code below does not work (error message included). x - c(Choice 1, Choice 2, Choice 3) switch(Choice 2, x[1]=My first choice, x[2]=My 2nd choice, x[3]=My 3rd choice) Error: unexpected '=' in switch(Choice 2, x[1]= On an earlier reply, David Winsemius suggested using the 'match' function instead. Perhaps that is the way to go, bypassing 'switch' altogether. But I would like to know why the code above does not work. Again, many thanks, Mauricio Mauricio, I haven't seen how you are trying to use this construction, or what you want the result to look like, so I am only guessing. But does something like the following get you closer to what you want? x - c(Choice 1, Choice 2, Choice 3) switch(x[2], 'Choice 1'=My first choice, 'Choice 2'=My 2nd choice, 'Choice 3'=My 3rd choice) Hope this is helpful, Dan Daniel Nordlund Bothell, WA USA __ R-help@r-project.org 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: find the index of the minimum of entries
Dear All, I would like to ask a question on how to find the index of the minimum of entries of a numeric vector, without using loops or user defined functions. Suppose we have a vector: a - c(3,1,2) then, min(a) = 1 and its index is 2. Target: how to get the index of this minimum? How to get the indices if multiple entries assume this same minimum? Thank you, Chee [[alternative HTML version deleted]] __ R-help@r-project.org 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] THX-- How to use 'switch' with strings containing spaces?
Your code example doesn't work because x[3]='My 3rd choice' is not a valid named parameter assignment for a function, and that is because x[3] is not a valid name for a function argument. The _content_ of x[3] might be, but argument names aren;t parsed in this context (and indeed only would be using something like assign()). So what R has done is say I've found a token x[3]; that's an array dereference on x so I must evaluate that, just as I would if I encountered x[3] on its own. Now let me make sure there isn't another bracket or subset operation on that x[3]... ooh look an unexpected '=' sign. That shouldn't be there, there should only be a space, a comma or a subset operator. Other functions do exactly the same when they find an '=' in this position. If you want to use switch with variable match lists, use match in the initial expression to get an integer, and rely on integer indexing in swich() for the return value. S Ellison -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Daniel Nordlund Sent: 24 August 2011 16:32 To: r-help@r-project.org Subject: Re: [R] THX-- How to use 'switch' with strings containing spaces? -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Mauricio Cornejo Sent: Wednesday, August 24, 2011 7:53 AM To: Richard M. Heiberger Cc: r-help@r-project.org Subject: [R] THX-- How to use 'switch' with strings containing spaces? Richard, Thanks for your observation and tip. My apologies that the 'expr' seemed undefined. That was intentional on my part as I only wanted to show the form of the non-working code. Let me be clearer by updating the code with what I actually type at the command line. The code below does not work (error message included). x - c(Choice 1, Choice 2, Choice 3) switch(Choice 2, x[1]=My first choice, x[2]=My 2nd choice, x[3]=My 3rd choice) Error: unexpected '=' in switch(Choice 2, x[1]= On an earlier reply, David Winsemius suggested using the 'match' function instead. Perhaps that is the way to go, bypassing 'switch' altogether. But I would like to know why the code above does not work. Again, many thanks, Mauricio Mauricio, I haven't seen how you are trying to use this construction, or what you want the result to look like, so I am only guessing. But does something like the following get you closer to what you want? x - c(Choice 1, Choice 2, Choice 3) switch(x[2], 'Choice 1'=My first choice, 'Choice 2'=My 2nd choice, 'Choice 3'=My 3rd choice) Hope this is helpful, Dan Daniel Nordlund Bothell, WA USA __ R-help@r-project.org 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. *** This email and any attachments are confidential. Any use...{{dropped:8}} __ R-help@r-project.org 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: find the index of the minimum of entries
which.min() more generally which(a==min(a)) Michael Weylandt On Wed, Aug 24, 2011 at 12:03 PM, Chee Chen chee.c...@yahoo.com wrote: Dear All, I would like to ask a question on how to find the index of the minimum of entries of a numeric vector, without using loops or user defined functions. Suppose we have a vector: a - c(3,1,2) then, min(a) = 1 and its index is 2. Target: how to get the index of this minimum? How to get the indices if multiple entries assume this same minimum? Thank you, Chee [[alternative HTML version deleted]] __ R-help@r-project.org 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. [[alternative HTML version deleted]] __ R-help@r-project.org 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.oo modify an object inside another classes method
I didn't see an answer to this and I THINK I sorted it out myself so I thought I'd post it for anyone who is interested (in using it or correcting it): My original question: Can someone show me how to modify one (R.oo) class's object inside another (R.oo) class's method? Is that possible with the R.oo package? A quick example or reference to an example would be outstanding... Solution: setConstructorS3(Person, function(age) { if (missing(age)) age - NA; extend(Object(), Person, .age=age ) }) setMethodS3(getAge, Person, function(this, ...) { this$.age; }) setMethodS3(setAge, Person, function(this, newAge, ...) { if (!is.numeric(newAge)) throw(Age must be numeric: , newAge); if (newAge 0) throw(Trying to set a negative age: , newAge); this$.age - newAge; }) setConstructorS3(AgeMultiplier, function() { extend(Object(), AgeMultiplier, .age=NA, .age_multiplied=NA ) }) setMethodS3(doMultiply, AgeMultiplier, function(this,per_obj, ...) { this$.age_multiplied = per_obj$age * 2; per_obj$age = this$.age_multiplied; }) Use example: p1 - Person(67) am1 = AgeMultiplier() am1$doMultiply(p1) p1$age # 134 On Tue, Aug 23, 2011 at 8:22 AM, Ben qant ccqu...@gmail.com wrote: Can someone show me how to modify one (R.oo) class's object inside another (R.oo) class's method? Is that possible with the R.oo package? A quick example or reference to an example would be outstanding... Thanks, Ben [[alternative HTML version deleted]] __ R-help@r-project.org 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: find the index of the minimum of entries
Try which.min(a) On Wed, Aug 24, 2011 at 1:03 PM, Chee Chen chee.c...@yahoo.com wrote: Dear All, I would like to ask a question on how to find the index of the minimum of entries of a numeric vector, without using loops or user defined functions. Suppose we have a vector: a - c(3,1,2) then, min(a) = 1 and its index is 2. Target: how to get the index of this minimum? How to get the indices if multiple entries assume this same minimum? Thank you, Chee [[alternative HTML version deleted]] __ R-help@r-project.org 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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O __ R-help@r-project.org 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] silently testing for data from another package for .Rd examples
.packages(all = TRUE) will give you a list of all available packages without really loading them like require(). Regards, Yihui -- Yihui Xie xieyi...@gmail.com Phone: 515-294-2465 Web: http://yihui.name Department of Statistics, Iowa State University 2215 Snedecor Hall, Ames, IA On Wed, Aug 24, 2011 at 9:28 AM, Michael Friendly frien...@yorku.ca wrote: In an .Rd example for a package, I want to use data from another package, but avoid loading the entire package and avoid errors/warnings if that other package is not available. If I don't care about loading the other package, I can just do: if (require(ElemStatLearn, quietly=TRUE)) { data(prostate) # rest of example } I'd rather just be able to do something like: if (data(prostate, package=ElemStatLearn)) { # rest of example } but it appears that data() doesn't return anything useful (like FALSE or NULL) in case the named data set doesn't exist, or the package cannot be found. Below are some test cases in a fresh R 2.13.1 session. Is there someway I can incorporate such a data example silently without errors or warnings if the package doesn't exist, as is the case with require()? data(prostate, package=ElemStatLearn) dd - data(prostate, package=ElemStatLearn) dd [1] prostate dd2 - data(x, package=ElemStatLearn) Warning message: In data(x, package = ElemStatLearn) : data set 'x' not found dd2 [1] x dd2 - data(x, package=ElemStatLearn, verbose=FALSE) Warning message: In data(x, package = ElemStatLearn, verbose = FALSE) : data set 'x' not found dd3 - data(z, package=foobar) Error in find.package(package, lib.loc, verbose = verbose) : there is no package called 'foobar' dd3 Error: object 'dd3' not found try() doesn't seem to help here: ddtry - try(data(z, package=foobar)) Error in find.package(package, lib.loc, verbose = verbose) : there is no package called 'foobar' ddtry [1] Error in find.package(package, lib.loc, verbose = verbose) : \n there is no package called 'foobar'\n attr(,class) [1] try-error -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 4700 Keele Street Web: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA __ R-help@r-project.org 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@r-project.org 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] Easier ways to create .Rd files?
To avoid a possible confusion, please note it is roxygen2 rather than roxygen; both are packages on CRAN, but roxygen2 is an improved version and under maintenance; I guess the development of the original roxygen package has stopped. Emacs+ESS helps a whole lot in writing documentation! I really love C-c C-o Regards, Yihui -- Yihui Xie xieyi...@gmail.com Phone: 515-294-2465 Web: http://yihui.name Department of Statistics, Iowa State University 2215 Snedecor Hall, Ames, IA On Wed, Aug 24, 2011 at 3:31 AM, Jonathan Greenberg greenb...@ucdavis.edu wrote: Thanks Kevin! Got started with roxygen tonight! Cheers! On Tue, Aug 23, 2011 at 6:28 PM, Kevin Wright kw.s...@gmail.com wrote: I like the roxygen2 package for combining code and documentation. If you use Emacs + ESS, it will even create much of the roxygen code for you (and auto-revise it if you change the function arguments). Kevin Wright On Tue, Aug 23, 2011 at 6:55 PM, Jonathan Greenberg greenb...@ucdavis.edu wrote: R-helpers: Are there any ways to auto-generate R-friendly (e.g. will pass a compilation check) .Rd files given a set of .R code? How about GUIs that help properly format the .Rd files? Thanks! I want a basic set of .Rd files that I can update as I go, but as with most things my documentation typically lags behind my coding by a few days. --j -- Jonathan A. Greenberg, PhD Assistant Project Scientist Center for Spatial Technologies and Remote Sensing (CSTARS) Department of Land, Air and Water Resources University of California, Davis One Shields Avenue Davis, CA 95616 Phone: 415-763-5476 AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307 __ R-help@r-project.org 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. -- Jonathan A. Greenberg, PhD Assistant Project Scientist Center for Spatial Technologies and Remote Sensing (CSTARS) Department of Land, Air and Water Resources University of California, Davis One Shields Avenue Davis, CA 95616 Phone: 415-763-5476 AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307 __ R-help@r-project.org 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@r-project.org 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] Change color in forest.rma (metafor)
Thank you, Bernd, for looking into this. Yes, at the moment, the color of the summary estimate for models without moderators is hard-coded (as black). I didn't think people may want to change that. I guess I was wrong =) A dirty solution for the moment is to add: addpoly(dfs, efac=6, row=-1, col=red, border=red, annotate=F, mlab=) after the call to forest(). You will get a warning message (since the border argument gets passed to the text() function inside addpoly() and that's not a par for text), but you can just ignore that. Best, -- Wolfgang Viechtbauer Department of Psychiatry and Neuropsychology School for Mental Health and Neuroscience Maastricht University, P.O. Box 616 6200 MD Maastricht, The Netherlands Tel: +31 (43) 368-5248 Fax: +31 (43) 368-8689 Web: http://www.wvbauer.com -Original Message- From: Bernd Weiss [mailto:bernd.we...@uni-koeln.de] Sent: Wednesday, August 24, 2011 16:22 To: Paola Tellaroli Cc: w...@metafor-project.org; r-help@r-project.org Subject: Re: [R] Change color in forest.rma (metafor) Am 24.08.2011 07:50, schrieb Paola Tellaroli: My script is the following: library(metafor) yi-c(-0.1, 0.2, 0.3, 0.4) sei-c(0.4, 0.2, 0.6, 0.1) vi-sei^2 studi-c(A, B, C, D) eventi.c-c(10, 5, 7, 6) n.c-c(11, 34, 25, 20) eventi.a-c(2, 7, 6, 5) n.a-c(11, 35, 25, 15) dfs-rma(yi, vi, method=DL) dfs windows(height=6, width=10, pointsize=10) windowsFonts(B=windowsFont(Bookman Old Style)) forest.rma(dfs, slab=studi, xlim=c(-15, 10), ilab=cbind(eventi.c, n.c, eventi.a, n.a), ilab.xpos=c(-9.5, -8, -6, -4.5), cex=1.2, at=c(-2, -1, 0, 1, 2), family=B, xlab=Hazard Ratio (log scale), mlab=Random Effects Model, efac=5, col=red, border=red) text(-10, -1.3, paste(Heterogeneity: I-squared=, paste(paste(round(dfs$I2, 2), %, sep=), paste(p, round(dfs$QEp, 4), sep==), sep=, ), sep=), font=4, cex=1.2, family=B) op-par(cex=1.2, font=2, family=B, oma=c(0.5, 0.5, 0.5, 0.5), mar=c(0.5, 0.5, 0.5, 0.5)) text(x=c(-9.5, -8, -6, -4.5), 6, c(Events, N, Events, N), cex=1.2 ) text(c(-8.7, -5.5, 8), 6.5, c(S, A, Log)) text(-15, 6, Trials, pos=4) text(10, 6, Hazard Ratio [95% CI], pos=2) par(op) Even if I have specified col=red, border=red, color of squares and diamond rests black! Why? As far as I know, col and border do only affect the fitted values (diamonds), i.e. the FEM/REM estimators (see ?forest.rma: col: character string specifying the name of a color to use for _the fitted_ values ('darkgray' by default).) Furthermore, I had a quick look at the source code and it might be a bug. If I replace in line 2770 the line cex * efac), col = black, ...) with cex * efac), col = col, ...) you can at least specify your own colour. Changing the border color seems a bit more tricky... However, Wolfgang Viechbauer (the package author) is always a very responsive and helpful person and I suggest you better wait for his answer. Bernd __ R-help@r-project.org 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] silently testing for data from another package for .Rd examples
Actually it is recommended to test for the availability of a valid package with find.package(), particularly in this case where the name of the package is already know. Best, Uwe On 24.08.2011 18:29, Yihui Xie wrote: .packages(all = TRUE) will give you a list of all available packages without really loading them like require(). Regards, Yihui -- Yihui Xiexieyi...@gmail.com Phone: 515-294-2465 Web: http://yihui.name Department of Statistics, Iowa State University 2215 Snedecor Hall, Ames, IA On Wed, Aug 24, 2011 at 9:28 AM, Michael Friendlyfrien...@yorku.ca wrote: In an .Rd example for a package, I want to use data from another package, but avoid loading the entire package and avoid errors/warnings if that other package is not available. If I don't care about loading the other package, I can just do: if (require(ElemStatLearn, quietly=TRUE)) { data(prostate) # rest of example } I'd rather just be able to do something like: if (data(prostate, package=ElemStatLearn)) { # rest of example } but it appears that data() doesn't return anything useful (like FALSE or NULL) in case the named data set doesn't exist, or the package cannot be found. Below are some test cases in a fresh R 2.13.1 session. Is there someway I can incorporate such a data example silently without errors or warnings if the package doesn't exist, as is the case with require()? data(prostate, package=ElemStatLearn) dd- data(prostate, package=ElemStatLearn) dd [1] prostate dd2- data(x, package=ElemStatLearn) Warning message: In data(x, package = ElemStatLearn) : data set 'x' not found dd2 [1] x dd2- data(x, package=ElemStatLearn, verbose=FALSE) Warning message: In data(x, package = ElemStatLearn, verbose = FALSE) : data set 'x' not found dd3- data(z, package=foobar) Error in find.package(package, lib.loc, verbose = verbose) : there is no package called 'foobar' dd3 Error: object 'dd3' not found try() doesn't seem to help here: ddtry- try(data(z, package=foobar)) Error in find.package(package, lib.loc, verbose = verbose) : there is no package called 'foobar' ddtry [1] Error in find.package(package, lib.loc, verbose = verbose) : \n there is no package called 'foobar'\n attr(,class) [1] try-error -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 4700 Keele StreetWeb: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA __ R-help@r-project.org 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@r-project.org 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@r-project.org 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] Function rank() for data frames (or multiple vectors)?
On Aug 24, 2011, at 11:09 AM, Sebastian Bauer wrote: Hello, I'd like to rank rows of a data frame similar to what rank() does for vectors. However, ties should be broken by columns that I specify. If it is not possible to break a ties (because the row data is essentially the same), I'd like to have the same flexibility that rank() offers. Is there an elegant solution to this simple problem in R? Basically, what I need is a mixture of order() and rank(). While the former allows to specify multiple vectors, it doesn't provide the flexibility of rank() such that I can specify what happens if ties can not be broken. An example of this simple problem would clarify this greatly. I cannot tell what flexibility in 'rank' is missing in 'order'. -- David Winsemius, MD West Hartford, CT __ R-help@r-project.org 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] data manipulation and summaries with few million rows
I have a data set with about 6 million rows and 50 columns. It is a mixture of dates, factors, and numerics. What I am trying to accomplish can be seen with the following simplified data, which is given as dput output below. head(myData) mydate gender mygroup id 1 2012-03-25 F A 1 2 2005-05-23 F B 2 3 2005-09-08 F B 2 4 2005-12-07 F B 2 5 2006-02-26 F C 2 6 2006-05-13 F C 2 For each id, I want to count the number of changes of the variable 'mygroup' that occur. For example, id=1 has 0 changes because it is observed only once. id=2 has 2 changes (B to C, and C to D). I also need to calculate the total observation time for each id using the variable mydate. In the end, I am trying to have a new data set in which each row has an id, days observed, number of changes, and gender. I made some simple summaries using data.table and plyr, but I'm stuck on this reformatting. Thanks for your help. myData - structure(list(mydate = c(2012-03-25, 2005-05-23, 2005-09-08, 2005-12-07, 2006-02-26, 2006-05-13, 2006-09-01, 2006-12-12, 2006-02-19, 2006-05-03, 2006-04-23, 2007-12-08, 2011-03-19, 2007-12-20, 2008-06-15, 2008-12-16, 2009-06-07, 2009-10-09, 2010-01-28, 2007-06-05), gender = c(F, F, F, F, F, F, F, F, F, F, F, F, F, M, M, M, M, M, M, M), mygroup = c(A, B, B, B, C, C, C, D, D, D, D, D, D, A, A, A, B, B, B, A), id = c(1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L)), .Names = c(mydate, gender, mygroup, id), class = data.frame, row.names = c(NA, -20L )) sessionInfo() R version 2.13.1 (2011-07-08) Platform: x86_64-unknown-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base __ R-help@r-project.org 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] How to do cross validation with glm?
Hi All, I have a fitted model called glm.fit which I used glm and data dat is my data frame pred= predict(glm.fit, data = dat, type=response) to predict how it predicts on my whole data but obviously I have to do cross-validation to train the model on one part of my data and predict on the other part. So, I searched for it and I found a function cv.glm which is in package boot. So, I tired to use it as: cv.glm = (cv.glm(dat, glm.fit, cost, K=nrow(dat))$delta) but I am not sure how to do the prediction for the hold-out data. Is there any better way for cross-validation to learn a model on training data and test it on test data in R? Thanks, Andra __ R-help@r-project.org 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] Function rank() for data frames (or multiple vectors)?
Hi! I'd like to rank rows of a data frame similar to what rank() does for vectors. However, ties should be broken by columns that I specify. If it is not possible to break a ties (because the row data is essentially the same), I'd like to have the same flexibility that rank() offers. Is there an elegant solution to this simple problem in R? Basically, what I need is a mixture of order() and rank(). While the former allows to specify multiple vectors, it doesn't provide the flexibility of rank() such that I can specify what happens if ties can not be broken. An example of this simple problem would clarify this greatly. I cannot tell what flexibility in 'rank' is missing in 'order'. Thanks for your answer. For instance, if I have two vectors such as 1 1 1 2 1 2 1 3 2 1 that I want combinedly ranked. I'd like to get an output 1 2 2 4 5 or (ties.method=average) 1 2.5 2.5 4 5 Basically, I need a function similar to the rank() function that accepts more than one vector (as order() does). Best, Sebastian __ R-help@r-project.org 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 do cross validation with glm?
What you describe is not cross-validation, so I am afraid we do not know what you mean. And cv.glm does 'prediction for the hold-out data' for you: you can read the code to see how it does so. I suspect you mean you want to do validation on a test set, but that is not what you actually claim. There are lots of examples of this sort of thing in MASS (the book, scripts in the package). On Wed, 24 Aug 2011, Andra Isan wrote: Hi All, I have a fitted model called glm.fit which I used glm and data dat is my data frame pred= predict(glm.fit, data = dat, type=response) to predict how it predicts on my whole data but obviously I have to do cross-validation to train the model on one part of my data and predict on the other part. So, I searched for it and I found a function cv.glm which is in package boot. So, I tired to use it as: cv.glm = (cv.glm(dat, glm.fit, cost, K=nrow(dat))$delta) but I am not sure how to do the prediction for the hold-out data. Is there any better way for cross-validation to learn a model on training data and test it on test data in R? Thanks, Andra __ R-help@r-project.org 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. -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@r-project.org 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 do cross validation with glm?
Hi, Thanks for the reply. What I meant is that, I would like to partition my dat data (a data frame) into training and testing data and then evaluate the performance of the model on test data. So, I thought cross validation is the natural choice to see how the prediction works on the hold-out data. Is there any example that I can take a look to see how to do cross validation and get the prediction results on my data? Thanks a lot, Andra --- On Wed, 8/24/11, Prof Brian Ripley rip...@stats.ox.ac.uk wrote: From: Prof Brian Ripley rip...@stats.ox.ac.uk Subject: Re: [R] How to do cross validation with glm? To: Andra Isan andra_i...@yahoo.com Cc: r-help@r-project.org Date: Wednesday, August 24, 2011, 10:11 AM What you describe is not cross-validation, so I am afraid we do not know what you mean. And cv.glm does 'prediction for the hold-out data' for you: you can read the code to see how it does so. I suspect you mean you want to do validation on a test set, but that is not what you actually claim. There are lots of examples of this sort of thing in MASS (the book, scripts in the package). On Wed, 24 Aug 2011, Andra Isan wrote: Hi All, I have a fitted model called glm.fit which I used glm and data dat is my data frame pred= predict(glm.fit, data = dat, type=response) to predict how it predicts on my whole data but obviously I have to do cross-validation to train the model on one part of my data and predict on the other part. So, I searched for it and I found a function cv.glm which is in package boot. So, I tired to use it as: cv.glm = (cv.glm(dat, glm.fit, cost, K=nrow(dat))$delta) but I am not sure how to do the prediction for the hold-out data. Is there any better way for cross-validation to learn a model on training data and test it on test data in R? Thanks, Andra __ R-help@r-project.org 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. -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595 __ R-help@r-project.org 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] data manipulation and summaries with few million rows
Hi Juliet: Here's a Q D solution: # (1) plyr f - function(d) length(unique(d$mygroup)) - 1 ddply(myData, .(id), f) id V1 1 1 0 2 2 2 3 3 1 4 4 0 # (2) data.table myDT - data.table(myData, key = 'id') myDT[, list(nswitch = length(unique(mygroup)) - 1), by = 'id'] If one can switch back and forth between levels more than once, then the above is clearly not appropriate. A more robust method would be to employ rle() [run length encoding]: g - function(d) length(rle(d$mygroup)$lengths) - 1 ddply(myData, .(id), g)# gives the same answer as above myDT[, list(nswitch = length(rle(mygroup)$lengths) - 1), by = 'id'] # ditto HTH, Dennis On Wed, Aug 24, 2011 at 9:48 AM, Juliet Hannah juliet.han...@gmail.com wrote: I have a data set with about 6 million rows and 50 columns. It is a mixture of dates, factors, and numerics. What I am trying to accomplish can be seen with the following simplified data, which is given as dput output below. head(myData) mydate gender mygroup id 1 2012-03-25 F A 1 2 2005-05-23 F B 2 3 2005-09-08 F B 2 4 2005-12-07 F B 2 5 2006-02-26 F C 2 6 2006-05-13 F C 2 For each id, I want to count the number of changes of the variable 'mygroup' that occur. For example, id=1 has 0 changes because it is observed only once. id=2 has 2 changes (B to C, and C to D). I also need to calculate the total observation time for each id using the variable mydate. In the end, I am trying to have a new data set in which each row has an id, days observed, number of changes, and gender. I made some simple summaries using data.table and plyr, but I'm stuck on this reformatting. Thanks for your help. myData - structure(list(mydate = c(2012-03-25, 2005-05-23, 2005-09-08, 2005-12-07, 2006-02-26, 2006-05-13, 2006-09-01, 2006-12-12, 2006-02-19, 2006-05-03, 2006-04-23, 2007-12-08, 2011-03-19, 2007-12-20, 2008-06-15, 2008-12-16, 2009-06-07, 2009-10-09, 2010-01-28, 2007-06-05), gender = c(F, F, F, F, F, F, F, F, F, F, F, F, F, M, M, M, M, M, M, M), mygroup = c(A, B, B, B, C, C, C, D, D, D, D, D, D, A, A, A, B, B, B, A), id = c(1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L)), .Names = c(mydate, gender, mygroup, id), class = data.frame, row.names = c(NA, -20L )) sessionInfo() R version 2.13.1 (2011-07-08) Platform: x86_64-unknown-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base __ R-help@r-project.org 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@r-project.org 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] Function rank() for data frames (or multiple vectors)?
On Aug 24, 2011, at 1:11 PM, Sebastian Bauer wrote: Hi! I'd like to rank rows of a data frame similar to what rank() does for vectors. However, ties should be broken by columns that I specify. If it is not possible to break a ties (because the row data is essentially the same), I'd like to have the same flexibility that rank() offers. Is there an elegant solution to this simple problem in R? Basically, what I need is a mixture of order() and rank(). While the former allows to specify multiple vectors, it doesn't provide the flexibility of rank() such that I can specify what happens if ties can not be broken. An example of this simple problem would clarify this greatly. I cannot tell what flexibility in 'rank' is missing in 'order'. Thanks for your answer. For instance, if I have two vectors such as 1 1 1 2 1 2 1 3 2 1 that I want combinedly ranked. I'd like to get an output 1 2 2 4 5 or (ties.method=average) 1 2.5 2.5 4 5 Basically, I need a function similar to the rank() function that accepts more than one vector (as order() does). Can't you just paste the columns and run rank on the results? 'rank' accepts character vectors. Best, Sebastian David Winsemius, MD West Hartford, CT __ R-help@r-project.org 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] data manipulation and summaries with few million rows
Thanks Dennis! I'll check this out. Just to clarify, I need the total number of switches/changes regardless of if that state had occurred in the past. So A-A-B-A, would have 2 changes: A to B and B to A. Thanks again. On Wed, Aug 24, 2011 at 1:28 PM, Dennis Murphy djmu...@gmail.com wrote: Hi Juliet: Here's a Q D solution: # (1) plyr f - function(d) length(unique(d$mygroup)) - 1 ddply(myData, .(id), f) id V1 1 1 0 2 2 2 3 3 1 4 4 0 # (2) data.table myDT - data.table(myData, key = 'id') myDT[, list(nswitch = length(unique(mygroup)) - 1), by = 'id'] If one can switch back and forth between levels more than once, then the above is clearly not appropriate. A more robust method would be to employ rle() [run length encoding]: g - function(d) length(rle(d$mygroup)$lengths) - 1 ddply(myData, .(id), g) # gives the same answer as above myDT[, list(nswitch = length(rle(mygroup)$lengths) - 1), by = 'id'] # ditto HTH, Dennis On Wed, Aug 24, 2011 at 9:48 AM, Juliet Hannah juliet.han...@gmail.com wrote: I have a data set with about 6 million rows and 50 columns. It is a mixture of dates, factors, and numerics. What I am trying to accomplish can be seen with the following simplified data, which is given as dput output below. head(myData) mydate gender mygroup id 1 2012-03-25 F A 1 2 2005-05-23 F B 2 3 2005-09-08 F B 2 4 2005-12-07 F B 2 5 2006-02-26 F C 2 6 2006-05-13 F C 2 For each id, I want to count the number of changes of the variable 'mygroup' that occur. For example, id=1 has 0 changes because it is observed only once. id=2 has 2 changes (B to C, and C to D). I also need to calculate the total observation time for each id using the variable mydate. In the end, I am trying to have a new data set in which each row has an id, days observed, number of changes, and gender. I made some simple summaries using data.table and plyr, but I'm stuck on this reformatting. Thanks for your help. myData - structure(list(mydate = c(2012-03-25, 2005-05-23, 2005-09-08, 2005-12-07, 2006-02-26, 2006-05-13, 2006-09-01, 2006-12-12, 2006-02-19, 2006-05-03, 2006-04-23, 2007-12-08, 2011-03-19, 2007-12-20, 2008-06-15, 2008-12-16, 2009-06-07, 2009-10-09, 2010-01-28, 2007-06-05), gender = c(F, F, F, F, F, F, F, F, F, F, F, F, F, M, M, M, M, M, M, M), mygroup = c(A, B, B, B, C, C, C, D, D, D, D, D, D, A, A, A, B, B, B, A), id = c(1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L)), .Names = c(mydate, gender, mygroup, id), class = data.frame, row.names = c(NA, -20L )) sessionInfo() R version 2.13.1 (2011-07-08) Platform: x86_64-unknown-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base __ R-help@r-project.org 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@r-project.org 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] Function rank() for data frames (or multiple vectors)?
Hi! in R? Basically, what I need is a mixture of order() and rank(). While the former allows to specify multiple vectors, it doesn't provide the flexibility of rank() such that I can specify what happens if ties can not be broken. An example of this simple problem would clarify this greatly. I cannot tell what flexibility in 'rank' is missing in 'order'. Thanks for your answer. For instance, if I have two vectors such as 1 1 1 2 1 2 1 3 2 1 that I want combinedly ranked. I'd like to get an output 1 2 2 4 5 or (ties.method=average) 1 2.5 2.5 4 5 Basically, I need a function similar to the rank() function that accepts more than one vector (as order() does). Can't you just paste the columns and run rank on the results? 'rank' accepts character vectors. I was looking for an elegant solution ;) In the real case I have double values and this would be quite inefficient then. Best, Sebastian __ R-help@r-project.org 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 do cross validation with glm?
What is your sample size? I've had trouble getting reliable estimates using simple data splitting when N 20,000. Note that the following functions in the rms package facilitates cross-validation and bootstrapping for validating models: ols, validate, calibrate. Frank Andra Isan wrote: Hi, Thanks for the reply. What I meant is that, I would like to partition my dat data (a data frame) into training and testing data and then evaluate the performance of the model on test data. So, I thought cross validation is the natural choice to see how the prediction works on the hold-out data. Is there any example that I can take a look to see how to do cross validation and get the prediction results on my data? Thanks a lot, Andra --- On Wed, 8/24/11, Prof Brian Ripley lt;rip...@stats.ox.ac.ukgt; wrote: From: Prof Brian Ripley lt;rip...@stats.ox.ac.ukgt; Subject: Re: [R] How to do cross validation with glm? To: Andra Isan lt;andra_i...@yahoo.comgt; Cc: r-help@r-project.org Date: Wednesday, August 24, 2011, 10:11 AM What you describe is not cross-validation, so I am afraid we do not know what you mean. And cv.glm does 'prediction for the hold-out data' for you: you can read the code to see how it does so. I suspect you mean you want to do validation on a test set, but that is not what you actually claim. There are lots of examples of this sort of thing in MASS (the book, scripts in the package). On Wed, 24 Aug 2011, Andra Isan wrote: Hi All, I have a fitted model called glm.fit which I used glm and data dat is my data frame pred= predict(glm.fit, data = dat, type=response) to predict how it predicts on my whole data but obviously I have to do cross-validation to train the model on one part of my data and predict on the other part. So, I searched for it and I found a function cv.glm which is in package boot. So, I tired to use it as: cv.glm = (cv.glm(dat, glm.fit, cost, K=nrow(dat))$delta) but I am not sure how to do the prediction for the hold-out data. Is there any better way for cross-validation to learn a model on training data and test it on test data in R? Thanks, Andra __ R-help@r-project.org 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. -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595 __ R-help@r-project.org 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. - Frank Harrell Department of Biostatistics, Vanderbilt University -- View this message in context: http://r.789695.n4.nabble.com/How-to-do-cross-validation-with-glm-tp3765994p3766108.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] Function rank() for data frames (or multiple vectors)?
On Aug 24, 2011, at 1:37 PM, Sebastian Bauer wrote: Hi! in R? Basically, what I need is a mixture of order() and rank(). While the former allows to specify multiple vectors, it doesn't provide the flexibility of rank() such that I can specify what happens if ties can not be broken. An example of this simple problem would clarify this greatly. I cannot tell what flexibility in 'rank' is missing in 'order'. Thanks for your answer. For instance, if I have two vectors such as 1 1 1 2 1 2 1 3 2 1 that I want combinedly ranked. I'd like to get an output 1 2 2 4 5 or (ties.method=average) 1 2.5 2.5 4 5 Basically, I need a function similar to the rank() function that accepts more than one vector (as order() does). Can't you just paste the columns and run rank on the results? 'rank' accepts character vectors. I was looking for an elegant solution ;) In the real case I have double values and this would be quite inefficient then. Still no r-code: Then what about rank(order(...) , further-ties.method-argument) ? I'm perhaps not seeing the problem clearly? Best, Sebastian David Winsemius, MD West Hartford, CT __ R-help@r-project.org 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] read.table truncated data?
Hi R users, I was using read.table to read a file. The data.fame looked alright, but I found not all rows are read by the read.table. What's wrong with it? It didn't give me any warning or error messages. Why the data are truncated? Thanks. $ wc -l all/isoform_exp.diff 42847 all/isoform_exp.diff a=read.table('all/isoform_exp.diff', header=T, sep='\t') nrow(a) [1] 21423 -- Best, Zhenjiang [[alternative HTML version deleted]] __ R-help@r-project.org 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: convert entry of a list into a matrix
Dear All, As always, I appreciate all your help. I would like to know the easiest way to convert each of the homogeneous elements of a numeric list into a matrix. Each element of this list is also a list such that when displayed, looks like a 2-by-3 matrix , I would like to convert each of them into a matrix, without changing the double index of each entry. Suppose: a- vector(list,2) a[[1]] [,1] [,2] [,3] [1,] 1 2 0 [2,] 3 4 5 a[[2]] [,1] [,2] [,3] [1,] 5 69 [2,] 78 10 is.list(a[[1]]) True Target: I would like to convert a[[2]] into a matrix, keeping the double index, into mat1 [,1] [,2] [,3] [1,] 5 6 9 [2,] 7810 The list I have is huge and so is each of its elements, and do not want to use unlist because I do not understand it fully. Thank you, Chee [[alternative HTML version deleted]] __ R-help@r-project.org 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] read.table truncated data?
Hi, On Wed, Aug 24, 2011 at 2:18 PM, zhenjiang xu zhenjiang...@gmail.com wrote: Hi R users, I was using read.table to read a file. The data.fame looked alright, but I found not all rows are read by the read.table. What's wrong with it? It didn't give me any warning or error messages. Why the data are truncated? Thanks. $ wc -l all/isoform_exp.diff 42847 all/isoform_exp.diff a=read.table('all/isoform_exp.diff', header=T, sep='\t') nrow(a) [1] 21423 This is a common problem. You need to take a look at the last row that was imported, and the rows around 21423 in the original file. Common causes include stray single or double quotation marks, and other special characters in your file like the default comment.char # Sarah -- Sarah Goslee http://www.functionaldiversity.org __ R-help@r-project.org 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: convert entry of a list into a matrix
I'm not sure I understand your question: a[[2]] is a matrix. a - list(matrix(1:6,2),matrix(5:10,2)) is.matrix(a[[2]]) TRUE x = a[[2]] is.matrix(x) TRUE x+2 [,1] [,2] [,3] [1,] 7 9 11 [2,] 810 12 a[[2]] + 2 [,1] [,2] [,3] [1,] 7 9 11 [2,] 810 12 What else do you need? Michael Weylandt On Wed, Aug 24, 2011 at 2:28 PM, Chee Chen chee.c...@yahoo.com wrote: Dear All, As always, I appreciate all your help. I would like to know the easiest way to convert each of the homogeneous elements of a numeric list into a matrix. Each element of this list is also a list such that when displayed, looks like a 2-by-3 matrix , I would like to convert each of them into a matrix, without changing the double index of each entry. Suppose: a- vector(list,2) a[[1]] [,1] [,2] [,3] [1,] 1 2 0 [2,] 3 4 5 a[[2]] [,1] [,2] [,3] [1,] 5 69 [2,] 78 10 is.list(a[[1]]) True Target: I would like to convert a[[2]] into a matrix, keeping the double index, into mat1 [,1] [,2] [,3] [1,] 5 6 9 [2,] 7810 The list I have is huge and so is each of its elements, and do not want to use unlist because I do not understand it fully. Thank you, Chee [[alternative HTML version deleted]] __ R-help@r-project.org 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. [[alternative HTML version deleted]] __ R-help@r-project.org 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: convert entry of a list into a matrix
Rereading your email, still not sure what the question is -- perhaps you could give a better code example to illustrate the difference between a[[2]] and mat1 -- but, since you mentioned briefly lists of lists, have you looked at unlist(, recursive = F)? If applied to a list of lists, it won't unlist the sub-lists and that gives you a list of matrices, which, as I said before, are matrices when subsetted with `[[`, but not `[`. Michael On Wed, Aug 24, 2011 at 2:37 PM, R. Michael Weylandt michael.weyla...@gmail.com wrote: I'm not sure I understand your question: a[[2]] is a matrix. a - list(matrix(1:6,2),matrix(5:10,2)) is.matrix(a[[2]]) TRUE x = a[[2]] is.matrix(x) TRUE x+2 [,1] [,2] [,3] [1,] 7 9 11 [2,] 810 12 a[[2]] + 2 [,1] [,2] [,3] [1,] 7 9 11 [2,] 810 12 What else do you need? Michael Weylandt On Wed, Aug 24, 2011 at 2:28 PM, Chee Chen chee.c...@yahoo.com wrote: Dear All, As always, I appreciate all your help. I would like to know the easiest way to convert each of the homogeneous elements of a numeric list into a matrix. Each element of this list is also a list such that when displayed, looks like a 2-by-3 matrix , I would like to convert each of them into a matrix, without changing the double index of each entry. Suppose: a- vector(list,2) a[[1]] [,1] [,2] [,3] [1,] 1 2 0 [2,] 3 4 5 a[[2]] [,1] [,2] [,3] [1,] 5 69 [2,] 78 10 is.list(a[[1]]) True Target: I would like to convert a[[2]] into a matrix, keeping the double index, into mat1 [,1] [,2] [,3] [1,] 5 6 9 [2,] 7810 The list I have is huge and so is each of its elements, and do not want to use unlist because I do not understand it fully. Thank you, Chee [[alternative HTML version deleted]] __ R-help@r-project.org 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. [[alternative HTML version deleted]] __ R-help@r-project.org 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] silently testing for data from another package for .Rd examples
On 8/24/2011 12:40 PM, Uwe Ligges wrote: Actually it is recommended to test for the availability of a valid package with find.package(), particularly in this case where the name of the package is already know. Best, Uwe Thanks. So I guess the idiom I'm looking for is length((find.package(, quiet=TRUE))) [1] 0 length((find.package(lattice, quiet=TRUE))) [1] 1 However, AFAICS, find.package() was only introduced in R 2.13.x, so I'm a bit reluctant to use it in a new package that need not otherwise require the latest major version. The following (from Yihui) works for earlier versions, at least R 2.12.2 %in% .packages(all=TRUE) [1] FALSE lattice %in% .packages(all=TRUE) [1] TRUE On 24.08.2011 18:29, Yihui Xie wrote: .packages(all = TRUE) will give you a list of all available packages without really loading them like require(). Regards, Yihui -- Yihui Xiexieyi...@gmail.com Phone: 515-294-2465 Web: http://yihui.name Department of Statistics, Iowa State University 2215 Snedecor Hall, Ames, IA On Wed, Aug 24, 2011 at 9:28 AM, Michael Friendlyfrien...@yorku.ca wrote: In an .Rd example for a package, I want to use data from another package, but avoid loading the entire package and avoid errors/warnings if that other package is not available. If I don't care about loading the other package, I can just do: if (require(ElemStatLearn, quietly=TRUE)) { data(prostate) # rest of example } I'd rather just be able to do something like: if (data(prostate, package=ElemStatLearn)) { # rest of example } but it appears that data() doesn't return anything useful (like FALSE or NULL) in case the named data set doesn't exist, or the package cannot be found. Below are some test cases in a fresh R 2.13.1 session. Is there someway I can incorporate such a data example silently without errors or warnings if the package doesn't exist, as is the case with require()? data(prostate, package=ElemStatLearn) dd- data(prostate, package=ElemStatLearn) dd [1] prostate dd2- data(x, package=ElemStatLearn) Warning message: In data(x, package = ElemStatLearn) : data set 'x' not found dd2 [1] x dd2- data(x, package=ElemStatLearn, verbose=FALSE) Warning message: In data(x, package = ElemStatLearn, verbose = FALSE) : data set 'x' not found dd3- data(z, package=foobar) Error in find.package(package, lib.loc, verbose = verbose) : there is no package called 'foobar' dd3 Error: object 'dd3' not found try() doesn't seem to help here: ddtry- try(data(z, package=foobar)) Error in find.package(package, lib.loc, verbose = verbose) : there is no package called 'foobar' ddtry [1] Error in find.package(package, lib.loc, verbose = verbose) : \n there is no package called 'foobar'\n attr(,class) [1] try-error -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 4700 Keele StreetWeb: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA __ R-help@r-project.org 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@r-project.org 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. -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 4700 Keele StreetWeb: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA __ R-help@r-project.org 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] Model selection and model efficiency - Search for opinions
Hi, In order to find the best models I use AIC, more specifically I calculate Akaike weights then Evidence Ratio (ER) and consider that models with a ER 2 are equally likely. But the same problem remain each time I do that. I selected the best models from a set of them, but I don't know if those models are efficient to predict (or at least represent) my data. I can have selected the best element(s) of the list of the worst models. Do you find it is correct to calculate R2 or pseudo-R2 for the best set of models in order to have an idea of the representativeness of those models and use this value to select the more efficient model ? I would be glad to hear your opinions about this ! Thanks, Arnaud [[alternative HTML version deleted]] __ R-help@r-project.org 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] Column of probabilities
Hi all, I have a vector xm say: xm = c(1,2,3,4,5,5,5,6,6) I want to return a vector with the corresponding probabilities based on the amount of times the numbers occurred. For example, I should get the following vector for xm: prob.xm = c(1/9, 1/9, 1/9, 1/9, 3/9, 3/9, 3/9, 2/9, 2/9) Any help greatly appreciated. -- Thanks, Jim. [[alternative HTML version deleted]] __ R-help@r-project.org 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] Column of probabilities
Try this: prob.xm - (table(xm)/length(xm))[match(xm, sort(unique(xm)))] Jean Jim Silverton wrote on 08/24/2011 02:31:05 PM: Hi all, I have a vector xm say: xm = c(1,2,3,4,5,5,5,6,6) I want to return a vector with the corresponding probabilities based on the amount of times the numbers occurred. For example, I should get the following vector for xm: prob.xm = c(1/9, 1/9, 1/9, 1/9, 3/9, 3/9, 3/9, 2/9, 2/9) Any help greatly appreciated. -- Thanks, Jim. [[alternative HTML version deleted]] __ R-help@r-project.org 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] Column of probabilities
If your numbers are all positive integers, this should work: (tabulate(xm)[xm])/length(xm) it can be put into a function for ease of use: probVec - function(x) {(tabulate(x)[x])/length(x)} You'll have some trouble if you have non-positive integers or non-integers. Let me know if you need to handle that case: it's not much harder (just a transform in and out of integers). Hope this helps, Michael On Wed, Aug 24, 2011 at 3:31 PM, Jim Silverton jim.silver...@gmail.comwrote: Hi all, I have a vector xm say: xm = c(1,2,3,4,5,5,5,6,6) I want to return a vector with the corresponding probabilities based on the amount of times the numbers occurred. For example, I should get the following vector for xm: prob.xm = c(1/9, 1/9, 1/9, 1/9, 3/9, 3/9, 3/9, 2/9, 2/9) Any help greatly appreciated. -- Thanks, Jim. [[alternative HTML version deleted]] __ R-help@r-project.org 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. [[alternative HTML version deleted]] __ R-help@r-project.org 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] Model selection and model efficiency - Search for opinions
1. As this is not really appropriate for R, I suggest replies be private. 2. You might try posting on various statistical forums, e.g. on http://stats.stackexchange.com/ -- Cheers, Bert On Wed, Aug 24, 2011 at 12:15 PM, Arnaud Mosnier a.mosn...@gmail.com wrote: Hi, In order to find the best models I use AIC, more specifically I calculate Akaike weights then Evidence Ratio (ER) and consider that models with a ER 2 are equally likely. But the same problem remain each time I do that. I selected the best models from a set of them, but I don't know if those models are efficient to predict (or at least represent) my data. I can have selected the best element(s) of the list of the worst models. Do you find it is correct to calculate R2 or pseudo-R2 for the best set of models in order to have an idea of the representativeness of those models and use this value to select the more efficient model ? I would be glad to hear your opinions about this ! Thanks, Arnaud [[alternative HTML version deleted]] __ R-help@r-project.org 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@r-project.org 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] Opening package manual from within R
Apparently my request to view the help pages is not a popular method among R users for gaining information. for me these pages are very helpful so I will follow up to completed this thread for future searchers. First thanks fo Prof. Brian Ripley. Your idea was spot on what I was looking for for generating a pdf from the package library. I worked out the following code that I added to my .First() library: #=manual - function(library, method=web){ LIB - substitute(library) LIB - as.character(LIB) METH - substitute(method) METH - as.character(METH) switch(METH, web = browseURL(paste(http://cran.r-project.org/web/packages/,LIB,/,LIB,.pdf;, sep = )), system = {unlink(paste(getwd(),/,LIB,.pdf,sep=)) path - find.package(LIB) system(paste(shQuote(file.path(R.home(bin), R)),CMD, Rd2pdf,shQuote(path)))}) } #= library is the package name method is either web or system (web is Internet based and faster where as system creates the pdf from the library latex code and is slower) #= Thanks for your responses! Tyler Date: Wed, 24 Aug 2011 07:12:24 +0100 From: rip...@stats.ox.ac.uk To: tyler_rin...@hotmail.com CC: r-help@r-project.org Subject: Re: [R] Opening package manual from within R On Tue, 23 Aug 2011, Tyler Rinker wrote: Simple question but searching rseek did not yield the results I wanted. Question: Is there a way to open a help manual for a package from within R. For instance I would like to type a function in r for the tm package and R would open that PDF as seen here: http://cran.r-project.org/web/packages/tm/tm.pdf -The vignette function exists for vignettes [vignette(package.name)] so I assume the same exists for manuals. You assume wrong. Vignettes PDFs are installed as part of the package (and often take minutes to regenerate): the PDF version of the help pages (what you seem to call 'the package manual') is not (in general). In many cases what other people (including the author, e.g. me for RODBC) call the 'package manual' is a PDF in the doc directory (which may or may not be a vignette). The assumption is that people will use search facilities or the hints given by the help titles in help(package=tm) or browse the HTML version of the same information (e.g. via help.start). But you can (provided you have pdflatex etc in your path) generate the PDF version of the help pages by R CMD Rd2pdf /path/to/installed/package It will even open it in a browser for you (unless you use --no-preview). You could easily encapsulate this in a function by e.g. showPDFmanual - function(package, lib.loc=NULL) { path - find.package(package, lib.loc) system(paste(shQuote(file.path(R.home(bin), R)), CMD, Rd2pdf, shQuote(path))) } Alternatively *for packages on CRAN only* you can access the version on CRAN by browseURL. -I do not want library(help=package.name) as this is not detailed enough. I am running R 2.14.0 beta on a windows 7 machine Reproducible code does not seem appropriate in this case. But accurate 'at a minimum' information (and no HTML) does. There is no such version as '2.14.0 beta', and will not be for a couple of months. If you are running a beta version of R it is old, so please update to a released or patched version. (Also, any version calling itself '2.14.0 Under development' is old and needs updating: the current R-devel displays no version number.) [[alternative HTML version deleted]] __ R-help@r-project.org 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. -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595 [[alternative HTML version deleted]] __ R-help@r-project.org 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] Append a value to a vector
This should be easy but it does not work I have 3 vectors*(activeT,inactT, activeR)*, the idea is that if the last value in inactT is higher than the last in activeT this value has to be append in active T and the last value in another vector call activeR has to be repeated. (at the bottom you can find the vectors) I have done this: activeT=round(as.numeric(activeT)) inactT= round(as.numeric(inactT)) lastV-round(as.numeric(tail(lat,1))) lastA-round(as.numeric(tail(activeT,1))) lastI-round(as.numeric(tail(inactT,1))) if (lastV!=lastA){ append(lastV, activeT) lastR=tail(activeR,1) append(activeR,lastR) } lastR has been appended to activeR but not lastV to activeV I guess that this is related to the attributes of the vectors this is why I applied as.numeric at all the vectors. Thank you for you time and your patience :) Claudio *this are the vectors:* activeT [1]26.11 341.11 376.11 459.11 466.21 532.11 935.11 1163.11 [9] 1721.11 6167.11 6513.11 7114.21 7225.11 7254.11 7728.11 7964.11 [17] 8630.11 8803.11 9186.11 9453.11 10132.11 10669.21 11326.11 11486.11 [25] 11508.11 11711.11 11726.11 13450.11 13465.11 15965.11 15979.11 16324.11 [33] 16827.11 16959.11 17809.11 19048.21 22673.11 23268.11 32596.11 33148.11 [41] 46717.11 inactT [1] 316.13 656.13 6378.13 8098.13 8099.13 10755.13 [7] 11440.13 15463.13 22474.13 22600.13 27936.13 27944.13 [13] 30757.13 32503.13 32506.13 32522.13 33082.13 51436.13 activeR [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 [[alternative HTML version deleted]] __ R-help@r-project.org 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] Append a value to a vector
Claudio Zanettini wrote on 08/24/2011 03:04:39 PM: This should be easy but it does not work I have 3 vectors*(activeT,inactT, activeR)*, the idea is that if the last value in inactT is higher than the last in activeT this value has to be append in active T When you say this value which one do you mean, the last value in inactT or the last value in activeT? and the last value in another vector call activeR has to be repeated. (at the bottom you can find the vectors) I have done this: activeT=round(as.numeric(activeT)) inactT= round(as.numeric(inactT)) lastV-round(as.numeric(tail(lat,1))) When I submit this line of your code, I get this error: Error in tail(lat, 1) : object 'lat' not found You didn't provide any information on the vector lat. Jean lastA-round(as.numeric(tail(activeT,1))) lastI-round(as.numeric(tail(inactT,1))) if (lastV!=lastA){ append(lastV, activeT) lastR=tail(activeR,1) append(activeR,lastR) } lastR has been appended to activeR but not lastV to activeV I guess that this is related to the attributes of the vectors this is why I applied as.numeric at all the vectors. Thank you for you time and your patience :) Claudio *this are the vectors:* activeT [1]26.11 341.11 376.11 459.11 466.21 532.11 935.11 1163.11 [9] 1721.11 6167.11 6513.11 7114.21 7225.11 7254.11 7728.11 7964.11 [17] 8630.11 8803.11 9186.11 9453.11 10132.11 10669.21 11326.11 11486.11 [25] 11508.11 11711.11 11726.11 13450.11 13465.11 15965.11 15979.11 16324.11 [33] 16827.11 16959.11 17809.11 19048.21 22673.11 23268.11 32596.11 33148.11 [41] 46717.11 inactT [1] 316.13 656.13 6378.13 8098.13 8099.13 10755.13 [7] 11440.13 15463.13 22474.13 22600.13 27936.13 27944.13 [13] 30757.13 32503.13 32506.13 32522.13 33082.13 51436.13 activeR [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 [[alternative HTML version deleted]] __ R-help@r-project.org 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] Column of probabilities
On Aug 24, 2011, at 3:31 PM, Jim Silverton wrote: Hi all, I have a vector xm say: xm = c(1,2,3,4,5,5,5,6,6) I want to return a vector with the corresponding probabilities based on the amount of times the numbers occurred. For example, I should get the following vector for xm: prob.xm = c(1/9, 1/9, 1/9, 1/9, 3/9, 3/9, 3/9, 2/9, 2/9) ?prop.table Usage (with table) prob.xm - round( prop.table(table(xm)), digits=3) prob.xm xm 1 2 3 4 5 6 0.111 0.111 0.111 0.111 0.333 0.222 -- David Winsemius, MD West Hartford, CT __ R-help@r-project.org 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] Append a value to a vector
I'm still a little confused about lastV and lastI. The code you provide uses lastV, but your description seems to refer to lastI. Test out this code and see if it is doing what you want it to do. lastI lastA activeT activeR if(lastI lastA) { activeT - c(activeT, lastI) activeR - c(activeR, tail(activeR,1)) } activeT activeR By the way, it's helpful to others if you cc r-help@r-project.org in any replies to keep the thread going. Jean Claudio Zanettini claudio.zanett...@gmail.com wrote on 08/24/2011 04:05:10 PM: Sure, sorry for that I was not very clear I did not mention that there was another the vector! The vector lat is a vector containing both the values of activeT and of inactT. activeT and inactT have been created subsetting the vector lat' The values are labeled such as that the decimal points indicate the kind of information #.11 = active responses= activeT #.13 = inactive responses= inacT All the values are sorted in crescent way so the lastV is the last values in the vector lat (composed by the 2 vectors), and so it is also the max value of all the values of activeT and inactiveT this is the vector lat: lat [1] 26.11316.13 341.11 376.11 459.11 466.21 [7] 516.61 532.11 656.13 935.11 1163.11 1721.11 [13] 6167.11 6378.13 6513.11 7114.21 7165.61 7225.11 [19] 7254.11 7728.11 7964.11 8098.13 8099.13 8630.11 [25] 8803.11 9186.11 9453.11 10132.11 10669.21 10720.61 [31] 10755.13 11326.11 11440.13 11486.11 11508.11 11711.11 [37] 11726.11 13450.11 13465.11 15463.13 15965.11 15979.11 [43] 16324.11 16827.11 16959.11 17809.11 19048.21 19098.61 [49] 22474.13 22600.13 22673.11 23268.11 27936.13 27944.13 [55] 30757.13 32503.13 32506.13 32522.13 32596.11 33082.13 [61] 33148.11 46717.11 51436.13 thanks for you reply :) Claudio 2011/8/24 Jean V Adams jvad...@usgs.gov Claudio Zanettini wrote on 08/24/2011 03:04:39 PM: This should be easy but it does not work I have 3 vectors*(activeT,inactT, activeR)*, the idea is that if the last value in inactT is higher than the last in activeT this value has to be append in active T When you say this value which one do you mean, the last value in inactT or the last value in activeT? and the last value in another vector call activeR has to be repeated. (at the bottom you can find the vectors) I have done this: activeT=round(as.numeric(activeT)) inactT= round(as.numeric(inactT)) lastV-round(as.numeric(tail(lat,1))) When I submit this line of your code, I get this error: Error in tail(lat, 1) : object 'lat' not found You didn't provide any information on the vector lat. Jean lastA-round(as.numeric(tail(activeT,1))) lastI-round(as.numeric(tail(inactT,1))) if (lastV!=lastA){ append(lastV, activeT) lastR=tail(activeR,1) append(activeR,lastR) } lastR has been appended to activeR but not lastV to activeV I guess that this is related to the attributes of the vectors this is why I applied as.numeric at all the vectors. Thank you for you time and your patience :) Claudio *this are the vectors:* activeT [1]26.11 341.11 376.11 459.11 466.21 532.11 935.11 1163.11 [9] 1721.11 6167.11 6513.11 7114.21 7225.11 7254.11 7728.11 7964.11 [17] 8630.11 8803.11 9186.11 9453.11 10132.11 10669.21 11326.11 11486.11 [25] 11508.11 11711.11 11726.11 13450.11 13465.11 15965.11 15979.11 16324.11 [33] 16827.11 16959.11 17809.11 19048.21 22673.11 23268.11 32596.11 33148.11 [41] 46717.11 inactT [1] 316.13 656.13 6378.13 8098.13 8099.13 10755.13 [7] 11440.13 15463.13 22474.13 22600.13 27936.13 27944.13 [13] 30757.13 32503.13 32506.13 32522.13 33082.13 51436.13 activeR [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 [[alternative HTML version deleted]] __ R-help@r-project.org 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] Append a value to a vector
Thank you, this work fine, and is not contorted like mine:) In this case lastV=LastI but depending on the data that I obtain lastV can be = LastA. Any way it works very good:) Thank you very much :) PS: but I still do not understand what was wrong in the script that I used, It was not very appropriate but it is strange that was not working, 2011/8/24 Jean V Adams jvad...@usgs.gov I'm still a little confused about lastV and lastI. The code you provide uses lastV, but your description seems to refer to lastI. Test out this code and see if it is doing what you want it to do. lastI lastA activeT activeR if(lastI lastA) { activeT - c(activeT, lastI) activeR - c(activeR, tail(activeR,1)) } activeT activeR By the way, it's helpful to others if you cc r-help@r-project.org in any replies to keep the thread going. Jean Claudio Zanettini claudio.zanett...@gmail.com wrote on 08/24/2011 04:05:10 PM: Sure, sorry for that I was not very clear I did not mention that there was another the vector! The vector lat is a vector containing both the values of activeT and of inactT. activeT and inactT have been created subsetting the vector lat' The values are labeled such as that the decimal points indicate the kind of information #.11 = active responses= activeT #.13 = inactive responses= inacT All the values are sorted in crescent way so the lastV is the last values in the vector lat (composed by the 2 vectors), and so it is also the max value of all the values of activeT and inactiveT this is the vector lat: lat [1] 26.11316.13 341.11 376.11 459.11 466.21 [7] 516.61 532.11 656.13 935.11 1163.11 1721.11 [13] 6167.11 6378.13 6513.11 7114.21 7165.61 7225.11 [19] 7254.11 7728.11 7964.11 8098.13 8099.13 8630.11 [25] 8803.11 9186.11 9453.11 10132.11 10669.21 10720.61 [31] 10755.13 11326.11 11440.13 11486.11 11508.11 11711.11 [37] 11726.11 13450.11 13465.11 15463.13 15965.11 15979.11 [43] 16324.11 16827.11 16959.11 17809.11 19048.21 19098.61 [49] 22474.13 22600.13 22673.11 23268.11 27936.13 27944.13 [55] 30757.13 32503.13 32506.13 32522.13 32596.11 33082.13 [61] 33148.11 46717.11 51436.13 thanks for you reply :) Claudio 2011/8/24 Jean V Adams jvad...@usgs.gov Claudio Zanettini wrote on 08/24/2011 03:04:39 PM: This should be easy but it does not work I have 3 vectors*(activeT,inactT, activeR)*, the idea is that if the last value in inactT is higher than the last in activeT this value has to be append in active T When you say this value which one do you mean, the last value in inactT or the last value in activeT? and the last value in another vector call activeR has to be repeated. (at the bottom you can find the vectors) I have done this: activeT=round(as.numeric(activeT)) inactT= round(as.numeric(inactT)) lastV-round(as.numeric(tail(lat,1))) When I submit this line of your code, I get this error: Error in tail(lat, 1) : object 'lat' not found You didn't provide any information on the vector lat. Jean lastA-round(as.numeric(tail(activeT,1))) lastI-round(as.numeric(tail(inactT,1))) if (lastV!=lastA){ append(lastV, activeT) lastR=tail(activeR,1) append(activeR,lastR) } lastR has been appended to activeR but not lastV to activeV I guess that this is related to the attributes of the vectors this is why I applied as.numeric at all the vectors. Thank you for you time and your patience :) Claudio *this are the vectors:* activeT [1]26.11 341.11 376.11 459.11 466.21 532.11 935.11 1163.11 [9] 1721.11 6167.11 6513.11 7114.21 7225.11 7254.11 7728.11 7964.11 [17] 8630.11 8803.11 9186.11 9453.11 10132.11 10669.21 11326.11 11486.11 [25] 11508.11 11711.11 11726.11 13450.11 13465.11 15965.11 15979.11 16324.11 [33] 16827.11 16959.11 17809.11 19048.21 22673.11 23268.11 32596.11 33148.11 [41] 46717.11 inactT [1] 316.13 656.13 6378.13 8098.13 8099.13 10755.13 [7] 11440.13 15463.13 22474.13 22600.13 27936.13 27944.13 [13] 30757.13 32503.13 32506.13 32522.13 33082.13 51436.13 activeR [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 [[alternative HTML version deleted]] __ R-help@r-project.org 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] Boxplot orders
Hi there, I have length data of an organism over the year and I want to make a boxplot. I get the boxplot just fine but the months are all out of order. In the data set they are in order from Jan-Dec...how can I fix this problem? Thanks so much in advance!! Phoebe [[alternative HTML version deleted]] __ R-help@r-project.org 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] Howto convert Linear Regression data to text
Dear all, How can I covert lm data to text in the form of y=ax+b, r2 and how do I calculate R-squared(r2)? Thanks. Code: x=18:29 y=c(7.1,7,7.7,8.2,8.8,9.7,9.9,7.1,7.2,8.8,8.7,8.5) res=lm(y~x) -- View this message in context: http://r.789695.n4.nabble.com/Howto-convert-Linear-Regression-data-to-text-tp3766230p3766230.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] unused argument(s) (Header = True) help!
Hi, I'm really new to R so I aoplogise if this is a stupid question. I'm trying to import data from a .txt file into R using the read.table command, the headers for the data columns are already in the text file so I add Header = True after the file location. The problem is I keep getting the error message *unused argument(s) (Header = True)*, does anyone know why? The format of the text file is like this (I've also tried spaces rather than tab to seperate the columns): TRAPSHANNON_INDEX 1 3.347 2 3.096 3 3.521 4 2.871 5 2.678 The commond looks like this: Trap1_data-read.table(C:/Documents and Settings/Samuel/Desktop/Biology/Independent study/Stats/Diversity indices/shannon index results trap 1.txt, Header = True) I would reall appreciate some help, Yours, Sam -- View this message in context: http://r.789695.n4.nabble.com/unused-argument-s-Header-True-help-tp3765651p3765651.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] Replacing NAs in one variable with values of another variable
Thank you Dan and Ista! Both of you are correct, I should have used NA rather than NA in my example. So the correct code should be: X -as.data.frame(matrix(c(9, 6, 1, 3, 9, NA, NA,NA,NA,NA, 6, 4, 3,NA, NA, NA, 5, 4, 1, 3), ncol=2)) names(X)-c(X1,X2) X$X1[is.na(X$X1)] - X$X2[is.na(X$X1)] Where the last line replaces the missing observations of X1 by those of X2. The if else statement also works. Thank you very much, again! Rita -- View this message in context: http://r.789695.n4.nabble.com/Replacing-NAs-in-one-variable-with-values-of-another-variable-tp3763269p3765317.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] ddply from plyr package - any alternatives?
Hello everyone, I was asked to repost this again, sorry for any inconvenience. I'm looking replacement for ddply function from plyr package. Function allows to apply function by category stored in any column/columns. Regular loops or lapplys slow down greatly because my unique combination count exceeds 9000. Is there any available solution which allow me to apply function by category? currently my code looks like snippet below ddply(myData, c(country_name, product_name), myFunction) Please note that I'm looking for decently performing resolution. Thanks in advance! With regards, Adam. -- View this message in context: http://r.789695.n4.nabble.com/ddply-from-plyr-package-any-alternatives-tp3765936p3765936.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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 (stats) newcomer.... help!
Many thanks for your response. unfortunately, it appears that I'm the closest thing in the vicinity to a local expert (chilling times indeed...), but i will certainly look at the booklist in terms of the number of data points, we have: two shores, three treatments, three replicates of each treatment (arranged in a randomised block design), Shore A housed 6 species (total of 54 samples) Shore B housed 4 species (total of 36 samples) I do wish to treat each species as a separate comparison, so will look at nlme for more info of how to do this. many thanks again. -- View this message in context: http://r.789695.n4.nabble.com/R-stats-newcomer-help-tp3764819p3765487.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] Controling R from MS Access
answered my own question, just use the call shell function in vb woohoo -- View this message in context: http://r.789695.n4.nabble.com/Controling-R-from-MS-Access-tp2719751p3766037.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] df of numerator and denominator
hello I need to know the dfn and dfd of my Anova. But in the Anova output there is only Df. Is this the dfn or the dfd? and how do I get both of it in R? Thanks for any answers -- View this message in context: http://r.789695.n4.nabble.com/df-of-numerator-and-denominator-tp3765526p3765526.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] Controling R from MS Access
Hello did you happen to figure this out? I am just learning about using R, i have a whack of fish data in MSAccess...and i want to take whatever functions access is limited by with stats, and then call R to do them i know the package RODBC works great to read data from your mdb, but i want to have a command button, once clicked, activate R, and run a script, then send the analysis back to a table in the mdb, any help would be greatly appreciated thanks, doug -- View this message in context: http://r.789695.n4.nabble.com/Controling-R-from-MS-Access-tp2719751p3765671.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] Regression by factor using sapply
Apologies for the elementary nature of the question (yes, I'm another newbie)... I'd like to perform a multiple regression on a single data set containing a representation of energy consumption and temperatures containing account number, usage (KWh), heating degree days (HDD) and cooling degree (CDD) days. I want to get the coefficients back from the following equation: lm(AvgKWh ~ AvgHDD + AvgCDD, data=usage) Given that the data set contains the usage of different accounts (e.g. some large energy users and some small energy users) I do not want to perform the equation just one time. Instead, I want to re-calculate the coefficients (and associated measures of goodness of fit) for each account using the same equation and return the corresponding coefficients by the account number identifier. I thought I had figured out how to do this using by and sapply formula but I keep getting an error message of: $ operator is invalid for atomic vectors Here is what I've bee trying to use # data is stored in a table called usage; other than the ActNo field, all the fields are numeric byDD - function(data) {lm(AvgKWh~ AvgHDD + AvgCDD, data=data)} byActNo - by(usage, usage$ActNo, FUN=byDD) sapply(byActNo, summary(byActno)$coef) Thanks in advance! I'm sure a similar question has been covered somewhere but everytime I follow the message stream I hit a deadend. -- View this message in context: http://r.789695.n4.nabble.com/Regression-by-factor-using-sapply-tp3766145p3766145.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] Append a value to a vector
This should be easy but it does not work I have 3 vectors*(activeT,inactT, activeR)*, the idea is that if the last value in inactT is higher than the last in activeT this value has to be append in active T and the last value in another vector call activeR has to be repeated. (at the bottom you can find the vectors) I have done this: activeT=round(as.numeric(activeT)) inactT= round(as.numeric(inactT)) lastV-round(as.numeric(tail(lat,1))) lastA-round(as.numeric(tail(activeT,1))) lastI-round(as.numeric(tail(inactT,1))) if (lastV!=lastA){ append(lastV, activeT) lastR=tail(activeR,1) append(activeR,lastR) } lastR has been appended to activeR but not lastV to activeV I guess that this is related to the attributes of the vectors this is why I applied as.numeric at all the vectors. Thank you for you time and your patience :) Claudio *this are the vectors:* activeT [1]26.11 341.11 376.11 459.11 466.21 532.11 935.11 1163.11 [9] 1721.11 6167.11 6513.11 7114.21 7225.11 7254.11 7728.11 7964.11 [17] 8630.11 8803.11 9186.11 9453.11 10132.11 10669.21 11326.11 11486.11 [25] 11508.11 11711.11 11726.11 13450.11 13465.11 15965.11 15979.11 16324.11 [33] 16827.11 16959.11 17809.11 19048.21 22673.11 23268.11 32596.11 33148.11 [41] 46717.11 inactT [1] 316.13 656.13 6378.13 8098.13 8099.13 10755.13 [7] 11440.13 15463.13 22474.13 22600.13 27936.13 27944.13 [13] 30757.13 32503.13 32506.13 32522.13 33082.13 51436.13 activeR [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 -- View this message in context: http://r.789695.n4.nabble.com/Append-a-value-to-a-vector-tp3766180p3766180.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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] as.numeric() and POSIXct format
Hi! I'm confused by this: as.numeric(as.POSIXct(518400,origin=2001-01-01)) [1] 978822000 I guess the problem is that as.numeric() assumes a different origin, but cannot find any default origin. How can I get back the seconds from the POSIXct format? In other words, which the inverse function of as.POSIXct()? I've tried as.numeric and unclass() using a origin= argument, but this does not work. Thanks Agus -- Dr. Agustin Lobo Institut de Ciencies de la Terra Jaume Almera (CSIC) LLuis Sole Sabaris s/n 08028 Barcelona Spain Tel. 34 934095410 Fax. 34 934110012 email: agustin.l...@ija.csic.es __ R-help@r-project.org 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 by command
Hello, I am a new user of R, and I'd be grateful if someone could help me with the following: I would like to compute the mean of variable trust in dataframe foo, but separately for each level of variable V2. That is, I'd like to compute the mean of trust at each level of V2. I have done this: tmp - by(foo, foo$V2, function(x) mean(foo$trust, na.rm=T)) tmp Doing this does indeed give me a mean for variable trust at each level of V2 - but the problem is that I get the exact same mean for each level of V2. The means are not the really the same across levels of V2, but instead of getting the mean for each level of V2 I get the overall mean for the variable in the dataframe listed over and over for each level of V2. I have been attempting to figure this out for a while now, but I just can't seem to figure it out. Thanks for your time! Ari -- View this message in context: http://r.789695.n4.nabble.com/help-with-by-command-tp3766285p3766285.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org 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.