RE: [R] Strange (non-deterministic) problem with strsplit
> "HenrikB" == Henrik Bengtsson <[EMAIL PROTECTED]> > on Sat, 17 Jul 2004 01:59:17 +0200 writes: HenrikB> [Moving this thread to R-devel instead] I suspect HenrikB> your "random" results are due to a bug in HenrikB> gsub(). On my R v1.9.0 (Rterm and Rgui) R crashes HenrikB> when I do HenrikB> % R --vanilla >> gsub(" ", "", "abb + c | a*b", perl=TRUE) HenrikB> Trying >> gsub(" ", "", "b c + d | a * b", perl=TRUE) HenrikB> and I'll get NULL. With >> gsub("\\s", "", "bc + d | a * b", perl=TRUE) HenrikB> it works as expected. So there is something buggy HenrikB> for sure. HenrikB> This might have been fixed in R v1.9.1 or its HenrikB> patched version. probably not. Here are results from 1.91-patched > gsub(" ", "", "b c + d | a * b", perl=TRUE) NULL > gsub("\\s", "", "b c + d | a * b", perl=TRUE) NULL > gsub("\\s", "", "bc + d | a * b", perl=TRUE) [1] "bc+d|a*b" > gsub(" ", "", "bc + d | a * b", perl=TRUE) [1] "bc+d|a*b" > Martin Maechler, ETH Zurich __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] sas to r
Adaikalavan Ramasamy wrote: On Fri, 2004-07-16 at 23:18, Greg Adkison wrote: I would be incredibly grateful to anyone who'll help me translate some SAS code into R code. Searching for "SAS code OR script OR translate" on http://maths.newcastle.edu.au/~rking/R/ gives a few results, one of which looks promising is http://tolstoy.newcastle.edu.au/R/help/04/04/0009.html and I anticipated the above citation from 01 Apr 2004 (hint: 01 April !) when I looked at the thread and realized URLs were cited ... Uwe http://tolstoy.newcastle.edu.au/R/help/04/02/0660.html Say for example that I have a dataset named "dat1" that includes five variables: wshed, site, species, bda, and sla. I can calculate with the following SAS code the mean, CV, se, and number of observations of "bda" and "sla" for each combination of "wshed," "species," and "site," restricting the species considered to only three of several species in dat1 (b, c, and p). Moreover, I can output these calculations and grouping variables to a dataset named "dat2" that will reside in RAM and include the variables wshed, site, species, mBdA, msla, cBda, sBdA, ssla, nBda, and nsla. data(iris) attach(iris) iris[c(1,2,51,52,101,102), ] Sepal.Length Sepal.Width Petal.Length Petal.WidthSpecies 15.1 3.5 1.4 0.2 setosa 24.9 3.0 1.4 0.2 setosa ... 51 7.0 3.2 4.7 1.4 versicolor 52 6.4 3.2 4.5 1.5 versicolor ... 101 6.3 3.3 6.0 2.5 virginica 102 5.8 2.7 5.1 1.9 virginica tapply(Sepal.Length, Species, function(x) c( mean(x), sd(x)/mean(x), length(x) )) $setosa [1] 5.0060 0.07041344 50. $versicolor [1] 5.9360 0.08695606 50. $virginica [1] 6.5880 0.09652089 50. proc sort data=dat1; by wshed site species; proc means data=dat1 noprint mean cv stderr n; by wshed site species; where species in ('b', 'c', 'p'); var BdA sla; output out=dat2 mean=mBdA msla cv=cBdA csla stderr=sBdA ssla n=nBdA nsla; Thanks, Greg __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Random Fields
Michael Axelrod wrote: I have tried to install the package RandomFields (using the packages menu) from CRAN and it fails to open the zip file. I then tried downloading the package from the author's site, but it still fails to install. Anyone had any success with this? Or am I doing something wrong. Thanks in advance for any help. Please try again. It should work now (at least from CRAN master). Uwe Ligges Michael Axelrod __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] sas to r
Greg Adkison wrote: I would be incredibly grateful to anyone who'll help me translate some SAS code into R code. Say for example that I have a dataset named "dat1" that includes five variables: wshed, site, species, bda, and sla. I can calculate with the following SAS code the mean, CV, se, and number of observations of "bda" and "sla" for each combination of "wshed," "species," and "site," restricting the species considered to only three of several species in dat1 (b, c, and p). Moreover, I can output these calculations and grouping variables to a dataset named "dat2" that will reside in RAM and include the variables wshed, site, species, mBdA, msla, cBda, sBdA, ssla, nBda, and nsla. proc sort data=dat1; by wshed site species; proc means data=dat1 noprint mean cv stderr n; by wshed site species; where species in ('b', 'c', 'p'); var BdA sla; output out=dat2 mean=mBdA msla cv=cBdA csla stderr=sBdA ssla n=nBdA nsla; Thanks, Greg The following handles any number of analysis variables, with automatic naming of all statistics computed from them. It requires the Hmisc package. # Generate some data. Put one NA in sla. set.seed(1) dat1 <- expand.grid(wshed=1:2, site=c('A','B'), species=c('a','b','c','p'), reps=1:10) n <- nrow(dat1) dat1 <- transform(dat1, BdA = rnorm(n, 100, 20), sla = c(rnorm(n-1, 200, 30), NA)) # Can use upData function in Hmisc in place of transform # Summarization function, per stratum, for a matrix of analysis # variables g <- function(y) { n <- apply(y, 2, function(z) sum(!is.na(z))) m <- apply(y, 2, mean, na.rm=TRUE) s <- apply(y, 2, sd, na.rm=TRUE) cv <- s/m se <- s/sqrt(n) w <- c(m, cv, se, n) names(w) <- t(outer(c('m','c','s','n'), colnames(y), paste, sep='')) w } library(Hmisc) dat2 <- with(dat1, summarize(cbind(BdA, sla), llist(wshed, site, species), g, subset=species %in% c('b','c','p'), stat.name='mBdA') ) options(digits=3) dat2 # is a data frame wshed site species mBdA msla cBdA csla sBdA ssla nBdA nsla 1 1A b 100.5 195 0.133 0.1813 4.23 11.20 10 10 2 1A c 99.7 206 0.101 0.1024 3.17 6.68 10 10 3 1A p 101.4 188 0.239 0.1580 7.65 9.39 10 10 4 1B b 109.9 203 0.118 0.1433 4.09 9.21 10 10 5 1B c 98.4 221 0.193 0.1250 6.01 8.72 10 10 6 1B p 102.9 203 0.216 0.1446 7.03 9.29 10 10 7 2A b 95.8 195 0.241 0.2011 7.31 12.40 10 10 8 2A c 98.7 207 0.194 0.1274 6.04 8.33 10 10 9 2A p 102.2 191 0.217 0.1709 7.01 10.31 10 10 10 2B b 97.8 191 0.235 0.2079 7.27 12.58 10 10 11 2B c 100.9 194 0.164 0.0987 5.24 6.07 10 10 12 2B p 103.0 209 0.144 0.0769 4.69 5.35 109 -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] sas to r
I am must be a bigger/slower fool to have fallen for April fools trick in the mid of July ! Sorry Greg for misleading you. On Sat, 2004-07-17 at 11:43, Uwe Ligges wrote: > Adaikalavan Ramasamy wrote: > > > On Fri, 2004-07-16 at 23:18, Greg Adkison wrote: > > > >>I would be incredibly grateful to anyone who'll help me translate some > >>SAS code into R code. > > > > > > Searching for "SAS code OR script OR translate" on > > http://maths.newcastle.edu.au/~rking/R/ gives a few results, one of > > which looks promising is > > > > http://tolstoy.newcastle.edu.au/R/help/04/04/0009.html and > > I anticipated the above citation from 01 Apr 2004 (hint: 01 April !) > when I looked at the thread and realized URLs were cited ... > > Uwe > > > > http://tolstoy.newcastle.edu.au/R/help/04/02/0660.html > > > > > >>Say for example that I have a dataset named "dat1" that includes five > >>variables: wshed, site, species, bda, and sla. I can calculate with the > >>following SAS code the mean, CV, se, and number of observations of > >>"bda" and "sla" for each combination of "wshed," "species," and "site," > >>restricting the species considered to only three of several species in > >>dat1 (b, c, and p). Moreover, I can output these calculations and > >>grouping variables to a dataset named "dat2" that will reside in RAM > >>and include the variables wshed, site, species, mBdA, msla, cBda, > >>sBdA, ssla, nBda, and nsla. > > > > > > data(iris) > > attach(iris) > > iris[c(1,2,51,52,101,102), ] > > Sepal.Length Sepal.Width Petal.Length Petal.WidthSpecies > > 15.1 3.5 1.4 0.2 setosa > > 24.9 3.0 1.4 0.2 setosa > > ... > > 51 7.0 3.2 4.7 1.4 versicolor > > 52 6.4 3.2 4.5 1.5 versicolor > > ... > > 101 6.3 3.3 6.0 2.5 virginica > > 102 5.8 2.7 5.1 1.9 virginica > > > > > >>tapply(Sepal.Length, Species, function(x) c( mean(x), sd(x)/mean(x), > > > > length(x) )) > > $setosa > > [1] 5.0060 0.07041344 50. > > > > $versicolor > > [1] 5.9360 0.08695606 50. > > > > $virginica > > [1] 6.5880 0.09652089 50. > > > > > > > >>proc sort data=dat1; > >> by wshed site species; > >>proc means data=dat1 noprint mean cv stderr n; > >> by wshed site species; > >> where species in ('b', 'c', 'p'); > >> var BdA sla; > >> output out=dat2 > >>mean=mBdA msla > >>cv=cBdA csla > >>stderr=sBdA ssla > >>n=nBdA nsla; > >> > >>Thanks, > >>Greg > >> > >>__ > >>[EMAIL PROTECTED] mailing list > >>https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >> > > > > > > __ > > [EMAIL PROTECTED] mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] sas to r
Adaikalavan Ramasamy wrote: I am must be a bigger/slower fool to have fallen for April fools trick in the mid of July ! Sorry Greg for misleading you. Adaikalavan, I think you don't need to apologize. I had read that message three times, and visited the cited web page within that message which looked very professional, before realizing that it is an April fool. And I had the advantage to know it was April 01 that day ;-) Uwe __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Running the optimization on the subset of parameters
If you use the mle() function in the `stats4' package, you don't have to do anything special. For example, > negloglik <- function(a, b) { a^2 + b^2 } > mle(negloglik, start = list(a = 2), fixed = list(b = 4)) Call: mle(minuslogl = negloglik, start = list(a = 2), fixed = list(b = 4)) Coefficients: ab 2.202682e-13 4.00e+00 > mle(negloglik, start = list(b = 2), fixed = list(a = 4)) Call: mle(minuslogl = negloglik, start = list(b = 2), fixed = list(a = 4)) Coefficients: ab 4.00e+00 2.202682e-13 > -roger Victoria Landsman wrote: Dear all, I'd like to find a minimum of (-loglik) function which is a function of k parameters. I'd like to run the minimization algorithm for the different subsets of the parameters and assign the fixed values to the complementary subset. How should I define my (-loglik) function such that it can be passed to the optim or other optimization function? Much thanks for any suggestions. Vicky Landsman. [[alternative HTML version deleted]] __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] areg.boot use of inverseTrans and ytype
Anne wrote: Hi R helpers! I'm still a bit ( alot) confused by the use of "inverseTrans" and "ytype" in areg.boot (Hmisc): What I want to do seems very simple, but I do not get the result I want: plot the predicted values in the original scale. (I did not understand the documentation, sorry!) for instance the following code f<-areg.boot(Pe[here]~monotone(t[here])+monotone(v[here])+I(Pa[here]-Pm[here])+..,B=100) In models don't subset every variable. Use subset=here. f.origScale<-Function(f,ytype="inverse") # works up to here OK, back to original Pe scale Now if use predict() to make prediction for given input values , how do I transform the result back into the original response scale? (in the documentation: A 'predict' method computes predicted values on the original or transformed response scale, or a matrix of transformed predictors) so I tried y.predicted<-predict(f,data2predict, type='inverse') #result is in transformed scale y.predicted<-predict(f,data2predict, type='fitted',scale="original")without success y.predicted <- predict(f, statistic='fitted') # similar to statistic='median' (I did not find the documentation for the predict method of areg.boot) It's there. Do ?areg.boot and you'll see it. Frank as the inverse transformation of response obtained by areg.boot is not analytical, I do not see how I can use inverseTrans . Thank for any help Anne Anne Piotet Tel: +41 79 359 83 32 (mobile) Email: [EMAIL PROTECTED] --- M-TD Modelling and Technology Development PSE-C CH-1015 Lausanne Switzerland Tel: +41 21 693 83 98 Fax: +41 21 646 41 33 -- [[alternative HTML version deleted]] __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] sas to r
Here is an improvement to what I sent earlier today. g <- function(y) { s <- apply(y, 2, function(z) { z <- z[!is.na(z)] n <- length(z) if(n==0) c(NA,NA,NA,0) else if(n==1) c(z, NA,NA,1) else { m <- mean(z) s <- sd(z) c(Mean=m, CV=s/m, SE=s/sqrt(n), N=n) } }) w <- as.vector(s) names(w) <- as.vector(outer(rownames(s), colnames(s), paste, sep='')) w } library(Hmisc) dat2 <- with(dat1, summarize(cbind(BdA, sla), llist(wshed, site, species), g, subset=species %in% c('b','c','p'), stat.name=NULL) ) options(digits=3) dat2 # is a data frame wshed site species MeanBdA CVBdA SEBdA NBdA Meansla CVsla SEsla Nsla 1 1A b 100.5 0.133 4.23 10 195 0.1813 11.20 10 2 1A c99.7 0.101 3.17 10 206 0.1024 6.68 10 3 1A p 101.4 0.239 7.65 10 188 0.1580 9.39 10 4 1B b 109.9 0.118 4.09 10 203 0.1433 9.21 10 5 1B c98.4 0.193 6.01 10 221 0.1250 8.72 10 6 1B p 102.9 0.216 7.03 10 203 0.1446 9.29 10 7 2A b95.8 0.241 7.31 10 195 0.2011 12.40 10 8 2A c98.7 0.194 6.04 10 207 0.1274 8.33 10 9 2A p 102.2 0.217 7.01 10 191 0.1709 10.31 10 10 2B b97.8 0.235 7.27 10 191 0.2079 12.58 10 11 2B c 100.9 0.164 5.24 10 194 0.0987 6.07 10 12 2B p 103.0 0.144 4.69 10 209 0.0769 5.359 # Another approach, but does casewise deletion of NAs and computes all # possible marginal summaries dat2 <- summary(cbind(BdA, sla) ~ wshed + site + species, data=dat1, fun=g, method='cross') print.data.frame(dat2) # after Sept 2004 just say dat2 # dat2 is similar to but not really a data frame, with a matrix of statistics S # Remove method='cross' to just get all one-way summaries -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] R question about spectrum metric
Dear R community, I would like to conduct a simulation study that involves the generation and recovery of times series spectra. Spectrum analysis is a new area for me so I am very much in the crawling stage at this point. I am having difficulty understanding the output of the spec.pgram (or spectrum) function. Specifically, I do not understand the metric of the results. In my simulation the recovered coefficients correlate .99 with the generating coefficients, so everything appears in order -- accept for my confusion regarding the metric of the recovered coefficients. I have written a function (makeWave) to generate the series. I then call this function with the required arguments. Next I analyze the resulting series with spec.pgram and compare the results with the generating coefficients (at the appropriate frequencies). In a small simulation study the generating and recovered coefficients correlated .99 -- however the metric of the two sets of coefficients differ by several orders of magnitude. I would be very happy to send the actual code of this simulation (less than 2 pages of code) to anyone who could help me understand how to scale my recovered coefficients to the metric of the generating parameters. Specifically, I am looking for a general solution to the scaling problem (if one exists). Thank you in advance for any and all help. Niels Waller Vanderbilt University R 1.9.1 Windows XP ##--## ##FUNCTION: makeWave ##Purpose:to generate periodic time series (no white noise) ##Arguments ## c0:: coefficient for frequency 0 ## c.n :: vector of (complex) coefficients for frequencies 1...+n ## cminus.n :: vector of (complex) coefficients for frequencies 1...-n ## N :: number of time points in generated wave ## f :: fundamental frequency of wave makeWave<-function(c0, c.n, cminus.n, N, f){ k<-1:N #k = time point x<-rep(0,N) w <- 2*pi*f for(t.i in 1:N){ ## over time t.i x[t.i] <- c0 temp<-0 for(j in 1:length(c.n)){ ## over frequency j temp<-temp + c.n[j] * exp(1i*w*j*t.i) + cminus.n[j] * exp(1i*w*j*t.i) } x[t.i]<-x[t.i]+temp } x ## composite wave } __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] More on global environment
You may want to look at the notes on and functions for workspace management that guide me. They can be downloaded from http://faculty.washington.edu/lunnebor/Stat342/ by checking on "Exercises." I use the .GlobalEnv (position 1 on search path) solely for scratch and have project work attached further down the path. The two functions move() and rm.sv() contributed by my colleague John Miyamoto and described in the above make this easy to do. Michael Prager wrote: : Date: Thu, 15 Jul 2004 14:45:41 -0400 : From: "Mike Prager" <[EMAIL PROTECTED]> : Subject: [R] More on global environment : To: R Help list <[EMAIL PROTECTED]> : Message-ID: <[EMAIL PROTECTED]> : Content-Type: text/plain; charset="us-ascii"; format=flowed : : To follow up on my previous question, suppose a user R session wants to : unload one workspace and load another within an R session. Is the : following the correct sequence? : (snip) : Michael Prager, Ph.D. : NOAA Center for Coastal Fisheries and Habitat Research : Beaufort, North Carolina 28516 : http://shrimp.ccfhrb.noaa.gov/~mprager/ ** Cliff Lunneborg, Professor Emeritus, Statistics & Psychology, University of Washington, Seattle [EMAIL PROTECTED] __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] gray background in png
Hi all, I'm having a problem creating png images with trellis.device. I would like to create many plots with a white background using a sequence number in the file argument (i.e. "%02d"). The first plot is as expected with a white background. However, the second and all subsequent plots have a gray background. I would like all plots to have a white (or transparent) background. Below is an example: library(lattice) z <- expand.grid(A = LETTERS[1:8], B = letters[1:6]) z$x <- z$y <- rep(1, nrow(z)) trellis.device(png, file = "test%02d.png", bg = "white") lset(col.whitebg()) xyplot(y ~ x | A * B, data = z, layout = c(4, 6)) dev.off() This is also the case for jpeg and bmp though win.metafile and postscript do not have this problem. Is this a bug or am I missing something obvious? I found one reference that implies this may be expected behaviour but I'm not sure if I interpretted the comment correctly. http://finzi.psych.upenn.edu/R/Rhelp02a/archive/18890.html > R.version _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major1 minor9.1 year 2004 month06 day 21 language R > help(package = lattice) Information on Package 'lattice' Description: Package: lattice Version: 0.9-12 Date: 2004/06/01 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html