[R] how to adding colors to data points in scatter plot in R?
Does anybody know how to adding colors to data points in scatter plot in R? I have the following R data frame - Voter_Name, Education_years, Income, Registered Ricky, 18, 4, Y Lisa, 20, 34000, N Jon, 10, 35000, Y ... I am doing a scatter plot in R for those data points - Income vs. EducationYears. I would also like to using different colors to differentiate whether that data point is a Registered Voter or not. Does anybody know how to adding colors to data points in scatter plot in R? Thanks in advance!! -- View this message in context: http://www.nabble.com/how-to-adding-colors-to-data-points-in-scatter-plot-in-R--tp23681471p23681471.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] Clean up a complex variable
Dear all, I need to clean up one variables in a dataset. e.g. lets say the dataset is trial, the variable for cleaning up is V1 trial$V1 [1] 0(a=1) 0(b=1) 0.133(b=1) 0.555(a=1) 5.32(a=1) what i need to do is to remove the text (a=1) and (b=1) and the in the V1, and then convert to a numeric variable, and als I am aslo requested that when the value has a=1, the value needs to be divided by 5. what I did is: trialchara-as.character(trial$V1) trialnum-gsub((a=1)|(b=1)|,,trialchara) the result is [1] 0 () 0 () 0.133 () 0.555 () 5.32 () How can I get rid of the () symbol? How can I do this part when the value has a=1, the value needs to be divided by 5. ? Can anyone please give me some hints here? Thanks a lot. John [[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] Draw a rectangle on top of an image using RGtk2?
Dear all, I use gtkImageFromFile to display an image. Then I want to do some gsignal to handle mouse event. I click the mouse and move a another position and release. I can get the position of the firs click and the release position, then I would to draw a rectangle to display the region I have selected. I need some hints on what functions should I look for. I tried to google but don't know how. Thanks. -- HUANG Ronggui, Wincent PhD Candidate Dept of Public and Social Administration City University of Hong Kong Home page: http://asrr.r-forge.r-project.org/rghuang.html __ 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 adding colors to data points in scatter plot in R?
Dear Tim, Have a look at the ggplot2 package. library(ggplot2) ggplot(your.data.frame, aes(x = EducationYears, y = Income, colour = Registered)) + geom_point() You find a lot of examples at the ggplot2 website: http://had.co.nz/ggplot2/ HTH, Thierry ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 thierry.onkel...@inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -Oorspronkelijk bericht- Van: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Namens Tim LIU Verzonden: zaterdag 23 mei 2009 7:18 Aan: r-help@r-project.org Onderwerp: [R] how to adding colors to data points in scatter plot in R? Does anybody know how to adding colors to data points in scatter plot in R? I have the following R data frame - Voter_Name, Education_years, Income, Registered Ricky, 18, 4, Y Lisa, 20, 34000, N Jon, 10, 35000, Y ... I am doing a scatter plot in R for those data points - Income vs. EducationYears. I would also like to using different colors to differentiate whether that data point is a Registered Voter or not. Does anybody know how to adding colors to data points in scatter plot in R? Thanks in advance!! -- View this message in context: http://www.nabble.com/how-to-adding-colors-to-data-points-in-scatter-plo t-in-R--tp23681471p23681471.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. Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document. __ 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] Negative value for adjustedRandIndex?
On Thu, 2009-05-21 at 05:28 -0700, Pooka wrote: Hello, I am a very new user to R so please have patience with me. :clap: I am trying to evalute the internal response for a couple of different cluster methods with the help of the AdjustedRandIndex, which is included in the mclust package. However, I do get a bit puzzled when I get a negative value as the value should be in intervall of [0,1], am I correct? 0 indicates the agreement between your two raters is the same as the agreement you'd expect to get as a result of random chance. A negative value therefore indicates that the agreement between the two raters is even lower than that. G Have I done something wrong? Or should I interpret the negative value as the two methods do not group the cases to the same cluster? I have for example used k-means and k-median, and choosen five clusters. Each respondent has been labelled as either beloning to cluster 1,2,3,4 or 5. I have picked out the first 32 respondents. Example: a - c(1,2,3,4,5,2,1,5,1,2,2,4,2,3,3,2,4,5,5,4,4,4,5,5,2,3,1,5,5,5,4,1) b - c(5,5,5,2,5,3,2,2,2,5,3,4,5,5,2,5,2,3,5,5,5,3,5,3,3,2,4,5,2,2,2,3) adjustedRandIndex (a,b) [1] -0.01608168 Thanks in advance, Pooka -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ 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] Clustered data with Design package--bootcov() vs. robcov()
Another question related to bootcov(): A reviewer is concerned with the fact that bootstrapping the standard errors does not give the same answers each time. What is a good way to address this concern? Could I bootstrap, say, 100 times and report the mean standard error of those 100 estimates? I am already doing 1,000 replications in the bootstrap, but of course the answer is still slightly different each time. Frank E Harrell Jr wrote: robcov does not use bootstrapping. It uses the cluster sandwich (Huber-White) variance-covariance estimator for which there are references in the help file (see especially Lin). Both robcov and bootcov work best when there is a large number of small clusters. If the clusters are somewhat large and greatly vary in size, expect to be in trouble and consider a full modeling approach (generalized least squares, mixed models, etc.). One advantage of robcov is that you get the same result every time, unlike bootstrapping. But even in the case of cluster sizes of one, the sandwich estimator can be inefficient (see the Gould paper) or can result in the right estimates of the wrong quantity (see a paper by Friedman in American Statistician). Frank Thank you. -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University __ 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://www.nabble.com/Clustered-data-with-Design-package--bootcov%28%29-vs.-robcov%28%29-tp23016400p23683238.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] Constraining linear regression model
Hi All, I have two questions: I am computing a linear regression model with 0 as Intercept. Well, I would like the sum of my predicted values be equal to a constant and therefore analyze if my coefficients are significatively different using or not this constraint. Does anyone know how I can constrain my model in a such way? Here is the code: data-read.table (input.txt, header=T, dec=., sep=\t); attach(data) lm -lm(pop ~ ag + sav + mf -1, data=data) pred - predict(lm) sum(pred) So I want to constrain my sum (pred) to be equal to C, with C=sum(pop) My second question is: is it possible to make the same constraint BUT with C as a vector of values? Let's say If I have 5 observations in 'data', with 'pop' as the first column. I want to compute the same model as above with a 'vector' of constraints. In that case, C=xi (with i from 1 to 3) x1 = sum (data[c(2,4), 1]) x2 = sum(data [c(1,3), 1]) x3 = data[5,1] Thanks in advance -- Any help is welcome! Jean-Paul Kibambe __ 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] automatic model selection based on BIC in MLE
On Thu, 2009-05-21 at 20:47 -0700, Michael wrote: Hi all, Could anybody point me to an automatic model selection based on BIC for my MLE fitting problem, in R? ?stepAIC I would imagine I just have to supply the MLE LLF function, and the dimension of the problem and the number of observations should be embedded in the definition of the LLF itself. If you write a method for the logLik generic for your objects, then the AIC() function in R can be used to compute BIC. Take a look at ?AIC and getAnywhere(AIC.default) and then at the existing methods for logLik functions to see what you need to copy for your object. Once you have logLik and therefore AIC working, stepAIC will just work for your objects. Whether stepwise selection makes sense for your purposes is another question. G And then the automatical model selection procedure will select the best model for me. 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. -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ 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] Clustered data with Design package--bootcov() vs. robcov()
jjh21 wrote: Another question related to bootcov(): A reviewer is concerned with the fact that bootstrapping the standard errors does not give the same answers each time. What is a good way to address this concern? Could I bootstrap, say, 100 times and report the mean standard error of those 100 estimates? I am already doing 1,000 replications in the bootstrap, but of course the answer is still slightly different each time. First, you can argue that everything we estimate has a margin of error and that the variation across different runs of the bootstrap is within the statistical precision of what can be estimated. Second, run the bootstrap with 10,000 replications and be done with it. Frank Frank E Harrell Jr wrote: robcov does not use bootstrapping. It uses the cluster sandwich (Huber-White) variance-covariance estimator for which there are references in the help file (see especially Lin). Both robcov and bootcov work best when there is a large number of small clusters. If the clusters are somewhat large and greatly vary in size, expect to be in trouble and consider a full modeling approach (generalized least squares, mixed models, etc.). One advantage of robcov is that you get the same result every time, unlike bootstrapping. But even in the case of cluster sizes of one, the sandwich estimator can be inefficient (see the Gould paper) or can result in the right estimates of the wrong quantity (see a paper by Friedman in American Statistician). Frank Thank you. -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University __ 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 E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University __ 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 adding colors to data points in scatter plot in R?
Simply use the col paramater, with a factor to index the colors. Example: # generate some data x - rnom(100) y - rnorm(x) z - as.factor(rbinom(x,1,0.5)) # plot plot(x,y, col=c('red','blue')[z]) HTH, Matthieu __ 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: C stack usage is too close to the limit (can't understand explanations of how to fix this)
you can find the R_CStackLimit in Rinterface.h after that you will know how to use it! ÔÚ2009-05-21?17:53:54£¬anon36?ano...@yahoo.com?дµÀ£º Apparently?the?way?to?deal?with?this?error?message?is?to?set ??R_CStackLimit?=?(uintptr_t)-1 I?tried?typing?this?in?the?R?console,?but?it?says?Error:?object R_CStackLimit?not?found. So?where?do?I?type?it??In?one?of?the?initialization?files?that?R?uses?when it?starts?up? I?can't?find?the?answer?anywhere.?Please?note?that?I?don't?understand?words like?DLL?or?embed,?and?I?don't?understand?how?R?works?at?any?deep?level. I?am?using?R?2.8.1?on?Windows?XP.? --? View?this?message?in?context:?http://www.nabble.com/%22Error%3A-C-stack-usage-is-too-close-to-the-limit%22-%28can%27t-understand-explanations-of-how-to-fix-this%29-tp23650518p23650518.html Sent?from?the?R?help?mailing?list?archive?at?Nabble.com. __ r-h...@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] Constraining linear regression model
Jean-Paul Kibambe Lubamba jean-paul.kiba...@uclouvain.be wrote I have two questions: I am computing a linear regression model with 0 as Intercept. Well, I would like the sum of my predicted values be equal to a constant and therefore analyze if my coefficients are significatively different using or not this constraint. Does anyone know how I can constrain my model in a such way? Here is the code: data-read.table (input.txt, header=T, dec=., sep=\t); attach(data) lm -lm(pop ~ ag + sav + mf -1, data=data) pred - predict(lm) sum(pred) So I want to constrain my sum (pred) to be equal to C, with C=sum(pop) My second question is: is it possible to make the same constraint BUT with C as a vector of values? Let's say If I have 5 observations in 'data', with 'pop' as the first column. I want to compute the same model as above with a 'vector' of constraints. In that case, C=xi (with i from 1 to 3) x1 = sum (data[c(2,4), 1]) x2 = sum(data [c(1,3), 1]) x3 = data[5,1] Thanks in advance -- Any help is welcome! This sounds very odd. OLS linear regress *does* constrain the predicted values - the constraint is to minimize the sum of squared error. There are variations of regression that use different constraints, like sums of absolute errors and so on but why constrain the sum of predicted values to a constant? Peter Peter L. Flom, PhD Statistical Consultant www DOT peterflomconsulting DOT 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] counting occurrence of text in a dataframe
Hello list. I am hoping for some help with a relatively simple problem. I have a data frame arranged as below. I want to be able to count the occurrence of each gene (eg let-7e) by Experiment. In other words how many times does a given gene crop up in the dataframe. I tried table but couldn't work out how to get the output I want. I have also considered rearranging this data into a list (by gene) and then counting the length of each gene element. However I thought that there might be a more elegant solution. Tanaka Mitchell Wang Hunter Chen Chim miR-191* let-7e let-7b miR-126let-7a let-7g miR-198let-7f let-7c miR-146a let-7b let-7i miR-22 let-7g miR-1224 miR-16 let-7d miR-130b miR-223let-7i miR-124miR-191let-7f miR-133a miR-296let7fG15A miR-125a-3pmiR-222let-7g miR-140 miR-30dmiR-101miR-125b-5pmiR-223let-7i miR-142-5p miR-370miR-103miR-133a miR-24 miR-101miR-146 miR-486miR-125a-5pmiR-133b miR-26amiR-103miR-148a miR-498miR-126miR-135a* miR-32 miR-106a miR-152 Thanks for any advice. Cheers Iain __ 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] Draw a rectangle on top of an image using RGtk2?
On Fri, May 22, 2009 at 11:27 PM, Ronggui Huang ronggui.hu...@gmail.comwrote: Dear all, I use gtkImageFromFile to display an image. Then I want to do some gsignal to handle mouse event. I click the mouse and move a another position and release. I can get the position of the firs click and the release position, then I would to draw a rectangle to display the region I have selected. I need some hints on what functions should I look for. I tried to google but don't know how. The GtkImage widget is just for showing images. If you want to start doing interactive graphics, I'd suggest moving to the more general GtkDrawingArea widget and connecting to the expose-event signal. You can then use GdkPixbuf for loading and drawing the image onto the drawing area. And then draw a rectangle on top with gdkDrawRectangle(). See demos drawingArea and images. Michael Thanks. -- HUANG Ronggui, Wincent PhD Candidate Dept of Public and Social Administration City University of Hong Kong Home page: http://asrr.r-forge.r-project.org/rghuang.html __ 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] counting occurrence of text in a dataframe
Try this: Lines - Tanaka Mitchell Wang Hunter Chen Chim miR-191* let-7e let-7b miR-126let-7a let-7g miR-198let-7f let-7c miR-146a let-7b let-7i miR-22 let-7g miR-1224 miR-16 let-7d miR-130b miR-223let-7i miR-124miR-191let-7f miR-133a miR-296let7fG15A miR-125a-3p miR-222let-7g miR-140 miR-30dmiR-101miR-125b-5p miR-223let-7i miR-142-5p miR-370miR-103miR-133a miR-24 miR-101miR-146 miR-486miR-125a-5p miR-133b miR-26amiR-103miR-148a miR-498miR-126miR-135a* miR-32 miR-106a miR-152 DF - read.table(textConnection(Lines), header = TRUE, as.is = TRUE) xtabs(~ values + ind, stack(DF)) On Sat, May 23, 2009 at 8:44 AM, Iain Gallagher iaingallag...@btopenworld.com wrote: Hello list. I am hoping for some help with a relatively simple problem. I have a data frame arranged as below. I want to be able to count the occurrence of each gene (eg let-7e) by Experiment. In other words how many times does a given gene crop up in the dataframe. I tried table but couldn't work out how to get the output I want. I have also considered rearranging this data into a list (by gene) and then counting the length of each gene element. However I thought that there might be a more elegant solution. Tanaka Mitchell Wang Hunter Chen Chim miR-191* let-7e let-7b miR-126 let-7a let-7g miR-198 let-7f let-7c miR-146a let-7b let-7i miR-22 let-7g miR-1224 miR-16 let-7d miR-130b miR-223 let-7i miR-124 miR-191 let-7f miR-133a miR-296 let7fG15A miR-125a-3pmiR-222 let-7g miR-140 miR-30d miR-101 miR-125b-5pmiR-223 let-7i miR-142-5p miR-370 miR-103 miR-133a miR-24 miR-101 miR-146 miR-486 miR-125a-5pmiR-133b miR-26a miR-103 miR-148a miR-498 miR-126 miR-135a* miR-32 miR-106a miR-152 Thanks for any advice. Cheers Iain __ 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] counting occurrence of text in a dataframe
On Sat, 23 May 2009 12:44:19 + (GMT) Iain Gallagher iaingallag...@btopenworld.com wrote: IG I am hoping for some help with a relatively simple problem. I have IG a data frame arranged as below. I want to be able to count the IG occurrence of each gene (eg let-7e) by Experiment. In other words IG how many times does a given gene crop up in the dataframe. IG Tanaka Mitchell Wang Hunter Chen Chim IG miR-191* let-7e let-7b miR-126let-7a let-7g IG miR-198let-7f let-7c miR-146a let-7b let-7i Hi Iain, I would rearrange the dataframe to the following structure: Gene Experiment let-7e Mitchell ... then you can use ftable. probably you could use reshape for your data. I could not figure out how to do it without a time var, so here a quick and dirty way: exp.wide-data.frame(Tim=c(a,a,b),Struppi=c(b,b,b),More=c(a,b,b)) nam-names(exp.wide) ctL-length(exp.wide[1,]) ctW-length(exp.wide[,1]) exp.long-cbind(as.character(exp.wide[,1]),rep(nam[1],ctW)) for(i in 2:ctL){ tmp-cbind(as.character(exp.wide[,i]),nam[i]) exp.long-rbind(exp.long,tmp) } exp.long-as.data.frame(exp.long) names(exp.long)-c(gene,exp) ftable(exp.long) hth Stefan __ 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] optimizing function over x,y
Hello all, I would like to maximize or minimize a given math function over a specific set of values. I was checking out Wolfram Alpha (http://www70.wolframalpha.com/) and it can do simple optimization problems in math, such as maximize 15*x - x**2 over 0 to 15 (http://www70.wolframalpha.com/input/?i=maximize+15*x+-+x**2+over+0+to+15) which finds the maximum value and input for x in the range 0 to 15. Very cool. (This is of course a very simple example). Reading the R documentation I found out that with R I can do this: f-function(x) (15*x)-x**2 optimize(f, c(0,15), lower=0,upper=15, maximum=TRUE) which works very nicely too! What I would like to do, but what Wolfram Alpha apparently can't do (according to a post in their community forum) is to maximize (or minimize) functions that contain two variables, x and y, or more. So for example a very simple example would be minimize x**2 + y**2 over x:0 to 15, y:0-12 -- this does not work -- though I'm unclear about -- the correct syntax too. Is there a way to do this in R in just a neat and easy way as R optimizes a 1-D function? For this particular instance I am interested in the minimum of x * sin(4*x) + 1.1 * sin(2*y), where x,y in range 0-10 so an example of this in R would be great, though in other problems the range may not be identical for x and y. Thanks, Esmail ps: Has anyone written any programs in Python that they then have used to call R functions? I am thinking of writing some of my scripts in Python, but then accessing some of R's functions. __ 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] Luis Miguel Delgado Gomez/BBK está ausente d e la oficina.
Estaré ausente de la oficina desde el 22/05/2009 y no volveré hasta el 04/06/2009. Responderé a su mensaje cuando regrese. [[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] optimizing function over x,y
Look at nlminb() or optim(), in particular the option `method = L-BFGS-B' or the function spg() in BB package. With these you can optimize over any number of variables. Ravi. Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu - Original Message - From: Esmail esmail...@gmail.com Date: Saturday, May 23, 2009 10:01 am Subject: [R] optimizing function over x,y To: R mailing list r-help@r-project.org Hello all, I would like to maximize or minimize a given math function over a specific set of values. I was checking out Wolfram Alpha ( and it can do simple optimization problems in math, such as maximize 15*x - x**2 over 0 to 15 ( which finds the maximum value and input for x in the range 0 to 15. Very cool. (This is of course a very simple example). Reading the R documentation I found out that with R I can do this: f-function(x) (15*x)-x**2 optimize(f, c(0,15), lower=0,upper=15, maximum=TRUE) which works very nicely too! What I would like to do, but what Wolfram Alpha apparently can't do (according to a post in their community forum) is to maximize (or minimize) functions that contain two variables, x and y, or more. So for example a very simple example would be minimize x**2 + y**2 over x:0 to 15, y:0-12 -- this does not work -- though I'm unclear about -- the correct syntax too. Is there a way to do this in R in just a neat and easy way as R optimizes a 1-D function? For this particular instance I am interested in the minimum of x * sin(4*x) + 1.1 * sin(2*y), where x,y in range 0-10 so an example of this in R would be great, though in other problems the range may not be identical for x and y. Thanks, Esmail ps: Has anyone written any programs in Python that they then have used to call R functions? I am thinking of writing some of my scripts in Python, but then accessing some of R's functions. __ R-help@r-project.org mailing list PLEASE do read the posting guide 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] create vectors within a double loop
Hi R-list. This is my first post. I'll try to be as precise as possible with the difficulty I have to get things done. I have a hard time trying to construct a double for loop and create within the inner loop new objects (in this case vectors). I posted this question in a non-directly related with pure R-problems list (in grass-stats). In addition, I think I wasn't precise enough. Many thanks for your time in advance, Nikos --- The data * A list of 10 different names called classifications * data.frames starting with the classification names and a common _thresholds suffix. Each _thresholds data.frame contains 10 columns with numeric values (each column consists of 875 rows) The goal I want to create a new vector for each classification which will hold 10 numeric values (e.g. some sum) one for each column in the classification-thresholds objects. The code # loop over classifications for (x in classifications) { # loop over sequence 1 to 10 for (i in 1:10) # store sum's per source column assign ( (paste ( x, _sums, sep = )[i], sum ( !is.na ( get ( paste ( x, _thresholds, sep = ) )[ ,i] ) ) ) } The problem The above piece of code gives me 10 new vectors but with only 1 value (e.g. the last derived from the loop) _instead_ of 10. Questions 1. How can I construct this properly 2. Related question: how can I print the structure of each column of each classification with a for loop? e.g. # a single loop work perfectly as follows: for (i in 1:10) str(burned_eig_normalised_cov.omission_thresholds[,i]) int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... logi [1:875] NA NA NA NA NA NA ... # now, I would like to say for (x in classifications) { for (i in 1:10) str(paste(x, .omission_thresholds, sep=)[,i]) } Error in paste(x, .omission_thresholds, sep = )[, i] : incorrect number of dimensions Why is this wrong? Given the common prefix, how can I paste the prefix and the suffix and access the columns? __ 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] dimnames does not match array extent
Dear R help-list, I am trying to calculate Monmoniers algorithm. I have 3 data sets: coordinates (coord), distance matrix (comdist) and geographical distance matrix (geodist). I consistently get the error dimnames does not match array extent. However, when I use the function structure I get the following results: str(coord) 'data.frame': 114 obs. of 3 variables: $ sites : Factor w/ 114 levels beni.bl,beni.pen,..: 1 2 3 4 5 6 7 8 9 10 ... $ xcoord: num 2.16 1.35 9.01 9.72 9.83 ... $ ycoord: num 6.92 11.02 5.24 4.81 5 ... str(comdist) Class 'dist' atomic [1:6441] 0.00 2.74e-05 5.58e-01 3.65e-01 6.03e-03 ... ..- attr(*, Labels)= chr [1:114] beni.bl beni.pen came.kor came.kup ... ..- attr(*, Size)= int 114 ..- attr(*, call)= language as.dist.default(m = com, diag = TRUE, upper = TRUE) ..- attr(*, Diag)= logi TRUE ..- attr(*, Upper)= logi TRUE str(geodist) Class 'dist' atomic [1:6441] 4.18 7.05 7.84 7.91 8.02 ... ..- attr(*, Labels)= chr [1:114] beni.bl beni.pen came.kor came.kup ... ..- attr(*, Size)= int 114 ..- attr(*, call)= language as.dist.default(m = geo, diag = TRUE, upper = TRUE) ..- attr(*, Diag)= logi TRUE ..- attr(*, Upper)= logi TRUE The label names are exactly identical. Any hint what causes the error and how to avoid it? Any help is highly appreciated! best wishes Johannes [[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] Draw a rectangle on top of an image using RGtk2?
Thanks, Michael. Just one more follow-up question. Is there other way to get the GdkDrawable (here da2) without using - or other assignment operation from within expose_fn? I thought da$GetRootWindow() would work, but it does not. da - gtkDrawingArea() da2 - NULL expose_fn - function(widget,event,...){ img - gdkPixbufNewFromFile(/media/wind/Pictures/kaehatu.jpg)$retval da2 -widget[[window]] gdkDrawPixbuf(da2, gc = NULL, pixbuf=img, event[[area]][[x]], event[[area]][[y]], event[[area]][[x]], event[[area]][[y]], event[[area]][[width]], event[[area]][[height]]) return(FALSE) } gSignalConnect(da,expose-event,expose_fn) w-gtkWindow(show=F) w$SetSizeRequest(400,300) w$Add(da) w$Show() dgc - gdkGCNew(da2) gdkGCSetLineAttributes(dgc, line.width=2, line.style=solid,round,round) gdkDrawRectangle(da2,dgc,FALSE,10,10,100,100) Ronggui 2009/5/23 Michael Lawrence mflaw...@fhcrc.org: On Fri, May 22, 2009 at 11:27 PM, Ronggui Huang ronggui.hu...@gmail.com wrote: Dear all, I use gtkImageFromFile to display an image. Then I want to do some gsignal to handle mouse event. I click the mouse and move a another position and release. I can get the position of the firs click and the release position, then I would to draw a rectangle to display the region I have selected. I need some hints on what functions should I look for. I tried to google but don't know how. The GtkImage widget is just for showing images. If you want to start doing interactive graphics, I'd suggest moving to the more general GtkDrawingArea widget and connecting to the expose-event signal. You can then use GdkPixbuf for loading and drawing the image onto the drawing area. And then draw a rectangle on top with gdkDrawRectangle(). See demos drawingArea and images. Michael Thanks. -- HUANG Ronggui, Wincent PhD Candidate Dept of Public and Social Administration City University of Hong Kong Home page: http://asrr.r-forge.r-project.org/rghuang.html __ 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. -- HUANG Ronggui, Wincent PhD Candidate Dept of Public and Social Administration City University of Hong Kong Home page: http://asrr.r-forge.r-project.org/rghuang.html __ 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] build CONTENTS or 00Index.html without installing whole package
Replying to myself again, I've now figured out how to build all the help files, including 00Index.html, without actually building the package. The point is to construct a complete list of all html help files for searching. The one thing I cannot figure out is how to build the index of all packages which (for me) is at /usr/share/doc/R-2.9.0/html/packages.html Apparently this requires something other than DESCRIPTION and 00Index.html files in each package, which is all I have. Here is the script: #!/bin/bash # makes indexable help files for R packages, including pdf vignettes # usage inst.bat [files] --- quotes needed for wildcards like *.tar.gz for PKG in `ls $1` do tar xfz $PKG PK=`echo $PKG | /bin/sed -e 's/.tar.gz//' | cut -d_ -f1` echo $PK mkdir -pv /usr/lib/R/library/$PK mkdir -pv /usr/lib/R/library/$PK/html # copy description (which contains version number) cp $PK/DESCRIPTION /usr/lib/R/library/$PK # move vignettes if present if [ -d $PK/inst/doc ]; then mkdir -pv /usr/lib/R/library/$PK/doc cp $PK/inst/doc/* /usr/lib/R/library/$PK/doc fi # make html files R CMD perl /usr/share/R/perl/build-help.pl --html /home/baron/$PK /usr/lib/R/library/ # make indices Rscript --vanilla -e tools:::.writePkgIndices('$PK','/usr/lib/R/library/$PK') rm -rf $PK done # try to build package list, doesn't work # Rscript --no-init-file --no-save -e make.packages.html(packages=FALSE) -- Jonathan Baron, Professor of Psychology, University of Pennsylvania Home page: http://www.sas.upenn.edu/~baron __ 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] Save values from an expression that is created from deriv command
Dear all, I want to save a value from an expression that I created by using the deriv function. So for example the output from an expression using the deriv command is: expression({ .expr7 - 4 * b.nox.2^2 - 12 * b.nox.3 * b.nox.1 .expr9 - -2 * b.nox.2 - sqrt(.expr7) .expr10 - 6 * b.nox.3 .value - .expr9/.expr10 .grad - array(0, c(length(.value), 1L), list(NULL, c(b.nox.3))) .grad[, b.nox.3] - 0.5 * (12 * b.nox.1 * .expr7^-0.5)/.expr10 - .expr9 * 6/.expr10^2 attr(.value, gradient) - .grad .value }) The problem is that I do not know how to store the: .grad[, b.nox.3] - 0.5 * (12 * b.nox.1 * .expr7^-0.5)/.expr10 - .expr9 * 6/.expr10^2 and then use its numerical value in my code. How can I store the above value or any output from the expression from deriv command so I can use it later in the code? Any help would be highly appreciated Thanks Dimitris -- View this message in context: http://www.nabble.com/Save-values-from-an-expression-that-is-created-from-deriv-command-tp23686186p23686186.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] Save values from an expression that is created from deriv command
Try this: D(expression(x*x), x) On Sat, May 23, 2009 at 12:46 PM, dimitris kapetanakis gami...@gmail.com wrote: Dear all, I want to save a value from an expression that I created by using the deriv function. So for example the output from an expression using the deriv command is: expression({ .expr7 - 4 * b.nox.2^2 - 12 * b.nox.3 * b.nox.1 .expr9 - -2 * b.nox.2 - sqrt(.expr7) .expr10 - 6 * b.nox.3 .value - .expr9/.expr10 .grad - array(0, c(length(.value), 1L), list(NULL, c(b.nox.3))) .grad[, b.nox.3] - 0.5 * (12 * b.nox.1 * .expr7^-0.5)/.expr10 - .expr9 * 6/.expr10^2 attr(.value, gradient) - .grad .value }) The problem is that I do not know how to store the: .grad[, b.nox.3] - 0.5 * (12 * b.nox.1 * .expr7^-0.5)/.expr10 - .expr9 * 6/.expr10^2 and then use its numerical value in my code. How can I store the above value or any output from the expression from deriv command so I can use it later in the code? Any help would be highly appreciated Thanks Dimitris -- View this message in context: http://www.nabble.com/Save-values-from-an-expression-that-is-created-from-deriv-command-tp23686186p23686186.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-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] Search for longest consecutive occurrence
Dear R Users, I am trying to write a script to count the longest consecutive occurring 1 in a sequence: test-c(1,1,1,1,1,1,1,0,0,0,0,1,1,0,1,0,1,0,1,1) In the case of the object test, 1 occurs 7 consecutive times which is the longest consecutive within the sequence. I know I can always do a thorough from start to end search and use a counter to count. But, do you know there can be a smarter way to achieve this in R? Thanks a lot! - John __ 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] Search for longest consecutive occurrence
Try this: with(rle(test), max(lengths[!!values])) On Sat, May 23, 2009 at 1:09 PM, tsunhin wong thjw...@gmail.com wrote: Dear R Users, I am trying to write a script to count the longest consecutive occurring 1 in a sequence: test-c(1,1,1,1,1,1,1,0,0,0,0,1,1,0,1,0,1,0,1,1) In the case of the object test, 1 occurs 7 consecutive times which is the longest consecutive within the sequence. I know I can always do a thorough from start to end search and use a counter to count. But, do you know there can be a smarter way to achieve this in R? Thanks a lot! - John __ 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] Search for longest consecutive occurrence
Thanks! I tested it using: test-c(1,1,1,1,1,0,0,0,0,0,0,0,1,1,0,1,0,1,0,1,1) that has a longer 7 zeros and 5 ones. What part of the script does the selection of ones instead of zeros? - John On Sat, May 23, 2009 at 1:17 PM, Gabor Grothendieck ggrothendi...@gmail.com wrote: Try this: with(rle(test), max(lengths[!!values])) On Sat, May 23, 2009 at 1:09 PM, tsunhin wong thjw...@gmail.com wrote: Dear R Users, I am trying to write a script to count the longest consecutive occurring 1 in a sequence: test-c(1,1,1,1,1,1,1,0,0,0,0,1,1,0,1,0,1,0,1,1) In the case of the object test, 1 occurs 7 consecutive times which is the longest consecutive within the sequence. I know I can always do a thorough from start to end search and use a counter to count. But, do you know there can be a smarter way to achieve this in R? Thanks a lot! - John __ 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] Search for longest consecutive occurrence
!! On Sat, May 23, 2009 at 1:52 PM, tsunhin wong thjw...@gmail.com wrote: Thanks! I tested it using: test-c(1,1,1,1,1,0,0,0,0,0,0,0,1,1,0,1,0,1,0,1,1) that has a longer 7 zeros and 5 ones. What part of the script does the selection of ones instead of zeros? - John On Sat, May 23, 2009 at 1:17 PM, Gabor Grothendieck ggrothendi...@gmail.com wrote: Try this: with(rle(test), max(lengths[!!values])) On Sat, May 23, 2009 at 1:09 PM, tsunhin wong thjw...@gmail.com wrote: Dear R Users, I am trying to write a script to count the longest consecutive occurring 1 in a sequence: test-c(1,1,1,1,1,1,1,0,0,0,0,1,1,0,1,0,1,0,1,1) In the case of the object test, 1 occurs 7 consecutive times which is the longest consecutive within the sequence. I know I can always do a thorough from start to end search and use a counter to count. But, do you know there can be a smarter way to achieve this in R? Thanks a lot! - John __ 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] Search for longest consecutive occurrence
Thanks for introducing me to with and the smart use of R subscripting Now I know I can do something like this. test-c(1,1,1,1,1,0,0,0,0,0,2,2,2,2,2,2,0,1,0,1,1,0,0,0,0,0,0,0) with(rle(test), max(lengths[values==2])) [1] 6 with(rle(test), max(lengths[values==1])) [1] 5 with(rle(test), max(lengths[values==0])) [1] 7 Many thanks! - John On Sat, May 23, 2009 at 1:54 PM, Gabor Grothendieck ggrothendi...@gmail.com wrote: !! On Sat, May 23, 2009 at 1:52 PM, tsunhin wong thjw...@gmail.com wrote: Thanks! I tested it using: test-c(1,1,1,1,1,0,0,0,0,0,0,0,1,1,0,1,0,1,0,1,1) that has a longer 7 zeros and 5 ones. What part of the script does the selection of ones instead of zeros? - John On Sat, May 23, 2009 at 1:17 PM, Gabor Grothendieck ggrothendi...@gmail.com wrote: Try this: with(rle(test), max(lengths[!!values])) On Sat, May 23, 2009 at 1:09 PM, tsunhin wong thjw...@gmail.com wrote: Dear R Users, I am trying to write a script to count the longest consecutive occurring 1 in a sequence: test-c(1,1,1,1,1,1,1,0,0,0,0,1,1,0,1,0,1,0,1,1) In the case of the object test, 1 occurs 7 consecutive times which is the longest consecutive within the sequence. I know I can always do a thorough from start to end search and use a counter to count. But, do you know there can be a smarter way to achieve this in R? Thanks a lot! - John __ 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] create vectors within a double loop
You might want to look at how to use 'lapply' to create lists. Here is one way of doing it: # create test data a_threshold - b_threshold - as.data.frame(matrix(sample(c(1:5, NA), 100, TRUE), 10)) classification - c('a', 'b') result - lapply(classification, function(.cls){ + colSums(!is.na(get(paste(.cls, '_threshold', sep='' + }) result [[1]] V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 9 9 9 9 8 7 7 8 9 8 [[2]] V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 9 9 9 9 8 7 7 8 9 8 On Sat, May 23, 2009 at 10:32 AM, Nikos Alexandris nikos.alexand...@felis.uni-freiburg.de wrote: Hi R-list. This is my first post. I'll try to be as precise as possible with the difficulty I have to get things done. I have a hard time trying to construct a double for loop and create within the inner loop new objects (in this case vectors). I posted this question in a non-directly related with pure R-problems list (in grass-stats). In addition, I think I wasn't precise enough. Many thanks for your time in advance, Nikos --- The data * A list of 10 different names called classifications * data.frames starting with the classification names and a common _thresholds suffix. Each _thresholds data.frame contains 10 columns with numeric values (each column consists of 875 rows) The goal I want to create a new vector for each classification which will hold 10 numeric values (e.g. some sum) one for each column in the classification-thresholds objects. The code # loop over classifications for (x in classifications) { # loop over sequence 1 to 10 for (i in 1:10) # store sum's per source column assign ( (paste ( x, _sums, sep = )[i], sum ( !is.na ( get ( paste ( x, _thresholds, sep = ) )[ ,i] ) ) ) } The problem The above piece of code gives me 10 new vectors but with only 1 value (e.g. the last derived from the loop) _instead_ of 10. Questions 1. How can I construct this properly 2. Related question: how can I print the structure of each column of each classification with a for loop? e.g. # a single loop work perfectly as follows: for (i in 1:10) str(burned_eig_normalised_cov.omission_thresholds[,i]) int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... logi [1:875] NA NA NA NA NA NA ... # now, I would like to say for (x in classifications) { for (i in 1:10) str(paste(x, .omission_thresholds, sep=)[,i]) } Error in paste(x, .omission_thresholds, sep = )[, i] : incorrect number of dimensions Why is this wrong? Given the common prefix, how can I paste the prefix and the suffix and access the columns? __ 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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[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] Clean up a complex variable
Here is one way of solving it: x - c('0(a=1)' ,'0(b=1)' ,'0.133(b=1)' ,'0.555(a=1)' ,'5.32(a=1)') # ( and ) have special meaning in regular expressions so they have to be escaped (y - as.numeric(gsub(\\(.*\\ file://(.*//)|, , x))) [1] 0.000 0.000 0.133 0.555 5.320 # find a=1 to divide by 5 a.equal - grep('a=1', x) y[a.equal] - y[a.equal] / 5 y [1] 0.000 0.000 0.133 0.111 1.064 On Sat, May 23, 2009 at 2:25 AM, andyer weng nzand...@gmail.com wrote: Dear all, I need to clean up one variables in a dataset. e.g. lets say the dataset is trial, the variable for cleaning up is V1 trial$V1 [1] 0(a=1) 0(b=1) 0.133(b=1) 0.555(a=1) 5.32(a=1) what i need to do is to remove the text (a=1) and (b=1) and the in the V1, and then convert to a numeric variable, and als I am aslo requested that when the value has a=1, the value needs to be divided by 5. what I did is: trialchara-as.character(trial$V1) trialnum-gsub((a=1)|(b=1)|,,trialchara) the result is [1] 0 () 0 () 0.133 () 0.555 () 5.32 () How can I get rid of the () symbol? How can I do this part when the value has a=1, the value needs to be divided by 5. ? Can anyone please give me some hints here? Thanks a lot. John [[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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[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] how to insert NULLs in lists?
On Fri, May 22, 2009 at 7:32 PM, markle...@verizon.net wrote: Hi Kynn: this oddity is discussed in Patrick Burn's document called The R Inferno. I don't recall the fix so I'm not sure if below is the same as what his book says to do but it seems to do what you want. Wow, I sure hit the jackpot with this link to Burn's Inferno. It's sooo great. Thanks! And to Bert, too. KJ __ 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] getting % influence for 2 factors in LDA
Hello R-list, I am preforming an lda on the following data. Curvature Diameter Quality 1 2.95 6.63Passed 2 2.53 7.79Passed 3 3.57 5.65Passed 4 3.16 5.47Passed 5 2.58 4.46 NotPassed 6 2.16 6.22 NotPassed 7 3.27 3.52 NotPassed I use lda: ddd-lda(Quality~ Curvature+Diameter, data=momo) ddd Call: lda(Quality ~ Curvature + Diameter, data = momo) Prior probabilities of groups: NotPassedPassed 0.5 0.5 Group means: Curvature Diameter NotPassed 2.63 5.38 Passed 3.016667 6.69 AND lda$scaling: gives me LD1 Curvature 2.372923 Diameter 1.368995 My question is, I would like to calculate the % influence that 1 factor has vs the other meaning does 1 factor have 0.6 influence and the other have 0.4? TIA [[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] Naming a random effect in lmer
I am having trouble modifying the lmer code to make the change that William Dunlap suggested, and I believe that this is because the environment is locked. I tried: 1. Revising the lmerFactorList function in a word processor and copying/pasting into lmer, but this did not work because the environment is locked. 2. Setting the environment and modifying the functionality of the lmerFactorList function as recommended in this post (https://stat.ethz.ch/pipermail/r-devel/2006-July/038547.html), but the unmodified lmerFactorList function was still used in the lmer call and the same error was generated. 3. Creating a new version of the lme4 package for my modified function with the makecdfenv package, but I do not know how to create a CDF file, feel that I am well beyond my programming expertise, and hope there is a more simple resolution to this issue. Thanks for any help -- las Leigh Ann Starcevich Doctoral student Oregon State University Corvallis, Oregon At 03:30 PM 5/22/2009 -0700, William Dunlap wrote: -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of spencerg Sent: Friday, May 22, 2009 3:01 PM To: Leigh Ann Starcevich Cc: r-help@r-project.org Subject: Re: [R] Naming a random effect in lmer [ ... elided statistical advice ... ] If you and your advisor still feel that what you are doing makes sense, I suggest you first get the source code via svn checkout svn://svn.r-forge.r-project.org/svnroot/lme4 (or by downloading lme4_0.999375-30.tar.gz from http://cran.fhcrc.org/web/packages/lme4/index.html;), then walk through the code line by line using the debug function (or browser or the debug package). From this, you will likely see either (a) how you can do what you want differently to achieve the same result or (b) how to modify the code so it does what you want. The coding error is right in the error message: Error in names(bars) - unlist(lapply(bars, function(x) deparse(x[[3]]))) and I suspect that traceback() would tell you that came from a call to lmerFactorList. That code implicitly assumes that deparse() will produce a scalar character vector, but it doesn't if the input expression is complicated enough. Changing the deparse(x[[3]]) to deparse(x[[3]])[1] or paste(collapse= , deparse(x[[3]])[1]) would fix it. The first truncates the name and the second my make a very long name. There is at least one other use of that idiom in the lme4 code and your dataset and analysis may require that all of them be fixed. Hope this helps. Spencer Leigh Ann Starcevich wrote: Here is a test data set and code. I am including the data set after the code and discussion to make reading easier. Apologies for the size of the data set, but my problem occurs when there are a lot of Z variables. Thanks for your time. # Enter data below # Sample code library(lme4) mb- length(unique(testsamp$WYear)) # Create the formula for the set of identically distributed random effects Zs- paste(Z,2:(mb-1)),sep=) Trendformula -as.formula(paste(LogY ~ WYear + (1+WYear|Site) + (1|, randommodel=paste(paste(Zs,collapse=+), fittest-lmer(Trendformula, data = testsamp) summary(fittest) # Here I get an error because the name of the random effect is too long to print # in the random effects output (I think). # The error message is: Error in names(bars) - unlist(lapply(bars, function(x) # deparse(x[[3]]))) : 'names' attribute [3] must be the same length as the vector [2] # However, when fewer Z variables are used in the random portion of the model, # there is no error. # Using only Z2 + ... + Z9 for the random intercept Zs2- paste(Z,2:9,sep=) Trendformula2 -as.formula(paste(LogY ~ WYear + (1+WYear|Site) + (1|, randommodel=paste(paste(Zs2,collapse=+), fittest2-lmer(Trendformula2, data = testsamp) summary(fittest2) # Is there a way to either name the set of iid random effects something else or # to define a random variable that could be used in the model to create a # random intercept? # I have had some success in lme, but it would be helpful for my simulation if I # could conduct this analysis with lmer. My model in lme is not correctly # estimating one of the variance components (random Site intercept). # I am using: detach(package:lme4) library(nlme) random.model.lme -as.formula(paste(~-1+,paste(paste(Z,2:(mb-1),sep=),col lapse=+))) n-dim(testsamp)[1] testsampgroup - rep(1,n) testsamp.lme -cbind(testsamp,testsampgroup) testgroupSamp- groupedData(LogY ~ WYearCen|testsampgroup, inner= ~Site, data= data.frame(testsamp.lme)) fittest3-lme(LogY ~ WYearCen, random= pdBlocked(list(pdIdent(~-1+WYearCen:as.factor(Site)), pdIdent(~-1+as.factor(Site)), pdIdent(random.model.lme))),data=
[R] Cream Text Editor
I'm interested in easing my way into learning VIM by first using the Cream text editor, liking the idea that it will work on both my Linux and Windows computers. I've installed Cream on my Linux machine, but can't figure out how to make Cream talk to R? Does anybody know? I'm using Ubuntu if it makes a difference. Thanks. -- View this message in context: http://www.nabble.com/Cream-Text-Editor-tp23688559p23688559.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] create vectors within a double loop
jim holtman: You might want to look at how to use 'lapply' to create lists. Here is one way of doing it: # create test data a_threshold - b_threshold - as.data.frame(matrix(sample(c(1:5, NA), 100, TRUE), 10)) classification - c('a', 'b') result - lapply(classification, function(.cls){ + colSums(!is.na(get(paste(.cls, '_threshold', sep='' + }) Jim, thank you for your time. You saved my day ;-). Just for your interest, my code (even if not elegant) works also with colSums(). Great function. Of course, the lapply() is probably the elegant solution to many tasks like mine. Kindest regards, Nikos The code # loop over classifications for (x in classifications) { # loop over sequence 1 to 10 for (i in 1:10) # store sum's per source column assign ( (paste ( x, _sums, sep = )[i], # changed from sum to colSums colSums ( !is.na ( get ( paste ( x, _thresholds, sep = ) ) # removed the [ ,i] # ) ) ) } The 2nd question remains or it is again solved with an _apply()_ function. Maybe I'll find the answer in R_inferno? Questions --%--- 2. Related question: how can I print the structure of each column of each classification with a for loop? e.g. # a single loop work perfectly as follows: for (i in 1:10) str(burned_eig_normalised_cov.omission_thresholds[,i]) int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... int [1:875] NA NA NA NA NA NA NA NA NA NA ... logi [1:875] NA NA NA NA NA NA ... # now, I would like to say for (x in classifications) { for (i in 1:10) str(paste(x, .omission_thresholds, sep=)[,i]) } Error in paste(x, .omission_thresholds, sep = )[, i] : incorrect number of dimensions Why is this wrong? Given the common prefix, how can I paste the prefix and the suffix and access the columns? __ 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] Cream Text Editor
On 2009-May-23 , at 17:40 , Paul Heinrich Dietrich wrote: I'm interested in easing my way into learning VIM by first using the Cream text editor, liking the idea that it will work on both my Linux and Windows computers. I've installed Cream on my Linux machine, but can't figure out how to make Cream talk to R? Does anybody know? I'm using Ubuntu if it makes a difference. Thanks. You should install the R Vim Plugin and its dependencies: http://www.vim.org/scripts/script.php?script_id=2628 This creates commands and icons dedicated to the interaction between Vim and R. Then switch cream Settings Preferences Expert Mode. This will allow you to work in Cream and have all the simple keyboard shortcuts (Control-C, Control-V etc.) but still be able to switch between modes as in vim. By default you are in insert mode. You need to switch to normal mode (by pressing ESC) to be able to use the commands of the R-Vim plugin. The workflow is therefore: - open a R file - edit stuff - press ESC (to switch to non-edit mode) - start R in a terminal (click the icon or press F2) - send lines/selection (F9) or document (F5) - press ESC (to switch back to insert mode) - edit 2 lines - ESC - F9 - F9 - ESC - edit again etc... The terminal opened this way does not work completely as a regular one and there are some caveats when reading help and using general command line editing shortcuts (Ctrl-R for backward search for example). I haven't found a way around them so I usually open a second terminal to read the help in, or set R to display the help as HTML files in a browser window. I must say that those caveats can be quite serious and I often find myself just using copy-paste from gedit in a terminal: - set your desktop to focus follow mouse - select text in your editor - move the mouse to the terminal - click middle mouse button - move the mouse back to the editor etc... More cumbersome but reliable. Final note: since you are on ubuntu, you may want to change the terminal from the default X-term to gnome-terminal. You have to edit the file .vim/ftplugin/r.vim. There is a line commented with the gnome- terminal command instead of xterm. Uncomment this one and comment the xterm one. JiHO --- http://jo.irisson.free.fr/ __ 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] Cream Text Editor
As pointed by JiHO the biggest disadvantage of using the plugin is that R is running through a pipe and consequently it is less interactive. Just a note: there is no need of Esc before F9. Almost all key bindings work in insert, normal and visual modes. The last version of the plugin allows the user to set the terminal emulator in the vimrc and now all key bindings are customizable. The details are in the plugin's documentation. Please write, to me if you find bugs in the plugin. Jakson JiHO wrote: On 2009-May-23 , at 17:40 , Paul Heinrich Dietrich wrote: I'm interested in easing my way into learning VIM by first using the Cream text editor, liking the idea that it will work on both my Linux and Windows computers. I've installed Cream on my Linux machine, but can't figure out how to make Cream talk to R? Does anybody know? I'm using Ubuntu if it makes a difference. Thanks. You should install the R Vim Plugin and its dependencies: http://www.vim.org/scripts/script.php?script_id=2628 This creates commands and icons dedicated to the interaction between Vim and R. Then switch cream Settings Preferences Expert Mode. This will allow you to work in Cream and have all the simple keyboard shortcuts (Control-C, Control-V etc.) but still be able to switch between modes as in vim. By default you are in insert mode. You need to switch to normal mode (by pressing ESC) to be able to use the commands of the R-Vim plugin. The workflow is therefore: - open a R file - edit stuff - press ESC (to switch to non-edit mode) - start R in a terminal (click the icon or press F2) - send lines/selection (F9) or document (F5) - press ESC (to switch back to insert mode) - edit 2 lines - ESC - F9 - F9 - ESC - edit again etc... The terminal opened this way does not work completely as a regular one and there are some caveats when reading help and using general command line editing shortcuts (Ctrl-R for backward search for example). I haven't found a way around them so I usually open a second terminal to read the help in, or set R to display the help as HTML files in a browser window. I must say that those caveats can be quite serious and I often find myself just using copy-paste from gedit in a terminal: - set your desktop to focus follow mouse - select text in your editor - move the mouse to the terminal - click middle mouse button - move the mouse back to the editor etc... More cumbersome but reliable. Final note: since you are on ubuntu, you may want to change the terminal from the default X-term to gnome-terminal. You have to edit the file .vim/ftplugin/r.vim. There is a line commented with the gnome-terminal command instead of xterm. Uncomment this one and comment the xterm one. JiHO --- http://jo.irisson.free.fr/ __ 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] using optimize() correctly ...
Hi, I am trying to use the optimize function to optimize a function. The results I am getting don't agree with what I compute on my own and when I look at the graph of f(x) = 100 + ((x-10)**2 + (x-10)) * cos(x-10), where -10 = x = 10 in gnuplot. I suspect I am making a mistake in the usage of the R optimize function, perhaps someone could point out where? f-function(x) 100 + ((x-10)**2 + (x-10)) * cos(x-10) to MAXIMIZE: result=optimize(f, c(-10,10), lower = -10, upper=10, maximum=TRUE) result $maximum [1] -2.728743 $objective [1] 247.3284 to MINIMIZE result=optimize(f, c(-10,10), lower = -10, upper=10, maximum=FALSE) result $minimum [1] 6.290112 $objective [1] 91.52681 However, I believe the correct values should be minimize: x = -5.838 val= -133.020 maximize: x = -8.957 val= 438.448 Thanks, Esmail This is w/ R version 2.8.1 (2008-12-22) under Ubuntu 9.04 __ 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] optimizing function over x,y
Ravi Varadhan wrote: Look at nlminb() or optim(), in particular the option `method = L-BFGS-B' or the function spg() in BB package. With these you can optimize over any number of variables. Ravi. Hi Ravi, Thanks for the leads, I'll take a look, though right now I have run into problems with even the simple (?) optimize function, so I'll first have to figure out what I'm doing wrong with it. Best, Esmail __ 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] rank reduction method in time series analysis?
Hello: Have you looked at Pfaff (2008) Analysis of Integrated and Cointegrated Time Series with R, 2nd ed. (Springer)? I have not read this book, but the title and table of contents sounds like it contains many alternative answers to your question, with the best among those being determined by aspects of your application that you did not mention. Moreover, the author has made substantive contributions to R, as witnessed by the following: library(RSiteSearch) Pfaff - RSiteSearch.function(Pfaff) summary(Pfaff) This identified 105 help pages in 5 packages containing the name Pfaff. Hope this helps. Spencer Michael wrote: Hi all, Suppose I have 100 simultaneous time series, what's the best statistical procedure to figure out a transformation of the data, and see if we could squeeze most of the information into a few transformed time series? 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] Cream Text Editor
On 2009-May-23 , at 20:16 , Jakson Alves de Aquino wrote: Just a note: there is no need of Esc before F9. Almost all key bindings work in insert, normal and visual modes. Well, without switching to the non-insert mode, I find that pressing F9 prints the commands in the file instead of executing them. Maybe that's specific to Cream. JiHO --- http://jo.irisson.free.fr/ __ 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] using optimize() correctly ...
Esmail Bonakdarian-4 wrote: Hi, I am trying to use the optimize function to optimize a function. The results I am getting don't agree with what I compute on my own and when I look at the graph of f(x) = 100 + ((x-10)**2 + (x-10)) * cos(x-10), where -10 = x = 10 in gnuplot. I suspect I am making a mistake in the usage of the R optimize function, perhaps someone could point out where? f-function(x) 100 + ((x-10)**2 + (x-10)) * cos(x-10) to MAXIMIZE: result=optimize(f, c(-10,10), lower = -10, upper=10, maximum=TRUE) result $maximum [1] -2.728743 $objective [1] 247.3284 to MINIMIZE result=optimize(f, c(-10,10), lower = -10, upper=10, maximum=FALSE) result $minimum [1] 6.290112 $objective [1] 91.52681 However, I believe the correct values should be minimize: x = -5.838 val= -133.020 maximize: x = -8.957 val= 438.448 Your function is not unimodal. The help for optimize states: If f is not unimodal, then optimize() may approximate a local, but perhaps non-global, minimum to the same accuracy. In you case, optimize is locating a local maximum/minimum. You should restrict the search to the interval [-10 , -7 ] so result=optimize(f, c(-10,10), lower = -10, upper=-7, maximum=TRUE) will locate your global maximum. Berend -- View this message in context: http://www.nabble.com/using-optimize%28%29-correctly-...-tp23689607p23690832.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.