Re: [R] Assigning a vector to every element of a list.
On Mon, Jul 2, 2012 at 6:16 PM, Spencer Maynes smayne...@gmail.com wrote: I have a vector d of unknown length, and a list b of unknown length. I would like to replace every element of b with d. Simply writing b-d does not work as R tries to fit every element of d to a different element of d, and b-rep(d,length(b)) does not work either as it makes a list of length length(d)*length(b) not a list of length(b). I know how to do this with a for loop, but I feel that there has to be a more efficient way. Any suggestions? Try this where the first line creates a list, L, whose elements we want to replace and the second line replaces every element with the indicated vector: L - list(1, 1:2, abc) L[] - list(1:4) L [[1]] [1] 1 2 3 4 [[2]] [1] 1 2 3 4 [[3]] [1] 1 2 3 4 -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.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] subset data based on values in multiple columns
Dear list members, I am trying to create a subset of a data frame based on conditions in two columns, and after spending much time trying (and search R-help) have not had any luck. Essentially, I have a data frame that is something like this: date-as.POSIXct(as.character(c(2012-01-25,2012-01-25,2012-01-26,2012-01-27,2012-01-27,2012-01-27))) time-as.POSIXct(as.character(c(13:20, 13:40, 14:00, 10:00, 10:20, 10:20)), format=%H:%M) count-c(12,14,11,12,12,8) data-data.frame(date,time,count) which looks like: date time count 1 2012-01-2513:20:00 12 2 2012-01-2513:40:00 14 3 2012-01-2614:00:00 11 4 2012-01-2710:00:00 12 5 2012-01-2710:20:00 12 6 2012-01-2710:20:00 8 I would like to create a subset by doing the following: for each unique date, only include one case which will be the case with the max value for the column labelled count. So the resulting subset would be: date time count 2 2012-01-2513:40:00 14 3 2012-01-2614:00:00 11 4 2012-01-2710:00:00 12 Some dates have two cases at which the count was the same, but I only want to include one case (I don't really mind which case it chooses, but if need be it could be based on the earliest time for which the same counts occurred). I have tried various loops with no success! I'm sure that there is an easy answer that I have not found! Any help is much appreciated!! All the best, Chandra [[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] subset data based on values in multiple columns
Hi Dear list members, I am trying to create a subset of a data frame based on conditions in two columns, and after spending much time trying (and search R-help) have not had any luck. Essentially, I have a data frame that is something like this: date-as.POSIXct(as.character(c (2012-01-25,2012-01-25,2012-01-26,2012-01-27,2012-01-27,2012-01-27))) time-as.POSIXct(as.character(c(13:20, 13:40, 14:00, 10:00, 10: 20, 10:20)), format=%H:%M) count-c(12,14,11,12,12,8) data-data.frame(date,time,count) which looks like: date time count 1 2012-01-2513:20:00 12 2 2012-01-2513:40:00 14 3 2012-01-2614:00:00 11 4 2012-01-2710:00:00 12 5 2012-01-2710:20:00 12 6 2012-01-2710:20:00 8 I would like to create a subset by doing the following: for each unique date, only include one case which will be the case with the max value for the column labelled count. So the resulting subset would be: date time count 2 2012-01-2513:40:00 14 3 2012-01-2614:00:00 11 4 2012-01-2710:00:00 12 Some dates have two cases at which the count was the same, but I only want to include one case (I don't really mind which case it chooses, but if need be it could be based on the earliest time for which the same counts occurred). I have tried various loops with no success! I'm sure that there is an easy answer that I have not found! Any help is much appreciated!! Just few days ago similarquestion was asked (selecting rows by maximum value of one variables in dataframe nested by another Variable). Here is what was recommended. do.call(rbind,lapply(split(data, data$date), function(x) x[which.max(x[,2]),])) Regards Petr All the best, Chandra [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]
Hi all, I would like create a new column in a data.frame (a1) to store 0, 1 data converted from a factor as below. a1$h2-NULL for (i in 1:dim(a1)[1]) { if (a1$h1[i]==H) a1$h2[i]-1 else a1$h2[i]-0 } My question: is it possible to remove the loop from above code to achieve the desired result? Thanks in advance, Jin Geoscience Australia Disclaimer: This e-mail (and files transmitted with it) is intended only for the person or entity to which it is addressed. If you are not the intended recipient, then you have received this e-mail by mistake and any use, dissemination, forwarding, printing or copying of this e-mail and its file attachments is prohibited. The security of emails transmitted cannot be guaranteed; by forwarding or replying to this email, you acknowledge and accept these risks. - [[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] Automating R script with Windows 7
Sounds like operating system troubles, not R troubles. If you want help with invoking R, please take a deep breath, read the FAQs and the posting guide, and tell us exactly what you did at the command line if you still think R is the problem. Note that any problems you may be having with the access permissions that your interactive or automatically-triggered environment is restricted by are almost impossible for us to remotely troubleshoot, and are not on-topic for this mailing list. --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. maviney mvine...@hotmail.com wrote: hi i have tried all day in getting this to work, but i have fail I cant even schedule it to open up rscript, even by manually i cant open rscript Pls help -- View this message in context: http://r.789695.n4.nabble.com/Automating-R-script-with-Windows-7-tp4446693p4635237.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] missing price datas before launched
HI, I have the price and volume data from own product and competitor's product: Year_Month Volume own product's price Volume competitor's price 1 201011 17583469.03 NA NA 2 201012 33899489.25 NA NA 3 201101 31306488.42 NA NA 4 201102 21272479.07 173 550 5 201103 24145462.99 2684 548 6 201104 20763433.87 4787 475 7 201105 25337410.22 9805 430 8 201106 21793422.5214096 388 9 201107 26081417.7617452 383 10 201108 21933420.8622432 373 11 201109 21560396.5317678 353 12 201110 28538360.4917864 349 13 20 20995346.4711857 340 14 201112 42961353.4935938 332 15 201201 26378341.4920544 330 16 201202 21531350.8216259 339 17 201203 19386365.9514361 348 18 201204 20441347.0612255 357 because competitor's product was launched at 201102. so I am having missing data before that time for competitor's product and volume after I merge all data together. my aim to set up the model based on the data. because I have so less data points. I don't want to delete those rows with NA records. but How should I handle this case? I am thinking to use mean imputation. but is it suitable for this case? Thank you so much for your reply. Kind regards, Tammy [[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] Is it possible to remove this loop? [SEC=UNCLASSIFIED]
Hello, Try: a1$h2 - 0 a1$h2[a1$h1==H] - 1 Regards Le 12/07/03 16:18, jin...@ga.gov.au a écrit : Hi all, I would like create a new column in a data.frame (a1) to store 0, 1 data converted from a factor as below. a1$h2-NULL for (i in 1:dim(a1)[1]) { if (a1$h1[i]==H) a1$h2[i]-1 else a1$h2[i]-0 } My question: is it possible to remove the loop from above code to achieve the desired result? Thanks in advance, Jin Geoscience Australia Disclaimer: This e-mail (and files transmitted with it) is intended only for the person or entity to which it is addressed. If you are not the intended recipient, then you have received this e-mail by mistake and any use, dissemination, forwarding, printing or copying of this e-mail and its file attachments is prohibited. The security of emails transmitted cannot be guaranteed; by forwarding or replying to this email, you acknowledge and accept these risks. - [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] carpet plots
On 07/03/2012 06:21 AM, Joseph Clark wrote: These carpet plots are also called heat maps and there's a current thread with the subject line Heat Maps in which I've given a couple of examples of code for them. The R function image() is very easy to use: image( x=(x values), y=(y values), z=(matrix of z values with x rows and y columns), col=(vector of colors), breaks=(vector of break points, one more than colors) ) What I'd like to know is: does anyone know how to easily create a legend/key to the color gradients, like the one in the right margin of Mueller's first example: (http://www.johannes-hopf.de/wordpress/wp-content/uploads/2009/12/Zufall-300x225.png)? Hi Joseph, Have a look at color2D.matplot and color.legend in the plotrix package. There is also sampcolorlegend in the shape package. Jim __ 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] Specify model with polynomial interaction terms up to degree n
Hello, Sorry, but it was you that misread some of the suggestions. I have written raw=TRUE not raw=raw. Just see m - matrix(1:6, ncol=2) # your example p2 - poly(m, degree=2, raw=TRUE) # it's raw=TRUE, not raw=raw !!! deg2 - attr(p2, 'degree') == 2 p2[, deg2] p6 - poly(m, degree=6, raw=TRUE) # now degree 6 deg6 - attr(p6, 'degree') == 6 p6[, deg6] Hope this helps, Rui Barradas Em 02-07-2012 23:52, YTP escreveu: I think you have taken my toy example seriously. Perhaps I wasn't clear, but I am in fact not working with a dataset of 3 observations of the numbers 1 through 6 and trying to estimate anything; that was an example to illustrate what I am asking for, namely, turning two variables like this X1 X2 1 4 2 5 3 6 into a dataset like this 1 4 16 4 10 25 9 18 36 where each column is the interaction between certain polynomial terms of the original variables, such that each column has a sum of exponents equal to 2 (or whatever degree n is desired). My apologies if I wasn't clear, any other ideas would be appreciated. It appears the poly command you mentioned is only taking powers of a single vector. -- View this message in context: http://r.789695.n4.nabble.com/Specify-model-with-polynomial-interaction-terms-up-to-degree-n-tp4635130p4635217.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.
Re: [R] Assigning a vector to every element of a list.
b - rep(list(d), length(b)) On 02/07/2012 23:16, Spencer Maynes wrote: I have a vector d of unknown length, and a list b of unknown length. I would like to replace every element of b with d. Simply writing b-d does not work as R tries to fit every element of d to a different element of d, and b-rep(d,length(b)) does not work either as it makes a list of length length(d)*length(b) not a list of length(b). I know how to do this with a for loop, but I feel that there has to be a more efficient way. Any suggestions? [[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. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ 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] Removing rows if certain elements are found in character string
Hello, Inline. Em 03-07-2012 01:15, jim holtman escreveu: You will have to change the 'i1' expression as follows: i1 - grepl(^([0D]|[0d])*$, dd$ch) i1 # matches strings with d D in them [1] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE # second string had 'd' 'D' in it so it was TRUE above and FALSE below i1new - grepl(^([0D]*$|[0d]*$), dd$ch) i1new [1] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE Right, apparently, I forgot that grep is greedy, and the test cases were not complete. I put a 'd' and 'D' in the second string and the original regular expression is equivalent to grepl(^[0dD]*$, dd$ch) This is only for the first request, and does not solve cases where there are characters other than '0', 'd' or 'D', but 'd' or 'D' are the first non-zero. This is the case of my 4th row, changed from the OP's data example. My regexpr for 'i2' is equivalent to this one, that I believe is more readable: i2b - grepl(^0{0,}[Dd], dd$ch) First a zero, that might occur zero or more times, then a 'd' or 'D', then and til the end, irrelevant. which will match strings containing d, D and 0. If you only want 'd' or 'D' (and not both), then you will have to use the one in 'i1new'. To the OP: bottom line, use Jim's 'i1new' and my 'i2' or 'i2b'. Rui Barradas On Mon, Jul 2, 2012 at 7:24 PM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, Try regular expressions instead. In this data.frame, I've changed row nr.4 to have a row with 'D' as first non-zero character. dd - read.table(text= ch count 1 00D0 0.007368 2 00d0 0.002456 3 0T00 0.007368 4 0DT0 0.007368 5 0T00 0.002456 6 0Td0 0.002456 7 T000 0.007368 8 T0D0 0.007368 9 T000 0.002456 10 T0d0 0.002456 , header=TRUE) dd i1 - grepl(^([0D]|[0d])*$, dd$ch) i2 - grepl(^0*[Dd], dd$ch) dd[!i1, ] dd[!i2, ] dd[!(i1 | i2), ] Hope this helps, Rui Barradas Em 02-07-2012 23:48, Claudia Penaloza escreveu: I would like to remove rows from the following data frame (df) if there are only two specific elements found in the df$ch character string (I want to remove rows with only 0 D or 0 d). Alternatively, I would like to remove rows if the first non-zero element is D or d. ch count 1 00D0 0.007368; 2 00d0 0.002456; 3 0T00 0.007368; 4 0TD0 0.007368; 5 0T00 0.002456; 6 0Td0 0.002456; 7 T000 0.007368; 8 T0D0 0.007368; 9 T000 0.002456; 10 T0d0 0.002456; I tried the following but it doesn't work if there is more than one character per string: df - df[!df$ch %in% c(0,D),] df - df[!df$ch %in% c(0,d),] Any help greatly appreciated, Claudia [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ 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] Fitting and Plotting the fitted distributions
Hello, This is here for some days now, and I've decided to give it a try. I've rewritten your fitfunction(), making it simpler. And include the gamma distribution in the list. require(MASS) fitfunction - function(Type, x) list(Type=Type, Fit=fitdistr(x, Type)) fun - function(x, data){ data - sort(data) if(x$Type == exponential) y - pexp(data, rate=x$Fit$estimate['rate']) else if(x$Type == geometric) y - pgeom(data, prob=x$Fit$estimate['prob']) else if(x$Type == log-normal) y - plnorm(data, meanlog=x$Fit$estimate['meanlog'], sdlog=x$Fit$estimate['sdlog']) else if(x$Type == normal) y - pnorm(data, mean=x$Fit$estimate['mean'], sd=x$Fit$estimate['sd']) else if(x$Type == Poisson) y - ppois(data, lambda=x$Fit$estimate['lambda']) else if(x$Type == gamma) y - pgamma(data, shape=x$Fit$estimate['shape'], rate=x$Fit$estimate['rate']) list(x=data, y=y) } set.seed(1) distrList - list(exponential, geometric, log-normal, normal, Poisson, gamma) On - round(abs(rnorm(1, sd=100))+5,digits=0) storeOn - lapply(distrList, fitfunction, x=On) str(storeOn) lapply(storeOn, function(x) AIC(x$Fit)) coord - lapply(storeOn, fun, On) color - seq_along(distrList) + 1 plot(ecdf(On), verticals= TRUE, do.p = FALSE, lwd=2) lapply(seq_along(coord), function(i) lines(coord[[i]]$x, coord[[i]]$y, col=color[i])) legend(right, legend=distrList, col=color, lty=1, bty=n) Hope this helps, Rui Barradas Em 02-07-2012 07:13, Alaios escreveu: Dear all, I have wrote some sample code that would allow me easier fit fast many distributions and check which of the fits performs better. My sample code (that you can of course execute it looks like that) distrList-list( exponential, geometric, log-normal, normal, Poisson) fitfunction-function(Type,x){ return (list(Type,(fitdistr(x,Type } require(MASS) On-round(abs(rnorm(1,sd=100))+5,digits=0) storeOn-lapply(distrList,fitdistr,x=On) plot(ecdf(On)) str(storeOn) what I am looking now is to plot with the initial dataset plot(ecdf(On)) all the fitted distributions over the same window. I am not sure though, if there is some straightforward way (i.e same random distribution generator) for the fitted paramemeters to plot those over the existing plot(ecdf(On)). Could you please help me with that? Regards Alex [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]
Hi Hi all, I would like create a new column in a data.frame (a1) to store 0, 1 data converted from a factor as below. a1$h2-NULL for (i in 1:dim(a1)[1]) { if (a1$h1[i]==H) a1$h2[i]-1 else a1$h2[i]-0 } My question: is it possible to remove the loop from above code to achieve the desired result? Untested a1$h2 - (a1$h1==H)*1 Regards Petr Thanks in advance, Jin Geoscience Australia Disclaimer: This e-mail (and files transmitted with it) is intended only for the person or entity to which it is addressed. If you are not the intended recipient, then you have received this e-mail by mistake and any use, dissemination, forwarding, printing or copying of this e-mail and its file attachments is prohibited. The security of emails transmitted cannot be guaranteed; by forwarding or replying to this email, you acknowledge and accept these risks. - [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Code scatter plot data from matrix with 3rd column
Hello, In order to avoid messing up the data, use dput. See below, in the end. As for your question, try this: set.seed(1234) xyz - data.frame(x=sample(20, 10), y=sample(20, 10), z=sample(0:1, 10, TRUE)) # pch=16 -- solid circle; cex=4 -- 4 fold expansion with(xyz, plot(x, y, col=z+1, pch=16, cex=4)) # color 0 is white If you have a matrix, not a data.frame, don't use with(), use xyz[, x], etc. The use of dput() dput(xyz) structure(list(x = c(3L, 12L, 11L, 18L, 14L, 10L, 1L, 4L, 8L, 6L), y = c(14L, 11L, 6L, 16L, 5L, 13L, 17L, 4L, 3L, 12L), z = c(0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L)), .Names = c(x, y, z ), row.names = c(NA, -10L), class = data.frame) Now all anyone has to do is to copy that output, from 'structure' onward and paste it in an R session. Try it, assign to a variable: xyz2 - structure(...etc...) ~~~ Hope this helps, Rui Barradas Em 02-07-2012 21:31, Kathryn B Walters-Conte escreveu: Hello I am looking for a simple way to plot my data from a matrix (or data frame) using a 3rd column as category to code the data points. for example: xyz 543240 104230 15901 203241 25781 3042340 357891 405670 45780 50531 Ideally, I'd like 0 or 1 to correspond to a color but I'd settle for a symbol at this point. I have tried working with pch but can't get it to work. Thanks Kat [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] R with VBA_Excel
Do you know how can I run a script on R from Excel without rExcel but with VBA and batch? -- View this message in context: http://r.789695.n4.nabble.com/R-with-VBA-Excel-tp4635265.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] Is it possible to remove this loop? [SEC=UNCLASSIFIED]
On 07/03/2012 05:18 PM, jin...@ga.gov.au wrote: Hi all, I would like create a new column in a data.frame (a1) to store 0, 1 data converted from a factor as below. a1$h2-NULL for (i in 1:dim(a1)[1]) { if (a1$h1[i]==H) a1$h2[i]-1 else a1$h2[i]-0 } My question: is it possible to remove the loop from above code to achieve the desired result? Hi Jin, Just to provide you with an embarrassment of alternatives: a1$h2-ifelse(a1$h1==H,1,0) Jim __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help with lmer formula
Thanks Joshua for the clear explanation. I've previously posted in r sig mixed, but I got no response... :( Cheers, - Camila On Mon, Jul 2, 2012 at 9:31 PM, Joshua Wiley jwiley.ps...@gmail.com wrote: Hi Camila, In mixed equation form instead of multilevel, it would be: Y_it = gamma_00 + gamma_10*X_it + gamma_11*W_it*X + (e_it + u_0t + u_1j*X) your code seems reasonable. Note that the random intercept and slope will be correlated in your specification (unstructured if you want, it is possible to force out, but is sensible starting place) model - lmer(Y ~ X + X:W + (X | ID), data = data) which gives: residual variance: e_it variance of intercept (constant term, gamma_00): u_0t variance of slope (gamma_10): u_1j*X as well as overall estimates for the intercept, slope of X, and the interaction of X and W. Bert is correct that R sig mixed models is the more appropriate list, but many people read both and there is no reason it cannot be answered here. Cheers, Josh On Mon, Jul 2, 2012 at 6:47 PM, Camila Mendes cacamende...@gmail.com wrote: Hey all - I am a newbie on mixed-effects models. I want to estimate the following model: Y_it = alpha_0t + alpha_1t*X_it + e_it alpha_0t = gamma_00 + u_0t alpha_1t = gamma_10 + gamma_11*W_it + u_1j Where Y is my outcome, X is my level-1 predictor, and W is my level 2 predictor. I am not sure if I am doing it right. Is this the correct specification of the formula? model = lmer(Y ~ X + X:Y + ( X | ID), data = data) Also, can you help me to write down the combined model formula? I tried by substituting on the first equation, but I got some weird interactions with the residual (?) Thanks a lot! - Camila [[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. -- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/ [[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] MLE
Homework?? We don't do homework here. If not, ?optim or look at the CRAN Optimize task view for optimizers. There is even a maxLik package that might be useful. -- Bert On Mon, Jul 2, 2012 at 8:58 PM, Ali Tamaddoni alicivilizati...@gmail.comwrote: Hi All I have a data frame called nbd with two columns (x and T). Based on this dataset I want to find the parameters of a distribution with the following log-liklihood function and with r and alpha as its parameters: log(gamma(nbd$x+r))-log(gamma(r))+r*log(alpha)-(r+nbd$x)*log(nbd$T+alpha) the initial value for both parameters is 1. I would be thankful if you could help me to solve this problem in R. Regards, Ali [[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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[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] Automating R script with Windows 7
On Mon, Mar 5, 2012 at 2:47 PM, vincent.deluard vincentdelua...@gmail.com wrote: Hi Jim, Please disregard my earlier post -- I have done some research and realized the space between after program was the the issue. I can now open RScript.exe from the command prompt using the abbreviated form: C:\PROGRA~1\R\R-2.11.1\bin\RScript.exe From your earlier post, I understand the structure to type in the command prompt should be: 'Path to RScript.exe' SPACE 'Path to the .R file I want to execute' Which should translate as the following on my machine: C:\PROGRA~1\R\R-2.11.1\bin\RScript.exe \C:\Users\Vincent\Documents\temp\test.r Yet I get the following error message: 'Fatal Error: cannot open file \C:\Users\Vincent\Documents\temp\test.r' I don't think you want the first backslash in the test.r path, i.e., it should be C:\Users\Vincent\Documents\temp\test.r instead of \C:\Users\Vincent\Documents\temp\test.r Best, Ista What am I doing wrong? Thanks for your help! -- View this message in context: http://r.789695.n4.nabble.com/Automating-R-script-with-Windows-7-tp4446693p4447406.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.
Re: [R] Wrapper function for multivariate arrays for ode
On Jul 2, 2012, at 10:09 PM, Tjun Kiat Teo wrote: I am trying to to write a wrapper function for the ode solver (under the package desolve) to enable it to take multivariate arrays. I know how to do it for 1 dimension arrays but my code breaks down when I try to do it for 2 dimensional arrays. Here is my code diffwrap-function(t,y,mu)vdpol(t=t,A[1:3,1:4]-y[1:12],B[1:12]- y[13:24],mu=mu) I do not know whether this explains your problems , but it generally is very dangerous practice to have items in function argument lists like: A[1:3,1:4]-y[1:12] and B[1:12]-y[13:24] I've often received similar messages when I mistaken entered - rather than = in that situation. The code runs with this definition (after loading pkg:deSolve rather than 'desolve'): diffwrap-function(t,y,mu) vdpol(t=t, A = matrix(y[1:12], 3,4), B=y[13:24], mu=mu) Whether the output is sensible mathematically is beyond my knowledge, -- David vdpol-function(t,A,B,mu) { list(c(mu, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, A[1,1], A[2,1], A[3,1], A[1,2], A[2,2], A[3,2], A[1,3], A[2,3], A[3,3], A[1,4], A[2,4], A[3,4]) ) } stiff-ode(y=rep(0,24),times=c(0,1),func=diffwrap,parms=1) I get keep getting the error message variable A[1,1] not found. __ 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. David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]
On Jul 3, 2012, at 5:08 AM, Jim Lemon wrote: On 07/03/2012 05:18 PM, jin...@ga.gov.au wrote: Hi all, I would like create a new column in a data.frame (a1) to store 0, 1 data converted from a factor as below. a1$h2-NULL for (i in 1:dim(a1)[1]) { if (a1$h1[i]==H) a1$h2[i]-1 else a1$h2[i]-0 } My question: is it possible to remove the loop from above code to achieve the desired result? Hi Jin, Just to provide you with an embarrassment of alternatives: a1$h2-ifelse(a1$h1==H,1,0) One more. Similar to Petr's, but perhaps a bit more accessible to a new R user: a1$h2 - as.numeric(a1$h1==H) I wasn't sure whether NA's would be handled in the same manner by these two methods so I tested: ifelse( factor(c(H, h, NA))==H, 1, 0) [1] 1 0 NA as.numeric( factor(c(H, h, NA))==H) [1] 1 0 NA -- David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Code scatter plot data from matrix with 3rd column
Your data is effectively unreadable. Please use dput() to supply sample data. Here is one way to do what you want for data frames using the ggplot2 package. library(ggplot2) mydata - data.frame(x = 1:10, y = rnorm(10), z = c(rep(1,4), rep(2, 6))) ggplot(mydata, aes(x, y, colour= as.factor( z))) + geom_point() John Kane Kingston ON Canada -Original Message- From: kbw1...@yahoo.com Sent: Mon, 2 Jul 2012 13:31:36 -0700 (PDT) To: r-help@r-project.org Subject: [R] Code scatter plot data from matrix with 3rd column Hello I am looking for a simple way to plot my data from a matrix (or data frame) using a 3rd column as category to code the data points. for example: xyz 543240 104230 15901 203241 25781 3042340 357891 405670 45780 50531 Ideally, I'd like 0 or 1 to correspond to a color but I'd settle for a symbol at this point. I have tried working with pch but can't get it to work. Thanks Kat [[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. GET FREE SMILEYS FOR YOUR IM EMAIL - Learn more at http://www.inbox.com/smileys Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™ and most webmails __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to do a graph with tree different colors??
Any sample data for us to work with? See ?dput for a good method of supplying sample data. John Kane Kingston ON Canada -Original Message- From: denissearchun...@yahoo.com.mx Sent: Mon, 2 Jul 2012 14:04:53 -0700 (PDT) To: r-help@r-project.org Subject: [R] how to do a graph with tree different colors?? hi i try to do a graph of a time series which shows in red the values -0.05, in blue the values 0.05 and in white the values between -0.05 and 0.05 for un exemple :http://www.appinsys.com/globalwarming/enso.htm thanks ! denisse -- View this message in context: http://r.789695.n4.nabble.com/how-to-do-a-graph-with-tree-different-colors-tp4635206.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. FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family! Visit http://www.inbox.com/photosharing to find out more! __ 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] carpet plots
Anything here that might help http://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/ John Kane Kingston ON Canada -Original Message- From: joeclar...@hotmail.com Sent: Mon, 2 Jul 2012 13:21:25 -0700 To: mueller.eisb...@googlemail.com, r-help@r-project.org Subject: Re: [R] carpet plots These carpet plots are also called heat maps and there's a current thread with the subject line Heat Maps in which I've given a couple of examples of code for them. The R function image() is very easy to use: image( x=(x values), y=(y values), z=(matrix of z values with x rows and y columns), col=(vector of colors), breaks=(vector of break points, one more than colors) ) What I'd like to know is: does anyone know how to easily create a legend/key to the color gradients, like the one in the right margin of Mueller's first example: (http://www.johannes-hopf.de/wordpress/wp-content/uploads/2009/12/Zufall-300x225.png)? // joseph w. clark , phd candidate \\ usc marshall school of business Date: Mon, 2 Jul 2012 20:05:12 +0200 From: mueller.eisb...@googlemail.com To: r-help@r-project.org Subject: [R] carpet plots Hi all, I wonder why there is so little software for carpet plots (german: Rasterdiagramm) (Three dimensional plot (x, y, z), the 3rd dimension (z) symbolized by colourgradients). Besides from one or the other non free software I only found an OpenOffice macro, a combination of Gnuplot and Excel (an Excel macro calling gnuplot) (http://www.johannes-hopf.de/2009/12/carpet-plot-version-1-3/9 and Quikgrid (http://www.galiander.ca/quikgrid/) which I use for bathymetric maps. Though I use one or two R scripts I have no deeper knowledge. Because I think That's a thing R can do!, I suppose, there are scripts for this purpose. Perhaps one of you knows such a script. I would be very grateful if you could point me to some information on this subject. Richard -- Richard Müller . Am Spring 9 . D-58802 Balve www.oeko-sorpe.de __ 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. FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks orcas on your desktop! __ 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] saving contour() plot info
{ I think this message got rejected at the 1st attempt - trying again} R 2.15.1 , windows XP I have a very non-stationary bivariate time-series - say {xt,yt} t=1 ... lots. I want to do a bivariate density contour-plot of the whole series and then step through the series 1 second at a time plotting that second's {x,y} subset on top of the contour plot and losing the previous second's subset so that the effect enables you to see an 'animation' of how the series 'travels' through different parts of the joint density as time passes. The only way I've found to do this is to repeatedly call contour() before plotting each seconds-worth of data ... this works, but because of the time taken to do the contour() calculations in each step of the loop , it has an unpleasant flashing appearance. Is it possible to run contour() just once and save the contour- plotting info, so that in each step of the loop I only have do the actual plotting of the contours? Or any other way of achieving the desired outcome. Grateful for any guidance ... Bob Kinley [[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] Problem using mtext to write onto a jpeg
Dear all, I am trying to write figures directly to a file using the jpeg() function. this_ylab=expression(paste(P,sep=)~lambda) this_xlab=expression(rho) jpeg(file_name.jpeg,width=100,height=100,units=mm,res=300) plot(input_values,output_values,pch=16,cex=cex_pt,xlab=,frame.plot=T,ann=F,axes=F) axis(side=2) axis(side=1,labels=T,xlab=) mtext(this_xlab,side=1,line=4,cex=2) mtext(this_ylab,side=2,line=3,cex=2) dev.off() However, I get the following error message: Error in mtext(this_xlab, side = 1, line = 4, cex = 2) : Metric information not available for this device I could put the argument specifying the x-label in the plot function but then I don't control where it goes. When I have used the tiff() function instead I don't have this problem. I am using R 2.11.1 on Mac OS X 10.7.3 Thanks, George -- View this message in context: http://r.789695.n4.nabble.com/Problem-using-mtext-to-write-onto-a-jpeg-tp4635258.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] Is it possible to remove this loop? [SEC=UNCLASSIFIED]
And one more alternative: a1$h2 - apply(a1,1, function(x) if (x[h1]==H) 1 else 0 ) -- View this message in context: http://r.789695.n4.nabble.com/Is-it-possible-to-remove-this-loop-SEC-UNCLASSIFIED-tp4635250p4635271.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] read the date format
hi I have a data like thursday, November 20,2012. I'm not able to convert into data format in R Can anyone please help - Thanks in Advance Arun -- View this message in context: http://r.789695.n4.nabble.com/read-the-date-format-tp4635245.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] question
I have already fitted several models using R code; arima(rates,c(p,d,q)) As I heard, best model produce the smallest AIC value, but maximum likelihood estimation procedure optimizer should converge. How to check whether maximum likelihood estimation procedure optimizer has converged or not? [[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] Assigning a vector to every element of a list.
Hi, If you want to assign a vector to every element of a list, vec1-11:20 list1-split(LETTERS[1:10],1:length(LETTERS[1:10])) list2-lapply(1:10,function(x) vec1) or, list3-lapply(list1,function(x) list1=vec1) or list4-list() vec2-1:5 list4[1:length(list1)]-list(vec2) # if you want to assign each element of vector to each element of list (assuming both have the same lengths), vec1-11:20 list1-split(LETTERS[1:10],1:length(LETTERS[1:10])) newlist-split(vec1,1:length(vec1)) A.K. - Original Message - From: Spencer Maynes smayne...@gmail.com To: r-help@r-project.org Cc: Sent: Monday, July 2, 2012 6:16 PM Subject: [R] Assigning a vector to every element of a list. I have a vector d of unknown length, and a list b of unknown length. I would like to replace every element of b with d. Simply writing b-d does not work as R tries to fit every element of d to a different element of d, and b-rep(d,length(b)) does not work either as it makes a list of length length(d)*length(b) not a list of length(b). I know how to do this with a for loop, but I feel that there has to be a more efficient way. Any suggestions? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] insert missing dates
Hello I have dataframes. mydata1 -data.frame(value=c(15,20,25,30,45,50),dates=c(2005-05-25 07:00:00 ,2005-05-25 19:00:00,2005-06-25 07:00:00,2005-06-25 19:00:00 ,2005-07-25 07:00:00,2005-8-25 19:00:00)) or mydata2 -data.frame(value=c(15,20,25,30,45,50),dates=c(2005-05-25 00:00:00 ,2005-05-25 00:10:00,2005-05-25 00:30:00,2005-05-25 00:40:00 ,2005-05-25 00:50:00,2005-5-25 01:10:00)) I have to get such dataframes mydata1 -data.frame(value=c(15,20,25,30,45,NA,NA,50),dates=c(2005-05-25 07:00:00,2005-05-25 19:00:00,2005-06-25 07:00:00,2005-06-25 19:00:00 ,2005-07-25 07:00:00,2005-07-25 19:00:00,2005-8-25 07:00:00, 2005-8-25 19:00:00)) or mydata2 -data.frame(value=c(15,20,NA,25,30,45,NA,50),dates=c(2005-05-25 00:00:00,2005-05-25 00:10:00,2005-05-25 00:20:00,2005-05-25 00:30:00 ,2005-05-25 00:40:00,2005-05-25 00:50:00,2005-5-25 01:00:00, 2005-5-25 01:10:00)) Regards, Aleksander. [[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] Automating R script with Windows 7
In R you should slashes instead of backslashes: C:\PROGRA~1\R\R-2.11.1\bin\RScript.exe C:/Users/Vincent/Documents/temp/test.r Bart -- View this message in context: http://r.789695.n4.nabble.com/Automating-R-script-with-Windows-7-tp4446693p4635260.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] how to do a graph with tree different colors??
R Graph Gallery contains lots of plot examples with source code: http://addictedtor.free.fr/graphiques/ -- View this message in context: http://r.789695.n4.nabble.com/how-to-do-a-graph-with-tree-different-colors-tp4635206p4635269.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] Error() model is singular - what does that mean
That I correct. I have only 9. Thanks for the explanation :) -- View this message in context: http://r.789695.n4.nabble.com/Error-model-is-singular-what-does-that-mean-tp4635103p4635263.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] nls problem
hi list, used versions: 2.12.1 and 2.14.0 under ubuntu and macosx. I recently stumbled over a problem with `nls', which occurs if the model is not specified explicitly but via an evaluation of a 'call' object. simple example: 8-- nlsProblem - function (len = 5) { #=== # purpose: to demonstrate an apparent problem with `nls' which occurs, # if the model is specified by passing th lhs as an evaled 'call' # object. The problem is related to the way `nls' tries to compute # its internal variable `varIndex' which rests on the assumption that # the dependent (y) and, possibly, the independent (x) variable # are identified by having a length equal to the `nls' variable # `respLength'. the problem arises when there are `varNames' # components accidentally having this length, too. # in the present example, setting the number of data points to # len=2 triggers the error since the `call' object `fifu' has this # length, too and `nls' constructs an erroneous `mf$formula' internally. #=== #generate some data x - seq(0, 4, len = len) y - exp(-x) y - rnorm(y, y, .001*y) data - list(x = x, y = y) #define suitable model model - y ~ exp(-k*x) fifu - model[[3]] #this fit is fine: fit1 - nls(model, data = data, start = c(k = 1)) print(summary(fit1)) #this fit crashes `nls' if len = 2: fit2 - nls(y ~ eval(fifu), data = data, start = c(k = 1)) print(summary(fit2)) } 8-- to see the problem call `nlsProblem(2)'. as explained in the above comments in the example function, I tracked it down to the way `nls' identifies x and y in the model expression. the problem surfaces in the line varIndex - n%%respLength == 0 (line 70 in the function listing from within R) which, in the case of `fit2' in the above example always returns a single TRUE index as long as `len != 2' (which seems fine for the further processing) but returns a TRUE value for the index of `fifu' as well if `len == 2'. question1: I'm rather sure it worked about 6 months ago with (ca.) 2.11.x under ubuntu. have there been changes in this area? question2: is something like the `fit2' line in the example expected to work or not? qeustion3: if it is not expected to work, should not the manpage include a corresponding caveat? question4: is there a a substitute/workaround for the `fit2' line which still allows to specify the rhs of the model via a variable instead of a constant (explicit) expression or function call? the above example is of course construed but in my use case I actually need this sort of thing. is there any chance that the way `nls' analyzes its `model' argument can be changed to parse the `eval(fifu)' construct correctly in all cases? since I'm currently not subscribed to the list I'd appreciate if responses could be Cc'ed to me directly. thanks in advance, joerg __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to do a graph with tree different colors??
Try using polygon() or grid.polygon() from the grid package. You can find code example on the R project title page. Go to http://r-project.org and click on the plot (the example plot showing PCA, clustering and factors, lower left are two graphs, similar to what you want). This will bring you to the source code, generating this plot. Search for Factor 1 on the page will show that this plot was produced with the function plotdens(). Search for plotdens will show its body and will discover, that it calls polygon() function. -- View this message in context: http://r.789695.n4.nabble.com/how-to-do-a-graph-with-tree-different-colors-tp4635206p4635268.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] Automating R script with Windows 7
Thank you for your help So i have tried many ways on different computers, but i believe i have an operating system, as I open RScript up in my local directory it just flashes at me and them disappears I tried to task schedule, using the following code C:\Program Files\R\R-2.12.1\bin\i386\Rscript.exe but the Rscript just flashes at me Thanks for ur assistance, its back to researching my problem -- View this message in context: http://r.789695.n4.nabble.com/Automating-R-script-with-Windows-7-tp4446693p4635275.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] problem in lodging package
Dear, I would like to use the siar program, when I lodging package the message appear, so I couldn't use the related calculations. The following object(s) are masked from package:spatstat:convexhull Could you please write me what shoud I do? Sukran Yalcin Ozdilek [[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] Problem using mtext to write onto a jpeg
On Jul 3, 2012, at 4:01 AM, georgeshirreff wrote: Dear all, I am trying to write figures directly to a file using the jpeg() function. this_ylab=expression(paste(P,sep=)~lambda) this_xlab=expression(rho) jpeg(file_name.jpeg,width=100,height=100,units=mm,res=300) Try instead something like: quartz(file=file_name.jpeg, type=jpeg, width=7,height=7) And follow-ups should go the R-SIG-Mac mailing list. And please update your installation of R. -- David. plot (input_values ,output_values,pch=16,cex=cex_pt,xlab=,frame.plot=T,ann=F,axes=F) axis(side=2) axis(side=1,labels=T,xlab=) mtext(this_xlab,side=1,line=4,cex=2) mtext(this_ylab,side=2,line=3,cex=2) dev.off() However, I get the following error message: Error in mtext(this_xlab, side = 1, line = 4, cex = 2) : Metric information not available for this device I could put the argument specifying the x-label in the plot function but then I don't control where it goes. When I have used the tiff() function instead I don't have this problem. I am using R 2.11.1 on Mac OS X 10.7.3 Thanks, George -- View this message in context: http://r.789695.n4.nabble.com/Problem-using-mtext-to-write-onto-a-jpeg-tp4635258.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. David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] saving contour() plot info
On 03/07/2012 10:36 AM, Robert Douglas Kinley wrote: { I think this message got rejected at the 1st attempt - trying again} R 2.15.1 , windows XP I have a very non-stationary bivariate time-series - say {xt,yt} t=1 ... lots. I want to do a bivariate density contour-plot of the whole series and then step through the series 1 second at a time plotting that second's {x,y} subset on top of the contour plot and losing the previous second's subset so that the effect enables you to see an 'animation' of how the series 'travels' through different parts of the joint density as time passes. The only way I've found to do this is to repeatedly call contour() before plotting each seconds-worth of data ... this works, but because of the time taken to do the contour() calculations in each step of the loop , it has an unpleasant flashing appearance. Generally the way to get rid of the flashing is to use dev.hold() while plotting, then dev.flush() when done. This isn't supported on all graphics devices. Is it possible to run contour() just once and save the contour- plotting info, so that in each step of the loop I only have do the actual plotting of the contours? It is possible to save the contour information. See ?contourLines. This doesn't save everything (e.g. the labelling), but it might be enough for you. You could also try using recordPlot() after plotting the contours, then replayPlot() when you want to reproduce them. Duncan Murdoch Or any other way of achieving the desired outcome. Grateful for any guidance ... Bob Kinley [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] read the date format
Hello, Try, with 'x' your date(s), as.Date(x, format=%A, %B %d,%Y) strptime(x, format=%A, %B %d,%Y) Note: this is in the help page for ?strptime Hope this helps, Rui Barradas Em 03-07-2012 07:04, arunkumar escreveu: hi I have a data like thursday, November 20,2012. I'm not able to convert into data format in R Can anyone please help - Thanks in Advance Arun -- View this message in context: http://r.789695.n4.nabble.com/read-the-date-format-tp4635245.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.
Re: [R] question
Hello, Inline. Em 03-07-2012 09:22, Sajeeka Nanayakkara escreveu: I have already fitted several models using R code; arima(rates,c(p,d,q)) As I heard, best model produce the smallest AIC value, but maximum likelihood estimation procedure optimizer should converge. How to check whether maximum likelihood estimation procedure optimizer has converged or not? By reading the help page. ?arima Value [...] codethe convergence value returned by _optim_. Hope this helps, Rui Barradas [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] saving contour() plot info
Many thanks for these ideas ... I'll try them, and report back Cheers Bob Kinley -Original Message- From: Duncan Murdoch [mailto:murdoch.dun...@gmail.com] Sent: 03 July 2012 15:54 To: Robert Douglas Kinley Cc: r-help@r-project.org Subject: Re: [R] saving contour() plot info On 03/07/2012 10:36 AM, Robert Douglas Kinley wrote: { I think this message got rejected at the 1st attempt - trying again} R 2.15.1 , windows XP I have a very non-stationary bivariate time-series - say {xt,yt} t=1 ... lots. I want to do a bivariate density contour-plot of the whole series and then step through the series 1 second at a time plotting that second's {x,y} subset on top of the contour plot and losing the previous second's subset so that the effect enables you to see an 'animation' of how the series 'travels' through different parts of the joint density as time passes. The only way I've found to do this is to repeatedly call contour() before plotting each seconds-worth of data ... this works, but because of the time taken to do the contour() calculations in each step of the loop , it has an unpleasant flashing appearance. Generally the way to get rid of the flashing is to use dev.hold() while plotting, then dev.flush() when done. This isn't supported on all graphics devices. Is it possible to run contour() just once and save the contour- plotting info, so that in each step of the loop I only have do the actual plotting of the contours? It is possible to save the contour information. See ?contourLines. This doesn't save everything (e.g. the labelling), but it might be enough for you. You could also try using recordPlot() after plotting the contours, then replayPlot() when you want to reproduce them. Duncan Murdoch Or any other way of achieving the desired outcome. Grateful for any guidance ... Bob Kinley [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] insert missing dates
Hello, The trick is to use seq() for date classes. First of all, when creating data.frames use stringsAsFactors=FALSE, in order not to convert them to factors. I've added this option to your data.frame() instructions. And then, mydata1$dates - as.POSIXct(mydata1$dates) mydata1b$dates - as.POSIXct(mydata1b$dates) mydata2$dates - as.POSIXct(mydata2$dates) mydata2b$dates - as.POSIXct(mydata2b$dates) Now, first we create the sequences then use merge(9, that will take care of inserting the NAs. # mydata1 tmp - tapply(mydata1$dates, format(mydata1$dates, %Y-%m-%d), function(x){ if(length(x) 2) paste(format(x, %Y-%m-%d), c(07:00:00, 19:00:00)) else sub(0 [[:alpha:]]+$, , x) }) tmp - as.POSIXct(unlist(tmp)) merge(mydata1, data.frame(dates=tmp), all.y=TRUE) # and mydata2 tmp - data.frame(dates=seq(mydata2$dates[1], mydata2$dates[n], by=10 mins)) merge(mydata2, tmp, all.y=TRUE) Hope this helps, Rui Barradas Em 03-07-2012 10:52, Васильченко Александр escreveu: Hello I have dataframes. mydata1 -data.frame(value=c(15,20,25,30,45,50),dates=c(2005-05-25 07:00:00 ,2005-05-25 19:00:00,2005-06-25 07:00:00,2005-06-25 19:00:00 ,2005-07-25 07:00:00,2005-8-25 19:00:00)) or mydata2 -data.frame(value=c(15,20,25,30,45,50),dates=c(2005-05-25 00:00:00 ,2005-05-25 00:10:00,2005-05-25 00:30:00,2005-05-25 00:40:00 ,2005-05-25 00:50:00,2005-5-25 01:10:00)) I have to get such dataframes mydata1 -data.frame(value=c(15,20,25,30,45,NA,NA,50),dates=c(2005-05-25 07:00:00,2005-05-25 19:00:00,2005-06-25 07:00:00,2005-06-25 19:00:00 ,2005-07-25 07:00:00,2005-07-25 19:00:00,2005-8-25 07:00:00, 2005-8-25 19:00:00)) or mydata2 -data.frame(value=c(15,20,NA,25,30,45,NA,50),dates=c(2005-05-25 00:00:00,2005-05-25 00:10:00,2005-05-25 00:20:00,2005-05-25 00:30:00 ,2005-05-25 00:40:00,2005-05-25 00:50:00,2005-5-25 01:00:00, 2005-5-25 01:10:00)) Regards, Aleksander. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Removing rows if certain elements are found in character string
Thank you Rui and Jim, both 'i1' and 'i1new' worked perfectly because there are no instances of 'Dd' or 'dD' in the data set (that I would/not want to include/exclude)... but I understand that 'i1new' targets precisely what I want. Why isn't a leader of zero's required for either 'i1' or 'i1new', as so? i1newer - grepl(^0{0,}[D]*$|^0{0,}[d]*$, dd$ch) Thank you again, Claudia On Tue, Jul 3, 2012 at 2:06 AM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, Inline. Em 03-07-2012 01:15, jim holtman escreveu: You will have to change the 'i1' expression as follows: i1 - grepl(^([0D]|[0d])*$, dd$ch) i1 # matches strings with d D in them [1] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE # second string had 'd' 'D' in it so it was TRUE above and FALSE below i1new - grepl(^([0D]*$|[0d]*$), dd$ch) i1new [1] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE Right, apparently, I forgot that grep is greedy, and the test cases were not complete. I put a 'd' and 'D' in the second string and the original regular expression is equivalent to grepl(^[0dD]*$, dd$ch) This is only for the first request, and does not solve cases where there are characters other than '0', 'd' or 'D', but 'd' or 'D' are the first non-zero. This is the case of my 4th row, changed from the OP's data example. My regexpr for 'i2' is equivalent to this one, that I believe is more readable: i2b - grepl(^0{0,}[Dd], dd$ch) First a zero, that might occur zero or more times, then a 'd' or 'D', then and til the end, irrelevant. which will match strings containing d, D and 0. If you only want 'd' or 'D' (and not both), then you will have to use the one in 'i1new'. To the OP: bottom line, use Jim's 'i1new' and my 'i2' or 'i2b'. Rui Barradas On Mon, Jul 2, 2012 at 7:24 PM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, Try regular expressions instead. In this data.frame, I've changed row nr.4 to have a row with 'D' as first non-zero character. dd - read.table(text= ch count 1 00D000**00 0.007368 2 00d000**00 0.002456 3 0T**00 0.007368 4 0DT000**00 0.007368 5 0T**00 0.002456 6 0Td000**00 0.002456 7 T0**00 0.007368 8 T0D000**00 0.007368 9 T0**00 0.002456 10 T0d000**00 0.002456 , header=TRUE) dd i1 - grepl(^([0D]|[0d])*$, dd$ch) i2 - grepl(^0*[Dd], dd$ch) dd[!i1, ] dd[!i2, ] dd[!(i1 | i2), ] Hope this helps, Rui Barradas Em 02-07-2012 23:48, Claudia Penaloza escreveu: I would like to remove rows from the following data frame (df) if there are only two specific elements found in the df$ch character string (I want to remove rows with only 0 D or 0 d). Alternatively, I would like to remove rows if the first non-zero element is D or d. ch count 1 00D000**00 0.007368; 2 00d000**00 0.002456; 3 0T**00 0.007368; 4 0TD000**00 0.007368; 5 0T**00 0.002456; 6 0Td000**00 0.002456; 7 T0**00 0.007368; 8 T0D000**00 0.007368; 9 T0**00 0.002456; 10 T0d000**00 0.002456; I tried the following but it doesn't work if there is more than one character per string: df - df[!df$ch %in% c(0,D),] df - df[!df$ch %in% c(0,d),] Any help greatly appreciated, Claudia [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://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. __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]
Re: [R] Removing rows if certain elements are found in character string
Hello, I'm glad it helped. See answer inline. Em 03-07-2012 17:09, Claudia Penaloza escreveu: Thank you Rui and Jim, both 'i1' and 'i1new' worked perfectly because there are no instances of 'Dd' or 'dD' in the data set (that I would/not want to include/exclude)... but I understand that 'i1new' targets precisely what I want. Why isn't a leader of zero's required for either 'i1' or 'i1new', as so? i1newer - grepl(^0{0,}[D]*$|^0{0,}[d]*$, dd$ch) Because both 'i1' and 'i1new' test from beginning to end of string, allowing only '0' and either 'd' or 'D', but not both (i1new). So, there's no need to explicitly test for a string that begins with '0'. Rui Barradas Thank you again, Claudia On Tue, Jul 3, 2012 at 2:06 AM, Rui Barradas ruipbarra...@sapo.pt mailto:ruipbarra...@sapo.pt wrote: Hello, Inline. Em 03-07-2012 01:15, jim holtman escreveu: You will have to change the 'i1' expression as follows: i1 - grepl(^([0D]|[0d])*$, dd$ch) i1 # matches strings with d D in them [1] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE # second string had 'd' 'D' in it so it was TRUE above and FALSE below i1new - grepl(^([0D]*$|[0d]*$), dd$ch) i1new [1] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE Right, apparently, I forgot that grep is greedy, and the test cases were not complete. I put a 'd' and 'D' in the second string and the original regular expression is equivalent to grepl(^[0dD]*$, dd$ch) This is only for the first request, and does not solve cases where there are characters other than '0', 'd' or 'D', but 'd' or 'D' are the first non-zero. This is the case of my 4th row, changed from the OP's data example. My regexpr for 'i2' is equivalent to this one, that I believe is more readable: i2b - grepl(^0{0,}[Dd], dd$ch) First a zero, that might occur zero or more times, then a 'd' or 'D', then and til the end, irrelevant. which will match strings containing d, D and 0. If you only want 'd' or 'D' (and not both), then you will have to use the one in 'i1new'. To the OP: bottom line, use Jim's 'i1new' and my 'i2' or 'i2b'. Rui Barradas On Mon, Jul 2, 2012 at 7:24 PM, Rui Barradas ruipbarra...@sapo.pt mailto:ruipbarra...@sapo.pt wrote: Hello, Try regular expressions instead. In this data.frame, I've changed row nr.4 to have a row with 'D' as first non-zero character. dd - read.table(text= ch count 1 00D000__00 0.007368 2 00d000__00 0.002456 3 0T__00 0.007368 4 0DT000__00 0.007368 5 0T__00 0.002456 6 0Td000__00 0.002456 7 T0__00 0.007368 8 T0D000__00 0.007368 9 T0__00 0.002456 10 T0d000__00 0.002456 , header=TRUE) dd i1 - grepl(^([0D]|[0d])*$, dd$ch) i2 - grepl(^0*[Dd], dd$ch) dd[!i1, ] dd[!i2, ] dd[!(i1 | i2), ] Hope this helps, Rui Barradas Em 02-07-2012 23:48, Claudia Penaloza escreveu: I would like to remove rows from the following data frame (df) if there are only two specific elements found in the df$ch character string (I want to remove rows with only 0 D or 0 d). Alternatively, I would like to remove rows if the first non-zero element is D or d. ch count 1 00D000__00 0.007368; 2 00d000__00 0.002456; 3 0T__00 0.007368; 4 0TD000__00 0.007368; 5 0T__00 0.002456; 6 0Td000__00 0.002456; 7 T0__00 0.007368; 8
Re: [R] R with VBA_Excel
On Jul 3, 2012, at 4:43 AM, cindy.dol wrote: Do you know how can I run a script on R from Excel without rExcel but with VBA and batch? It would seem that this question should be directed to a VBA mailing list. -- David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Automating R script with Windows 7
I did not suggest that you don't HAVE an operating system... simply that you don't know how to use yours. However you accomplish starting programs automatically, you WILL need to specify both the R interpreter (which you do seem to be accomplishing) and the name of the script you wish to run (which you don't appear to be doing). Appropriate use of quoting may be necessary if you have spaces in your paths. --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. maviney mvine...@hotmail.com wrote: Thank you for your help So i have tried many ways on different computers, but i believe i have an operating system, as I open RScript up in my local directory it just flashes at me and them disappears I tried to task schedule, using the following code C:\Program Files\R\R-2.12.1\bin\i386\Rscript.exe but the Rscript just flashes at me Thanks for ur assistance, its back to researching my problem -- View this message in context: http://r.789695.n4.nabble.com/Automating-R-script-with-Windows-7-tp4446693p4635275.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.
Re: [R] Assigning a vector to every element of a list.
Thanks guys for the help, I'm going to go with Patrick Burns answer because it seems to work the best for my situation, but these all seem like they should work. On Tue, Jul 3, 2012 at 2:51 AM, Patrick Burns pbu...@pburns.seanet.comwrote: b - rep(list(d), length(b)) On 02/07/2012 23:16, Spencer Maynes wrote: I have a vector d of unknown length, and a list b of unknown length. I would like to replace every element of b with d. Simply writing b-d does not work as R tries to fit every element of d to a different element of d, and b-rep(d,length(b)) does not work either as it makes a list of length length(d)*length(b) not a list of length(b). I know how to do this with a for loop, but I feel that there has to be a more efficient way. Any suggestions? [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/**blog http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') [[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] Problem using mtext to write onto a jpeg
Dear David, In fact just updating R seems to have fixed the problem. There's a lesson. Thanks a lot, George On 3 July 2012 17:02, David Winsemius [via R] ml-node+s789695n4635292...@n4.nabble.com wrote: On Jul 3, 2012, at 4:01 AM, georgeshirreff wrote: Dear all, I am trying to write figures directly to a file using the jpeg() function. this_ylab=expression(paste(P,sep=)~lambda) this_xlab=expression(rho) jpeg(file_name.jpeg,width=100,height=100,units=mm,res=300) Try instead something like: quartz(file=file_name.jpeg, type=jpeg, width=7,height=7) And follow-ups should go the R-SIG-Mac mailing list. And please update your installation of R. -- David. plot (input_values ,output_values,pch=16,cex=cex_pt,xlab=,frame.plot=T,ann=F,axes=F) axis(side=2) axis(side=1,labels=T,xlab=) mtext(this_xlab,side=1,line=4,cex=2) mtext(this_ylab,side=2,line=3,cex=2) dev.off() However, I get the following error message: Error in mtext(this_xlab, side = 1, line = 4, cex = 2) : Metric information not available for this device I could put the argument specifying the x-label in the plot function but then I don't control where it goes. When I have used the tiff() function instead I don't have this problem. I am using R 2.11.1 on Mac OS X 10.7.3 Thanks, George -- View this message in context: http://r.789695.n4.nabble.com/Problem-using-mtext-to-write-onto-a-jpeg-tp4635258.html Sent from the R help mailing list archive at Nabble.com. __ [hidden email] http://user/SendEmail.jtp?type=nodenode=4635292i=0mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius, MD West Hartford, CT __ [hidden email] http://user/SendEmail.jtp?type=nodenode=4635292i=1mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Problem-using-mtext-to-write-onto-a-jpeg-tp4635258p4635292.html To unsubscribe from Problem using mtext to write onto a jpeg, click herehttp://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_codenode=4635258code=Z2Vvcmdlc2hpcnJlZmZAZ29vZ2xlbWFpbC5jb218NDYzNTI1OHw0ODcyNjczNjk= . NAMLhttp://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewerid=instant_html%21nabble%3Aemail.namlbase=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespacebreadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml -- View this message in context: http://r.789695.n4.nabble.com/Problem-using-mtext-to-write-onto-a-jpeg-tp4635258p4635297.html Sent from the R help mailing list archive at Nabble.com. [[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] ggplot2: legend
Dear all, I produced the following graph with ggplot which is almost fine, yet I don't like that the legend for Means and Observations includes a line, though no line is used in the plot for those two (the line for Overall Mean on the other hand is wanted): library(ggplot2) ddf - data.frame(x = factor(rep(LETTERS[1:2], 5)), y = rnorm(10)) p - ggplot(ddf, aes(x = x, y = y)) p + geom_point(aes(colour=Observations, shape=Observations)) + stat_summary(aes(colour = Means, shape =Means), fun.y = mean, fun.ymin = min, fun.ymax = max, geom = point) + geom_hline(aes(yintercept = mean(y), linetype = Overall Mean), show_guide = TRUE) I tried to map the linetype in geom_point (and stat_summary) to NULL and I tried to set it to blank but neither worked. Is it furthermore possible to combine the two legends? My preferred plot would have two symbols with different colours and shapes for Means and Observations (but no line) and directly below that a line for Overall Mean (that is all the three items in one legend)? For that I tried to assign the same names to the legends but this did not work either. So any help would be highly appreciated. Kind Regards, Thorn Thaler Mathematician Applied Mathematics Nestec Ltd, Nestlé Research Center PO Box 44 CH-1000 Lausanne 26 Phone: +41 21 785 8220 Fax: +41 21 785 9486 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] read the date format
Hi, Hope this helps. date1- c(thursday November 20, 2012, friday November 21, 2012, saturday November 22, 2012) date2- as.Date(date1, format= %A %B %d, %Y) date2 [1] 2012-11-20 2012-11-21 2012-11-22 A.K. - Original Message - From: arunkumar akpbond...@gmail.com To: r-help@r-project.org Cc: Sent: Tuesday, July 3, 2012 2:04 AM Subject: [R] read the date format hi I have a data like thursday, November 20,2012. I'm not able to convert into data format in R Can anyone please help - Thanks in Advance Arun -- View this message in context: http://r.789695.n4.nabble.com/read-the-date-format-tp4635245.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] Data manipulation with aggregate
Hi everyone. I have these data : myData = data.frame(Name = c('a', 'a', 'b', 'b'), length = c(1,2,3,4), type = c('x','x','y','z')) which gives me: Name length type 1a 1x 2a 2x 3b 3y 4b 4 z I would group (mean) this DF using 'Name' as grouping factor. However, I have a field ('type') which is a string. I would like to use the unique value of this field when possible (i.e. when all the 'type' values are the same for each group) or replace with NA when 'type' has multiple values. In fact, I would like to obtain this: Name length type 1a 1.5x 2b 3.5NA For instance, I was using this command: aggregate(list(myData$length, myData$type), list(myData$Name), FUN = mean) But it can't deal with string data. I hope I have been clear enough. With regards, Phil -- View this message in context: http://r.789695.n4.nabble.com/Data-manipulation-with-aggregate-tp4635298.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] evaluating expressions contained in a dataframe
#I have a dataframe called tests that contain character expressions. These characters are rules that use data from within another dataframe. Is there any way within R I can access the rules in the dataframe called tests, and then evaluate these rules? #An example may better explain what I am trying to accomplish: tests - data.frame(matrix(data=c(info$country == 'Greenland', info$age 50), nrow=2, ncol=1)) names(tests) - rule info - data.frame(matrix(data=NA, nrow=5, ncol=3)) names(info) - c(first, country, age) info$name - c(Mary, Paul, Robert, John, Ivan) info$country - c(GReenland, Iceland, Ireland, Greenland, Greenland) info$age - c(30, 55, 66, 79, 80) #e.g. for info$country == 'Greenland' #I want: info$country == 'Greenland' [1] FALSE FALSE FALSE TRUE TRUE #e.g. info$country == 'Greenland' info$age 50 #I tried this, but it does not work: eval(tests$rule[1]) [[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] NADA Data Frame Format: Wide or Long?
I have water chemistry data with censored values (i.e., those less than reporting levels) in a data frame with a narrow (i.e., database table) format. The structure is: $ site: Factor w/ 64 levels D-1,D-2,D-3,..: 1 1 1 1 1 1 1 1 ... $ sampdate: Date, format: 2007-12-12 2007-12-12 ... $ preeq0 : logi TRUE TRUE TRUE TRUE TRUE TRUE ... $ param : Factor w/ 37 levels Ag,Al,Alk_tot,..: 1 2 8 17 3 4 9 ... $ quant : num 0.005 0.106 1 231 231 0.011 0.001 0.002 0.001 100 ... $ ceneq1 : logi TRUE FALSE TRUE FALSE FALSE FALSE ... $ floor : num 0 0.106 0 231 231 0.011 0 0 0 100 ... $ ceiling : num 0.005 0.106 1 231 231 0.011 0.001 0.002 0.001 100 ... The logical 'preeq0' separates sampdate into two groups; 'ceneq1' indicates censored/uncensored values; 'floor' and 'ceiling' are the minima and maxima for censored values. The NADA package methods will be used, but I have not found information on whether this format or the wide (i.e., spreadsheet) format should be used. The NADA.pdf document doesn't tell me; at least, I haven't found the answer there. I can apply reshape2 to melt and re-cast the data in wide format if that's what is appropriate. Please provide a pointer to documents I can read for an answer to this and related questions. Rich __ 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] integral with error:non-finite function value
Hi guys, I'm trying to use the the integral function to estimate the area under a PDF and a crossing curve. first I stated the function with several vectors in it: fn=function(a,b,F,mu,alpha,xi) { x-vector() fs-function(x) { c - (mu+(alpha*(1-(1-F)^xi)/xi)) tmp - (1 + (xi * (x - mu))/alpha) ((as.numeric(tmp 0) * (tmp^(-1/xi - 1) * exp(-tmp^(-1/xi/alpha)*((a*(x-c)^0.5)+(b*(x-c))) } return(fs)} #then I use the integral function xn-fn(1,2,0.98,824,300,-0.0098) horror-integrate(xn,lower=2021,upper=Inf) what I got is error message #Error in integrate(xn, lower = 2021, upper = Inf) : # non-finite function value can somebody help me by giving some light how to solve this problem? many thanks Best wishes, Al [[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] open mp problem when installing R
On 03/07/2012 06:56, Erin Hodgess wrote: Dear R People: I'm back to installing R from source, this time on a 64 bit machine. What OS? I'm using the R-Patched.tar.gz as my source. When I have the openmp option set to -fopenmp, I get the error that libgomp.spec is not found. Ok, so I commented out the reference to openmp. However, I ran an openmp hello world program using gcc and the -fopenmp option, and it ran fine. What am I missing, please? This is not the list to discuss such matters, but something is wrong with your toolchain. A version of gcc which supports OpenMP should have a libgomp.spec file, in one of the places it looks for .spec files. It maybe that you need to install something other than gcc to get it, especially on micro-packaged distributions. But e.g. on my Fedora 16 it is file /usr/lib/gcc/x86_64-redhat-linux/4.6.3/libgomp.spec which is part of the gcc RPM. Thanks, Erin -- Brian D. Ripley, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] integral with error:non-finite function value
Al Ehan wrote Hi guys, I'm trying to use the the integral function to estimate the area under a PDF and a crossing curve. first I stated the function with several vectors in it: fn=function(a,b,F,mu,alpha,xi) { x-vector() fs-function(x) { c - (mu+(alpha*(1-(1-F)^xi)/xi)) tmp - (1 + (xi * (x - mu))/alpha) ((as.numeric(tmp 0) * (tmp^(-1/xi - 1) * exp(-tmp^(-1/xi/alpha)*((a*(x-c)^0.5)+(b*(x-c))) } return(fs)} #then I use the integral function xn-fn(1,2,0.98,824,300,-0.0098) horror-integrate(xn,lower=2021,upper=Inf) what I got is error message #Error in integrate(xn, lower = 2021, upper = Inf) : # non-finite function value can somebody help me by giving some light how to solve this problem? many thanks Try xn(Inf) xn(1e6) xn(1e4) and then horror-integrate(xn,lower=2021,upper=1e4) horror Berend -- View this message in context: http://r.789695.n4.nabble.com/integral-with-error-non-finite-function-value-tp4635309p4635316.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] Assigning a vector to every element of a list.
Hi, Glad all of them worked. In my reply to you, my first solution was: list2-lapply(1:10,function(x) vec1) The more generic form should be: list2-lapply(1:length(list1),function(x) vec1) A.K. - Original Message - From: Spencer Maynes smayne...@gmail.com To: r-help@r-project.org Cc: Sent: Tuesday, July 3, 2012 12:47 PM Subject: Re: [R] Assigning a vector to every element of a list. Thanks guys for the help, I'm going to go with Patrick Burns answer because it seems to work the best for my situation, but these all seem like they should work. On Tue, Jul 3, 2012 at 2:51 AM, Patrick Burns pbu...@pburns.seanet.comwrote: b - rep(list(d), length(b)) On 02/07/2012 23:16, Spencer Maynes wrote: I have a vector d of unknown length, and a list b of unknown length. I would like to replace every element of b with d. Simply writing b-d does not work as R tries to fit every element of d to a different element of d, and b-rep(d,length(b)) does not work either as it makes a list of length length(d)*length(b) not a list of length(b). I know how to do this with a for loop, but I feel that there has to be a more efficient way. Any suggestions? [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/**blog http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] EM algorithm to find MLE of coeff in mixed effects model
I have a general question about coefficients estimation of the mixed model. I simulated a very basic model: Y|b=X*\beta+Z*b +\sigma^2* diag(ni); b follows N(0,\psi) #i.e. bivariate normal where b is the latent variable, Z and X are ni*2 design matrices, sigma is the error variance, Y are longitudinal data, i.e. there are ni measurements for object i. Parameters are \beta, \sigma, \psi; call them \theta. I wrote a regular EM, the M step is to maximize the log(f(Y,b;\theta)) by taking first order derivatives, setting to 0 and solving the equation. The E step involves the evaluation of E step, using Gauss Hermite approximation. (10 points) All are simulated data. X and Z are naive like cbind(rep(1,m),1:m) After 200 iterations, the estimated \beta converges to the true value while \sigma and \psi do not. Even after one iteration, the \sigma goes up from about 10^0 to about 10^1. I am confused since the \hat{\beta} requires \sigma and \psi from previous iteration. If something wrong then all estimations should be incorrect... Another question is that I calculated the logf(Y;\theta) to see if it increases after updating \theta. Seems decreasing. I thought the X and Z are linearly dependent would cause some issue but I also changed the X and Z to some random numbers from normal distribution. I also tried ECM, which converges fast but sigma and psi are not close to the desired values. Is this due to the limitation of some methods that I used? Can any one give some help? I am stuck for a week. I can send the code to you. First time to raise a question here. Not sure if it is proper to post all code. ## # the main R script n=100 beta=c(-0.5,1) vvar=2 #sigma^2=2 psi=matrix(c(1,0.2,0.2,1),2,2) b.m.true=mvrnorm(n=n,mu=c(0,0),Sigma=psi) #100*2 matrix, each row is the b_i Xi=cbind(rnorm(7,mean=2,sd=0.5),log(2:8)) #Xi=cbind(rep(1,7),1:7) y.m=matrix(NA,nrow=n,ncol=nrow(Xi)) #100*7, each row is a y_i Zi=Xi b.list=as.list(data.frame(t(b.m.true))) psi.old=matrix(c(0.5,0.4,0.4,0.5),2,2) #starting psi, beta and var, not exactly the same as the true value beta.old=c(-0.3,0.7) var.old=1.7 gausspar=gauss.quad(10,kind=hermite,alpha=0,beta=0) data.list.wob=list() for (i in 1:n) { data.list.wob[[i]]=list(Xi=Xi,yi=y.m[i,],Zi=Zi) } #compute true loglikelihood and initial loglikelihood truelog=0 for (i in 1:length(data.list.wob)) { truelog=truelog+loglike(data.list.wob[[i]],vvar,beta,psi) } truelog loglikeseq=c() loglikeseq[1]=sum(sapply(data.list.wob,loglike)) ECM=F for (m in 1:300) { Sig.hat=Zi%*%psi.old%*%t(Zi)+var.old*diag(nrow(Zi)) W.hat=psi.old-psi.old%*%t(Zi)%*%solve(Sig.hat)%*%Zi%*%psi.old Sigb=psi.old-psi.old%*%t(Zi)%*%solve(Sig.hat)%*%Zi%*%psi.old det(Sigb)^(-0.5) Y.minus.X.beta=t(t(y.m)-c(Xi%*%beta.old)) miu.m=t(apply(Y.minus.X.beta,MARGIN=1,function(s,B=psi.old%*%t(Zi)%*%solve(Sig.hat)) { B%*%s } )) ### each row is the miu_i tmp1=permutations(length(gausspar$nodes),2,repeats.allowed=T) tmp2=c(tmp1) a.mat=matrix(gausspar$nodes[tmp2],nrow=nrow(tmp1)) #a1,a1 #a1,a2 #... #a10,a9 #a10,a10 a.mat.list=as.list(data.frame(t(a.mat))) tmp1=permutations(length(gausspar$weights),2,repeats.allowed=T) tmp2=c(tmp1) weights.mat=matrix(gausspar$weights[tmp2],nrow=nrow(tmp1)) #w1,w1 #w1,w2 #... #w10,w9 #w10,w10 L=chol(solve(W.hat)) LL=sqrt(2)*solve(L) halfb=t(LL%*%t(a.mat)) # each page of b.array is all values of bi_k and bi_j for b_i b.list=list() for (i in 1:n) { b.list[[i]]=t(t(halfb)+miu.m[i,]) } #generate a list, each page contains Xi,yi,Zi, data.list=list() for (i in 1:n) { data.list[[i]]=list(Xi=Xi,yi=y.m[i,],Zi=Zi,b=b.list[[i]]) } #update sigma^2 t1=proc.time() tempaa=c() tempbb=c() for (j in 1:n) { #tempaa[j]=Eh4new(datai=data.list[[j]],weights.m=weights.mat,beta=beta.old) tempbb[j]=Eh4newv2(datai=data.list[[j]],weights.m=weights.mat,beta=beta.old) } var.new=mean(tempbb) if (ECM==T){var.old=var.new} sumXiXi=matrix(rowSums(sapply(data.list,function(s){t(s$Xi)%*%(s$Xi)})),ncol=ncol(Xi)) tempbb=c() for (j in 1:n) { tempbb=cbind(tempbb,Eh2new(data.list[[j]],weights.m=weights.mat)) } beta.new=solve(sumXiXi)%*%rowSums(tempbb) if (ECM==T){beta.old=beta.new} #update psi
[R] need help EM algorithm to find MLE of coeff in mixed effects model
Dear All, have a general question about coefficients estimation of the mixed model. I simulated a very basic model: Y|b=X*\beta+Z*b +\sigma^2* diag(ni); b follows N(0,\psi) #i.e. bivariate normal where b is the latent variable, Z and X are ni*2 design matrices, sigma is the error variance, Y are longitudinal data, i.e. there are ni measurements for object i. Parameters are \beta, \sigma, \psi; call them \theta. I wrote a regular EM, the M step is to maximize the log(f(Y,b;\theta)) by taking first order derivatives, setting to 0 and solving the equation. The E step involves the evaluation of E step, using Gauss Hermite approximation. (10 points) All are simulated data. X and Z are naive like cbind(rep(1,m),1:m) After 200 iterations, the estimated \beta converges to the true value while \sigma and \psi do not. Even after one iteration, the \sigma goes up from about 10^0 to about 10^1. I am confused since the \hat{\beta} requires \sigma and \psi from previous iteration. If something wrong then all estimations should be incorrect... Another question is that I calculated the logf(Y;\theta) to see if it increases after updating \theta. Seems decreasing. I thought the X and Z are linearly dependent would cause some issue but I also changed the X and Z to some random numbers from normal distribution. I also tried ECM, which converges fast but sigma and psi are not close to the desired values. Is this due to the limitation of some methods that I used? Can any one give some help? I am stuck for a week. I can send the code to you. First time to raise a question here. Not sure if it is proper to post all code. Below is the same as the zip file. ## # the main R script n=100 beta=c(-0.5,1) vvar=2 #sigma^2=2 psi=matrix(c(1,0.2,0.2,1),2,2) b.m.true=mvrnorm(n=n,mu=c(0,0),Sigma=psi) #100*2 matrix, each row is the b_i Xi=cbind(rnorm(7,mean=2,sd=0.5),log(2:8)) #Xi=cbind(rep(1,7),1:7) y.m=matrix(NA,nrow=n,ncol=nrow(Xi)) #100*7, each row is a y_i Zi=Xi b.list=as.list(data.frame(t(b.m.true))) psi.old=matrix(c(0.5,0.4,0.4,0.5),2,2) #starting psi, beta and var, not exactly the same as the true value beta.old=c(-0.3,0.7) var.old=1.7 gausspar=gauss.quad(10,kind=hermite,alpha=0,beta=0) data.list.wob=list() for (i in 1:n) { data.list.wob[[i]]=list(Xi=Xi,yi=y.m[i,],Zi=Zi) } #compute true loglikelihood and initial loglikelihood truelog=0 for (i in 1:length(data.list.wob)) { truelog=truelog+loglike(data.list.wob[[i]],vvar,beta,psi) } truelog loglikeseq=c() loglikeseq[1]=sum(sapply(data.list.wob,loglike)) ECM=F for (m in 1:300) { Sig.hat=Zi%*%psi.old%*%t(Zi)+var.old*diag(nrow(Zi)) W.hat=psi.old-psi.old%*%t(Zi)%*%solve(Sig.hat)%*%Zi%*%psi.old Sigb=psi.old-psi.old%*%t(Zi)%*%solve(Sig.hat)%*%Zi%*%psi.old det(Sigb)^(-0.5) Y.minus.X.beta=t(t(y.m)-c(Xi%*%beta.old)) miu.m=t(apply(Y.minus.X.beta,MARGIN=1,function(s,B=psi.old%*%t(Zi)%*%solve(Sig.hat)) { B%*%s } )) ### each row is the miu_i tmp1=permutations(length(gausspar$nodes),2,repeats.allowed=T) tmp2=c(tmp1) a.mat=matrix(gausspar$nodes[tmp2],nrow=nrow(tmp1)) #a1,a1 #a1,a2 #... #a10,a9 #a10,a10 a.mat.list=as.list(data.frame(t(a.mat))) tmp1=permutations(length(gausspar$weights),2,repeats.allowed=T) tmp2=c(tmp1) weights.mat=matrix(gausspar$weights[tmp2],nrow=nrow(tmp1)) #w1,w1 #w1,w2 #... #w10,w9 #w10,w10 L=chol(solve(W.hat)) LL=sqrt(2)*solve(L) halfb=t(LL%*%t(a.mat)) # each page of b.array is all values of bi_k and bi_j for b_i b.list=list() for (i in 1:n) { b.list[[i]]=t(t(halfb)+miu.m[i,]) } #generate a list, each page contains Xi,yi,Zi, data.list=list() for (i in 1:n) { data.list[[i]]=list(Xi=Xi,yi=y.m[i,],Zi=Zi,b=b.list[[i]]) } #update sigma^2 t1=proc.time() tempaa=c() tempbb=c() for (j in 1:n) { #tempaa[j]=Eh4new(datai=data.list[[j]],weights.m=weights.mat,beta=beta.old) tempbb[j]=Eh4newv2(datai=data.list[[j]],weights.m=weights.mat,beta=beta.old) } var.new=mean(tempbb) if (ECM==T){var.old=var.new} sumXiXi=matrix(rowSums(sapply(data.list,function(s){t(s$Xi)%*%(s$Xi)})),ncol=ncol(Xi)) tempbb=c() for (j in 1:n) { tempbb=cbind(tempbb,Eh2new(data.list[[j]],weights.m=weights.mat)) } beta.new=solve(sumXiXi)%*%rowSums(tempbb) if
Re: [R] VIM package - how to get the underlying code
On 02.07.2012 22:48, Mathias Worni wrote: Sorry for the misunderstanding. What I meant to say is that I cannot get the code for the specific graphic that I am running, so that I can keep my code reproducible. Which code for the graphics? To be reproducible, you need the R vesion, the package version and your source code (incluing a seed for random numbers), I do not see what else is required. Best, Uwe Ligges Thanks, Mathias On Mon, Jul 2, 2012 at 11:06 AM, Uwe Ligges lig...@statistik.tu-dortmund.de mailto:lig...@statistik.tu-dortmund.de wrote: On 01.07.2012 21:19, Mathias Worni wrote: Dear R-users, I am using R on a Mac using the latest version of R (2.15.1) working with R-studio. To perform multiple imputation for a dataset with some missing values, I am using the VIM package (http://goo.gl/rfGfr). While everything is working fine also with the GUI, I wonder if anybody knows how to get the code for the diagrams you can create using the GUI. Just download the source version of the VIM package and take a look into the code? Best, Uwe Ligges Thanks, Mathias [[alternative HTML version deleted]] R-help@r-project.org mailto:R-help@r-project.org mailing list https://stat.ethz.ch/mailman/__listinfo/r-help https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/__posting-guide.html 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] A challenging question: merging excel files under a specific pattern
Dear all, I have an excel file that contains 6 sheets 1,2,3,4,5,6 The analysis is repeated every 3 sheets Sheets 1, 2, 3: I want to add (horizontally) the data contained in the matrix : sheet2 (5:end,3:end ) of *Sheet2 * to the sheet3 such that the first element of the matrix *sheet2 (5:end,3:end ) * to occupy the location/cell sheet3(5,end+1 ) of sheet3. Say, that the output from this merging is sheetA. Then I want to add horizontally the data contained in the matrix : Sheet1 (5:end,3:end ) of Sheet 1 to the sheetA * such that the first element of the matrix *sheet1 (5: end,3:end ) to occupy the location/cell sheetA(5,end+1 ) of sheetA. As you can see 1)I add sheet2 (5:end,3:end ) * to *sheet3 at location *sheet3(5,end+1) * 2) then I add sheet1 (5:end,3:end ) to the output sheetA that results from the merging of sheets 2 and 3 at location sheetA((5,end+1). 3) The output is named ,say, sheetAA Similarly analysis holds for the other block of sheets 4,5,6. That is, Sheets 4, 5, 6: 1)I add sheet5 (5:end,3:end ) to sheet6 at location sheet6(5,end+1) 2) then I add sheet4 (5:end,3:end ) to the output sheetB that results from the merging of sheets 5 and 6 at location sheetB((5,end+1). 3) The output is named, say, sheetBB In my case I have a large sequence of sheets that I have to merge in this way. That is, 1,2,3,4,5,6,7,8,9,10,11,12,13,… But the logic is the same as described above. Is there any “easy” way to do that kind of merging? . Because when you have 13*3 =39 sheets is a bit tedious to do that merging manually. thanks __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Assigning a vector to every element of a list.
On 2012-07-03 09:47, Spencer Maynes wrote: Thanks guys for the help, I'm going to go with Patrick Burns answer because it seems to work the best for my situation, but these all seem like they should work. Patrick's solution is similar to Gabor's, but, personally, I favour Gabor's. Seems neatest and simplest to me. Peter Ehlers On Tue, Jul 3, 2012 at 2:51 AM, Patrick Burns pbu...@pburns.seanet.comwrote: b - rep(list(d), length(b)) On 02/07/2012 23:16, Spencer Maynes wrote: I have a vector d of unknown length, and a list b of unknown length. I would like to replace every element of b with d. Simply writing b-d does not work as R tries to fit every element of d to a different element of d, and b-rep(d,length(b)) does not work either as it makes a list of length length(d)*length(b) not a list of length(b). I know how to do this with a for loop, but I feel that there has to be a more efficient way. Any suggestions? [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/**blog http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Removing rows if certain elements are found in character string
Got it! Thank you Rui! cp On Tue, Jul 3, 2012 at 10:14 AM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, I'm glad it helped. See answer inline. Em 03-07-2012 17:09, Claudia Penaloza escreveu: Thank you Rui and Jim, both 'i1' and 'i1new' worked perfectly because there are no instances of 'Dd' or 'dD' in the data set (that I would/not want to include/exclude)... but I understand that 'i1new' targets precisely what I want. Why isn't a leader of zero's required for either 'i1' or 'i1new', as so? i1newer - grepl(^0{0,}[D]*$|^0{0,}[d]*$**, dd$ch) Because both 'i1' and 'i1new' test from beginning to end of string, allowing only '0' and either 'd' or 'D', but not both (i1new). So, there's no need to explicitly test for a string that begins with '0'. Rui Barradas Thank you again, Claudia On Tue, Jul 3, 2012 at 2:06 AM, Rui Barradas ruipbarra...@sapo.pt mailto:ruipbarra...@sapo.pt wrote: Hello, Inline. Em 03-07-2012 01:15, jim holtman escreveu: You will have to change the 'i1' expression as follows: i1 - grepl(^([0D]|[0d])*$, dd$ch) i1 # matches strings with d D in them [1] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE # second string had 'd' 'D' in it so it was TRUE above and FALSE below i1new - grepl(^([0D]*$|[0d]*$), dd$ch) i1new [1] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE Right, apparently, I forgot that grep is greedy, and the test cases were not complete. I put a 'd' and 'D' in the second string and the original regular expression is equivalent to grepl(^[0dD]*$, dd$ch) This is only for the first request, and does not solve cases where there are characters other than '0', 'd' or 'D', but 'd' or 'D' are the first non-zero. This is the case of my 4th row, changed from the OP's data example. My regexpr for 'i2' is equivalent to this one, that I believe is more readable: i2b - grepl(^0{0,}[Dd], dd$ch) First a zero, that might occur zero or more times, then a 'd' or 'D', then and til the end, irrelevant. which will match strings containing d, D and 0. If you only want 'd' or 'D' (and not both), then you will have to use the one in 'i1new'. To the OP: bottom line, use Jim's 'i1new' and my 'i2' or 'i2b'. Rui Barradas On Mon, Jul 2, 2012 at 7:24 PM, Rui Barradas ruipbarra...@sapo.pt mailto:ruipbarra...@sapo.pt wrote: Hello, Try regular expressions instead. In this data.frame, I've changed row nr.4 to have a row with 'D' as first non-zero character. dd - read.table(text= ch count 1 00D000**__00 0.007368 2 00d000**__00 0.002456 3 0T**__00 0.007368 4 0DT000**__00 0.007368 5 0T**__00 0.002456 6 0Td000**__00 0.002456 7 T0**__00 0.007368 8 T0D000**__00 0.007368 9 T0**__00 0.002456 10 T0d000**__00 0.002456 , header=TRUE) dd i1 - grepl(^([0D]|[0d])*$, dd$ch) i2 - grepl(^0*[Dd], dd$ch) dd[!i1, ] dd[!i2, ] dd[!(i1 | i2), ] Hope this helps, Rui Barradas Em 02-07-2012 23:48, Claudia Penaloza escreveu: I would like to remove rows from the following data frame (df) if there are only two specific elements found in the df$ch character string (I want to remove rows with only 0 D or 0 d). Alternatively, I would like to remove rows if the first non-zero element is D or d. ch count 1 00D000**__00 0.007368; 2 00d000**__00 0.002456; 3 0T**__00 0.007368; 4 0TD000**__00 0.007368; 5 0T**__00
[R] remove loop which compares row i to row i-1
I would like to remove a loop to speed up my code. I want to remove a loop which references the last row. In general I want to a remove a loop which looks something like this: for 2 to number of rows in a matrix do{ if indextrow-1 is currentIndexRow then do something. } My R code: for (i in 2:length(tUnitsort$Hour)){ ifelse(tUnitsort[i,4]=tUnitsort[i-1,4],(tempMC =tUnitsort[i,7]),tempMC ) #col. 4 = BlockNumber; note tests to see if the offers have change to the next set of blocks. ifelse(tUnitsort[i,4]=tUnitsort[i-1,4],(tempAC =tUnitsort[i,7]-(tUnitsort[i,8]-tUnitsort[i,9])),tempAC ) tUnitsort$MC[i] - tempMC tUnitsort$AC[i] - tempAC tUnitsort$PercentofMC[i] - tUnitsort$Size[i]/tempMC tUnitsort$PercentofAC[i] - tUnitsort$AvailableMW[i]/tempAC } -- View this message in context: http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327.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] evaluating expressions contained in a dataframe
Hello, I've changed the way you create data.frame 'tests', like it was the conditions were factors, which are coded as integers. We need them of class character to be parsed. Note that the same could be done with the data.frame 'info' but it's not absolutely needed. tests - data.frame(rule=c(info$country == 'Greenland', info$age 50), stringsAsFactors=FALSE) str(tests) tests #-- This does the trick fun - function(x){ f - function(){} # an empty function, no body force(x) # evaluate the argument body(f) - parse(text=x) # parse it and assign the function f # return the function } # See if it works expr1 - fun(tests$rule[1]) # This creates a function expr1() Hope this helps, Rui Barradas Em 03-07-2012 17:24, New RUser escreveu: #I have a dataframe called tests that contain character expressions. These characters are rules that use data from within another dataframe. Is there any way within R I can access the rules in the dataframe called tests, and then evaluate these rules? #An example may better explain what I am trying to accomplish: tests - data.frame(matrix(data=c(info$country == 'Greenland', info$age 50), nrow=2, ncol=1)) names(tests) - rule info - data.frame(matrix(data=NA, nrow=5, ncol=3)) names(info) - c(first, country, age) info$name - c(Mary, Paul, Robert, John, Ivan) info$country - c(GReenland, Iceland, Ireland, Greenland, Greenland) info$age - c(30, 55, 66, 79, 80) #e.g. for info$country == 'Greenland' #I want: info$country == 'Greenland' [1] FALSE FALSE FALSE TRUE TRUE #e.g. info$country == 'Greenland' info$age 50 #I tried this, but it does not work: eval(tests$rule[1]) [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Assigning a vector to every element of a list.
I've taken another look, and I actually think you're right. I'm going with Gabor's. Changing d to a list is such a simple solution that I can't believe I didn't try it earlier. On Tue, Jul 3, 2012 at 1:39 PM, Peter Ehlers ehl...@ucalgary.ca wrote: On 2012-07-03 09:47, Spencer Maynes wrote: Thanks guys for the help, I'm going to go with Patrick Burns answer because it seems to work the best for my situation, but these all seem like they should work. Patrick's solution is similar to Gabor's, but, personally, I favour Gabor's. Seems neatest and simplest to me. Peter Ehlers On Tue, Jul 3, 2012 at 2:51 AM, Patrick Burns pbu...@pburns.seanet.com* *wrote: b - rep(list(d), length(b)) On 02/07/2012 23:16, Spencer Maynes wrote: I have a vector d of unknown length, and a list b of unknown length. I would like to replace every element of b with d. Simply writing b-d does not work as R tries to fit every element of d to a different element of d, and b-rep(d,length(b)) does not work either as it makes a list of length length(d)*length(b) not a list of length(b). I know how to do this with a for loop, but I feel that there has to be a more efficient way. Any suggestions? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-helphttps://stat.ethz.ch/mailman/**listinfo/r-help https://stat.**ethz.ch/mailman/listinfo/r-**helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/**posting-guide.htmlhttp://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/bloghttp://www.portfolioprobe.com/**blog http://www.portfolioprobe.**com/bloghttp://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html 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] remove loop which compares row i to row i-1
Vectorize vectorize vectorize! if(x[-length(x)] x[-1]) {...} (where x is the whole vector of entries) Bill Dunlap has posted some elegant code within the last month or 2 aimed at this sort of thing, so search on his posts in the archive. -- Bert On Tue, Jul 3, 2012 at 12:10 PM, jcrosbie ja...@crosb.ie wrote: I would like to remove a loop to speed up my code. I want to remove a loop which references the last row. In general I want to a remove a loop which looks something like this: for 2 to number of rows in a matrix do{ if indextrow-1 is currentIndexRow then do something. } My R code: for (i in 2:length(tUnitsort$Hour)){ ifelse(tUnitsort[i,4]=tUnitsort[i-1,4],(tempMC =tUnitsort[i,7]),tempMC ) #col. 4 = BlockNumber; note tests to see if the offers have change to the next set of blocks. ifelse(tUnitsort[i,4]=tUnitsort[i-1,4],(tempAC =tUnitsort[i,7]-(tUnitsort[i,8]-tUnitsort[i,9])),tempAC ) tUnitsort$MC[i] - tempMC tUnitsort$AC[i] - tempAC tUnitsort$PercentofMC[i] - tUnitsort$Size[i]/tempMC tUnitsort$PercentofAC[i] - tUnitsort$AvailableMW[i]/tempAC } -- View this message in context: http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327.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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[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] remove loop which compares row i to row i-1
Of course it _should_ be: ifelse(x[-length(x)] x[-1], ...,...) Sorry... -- Bert On Tue, Jul 3, 2012 at 1:00 PM, Bert Gunter bgun...@gene.com wrote: Vectorize vectorize vectorize! if(x[-length(x)] x[-1]) {...} (where x is the whole vector of entries) Bill Dunlap has posted some elegant code within the last month or 2 aimed at this sort of thing, so search on his posts in the archive. -- Bert On Tue, Jul 3, 2012 at 12:10 PM, jcrosbie ja...@crosb.ie wrote: I would like to remove a loop to speed up my code. I want to remove a loop which references the last row. In general I want to a remove a loop which looks something like this: for 2 to number of rows in a matrix do{ if indextrow-1 is currentIndexRow then do something. } My R code: for (i in 2:length(tUnitsort$Hour)){ ifelse(tUnitsort[i,4]=tUnitsort[i-1,4],(tempMC =tUnitsort[i,7]),tempMC ) #col. 4 = BlockNumber; note tests to see if the offers have change to the next set of blocks. ifelse(tUnitsort[i,4]=tUnitsort[i-1,4],(tempAC =tUnitsort[i,7]-(tUnitsort[i,8]-tUnitsort[i,9])),tempAC ) tUnitsort$MC[i] - tempMC tUnitsort$AC[i] - tempAC tUnitsort$PercentofMC[i] - tUnitsort$Size[i]/tempMC tUnitsort$PercentofAC[i] - tUnitsort$AvailableMW[i]/tempAC } -- View this message in context: http://r.789695.n4.nabble.com/remove-loop-which-compares-row-i-to-row-i-1-tp4635327.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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[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] design matrix creation in R
Hello, I want to create a design matrix using R. Can you explain the code which creates the following please? I understand the first part. b=g1(?) does what? dd - data.frame(a = gl(3,4), b = gl(4,1,12)) # balanced 2-way dd a b 1 1 1 2 1 2 3 1 3 4 1 4 5 2 1 6 2 2 7 2 3 8 2 4 9 3 1 10 3 2 11 3 3 12 3 4 I am using the tree dataset in R. I want to form a reparameterized design matrix in ones, zeroes and minus ones. The dataframe dd is very important here. Can anyone assist here? Thanks in advance. Mary A. Marion [[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] design matrix creation in R
1. You need to learn to use R Help. It is there for a purpose. help (help) ## or ?help is where to start. 2. Before posting further, please read An Introduction to R. Ships with every distro. 3. ?gl 4. This is not the best way to do this anyway. dd - expand.grid(b=1:4,a=1:3) ## is preferable imho 5. A matrix is not a data frame. If you do not understand the difference between these 2 structures and do not wish to spend the time to learn, please stop using R. 6. You cannot reparametrize in terms of 1's 0's and -1's. b has 4 levels. Cheers, Bert On Tue, Jul 3, 2012 at 1:10 PM, mms...@comcast.net wrote: Hello, I want to create a design matrix using R. Can you explain the code which creates the following please? I understand the first part. b=g1(?) does what? dd - data.frame(a = gl(3,4), b = gl(4,1,12)) # balanced 2-way dd a b 1 1 1 2 1 2 3 1 3 4 1 4 5 2 1 6 2 2 7 2 3 8 2 4 9 3 1 10 3 2 11 3 3 12 3 4 I am using the tree dataset in R. I want to form a reparameterized design matrix in ones, zeroes and minus ones. The dataframe dd is very important here. Can anyone assist here? Thanks in advance. Mary A. Marion [[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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[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] design matrix creation in R
On Jul 3, 2012, at 4:10 PM, mms...@comcast.net wrote: Hello, I want to create a design matrix using R. Can you explain the code which creates the following please? I understand the first part. What first part if the next part is ... this ? b=g1(?) does what? That's just generating a factor variable. dd - data.frame(a = gl(3,4), b = gl(4,1,12)) # balanced 2-way dd a b 1 1 1 2 1 2 3 1 3 4 1 4 5 2 1 6 2 2 7 2 3 8 2 4 9 3 1 10 3 2 11 3 3 12 3 4 I am using the tree dataset in R. I want to form a reparameterized design matrix in ones, zeroes and minus ones. The dataframe dd is very important here. Can anyone assist here? Thanks in advance. Something like this? model.matrix(~a+b, data= dd, contrasts = list(a=contr.sum, b=contr.sum) ) (Intercept) a1 a2 b1 b2 b3 11 1 0 1 0 0 21 1 0 0 1 0 31 1 0 0 0 1 41 1 0 -1 -1 -1 51 0 1 1 0 0 61 0 1 0 1 0 71 0 1 0 0 1 81 0 1 -1 -1 -1 91 -1 -1 1 0 0 10 1 -1 -1 0 1 0 11 1 -1 -1 0 0 1 12 1 -1 -1 -1 -1 -1 attr(,assign) [1] 0 1 1 2 2 2 attr(,contrasts) attr(,contrasts)$a [1] contr.sum attr(,contrasts)$b [1] contr.sum -- David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] design matrix creation in R
On 03/07/2012 4:10 PM, mms...@comcast.net wrote: Hello, I want to create a design matrix using R. Can you explain the code which creates the following please? I understand the first part. b=g1(?) does what? That is gl, not g1 (i.e. gee ell not gee one).See ?gl for a description. Duncan Murdoch dd - data.frame(a = gl(3,4), b = gl(4,1,12)) # balanced 2-way dd a b 1 1 1 2 1 2 3 1 3 4 1 4 5 2 1 6 2 2 7 2 3 8 2 4 9 3 1 10 3 2 11 3 3 12 3 4 I am using the tree dataset in R. I want to form a reparameterized design matrix in ones, zeroes and minus ones. The dataframe dd is very important here. Can anyone assist here? Thanks in advance. Mary A. Marion [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] subset data based on values in multiple columns
Hi, Try this: data1-data.frame(date,time,count) dat1-data1[with(data1,rev(order(count))),] data2-subset(dat1,rle(dat1$count)$lengths==1) dat3-aggregate(data2$count,list(data2$date),max) colnames(dat3)-c(date,count) data4-merge(dat3,data2) data4-data4[,c(1,3,2)] data4 date time count 1 2012-01-25 2012-07-03 13:40:00 14 2 2012-01-26 2012-07-03 14:00:00 11 3 2012-01-27 2012-07-03 10:00:00 12 A.K. - Original Message - From: Chandra Salgado Kent c.salg...@cmst.curtin.edu.au To: r-help@r-project.org r-help@r-project.org Cc: Sent: Tuesday, July 3, 2012 2:55 AM Subject: [R] subset data based on values in multiple columns Dear list members, I am trying to create a subset of a data frame based on conditions in two columns, and after spending much time trying (and search R-help) have not had any luck. Essentially, I have a data frame that is something like this: date-as.POSIXct(as.character(c(2012-01-25,2012-01-25,2012-01-26,2012-01-27,2012-01-27,2012-01-27))) time-as.POSIXct(as.character(c(13:20, 13:40, 14:00, 10:00, 10:20, 10:20)), format=%H:%M) count-c(12,14,11,12,12,8) data-data.frame(date,time,count) which looks like: date time count 1 2012-01-25 13:20:00 12 2 2012-01-25 13:40:00 14 3 2012-01-26 14:00:00 11 4 2012-01-27 10:00:00 12 5 2012-01-27 10:20:00 12 6 2012-01-27 10:20:00 8 I would like to create a subset by doing the following: for each unique date, only include one case which will be the case with the max value for the column labelled count. So the resulting subset would be: date time count 2 2012-01-25 13:40:00 14 3 2012-01-26 14:00:00 11 4 2012-01-27 10:00:00 12 Some dates have two cases at which the count was the same, but I only want to include one case (I don't really mind which case it chooses, but if need be it could be based on the earliest time for which the same counts occurred). I have tried various loops with no success! I'm sure that there is an easy answer that I have not found! Any help is much appreciated!! All the best, Chandra [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] help with read.table.ffdf parameters
Marck, A little late, but perhaps this will help someone in the future. I am guessing that some of your integer fields contain scientific notation, and for some reason read.table is not interpreting those as integers. Consider changing the affected column classes from integer to numeric and I bet you'll see better results. Regards, Matt -- View this message in context: http://r.789695.n4.nabble.com/help-with-read-table-ffdf-parameters-tp3223805p4635337.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] design matrix creation in R
If David's response is what you were seeking, I would then ask: homework?? In general, explicit model matrices are not needed for linear modeling in R. Still not clear what you meant by reparametrization, but if David's interpretation is correct, my impossible comment is clearly wrong. -- Bert On Tue, Jul 3, 2012 at 1:30 PM, Bert Gunter bgun...@gene.com wrote: 1. You need to learn to use R Help. It is there for a purpose. help (help) ## or ?help is where to start. 2. Before posting further, please read An Introduction to R. Ships with every distro. 3. ?gl 4. This is not the best way to do this anyway. dd - expand.grid(b=1:4,a=1:3) ## is preferable imho 5. A matrix is not a data frame. If you do not understand the difference between these 2 structures and do not wish to spend the time to learn, please stop using R. 6. You cannot reparametrize in terms of 1's 0's and -1's. b has 4 levels. Cheers, Bert On Tue, Jul 3, 2012 at 1:10 PM, mms...@comcast.net wrote: Hello, I want to create a design matrix using R. Can you explain the code which creates the following please? I understand the first part. b=g1(?) does what? dd - data.frame(a = gl(3,4), b = gl(4,1,12)) # balanced 2-way dd a b 1 1 1 2 1 2 3 1 3 4 1 4 5 2 1 6 2 2 7 2 3 8 2 4 9 3 1 10 3 2 11 3 3 12 3 4 I am using the tree dataset in R. I want to form a reparameterized design matrix in ones, zeroes and minus ones. The dataframe dd is very important here. Can anyone assist here? Thanks in advance. Mary A. Marion [[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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[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] Data manipulation with aggregate
try this: myData = data.frame(Name = c('a', 'a', 'b', 'b'), length = c(1,2,3,4), type + = c('x','x','y','z')) result - do.call(rbind, lapply(split(myData, myData$Name), function(.name){ + data.frame(Name = .name$Name[1L] + , length = mean(.name$length) + , type = if (all(.name$type[1L] == .name$type)) .name$type[1L] else NA + ) + }) + ) result Name length type aa1.5x bb3.5 NA On Tue, Jul 3, 2012 at 12:04 PM, Filoche pmassico...@hotmail.com wrote: Hi everyone. I have these data : myData = data.frame(Name = c('a', 'a', 'b', 'b'), length = c(1,2,3,4), type = c('x','x','y','z')) which gives me: Name length type 1 a 1 x 2 a 2 x 3 b 3 y 4 b 4 z I would group (mean) this DF using 'Name' as grouping factor. However, I have a field ('type') which is a string. I would like to use the unique value of this field when possible (i.e. when all the 'type' values are the same for each group) or replace with NA when 'type' has multiple values. In fact, I would like to obtain this: Name length type 1 a 1.5 x 2 b 3.5 NA For instance, I was using this command: aggregate(list(myData$length, myData$type), list(myData$Name), FUN = mean) But it can't deal with string data. I hope I have been clear enough. With regards, Phil -- View this message in context: http://r.789695.n4.nabble.com/Data-manipulation-with-aggregate-tp4635298.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. -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. __ 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] EM algorithm to find MLE of coeff in mixed effects model
On Tue, Jul 3, 2012 at 12:41 PM, jimmycloud jimmycl...@gmail.com wrote: I have a general question about coefficients estimation of the mixed model. I have 2 ideas for you. 1. Fit with lme4 package, using the lmer function. That's what it is for. 2. If you really want to write your own EM algorithm, I don't feel sure that very many R and EM experts are going to want to read through the code you have because you don't follow some of the minimal readability guidelines. I accept the fact that there is no officially mandated R style guide, except for indent with 4 spaces, not tabs. But there are some almost universally accepted basics like 1. Use -, not =, for assignment 2. put a space before and after symbols like -, = , + , * , and so forth. 3. I'd suggest you get used to putting squiggly braces in the KR style. I have found the formatR package's tool tidy.source can do this nicely. From tidy.source, here's what I get with your code http://pj.freefaculty.org/R/em2.R Much more readable! (I inserted library(MASS) for you at the top, otherwise this doesn't even survive the syntax check.) When I copy from email to Emacs, some line-breaking monkey-business occurs, but I expect you get the idea. Now, looking at your cleaned up code, I can see some things to tidy up to improve the chances that some expert will look. First, R functions don't require return at the end, many experts consider it distracting. (Run lm or such and review how they write. No return at the end of functions). Second, about that big for loop at the top, the one that goes from m 1:300 I don't know what that loop is doing, but there's some code in it that I'm suspicious of. For example, this part: W.hat - psi.old - psi.old %*% t(Zi) %*% solve(Sig.hat) %*% Zi %*% psi.old Sigb - psi.old - psi.old %*% t(Zi) %*% solve(Sig.hat) %*% Zi %*% psi.old First, you've caused the possibly slow calculation of solve (Sig.hat) to run two times. If you really need it, run it once and save the result. Second, a for loop is not necessarily slow, but it may be easier to read if you re-write this: for (j in 1:n) { tempaa[j]=Eh4new(datai=data.list[[j]],weights.m=weights.mat,beta=beta.old) tempbb[j] - Eh4newv2(datai = data.list[[j]], weights.m = weights.mat, beta = beta.old) } like this: tempaa - lapply(data.list, Eh4new, weights.m, beta.old) tempbb - lapply(data.list, Eh4newv2, weights.m, beta.old) Third, here is a no-no tempbb - c() for (j in 1:n) { tempbb - cbind(tempbb, Eh2new(data.list[[j]], weights.m = weights.mat)) } That will call cbind over and over, causing a relatively slow memory re-allocation. See (http://pj.freefaculty.org/R/WorkingExamples/stackListItems.R) Instead, do this to apply Eh2new to each item in data.list tempbbtemp - lapply(data.list, Eh2new, weights.mat) and then smash the results together in one go tempbb - do.call(cbind, tempbbtemp) Fourth, I'm not sure on the matrix algebra. Are you sure you need solve to get the full inverse of Sig.hat? Once you start checking into how estimates are calculated in R, you find that the paper-and-pencil matrix algebra style of formula is generally frowned upon. OLS (in lm.fit) doesn't do solve(X'X), it uses a QR decomposition of matrices. OR look in MASS package ridge regression code, where the SVD is used to get the inverse. I wish I knew enough about the EM algorithm to gaze at your code and say aha, error in line 332, but I'm not. But if you clean up the presentation and tighten up the most obvious things, you improve your chances that somebody who is an expert will exert him/herself to do it. pj b follows N(0,\psi) #i.e. bivariate normal where b is the latent variable, Z and X are ni*2 design matrices, sigma is the error variance, Y are longitudinal data, i.e. there are ni measurements for object i. Parameters are \beta, \sigma, \psi; call them \theta. I wrote a regular EM, the M step is to maximize the log(f(Y,b;\theta)) by taking first order derivatives, setting to 0 and solving the equation. The E step involves the evaluation of E step, using Gauss Hermite approximation. (10 points) All are simulated data. X and Z are naive like cbind(rep(1,m),1:m) After 200 iterations, the estimated \beta converges to the true value while \sigma and \psi do not. Even after one iteration, the \sigma goes up from about 10^0 to about 10^1. I am confused since the \hat{\beta} requires \sigma and \psi from previous iteration. If something wrong then all estimations should be incorrect... Another question is that I calculated the logf(Y;\theta) to see if it increases after updating \theta. Seems decreasing. I thought the X and Z are linearly dependent would cause some issue but I also changed the X and Z to some random numbers from normal distribution. I also tried ECM, which converges fast but sigma and psi are not close to the desired values. Is this due
[R] Help! Please recommend good books/resources on visualizing data and understanding multivariate relations...
Hi all, Could you please help me? I am looking for books/pointers/resources/tutorials on visualizing complex/big data and on understanding multivariate relations in complicated data. More specifically, we have categorical variables and are interested in how to visualize the categorical data and visualize data conditioned upon categorical values. Could anybody please give me some pointers? Thanks a lot! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help! Please recommend good books/resources on visualizing data and understanding multivariate relations...
On Tue, 3 Jul 2012, Michael wrote: I am looking for books/pointers/resources/tutorials on visualizing complex/big data and on understanding multivariate relations in complicated data. Michael, You need to become familiar with the works of Edward Tufte, the dean of complex data visualization www.edwardtufte.com/. Edward Rolf Tufte is an American statistician and professor emeritus of political science, statistics, and computer science at Yale University. He is noted for his writings on information design and as a pioneer in the field of data visualization. -- Wikipedia Happy reading! Rich -- Richard B. Shepard, Ph.D. | Integrity - Credibility - Innovation Applied Ecosystem Services, Inc. |Helping Ensure Our Clients' Futures http://www.appl-ecosys.com Voice: 503-667-4517 Fax: 503-667-8863 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help! Please recommend good books/resources on visualizing data and understanding multivariate relations...
I found this [1] book interesting. About big data It really depends from a number of things... if can help, I know hdf5 work pretty Well with huge dataset . [1] http://www.ggobi.org/book/index.html On Jul 3, 2012 7:14 PM, Michael comtech@gmail.com wrote: Hi all, Could you please help me? I am looking for books/pointers/resources/tutorials on visualizing complex/big data and on understanding multivariate relations in complicated data. More specifically, we have categorical variables and are interested in how to visualize the categorical data and visualize data conditioned upon categorical values. Could anybody please give me some pointers? Thanks a lot! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Is it possible to remove this loop? [SEC=UNCLASSIFIED]
Thank you all for providing various alternatives. They are all pretty fast. Great help! Based on a test of a dataset with 800,000 rows, the time used varies from 0.04 to 11.56 s. The champion is: a1$h2 - 0 a1$h2[a1$h1==H] - 1 Regards, Jin Geoscience Australia Disclaimer: This e-mail (and files transmitted with it) is intended only for the person or entity to which it is addressed. If you are not the intended recipient, then you have received this e-mail by mistake and any use, dissemination, forwarding, printing or copying of this e-mail and its file attachments is prohibited. The security of emails transmitted cannot be guaranteed; by forwarding or replying to this email, you acknowledge and accept these risks. __ 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] Is it possible to remove this loop? [SEC=UNCLASSIFIED]
On 2012-07-03 17:23, jin...@ga.gov.au wrote: Thank you all for providing various alternatives. They are all pretty fast. Great help! Based on a test of a dataset with 800,000 rows, the time used varies from 0.04 to 11.56 s. The champion is: a1$h2 - 0 a1$h2[a1$h1==H] - 1 Interesting. My testing shows that Petr's solution is about twice as fast. Not that it matters much - the time is pretty small in any case. a0 - data.frame(h1 = sample(c(H,J,K), 1e7, replace = TRUE), stringsAsFactors = FALSE) a1 - a0 system.time({a1$h2 - 0; a1$h2[a1$h1 == H] - 1}) # user system elapsed # 1.470.481.96 a11 - a1 a1 - a0 system.time(a1$h2 - (a1$h1 == H) * 1) # user system elapsed # 0.370.170.56 a12 - a1 all.equal(a11,a12) #[1] TRUE Peter Ehlers Regards, Jin Geoscience Australia Disclaimer: This e-mail (and files transmitted with it) is intended only for the person or entity to which it is addressed. If you are not the intended recipient, then you have received this e-mail by mistake and any use, dissemination, forwarding, printing or copying of this e-mail and its file attachments is prohibited. The security of emails transmitted cannot be guaranteed; by forwarding or replying to this email, you acknowledge and accept these risks. __ 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] Is it possible to remove this loop? [SEC=UNCLASSIFIED]
Le 04/07/2012 12:43, Peter Ehlers a écrit : On 2012-07-03 17:23, jin...@ga.gov.au wrote: Thank you all for providing various alternatives. They are all pretty fast. Great help! Based on a test of a dataset with 800,000 rows, the time used varies from 0.04 to 11.56 s. The champion is: a1$h2 - 0 a1$h2[a1$h1==H] - 1 Interesting. My testing shows that Petr's solution is about twice as fast. Not that it matters much - the time is pretty small in any case. a0 - data.frame(h1 = sample(c(H,J,K), 1e7, replace = TRUE), stringsAsFactors = FALSE) a1 - a0 system.time({a1$h2 - 0; a1$h2[a1$h1 == H] - 1}) # user system elapsed # 1.470.481.96 a11 - a1 a1 - a0 system.time(a1$h2 - (a1$h1 == H) * 1) # user system elapsed # 0.370.170.56 a12 - a1 all.equal(a11,a12) #[1] TRUE Peter Ehlers I got the same result. Petr's solution is the fastest. Good to know it. Pascal Oettli Regards, Jin Geoscience Australia Disclaimer: This e-mail (and files transmitted with it) is intended only for the person or entity to which it is addressed. If you are not the intended recipient, then you have received this e-mail by mistake and any use, dissemination, forwarding, printing or copying of this e-mail and its file attachments is prohibited. The security of emails transmitted cannot be guaranteed; by forwarding or replying to this email, you acknowledge and accept these risks. __ 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] Date
Hi I have monthly data and the dates are in MM/YY Format I need to convert them into DD/MM/YY format by pasting 01 in place of DD to all the observations in my Year Column ex: YearStock Prices 01/2000 1 02/2000 2 03/2000 3 I need to convert them to Year Stock Prices 01/01/2000 1 01/02/2000 2 01/03/2000 3 [[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] Is it possible to remove this loop? [SEC=UNCLASSIFIED]
Thanks for your validation. Yes Peter's solution is the fastest, faster than the previous one by saving 25% time. It was missed out in my previous testing. Jin -Original Message- From: Pascal Oettli [mailto:kri...@ymail.com] Sent: Wednesday, 4 July 2012 2:07 PM To: Li Jin Cc: r-help@r-project.org Subject: Re: [R] Is it possible to remove this loop? [SEC=UNCLASSIFIED] Le 04/07/2012 12:43, Peter Ehlers a écrit : On 2012-07-03 17:23, jin...@ga.gov.au wrote: Thank you all for providing various alternatives. They are all pretty fast. Great help! Based on a test of a dataset with 800,000 rows, the time used varies from 0.04 to 11.56 s. The champion is: a1$h2 - 0 a1$h2[a1$h1==H] - 1 Interesting. My testing shows that Petr's solution is about twice as fast. Not that it matters much - the time is pretty small in any case. a0 - data.frame(h1 = sample(c(H,J,K), 1e7, replace = TRUE), stringsAsFactors = FALSE) a1 - a0 system.time({a1$h2 - 0; a1$h2[a1$h1 == H] - 1}) # user system elapsed # 1.470.481.96 a11 - a1 a1 - a0 system.time(a1$h2 - (a1$h1 == H) * 1) # user system elapsed # 0.370.170.56 a12 - a1 all.equal(a11,a12) #[1] TRUE Peter Ehlers I got the same result. Petr's solution is the fastest. Good to know it. Pascal Oettli Regards, Jin Geoscience Australia Disclaimer: This e-mail (and files transmitted with it) is intended only for the person or entity to which it is addressed. If you are not the intended recipient, then you have received this e-mail by mistake and any use, dissemination, forwarding, printing or copying of this e-mail and its file attachments is prohibited. The security of emails transmitted cannot be guaranteed; by forwarding or replying to this email, you acknowledge and accept these risks. __ 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. Geoscience Australia Disclaimer: This e-mail (and files transmitted with it) is intended only for the person or entity to which it is addressed. If you are not the intended recipient, then you have received this e-mail by mistake and any use, dissemination, forwarding, printing or copying of this e-mail and its file attachments is prohibited. The security of emails transmitted cannot be guaranteed; by forwarding or replying to this email, you acknowledge and accept these risks. __ 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] Date
?paste Please (re-) read the Introduction to R document supplied with the software for faster answers. Also, please read the Posting Guide mentioned at the bottom of every message on this list. In particular, providing data in raw tabular form is often ambiguous, and the use of the dput function to prepare data samples for questions will allow readers to more quickly identify difficulties you may be having. --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Akhil dua akhil.dua...@gmail.com wrote: Hi I have monthly data and the dates are in MM/YY Format I need to convert them into DD/MM/YY format by pasting 01 in place of DD to all the observations in my Year Column ex: YearStock Prices 01/2000 1 02/2000 2 03/2000 3 I need to convert them to Year Stock Prices 01/01/2000 1 01/02/2000 2 01/03/2000 3 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] (no subject)
Hi everyone I have data on stock prices and market indices and I need to run a seperate regression of every stock on market so I want to write a for loop so that I wont have to write codes again and again to run the regression... my data is in the format given below Date Stock1 Stock2 Stock3Market 01/01/2000 1 2 3 4 01/02/2000 5 6 7 8 01/03/2000 1 2 3 4 01/04/2000 5 6 7 8 So can any one help me how to write this loop [[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] loop for regression
-- Forwarded message -- From: Akhil dua akhil.dua...@gmail.com Date: Wed, Jul 4, 2012 at 10:33 AM Subject: To: r-help@r-project.org Hi everyone I have data on stock prices and market indices and I need to run a seperate regression of every stock on market so I want to write a for loop so that I wont have to write codes again and again to run the regression... my data is in the format given below Date Stock1 Stock2 Stock3Market 01/01/2000 1 2 3 4 01/02/2000 5 6 7 8 01/03/2000 1 2 3 4 01/04/2000 5 6 7 8 So can any one help me how to write this loop [[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] loop for regression
Homework? (We don't do homework here). -- Bert On Tue, Jul 3, 2012 at 10:08 PM, Akhil dua akhil.dua...@gmail.com wrote: -- Forwarded message -- From: Akhil dua akhil.dua...@gmail.com Date: Wed, Jul 4, 2012 at 10:33 AM Subject: To: r-help@r-project.org Hi everyone I have data on stock prices and market indices and I need to run a seperate regression of every stock on market so I want to write a for loop so that I wont have to write codes again and again to run the regression... my data is in the format given below Date Stock1 Stock2 Stock3Market 01/01/2000 1 2 3 4 01/02/2000 5 6 7 8 01/03/2000 1 2 3 4 01/04/2000 5 6 7 8 So can any one help me how to write this loop [[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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[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] How to use Sys.time() while writing a csv file name
Dear R helpers, I am using Beta distribution to generate the random no.s (recovery rates in my example). However, each time I need to save these random no.s in a csv format. To distinguish different csv files, one way I thought was use of Sys.time in the file name. My code is as follows - # My code rr = rbeta(25, 6.14, 8.12) lgd = 1 - mean(rr) write.csv(data.frame(recovery_rates = rr), file = paste(recovery_rates_at_, Sys.time(), .csv, sep = ), row.names = FALSE) However, I get following error - Error in file(file, ifelse(append, a, w)) : cannot open the connection In addition: Warning message: In file(file, ifelse(append, a, w)) : cannot open file 'recovery_rates_at_2012-07-04 1:14:05.csv': Invalid argument If instead of Sys.time, I use some other variable e.g. lgd as write.csv(data.frame(recovery_rates = rr), paste('rates_',lgd,'.csv', sep = ), row.names = FALSE) I am able to store these simulated recovery rates in different files. But I need to use Sys.time in my csv file name. (or is there any other way of writing these csv files so that files don't get over-written). Kindly guide. Regards and thanking in advance Vincy [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] loop for regression
?lm and note in particular the section beginning If response is a matrix... -- Bert On Tue, Jul 3, 2012 at 10:08 PM, Akhil dua akhil.dua...@gmail.com wrote: -- Forwarded message -- From: Akhil dua akhil.dua...@gmail.com Date: Wed, Jul 4, 2012 at 10:33 AM Subject: To: r-help@r-project.org Hi everyone I have data on stock prices and market indices and I need to run a seperate regression of every stock on market so I want to write a for loop so that I wont have to write codes again and again to run the regression... my data is in the format given below Date Stock1 Stock2 Stock3Market 01/01/2000 1 2 3 4 01/02/2000 5 6 7 8 01/03/2000 1 2 3 4 01/04/2000 5 6 7 8 So can any one help me how to write this loop [[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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[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] (no subject)
On 3 July 2012 22:03, Akhil dua akhil.dua...@gmail.com wrote: and I need to run a seperate regression of every stock on market so I want to write a for loop so that I wont have to write codes again and again to run the regression... 1. Do give a subject line -- a blank one is commonly used by a virus. 2. In R/S+/most functional languages, you do not want to write a for loop. Use apply (and friends) instead. my data is in the format given below Date Stock1 Stock2 Stock3Market 01/01/2000 1 2 3 4 01/02/2000 5 6 7 8 01/03/2000 1 2 3 4 01/04/2000 5 6 7 8 For example, if you wanted to know the stocks share of the total market as a fraction, you'd use something like: sapply(myData[,c(2:4)], function(x) { return(as.numeric(x)/as.numeric(myData[,4])) }) Hope that helps.. -- H -- Sent from my mobile device Envoyait de mon portable [[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 use Sys.time() while writing a csv file name
Hello, Try something like that: lgd - format(Sys.time(), %Y_%m_%d_%H_%M_%S) Regards, Pascal Le 04/07/2012 14:21, Vincy Pyne a écrit : Dear R helpers, I am using Beta distribution to generate the random no.s (recovery rates in my example). However, each time I need to save these random no.s in a csv format. To distinguish different csv files, one way I thought was use of Sys.time in the file name. My code is as follows - # My code rr = rbeta(25, 6.14, 8.12) lgd = 1 - mean(rr) write.csv(data.frame(recovery_rates = rr), file = paste(recovery_rates_at_, Sys.time(), .csv, sep = ), row.names = FALSE) However, I get following error - Error in file(file, ifelse(append, a, w)) : cannot open the connection In addition: Warning message: In file(file, ifelse(append, a, w)) : cannot open file 'recovery_rates_at_2012-07-04 1:14:05.csv': Invalid argument If instead of Sys.time, I use some other variable e.g. lgd as write.csv(data.frame(recovery_rates = rr), paste('rates_',lgd,'.csv', sep = ), row.names = FALSE) I am able to store these simulated recovery rates in different files. But I need to use Sys.time in my csv file name. (or is there any other way of writing these csv files so that files don't get over-written). Kindly guide. Regards and thanking in advance Vincy [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-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 use Sys.time() while writing a csv file name
You forgot to follow the posting guide and tell us what operating system you are using (sessionInfo), but I am going to guess that you are on Windows where the colon (:) is an illegal symbol in filenames. Try formatting the time explicitly in the conversion to character using the format string definitions found in ?strptime in a format that doesn't include colons. --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Vincy Pyne vincy_p...@yahoo.ca wrote: Dear R helpers, I am using Beta distribution to generate the random no.s (recovery rates in my example). However, each time I need to save these random no.s in a csv format. To distinguish different csv files, one way I thought was use of Sys.time in the file name. My code is as follows - # My code rr = rbeta(25, 6.14, 8.12) lgd = 1 - mean(rr) write.csv(data.frame(recovery_rates = rr), file = paste(recovery_rates_at_, Sys.time(), .csv, sep = ), row.names = FALSE) However, I get following error - Error in file(file, ifelse(append, a, w)) : � cannot open the connection In addition: Warning message: In file(file, ifelse(append, a, w)) : cannot open file 'recovery_rates_at_2012-07-04 1:14:05.csv': Invalid argument If instead of Sys.time, I use some other variable e.g. lgd as write.csv(data.frame(recovery_rates = rr), paste('rates_',lgd,'.csv', sep = ), row.names = FALSE) I am able to store these simulated recovery rates in different files. But I need to use Sys.time in my csv file name. (or is there any other way of writing these csv files so that files don't get over-written). Kindly guide. Regards and thanking in advance Vincy [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-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] loop for regression
Hi, A few comments. First a for loop is probably not optimally efficient. Consider instead (using a bulit in example dataset): lm(cbind(mpg, hp) ~ cyl + vs, data = mtcars) which gives: Call: lm(formula = cbind(mpg, hp) ~ cyl + vs, data = mtcars) Coefficients: mpg hp (Intercept) 39.6250 -15.6279 cyl -3.0907 27.5843 vs-0.9391 -19.1148 i.e., same predictors used on both outcomes. Note that this is substantially faster than running separately. See ?lm for details. If you need to run separate models (e.g., predictors are changing), and you have many models and a lot of data (which would not be surprising when working with stock data), consider using the RcppEigen package. You can get it by: install.packages(RcppEigen) require(RcppEigen) # load package it has a function called fastLm which is orders of magnitude faster than lm() and works almost identically. lapply(mtcars[, c(mpg, hp)], function(x) fastLm(X = cbind(Int = 1, mtcars[, c(cyl, vs)]), y = x)) you just give it the design matrix (X) and response vector (y) see ?fastLm Cheers, Josh On Tue, Jul 3, 2012 at 10:08 PM, Akhil dua akhil.dua...@gmail.com wrote: -- Forwarded message -- From: Akhil dua akhil.dua...@gmail.com Date: Wed, Jul 4, 2012 at 10:33 AM Subject: To: r-help@r-project.org Hi everyone I have data on stock prices and market indices and I need to run a seperate regression of every stock on market so I want to write a for loop so that I wont have to write codes again and again to run the regression... my data is in the format given below Date Stock1 Stock2 Stock3Market 01/01/2000 1 2 3 4 01/02/2000 5 6 7 8 01/03/2000 1 2 3 4 01/04/2000 5 6 7 8 So can any one help me how to write this loop [[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. -- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.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.