Re: [R] Spline Question
On 15 February 2012 19:00, Jeff Newmiller jdnew...@dcn.davis.ca.us wrote: What part of read the posting guide did you not understand? The provide commented, minimal, self-contained, reproducible code part? Off topic: why is there so much unfriendliness on this thread ? Both the above and DW's post seem unnecessarily aggressive and unhelpful. Michael --- Jeff Newmiller The . . Go Live... DCN:jdnew...@dcn.davis.ca.us Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Hasan Diwan hasan.di...@gmail.com wrote: Rolf, On 14 February 2012 21:26, Rolf Turner rolf.tur...@xtra.co.nz wrote: What did you actually *do* to create your spline? Did you use spline() or splinefun()? And if not, why not? Yes... I used the spline() function to get a list of points that should go though every point in the dataset, but it does not go through each point, just most of them. Is this what it is supposed to do? If so, my understanding of the maths underlying it are incorrect -- this is entirely possible as well. -- Sent from my mobile device Envoyait de mon portable __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] an unusual use for R
Brilliant Sarah ! I love seeing such unexpected and creative applications. I'm not a weaver but am a knitter (and a knotter actually) and have mused about using R to help design elements of textured knitting patterns e.g. as seen in single-colour, traditional fisherman's jumpers from England and Scotland. I've yet to do anything more than muse though. Hope it turns into a package :) Michael On 3 February 2012 09:54, Sarah Goslee sarah.gos...@gmail.com wrote: I thought some of you might be amused by this. In my non-work time, I'm an avid weaver and teacher of weaving. I'm working on a project involving creating many detailed weaving patterns, so I wrote R code to automate it. Details here: http://stringpage.com/blog/?p=822 If the overlap between R users and avid tablet weavers turns out to be 1, I'll polish it up and turn it into a package. Sarah -- Sarah Goslee http://www.functionaldiversity.org __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Regression type 2, x measured with error
Another search term is geometric mean regression. For simple models you can try the lmodel2 package. Michael On 2 February 2011 04:31, David Winsemius dwinsem...@comcast.net wrote: On Feb 1, 2011, at 10:41 AM, misil wrote: I wanna to do a Regression type 2 or Regression with X measured with erroranybody knows how can i make it in R?? thanks! There are several prior posts in the archives ... although perhaps not the Nabble archives since they start throwing away stuff after about a year. Search on orthogonal regression, total least sqaures regression or Deming regression. David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 calculate the consistency of different clusterings
Hello, I've been waiting to see if anyone else would answer this. I've previously used random reallocation of objects to groups (clusters) as a monte-carlo test of the informativeness of groups, as described here: http://lastresortsoftware.blogspot.com/2010/09/monte-carlo-testing-of-classification.html However, in your case it sounds like you want to investigate the influence of particular attributes (traits) or groups of attributes on the classification - is that correct ? If so, I can probably help with some R code but I'd need to know the clustering method you are using (e.g. hclust). Michael On 14 January 2011 02:36, Mao Jianfeng jianfeng@gmail.com wrote: Dear R-listers, I do clustering on tens of individuals by thousands of traits. I have known the assignment of each individual. I want to classify the individuals by randomly resampling different subsets of the traits, for example, randomly resampling 100 traits for 100 times, then 200 traits for 100 times, then 300 traits for 100 times, ,,. By each subset of traits, I do clustering of the same individuals. In the end, I want to get the consistency (in percentage) of each of these clusterings (as examples, here cluster.1, cluster.2 and cluster.3 in the dummy data) with the assignment which is already known (populations in the dummy data). I want to know how such work can be implemented, maybe by using R. #dummy data, clus.data - data.frame(individual = paste(ind, 1:12, sep = ), populations = c(rep(popA, 5), rep(popB, 7)), cluster.1 = c(rep(1, 5), rep(2, 7)), cluster.2 = c(rep(2, 4), rep(1, 8)), cluster.3 = c(rep(4, 7), rep(5, 5))) clus.data Thanks. -- Jian-Feng, Mao the Institute of Botany, Chinese Academy of Botany, __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] RSQLite - How to express(or save) a dataframe as an output?
Hi Amelia, You statement... dbGetPreparedQuery(con, INSERT INTO output(df) VALUES (?), data.frame(output)) ...is the problem. To insert an entire data.frame into the database use dbWriteTable. To insert with debSendPreparedQuery or dbGetPreparedQuery, the number of ? in the VALUES specifier needs to be the same as the number of fields that you are inserting values into (= the number of cols in your data.frame). For example, if you have a table Foo with fields Id, Name, Value, you could do this... mydf - data.frame(id=1:3, name=c(foo1, foo2, foo3), value=rnorm(3)) dbGetPreparedQuery(con, insert into Foo (values (?, ?, ?), mydf) which is equivalent to this... dbGetPreparedQuery(con, insert into Foo (Id, Name, Value) values (?, ?, ?), mydf) Hope that helps, Michael On 14 January 2011 18:19, Amelia Vettori amelia_vett...@yahoo.co.nz wrote: Dear R helpers Suppose following is an output due to some R process. I wish to save it as a table in 'temp.db' df - data.frame(x = c(5, 4, 3, 11), y = c(25, 16, 9, 121)) library(RSQLite) write('** Initializing','') drv - dbDriver(SQLite, shared.cache = TRUE) con - dbConnect(drv, dbname = temp.db, loadable.extensions=TRUE) on.exit(dbUnloadDriver(drv)) on.exit(dbDisconnect(con)) write('** Save output', '') dbBeginTransaction(con) dbGetPreparedQuery(con, INSERT INTO output(df) VALUES (?), data.frame(output)) dbCommit(con) dbGetPreparedQuery(con, INSERT INTO output(df) VALUES (?), data.frame(output)) # - I get following message Error in dbGetPreparedQuery(con, INSERT INTO output(df) VALUES (?), : error in evaluating the argument 'bind.data' in selecting a method for function 'dbGetPreparedQuery' [[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] Generation of Normal Random Numbers
I realized after posting that you can also just do this... x - matrix(rnorm(25 * length(m), m, s), ncol=4, byrow=TRUE) Michael On 11 January 2011 19:13, Amelia Vettori amelia_vett...@yahoo.co.nz wrote: Dear Mr Bedward and Mr Newmiller, Thanks a lot for the guidance. Really appreciated. Sagga K --- On *Tue, 11/1/11, Michael Bedward michael.bedw...@gmail.com* wrote: From: Michael Bedward michael.bedw...@gmail.com Subject: Re: [R] Generation of Normal Random Numbers To: saggak saggak1...@yahoo.co.in Cc: r-help@r-project.org Received: Tuesday, 11 January, 2011, 7:24 AM m - c(1004.1, 1028.3, 1044.3, 861.4) s - c(194.5899, 158.7052, 123.3000, 285.8695) x - mapply(function(mi, si) rnorm(25, mi, si), m, s) Hope this helps, Michael On 11 January 2011 17:44, saggak saggak1...@yahoo.co.in wrote: Dear R helpers I have a data frame as given below df = data.frame(A = c(776,827,836,995,855,1026,1203,1363,965,1195), B = c(806,953,1049,1056,1243,764,1148,1162,948,1154), C = c(959,1155,1193,1163,863,1070,1087,877,1132,944), D = c(906,760,978,1170,1009,883,1007,960,828,113)) # Actually the real data has number of vectors and not only A, B, C and D. m = as.numeric(lapply(df, mean)) s = as.numeric(lapply(df, sd)) gives m [1] 1004.1 1028.3 1044.3 861.4 s [1] 194.5899 158.7052 123.3000 285.8695 I need to generate 25 (normal) random numbers for each of these mean and corresponding standard deviation combination (m[i], s[i]). i.e. I need to have a table (dim 25 X 4) giving me random numbers. rnorm(25, m[1], s[1]) rnorm(25, m[2], s[2]) rnorm(25, m[3], s[3]) rnorm(25, m[4], s[4]) . ... . . ... . . ... . . ... . . ... . Kindly guide Thanking in advance Sagga K [[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. [[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 coerce part of each column of a matrix to a vector and merge them
Hello, The answer to this one drops out of the answer to your previous question... m - matrix(1:16, nrow=4) end - c(2,3,1,3) ii - cbind(sequence(end), rep(1:length(end), end)) x - m[ ii ] Hope this helps, Michael 2011/1/11 zhaoxing731 zhaoxing...@yahoo.com.cn: Hello Suppose I have a matrix mat=(1:16,2) [,1] [,2] [,3] [,4] [1,]159 13 [2,]26 10 14 [3,]37 11 15 [4,]48 12 16 there is a vector end=c(2,3,1,3) #coerce the 1st 2 numbers of the 1st column to a vector [1] 1 2 #coerce the 1st 3 numbers of the 2nd column and append it to the previous vector [1] 1 2 5 6 7 #coerce the 1st number of the 3rd column and append it to the previous vector [1] 1 2 5 6 7 9 #coerce the 1st 3 numbers of the 4th column and append it to the previous vector [1] 1 2 5 6 7 9 13 14 15 #they are specified by vector end a for loop mat-matrix(1:16,4) end-c(2,3,1,3) vec-numeric() for (i in 1:4) {vec-c(vec,mat[1:end[i],i]) } #the result vec [1] 1 2 5 6 7 9 13 14 15 but when I want to do it to a large dataset, it's inefficiency becomes a problem, so need vectorization Thank you in advance Yours sincerely ZhaoXing Department of Health Statistics West China School of Public Health Sichuan University No.17 Section 3, South Renmin Road Chengdu, Sichuan 610041 P.R.China __ 赶快注册雅虎超大容量免费邮箱? __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] gennerating skewed random numbers
Hi Ed, The sn package can generate random values from skewed Normal and t distributions. Also see here: http://azzalini.stat.unipd.it/SN/faq-r.html Michael On 11 January 2011 23:37, Ed Keith e_...@yahoo.com wrote: This is not exactly an R specific question, but I think the people on this list can probably help. I'm working on a simulation. In the model I have the first three moments of the distributions of the variables. I know how to generate a random number from a distribution given the first two moments assuming the third moment is 0. But I do not know how to generate a number drawn from a distribution with a nonzero third monument. If someone could point me to a good reference I would appreciate it. Thank you in advance, -EdK Ed Keith e_...@yahoo.com Blog: edkeith.blogspot.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] Global variables
Hi Sebastian, You might also find the proto package useful as a way of restricting the scope of variables. It provides a more intuitive (at least to me) way of packaging variables and functions up into environments that can be related in a hierarchy. Michael On 10 January 2011 23:48, Sebastien Bihorel sebastien.biho...@cognigencorp.com wrote: Thank Gabor and Duncan, That will be helpful. Gabor Grothendieck wrote: On Thu, Jan 6, 2011 at 4:59 PM, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 06/01/2011 4:45 PM, Sebastien Bihorel wrote: Dear R-users, Is there a way I can prevent global variables to be visible within my functions? Yes, but you probably shouldn't. You would do it by setting the environment of the function to something that doesn't have the global environment as a parent, or grandparent, etc. The only common examples of that are baseenv() and emptyenv(). For example, x - 1 f - function() print(x) Then f() will work, and print the 1. But if I do environment(f) - baseenv() then it won't work: f() Error in print(x) : object 'x' not found The problem with doing this is that it is not the way users expect functions to work, and it will probably have weird side effects. It is not the way things work in packages (even packages with namespaces will eventually search the global environment, the namespace just comes first). There's no simple way to do it and yet get access to functions in other packages besides base without explicitly specifying them (e.g. you'd need to use stats::lm(), not just lm(), etc.) A variation of this would be: environment(f) - as.environment(2) which would skip over the global environment, .GlobEnv, but would still search the loaded packages. In the example above x would not be found but it still could find lm, etc. [[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] Generation of Normal Random Numbers
m - c(1004.1, 1028.3, 1044.3, 861.4) s - c(194.5899, 158.7052, 123.3000, 285.8695) x - mapply(function(mi, si) rnorm(25, mi, si), m, s) Hope this helps, Michael On 11 January 2011 17:44, saggak saggak1...@yahoo.co.in wrote: Dear R helpers I have a data frame as given below df = data.frame(A = c(776,827,836,995,855,1026,1203,1363,965,1195), B = c(806,953,1049,1056,1243,764,1148,1162,948,1154), C = c(959,1155,1193,1163,863,1070,1087,877,1132,944), D = c(906,760,978,1170,1009,883,1007,960,828,113)) # Actually the real data has number of vectors and not only A, B, C and D. m = as.numeric(lapply(df, mean)) s = as.numeric(lapply(df, sd)) gives m [1] 1004.1 1028.3 1044.3 861.4 s [1] 194.5899 158.7052 123.3000 285.8695 I need to generate 25 (normal) random numbers for each of these mean and corresponding standard deviation combination (m[i], s[i]). i.e. I need to have a table (dim 25 X 4) giving me random numbers. rnorm(25, m[1], s[1]) rnorm(25, m[2], s[2]) rnorm(25, m[3], s[3]) rnorm(25, m[4], s[4]) . ... . . ... . . ... . . ... . . ... . Kindly guide Thanking in advance Sagga K [[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 apply function partial to each vector of a matrix
Hello, Here is one way... m - matrix(1:16, nrow=4) end - c(2, 3, 1, 3) ii - cbind(sequence(end), rep(1:length(end), end)) sums - tapply(m[ ii ], ii[ , 2], sum) And here is another way... sums - mapply(function(col, lastrow) sum(m[1:lastrow, col]), 1:ncol(m), end) Hope this helps, Michael On 11 January 2011 16:17, zhaoxing731 zhaoxing...@yahoo.com.cn wrote: Hello Suppose I have a matrix mat=(1:16,2) [,1] [,2] [,3] [,4] [1,]159 13 [2,]26 10 14 [3,]37 11 15 [4,]48 12 16 I just want to use the apply function to the matrix partially there is a vector end=c(2,3,1,3) #sum the 1st 2 numbers of the 1st column #sum the 1st 3 numbers of the 2nd column #sum the 1st number of the 3rd column #sum the 1st 3 numbers of the 4th column #they are specified by vector end a for loop for (i in 1:4) { outsum=sum[1:end[i],i] } but when I want to do it to a large dataset, it's inefficency becomes a problem, so need vectorization the apply function will sum all of each vector, which is not my purpose Thank you in advance Yours sincerely ZhaoXing Department of Health Statistics West China School of Public Health Sichuan University No.17 Section 3, South Renmin Road Chengdu, Sichuan 610041 P.R.China __ 赶快注册雅虎超大容量免费邮箱? __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Normal Distribution Quantiles
Just to add to the silly solutions, here's how I would have done it... mu - 40 sdev - 10 days - 100:120 # range to explore p - 0.8 days[ match(TRUE, qnorm(0.2, mu*days, sqrt(sdev * sdev * days)) = 4000) ] Michael On 9 January 2011 08:48, Bert Gunter gunter.ber...@gene.com wrote: If I understand what you have said below, it looks like you do NOT have the problem solved manually. You CAN use qnorm , and when you do so, your equation yields a simple quadratic which, of course, has an exact solution that you can calculate in R. Of course, one can use uniroot or whatever to solve the quadratic; or simulation or interpolation using pnorm. But other than the R practice, these are unnecessary and, in this case, a bit silly. Cheers, Bert On Sat, Jan 8, 2011 at 6:25 AM, Rainer Schuermann rainer.schuerm...@gmx.net wrote: Sounds like homework, which is not an encouraged use of the Rhelp list. You can either do it in theory... It is _from_ a homework but I have the solution already (explicitly got that done first!) - this was the pasted Latex code (apologies for that, but in plain text it looks unreadable[1], and I thought everybody here has his / her favorite Latrex editor open all the time anyway...). I'm just looking, for my own advancement and programming training, for a way of doing that in R - which, from your and Dennis' reply, doesn't seem to exist. I would _not_ misuse the list for getting homework done easily, I will not ask learning statistics questions here, and I will always try to find the solution myself before posting something here, I promise! Thanks anyway for the simulation advice, Rainer (4000 - (40*n)) -329 [1] --- = 1 200 (10*(n^-)) 2 On Saturday 08 January 2011 14:56:20 you wrote: On Jan 8, 2011, at 6:56 AM, Rainer Schuermann wrote: This is probably embarrassingly basic, but I have spent quite a few hours in Google and RSeek without getting a clue - probably I'm asking the wrong questions... There is this guy who has decided to walk through Australia, a total distance of 4000 km. His daily portion (mean) is 40km with an sd of 10 km. I want to calculate the number of days it takes to arrive with 80, 90, 95, 99% probability. I know how to do this manually, eg. for 95% $\Phi \left( \frac{4000-40n}{10 \sqrt{n}} \right) \leq 0.05$ find the z score... but how would I do this in R? Not qnorm(), but what is it? Sounds like homework, which is not an encouraged use of the Rhelp list. You can either do it in theory or you can simulate it. Here's a small step toward a simulation approach. cumsum(rnorm(100, mean=40, sd=10)) [1] 41.90617 71.09148 120.55569 159.56063 229.73167 255.35290 300.74655 snipped [92] 3627.25753 3683.24696 3714.11421 3729.41203 3764.54192 3809.15159 3881.71016 [99] 3917.16512 3932.00861 cumsum(rnorm(100, mean=40, sd=10)) [1] 38.59288 53.82815 111.30052 156.58190 188.15454 207.90584 240.64078 snipped [92] 3776.25476 3821.90626 3876.64512 3921.16797 3958.83472 3992.33155 4045.96649 [99] 4091.66277 4134.45867 The first realization did not make it in the expected 100 days so further efforts should extend the simulation runs to maybe 120 days. The second realization had him making it on the 98th day. There is an R replicate() function available once you get a function running that will return a specific value for an instance. This one might work: min(which(cumsum(rnorm(120, mean=40, sd=10)) = 4000) ) [1] 97 If you wanted a forum that does not explicitly discourage homework and would be a better place to ask theory and probability questions, there is CrossValidated: http://stats.stackexchange.com/faq Thanks in advance, and apologies for the level of question... Rainer __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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. -- Bert Gunter Genentech Nonclinical Biostatistics __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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
Re: [R] How to make a Cluster of Clusters
Hi Diego, It depends on your what your research questions are. You haven't told us :) For example, if you wanted to know whether (a) the environmental distance between lakes is correlated with spatial distance and (b) if the relationship changes over time you might do a series of Mantel tests. This would be working with your environmental and spatial distance matrices directly, rather than indirectly as is the case with dendrogram ultrametrics or ordination coordinates. Michael On 8 January 2011 00:02, Diego Pujoni diegopuj...@gmail.com wrote: Hi Michael, I agree with you and I will make this ordination. But I also want to check a spatial correlation of the variables, so I thought that comparing the dendrogram of the environmental variables with the dendrogram of the geographical distances of the lakes it will indicates if similar lakes are next to each other. But I have just one geographical coordinate for each lake, but 12 measures of environmental variables. How can I analyse this? Thank you very much for the attention Diego PJ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 make a Cluster of Clusters
Hello Diego, This might not be relevant, but on reading your question the first idea that struck me was that ordination trajectories of your lakes over time might be more informative than clustering. Michael On 5 January 2011 01:31, Diego Pujoni diegopuj...@gmail.com wrote: Dear R-help, In my Master thesis I measured 10 variables from 18 lakes. These measurements were taken 4 times a year in 3 depths, so I have 12 samples from each lake. I know that 12 samples can not be treated as replications, since they don't correspond to the same environmental characteristics and are not statistically independent, but I want to use these 12 samples as an estimate of an annual range the 18 lakes have of the 10 variables. I want to make a cluster analysis of the 18 lakes and my known possibilities were: 1- Make an average of the 12 samples from each lake and make the cluster (Using ward's method); 2- Use all 216 samples (18*12) to make the cluster (Which yields a mess). But I thought I could begin the cluster algorithm already with 18 clusters (Lakes) each with 12 individuals (samples) and normally proceed with the calculations (using ward's method). So I will obtain a cluster of the 18 lakes, but using the 12 samples. I got the cluster Fortran algorithm and I'm trying to translate it to the R language to see how it works and maybe implement this kind of cluster of cluster analysis. Does anyone knows if there is an algorithm that does this? Actually I did it by hand and got very good and meaningful results, but I want to implement it to try another merging criterias. Thanks Diego Pujoni Zooplankton Ecology Laboratory Biological Sciences Institute Federal University of Minas Gerais Brazil __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] RSQLite to input dataframe
Hi Amy, I'm not sure if I understand your question correctly so let me know if the following is off track. Starting with your example, here is how to create a data.frame and write it to a new table in a new database file... my.data = data.frame(X = c(US, UK, Canada, Australia, Newzealand), Y = c(52, 36, 74, 10, 98)) drv - dbDriver(SQLite) con - dbConnect(drv, myfilename.db) dbWriteTable(con, sometablename, my.data) To verify that the table is now in the file... dbListTables(con) To check the fields in the table (should match the colnames in your data.frame)... dbListFields(con, sometablename) To read the whole table into the workspace as a new data.frame my.data.copy - dbReadTable(con, sometablename) If you have data in a CSV file, and the contents are small enough to read in one go, you would use the read.csv function to read the contents of the file into a data.frame and then use dbWriteTable to transfer this to your database. Hope this helps, Michael On 4 January 2011 21:43, Amy Milano milano_...@yahoo.com wrote: Dear r helpers, At first, I apologize for raising a query which seems to be a stupid interpretation on my part. I am trying to learn SQLite. Following is an example given in the RSQLite.zip file (Page # 4) drv - dbDriver(SQLite) tfile - tempfile() con - dbConnect(drv, dbname = tfile) data(USArrests) dbWriteTable(con, arrests, USArrests) On the similar line I am trying to read my data. Suppose I have a dataframe as given below. DF = data.frame(X = c(US, UK, Canada, Australia, Newzealand), Y = c(52, 36, 74, 10, 98)) drv - dbDriver(SQLite) tfile - tempfile() con - dbConnect(drv, dbname = tfile) data(DF) dbWriteTable(con, ..., ...) # Didn't know what to write here. I understand I have raised a query in a stupid manner. I need to understand is there any way I can use SQLite to read dataframe or for that matter any csv file say e.g. 'DF.csv'. Please enlighten me. Amy [[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] Bayesian Belief Networks
Hello Walt, Have a look at the bnlearn and deal packages. Michael On 24 December 2010 01:29, Data Analytics Corp. w...@dataanalyticscorp.com wrote: Hi, Does anyone know of a package for or any implementation of a Bayesian Belief Network in R? Thanks, Walt Walter R. Paczkowski, Ph.D. Data Analytics Corp. 44 Hamilton Lane Plainsboro, NJ 08536 (V) 609-936-8999 (F) 609-936-3733 w...@dataanalyticscorp.com www.dataanalyticscorp.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] regression
Hello Ufuk, Here is one way to do it... # make up some data for this example x - matrix(runif(7 * 20), ncol=7) # a data.frame is most convenient for lm x - as.data.frame(x) colnames(x) - c(x1, x2, x3, x4, x5, x6, y) # a list to hold lm results x.lm - list() # run regressions for (i in 1:nrow(x)) { x.lm - c(x.lm, list( lm(y ~ ., data=x[-i, ]) )) } Now element i of x.lm will hold the results of the regression with data row i removed. Michael On 23 December 2010 08:57, ufuk beyaztas ufukbeyaz...@gmail.com wrote: Hi dear all, suppose that s is a statistic code; i have a matrix (x) which has 7 columns (1=x1,2=x23=x3,4=x4,5=x5,6=x6 and7=y) and has 20 rows. i want to do linear reggression like reg-lm(x[,7]~1+x[,1]+x[,2]+...+x[,6]) but i want to do delete i th row for nrows times and create regression model like above and compute each models' s statistics and list them. but i could not do. i always get only one model and statistic. How can i do this Thanks any idea! -- View this message in context: http://r.789695.n4.nabble.com/regression-tp3161328p3161328.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] Is there a join() function in R ? OR: simulating Combining ggplot2 and Google Maps by David Kahle
Hi Christiaan, That looks like the join function in the plyr package. Michael On 16 December 2010 22:06, christiaan pauw cjp...@gmail.com wrote: Hi everybody Im on R version 2.11.1 on Mac OS X I am working through David Kahle's example of using ggplot2 with Rgooglemaps (found here: https://github.com/hadley/ggplot2/wiki/Crime-in-Downtown-Houston,-Texas-:-Combining-ggplot2-and-Google-Maps). Excellent page by the way. I have downloaded the data and the code and want to learn how to do this by first simulating David's results and then working it through line by line to make sure I understand what every line of code does and then applying it to my own data. When I run the code however, it breaks on the following line: df - suppressMessages(join(df_xyll, m_map, type = 'right')) I do not seem to have the join() function. This looks almost SQLish. I have tried ??join and looking on CRAN and a google search but I cannot find this function. Is there a special library that one has to install for this to work? Can anyone help please. Best regards Christiaan [[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] Compare two dataframes
Hello Mark, This is how I do it but it's longer than your code :) unique.rows - function (df1, df2) { # Returns any rows of df1 that are not in df2 out - NULL for (i in 1:nrow(df1)) { found - FALSE for (j in 1:nrow(df2)) { if (all(df1[i,] == df2[j,])) { found - TRUE break } } if (!found) out - rbind(out, df1[i,]) } out } Michael On 17 December 2010 06:02, Mark Na mtb...@gmail.com wrote: Hello, I have two dataframes DF1 and DF2 that should be identical but are not (DF1 has some rows that aren't in DF2, and vice versa). I would like to produce a new dataframe DF3 containing rows in DF1 that aren't in DF2 (and similarly DF4 would contain rows in DF2 that aren't in DF1). I have a solution for this problem (see self contained example below) but it's awkward and requires making a new ID column by pasting together all of the columns in each DF and them comparing the two DFs based on this unique ID. Is there a better way? Many thanks for your help, Mark #compare two dataframes and extract uncommon rows #MAKE SOME DATA cars$id-paste(cars$speed,cars$dist,sep=) $create unique ID field by pasting all columns together cars1-cars[1:35,] cars2-cars[16:50,] #EXTRACT UNIQUE ROWS cars1_unique-cars1[cars1$id %in% setdiff(cars1$id,cars2$id),] #rows unique to cars1 (i.e., not in cars2) cars2_unique-cars2[cars2$id %in% setdiff(cars2$id,cars1$id),] #rows unique to cars2 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] predict.lm with new regressor names
Hi Anirban, You can do it like this... lm.foobar - lm(foo ~ bar) some.data - rnorm(200) predict(lm.foobar, newdata=list(bar=some.data)) Hope that helps. Michael On 16 December 2010 17:05, Anirban Mukherjee am...@cornell.edu wrote: Hi all, Suppose: y-rnorm(100) x1-rnorm(100) lm.yx-lm(y~x1) To predict from a new data source, one can use: # works as expected dum-data.frame(x1=rnorm(200)) predict(lm.yx, newdata=dum) Suppose lm.yx has been run and we have the lm object. And we have a dataframe that has columns that don't correspond by name to the original regressors. I very! naively assumed that doing this (below) would work. It does not. # does not work lm.yx$coefficients-c(Intercept, n.x1) dum2-data.frame(Int=rep(1,200), n.x1=rnorm(200)) predict(lm.yx, newdata=dum2) I know that a simple alternative is to do: # because we messed around with the lm object above, re-building lm.yx-lm(y~x1) # change names of dum2 to match names of coefficients of lm.yx names(dum2)-names(coefficients(lm.yx)) predict(lm.yx, newdata=dum2) Is there another way that involves changing the lm object rather than changing the prediction data.frame? Thanks, Anirban -- Anirban Mukherjee | Assistant Professor, Marketing LKCSB, Singapore Management University 5056 School of Business, 50 Stamford Road Singapore 178899 | +65-6828-1932 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Significance Help
Hello Samaire, I don't know much about eyesight measurements (other than that my own would probably be 0.1, 0.2) but I'll attempt some suggestions... For objective (1) you will have to define what you mean by significantly different. If you had data on left/right values from a larger population to use as a standard you could then compare each subject's left - right difference to the population. For objective (2), it would be better if you had forced them to throw the ball more than 3 times ! Anyway, something like this to start with... glm( hoops ~ lr.diff, family=binomial ) where hoops is a two column matrix with number of successful throws in col 1 and number of misses in col 2; lr.diff is absolute value of left - right value. Hope this helps. Michael On 15 December 2010 08:21, Samaire Holden samair...@hotmail.co.uk wrote: I have a set of results where I have the eyesight power of both left and right eyes for each participant (e.g. 0.75, 0.5). Each participant then had to throw basketballs into a hoop and the number of successful throws was recorded. I would like to do two things: 1. Test whether the eyesight power of the left and right eyes are significantly different from each other. 2. Test whether the number of successful throws is significantly affected by eyesight power (i.e. is there a difference between those people who have similar powers in left and right eyesight, to those who have significantly different powers?) I know that I may not have explained this particularly well, but I'm totally stumped as to where to start! I have only had basic lessons with R and now have to do this report! Many thanks, Samaire [[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] peak detection
Hi Joe, Just for info, I've done this in the past by applying lowess followed by diff to a vector, then identifying points with change of sign in the diffs. Michael On 14 December 2010 14:22, Joe Stuart joe.stu...@gmail.com wrote: Never mind. I did find this package, which seems to do the trick. Thanks http://rgm2.lab.nig.ac.jp/RGM2/R_man-2.9.0/library/msProcess/man/msExtrema.html On Mon, Dec 13, 2010 at 9:05 PM, Joe Stuart joe.stu...@gmail.com wrote: Hi, I was wondering if anyone knows of a package that contains the ability for peak/valley detection. Here is an example of what I'm looking for, only problem is that it's written in Matlab. http://www.billauer.co.il/peakdet.html Thanks for any help in advance. -Joe __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Pure curiosity
Just to follow up on Robert's comment, If you do an ls() you'll see that you've created objects V1, V2 in your global environment. A very similar question was discussed last week (I think... it's all a blur) in the context of using - instead of = with named function arguments. Michael On 13 December 2010 14:10, robert denham rjaden...@gmail.com wrote: On Mon, Dec 13, 2010 at 1:09 PM, robert denham rjaden...@gmail.com wrote: in test - data.frame(V1=c(1,2,3), V2=c(4,5,6)) you are using arguments to the data.frame function in the tag=value format. From the help: ...: these arguments are of either the form ‘value’ or ‘tag = value’. Component names are created based on the tag (if present) or the deparsed argument itself. in test - data.frame(V1-c(1,2,3), V2-c(4,5,6)) you are using the value part, with the values being c(1,2,3) and c(4,5,6). Someone might explain whats really going on, but I don't think the assignment of c(1,2,3) to the variable V1 is done before the data.frame is evaluated. On Mon, Dec 13, 2010 at 12:53 PM, Matt Cooper mattcst...@gmail.comwrote: Pure curiosity but does anyone know why '-' and '=' generate different columning headers? test - data.frame(V1=c(1,2,3), V2=c(4,5,6)) test V1 V2 1 1 4 2 2 5 3 3 6 test - data.frame(V1-c(1,2,3), V2-c(4,5,6)) test V1c.1..2..3. V2c.4..5..6. 1 1 4 2 2 5 3 3 6 names(test) [1] V1c.1..2..3. V2c.4..5..6. [[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. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R Plots for Recurrent Events - Suggestions are needed
Hi Haoda, I couldn't find a package that implements this, although I'm not familiar with the field so there could be something but using different terminology. However, looking at the the Google preview of Nelson (2003) which is cited by the page that you linked to, the calculations seem very simple (see page 46). Here is a function which I think does what is described there (warning: untested !)... mcf - function(events) { # events a data.frame or matrix where: # col 1 is unitID # col 2 is event time (integer) # col 3 is event type coded as: # 0: recurrence # 1: first appearance (left censoring time) # 2: last appearance (right censoring time) # # Order events data by time, unit id, event type events - events[ order(events[,2], events[,1], events[,3]), ] m - matrix(0, nrow=nrow(events), ncol=4) colnames(m) - c(time, Nrisk, incr, mcf) # copy event times m[, 1] - events[, 2] # number of units observed at each time m[, 2] - cumsum(ifelse(events[, 3] == 2, -1, events[, 3])) # incremental risk irecurrence - events[,3] == 0 m[irecurrence, 3] - 1 / m[irecurrence, 2] # cumulative risk (MCF estimate) m[, 4] - cumsum(m[, 3]) # return results (matrix rows with recurrent events) m[events[,3] == 0, ] } Hope this helps. Michael On 13 December 2010 03:47, Haoda Fu fu...@yahoo.com.cn wrote: Hi, I am wondering if there is a simple way to plot MCF(mean cumulative function) for recurrent events in R? MCF (http://www.weibull.com/hotwire/issue57/relbasics57.htm) And do you have some other recommendation in visualize recurrent events? I did some research online. Some people use ggplot2 to plot MCF, but it requires some additional coding. http://user2010.org/slides/Shentu.pdf Your help and suggestions are highly appreciated. Warm regards from Haoda __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] simple plotting question
Hello Erin, Try this... plot(x, y, type=b, pch=16) Michael On 13 December 2010 18:11, Erin Hodgess erinm.hodg...@gmail.com wrote: Dear R People: When I plot using type=b, I have circles and lines, which is as it should be. Is there a way to have filled in circles using the type argument, please? Or do I need to call the points function also, please? Thanks, Erin -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodg...@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-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 RSQLite adding a new column
Hi Michael, Sorry if I'm being slow, but I've read your post three times and still can't quite work out what you're trying to do (the changing variables names are a bit confusing). I use RSQLite a lot and might be able to help if you could explain your inputs and desired output in simple terms. (another) Michael On 11 December 2010 05:18, Michael D mike...@gmail.com wrote: I'm new to using sql so I'm having difficulties (and worries) in adding a new column of data to a table I have. Its a very large file (around 5 Gb) which is why I'm having to use SQL I have a table with variables ID, IDrec and IDdes and the variables IDrec and IDdes give a mapping of some other values but the other values are associated with the ID variable (think of IDrec and IDdes being character strings and ID being numeric) (Imagine the transposed) Table1: ID: 1,2,3,4,... IDrec: A,B,C,D... IDdes: B,C,A,E... So I've created a table with the final form I need it to be in dbGetQuery(db, CREATE TABLE Map (ID int, IDrec int, IDrec1 int, IDdes int, IDdes1 int)) And the finished table would look something like: Map: ID: 1, 2, 3, 4,... IDrec: 1, 2, 3, 4,... IDrec1: A, B, C, D,... IDdes: 2, 3, 1, 5, IDdes1: B, C, A, E,... So I copy in the first set of values easily: dbGetQuery(db, INSERT INTO Map(ID, IDrec, IDrec1, IDdes1) SELECT ID, ID, IDrec, IDdes FROM Ntemp) Giving me a table that looks like: Map: ID: 1, 2, 3, 4,... IDrec: 1, 2, 3, 4,... IDrec1: A, B, C, D,... IDdes: NA,NA,NA,NA,... IDdes1: B, C, A, E,... Then I create a new table with just the IDdes values I need: dbGetQuery(db, Create table temp2 as SELECT temp.ID FROM Ntemp, temp WHERE Ntemp.IDdes1 = temp.IDrec1) Giving me temp2 (not sure what the variable name is) V1: 2, 3, 1, 5,... But when I try to copy in the new data: dbGetQuery(db, INSERT INTO Map(IDdes) SELECT * FROM temp2) My map table isn't updated: Map: ID: 1, 2, 3, 4,... IDrec: 1, 2, 3, 4,... IDrec1: A, B, C, D,... IDdes: NA,NA,NA,NA,... IDdes1: B, C, A, E,... Is there something I'm missing? Or am I just going about inserting the IDdes variables the wrong way? Thanks for the help. Michael [[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 with two factors
Hello Martin, You were almost there :) T1 - subset(daten1, Geschlecht==M GG==A) Hope this helps. Michael On 10 December 2010 22:25, Martin Spindler martin.spind...@gmx.de wrote: Dear all, I have a dataframe of the following strucutre numacc_b coverage_b Geschlecht GG 1 0 1 W A 2 0 1 M A 3 0 1 M B 4 0 1 M B 5 0 1 W A 6 0 1 M B I would like to form a subset consisting of all entries with Geschlecht=M and GG=A. Using T1 - subset(daten1, Geschlecht==M, GG==A) delievers data frame with 0 columns and 6 rows T1 - subset(daten1, Geschlecht==M) delievers numacc_b coverage_b Geschlecht GG 2 0 1 M A 3 0 1 M B 4 0 1 M B 6 0 1 M B 9 0 1 M B 10 0 1 M B But I want to select the dataframe according to both factos. What can I do? Thank you answers in advance! Best, Martin -- GMX DSL Doppel-Flat ab 19,99 euro;/mtl.! Jetzt auch mit gratis Notebook-Flat! http://portal.gmx.net/de/go/dsl __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Number of dimension in Multidimensional Scaling
Just to add to Michael F's comments: I've looked for that elbow many a time but never found it :) Admittedly, I typically deal with fairly noisy, ecological data, but I think it's a mistake to try to identify the optimal number of dimensions. Better instead to concentrate on a useful number, ie. usefully descriptive; able to be related to other variables etc. Just my 2c Michael On 10 December 2010 06:38, Michael Friendly frien...@yorku.ca wrote: On 12/9/2010 7:26 AM, Petar Milin wrote: Hello! Very often one can hear that MDS usually ends with two-dimensional solution. Of course, there are methods, like Scree-test (proposed by Kruskal and Wish, 1981), to determine optimal number of dimensions. However, I am trying to find references to this two-dimensional gold-standard. Can anyone point me to authors which explicitly states that two-dimensions are typical and easiest to represent graphically? In Baayen's book (2008) one can find this statement. Are there more? In nonmetric MDS, goodness of fit is assessed by a Stress statistic (actually, there are several), measuring normalized SS (observed distances - fitted distances) There is no significance test of adequacy of 2, 3, 4, ... dimensions, so it is common practice to plot Stress vs # dimensions and look for an elbow, as in the Scree plot for exploratory factor analysis. I can't think of anyone who says 2 dimensions are typical, but they are certainly easier to plot and interpret graphically, or at least were before dynamic interactive graphics allowed one to easily plot in 3D and rotate by direct manipulation (rgl, rggobi+ggobi) My favorite recent book: Borg, I. and Groenen, P.: Modern Multidimensional Scaling: theory and applications (2nd ed.), Springer-Verlag New York, 2005 -- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 4700 Keele Street Web: http://www.datavis.ca Toronto, ONT M3J 1P3 CANADA __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Time out for a R Function
Below is a toy function with one way of doing it. There are bound to be better ways :) function(niter = 10, time.out = 3) { pretend.task - function() { Sys.sleep(0.5) } start - proc.time() for (iter in 1:niter) { pretend.task() cur - proc.time() - start if (cur[3] time.out) return(timed out) } return(completed) } On 7 December 2010 13:04, Santosh Srinivas santosh.srini...@gmail.com wrote: Hello Group, I have an R-function that works fine for most part but sometime runs into a long loop! (I'm lazy and short on time to debug right now so want to do something easy) For my purpose, it is ok to make few errors is there a way I can put a timeout on a function and the r-process needes to move on to the next step? Thank you. S __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Querying a data frame or data.table
Sounds just like the subset function (?) x - as.data.frame(matrix(sample(5, 100, rep=TRUE), ncol=10)) subset(x, V1 3 V2 5) Michael On 3 December 2010 19:05, Santosh Srinivas santosh.srini...@gmail.com wrote: Hello Group, Is there an easy way to query a data.frame or data.table (this is fast!) for multiple conditions? I don't want to use a SQL kind of statement. I am looking for something like a subset with multiple conditions. Any tips of the like the binary search methodology used for data.table would also help. Thanks in advance. S __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] using ``-'' in function argument
It's only obvious when someone points it out :) fubar is not created because, in the test x 3 returned FALSE, which means the cat function doesn't get used, which means the y arg (fubar - 6) is never required and therefore not evaluated. Evil isn't it ? Michael On 3 December 2010 20:18, Ivan Calandra ivan.calan...@uni-hamburg.de wrote: See below Le 12/3/2010 06:54, Berwin A Turlach a écrit : On Thu, 2 Dec 2010 23:34:02 -0500 David Winsemiusdwinsem...@comcast.net wrote: [...] Erik is telling you that your use of ncol-4 got evaluated to 4 and that the name of the resulting object was ignored, howevert the value of the operation was passed on to matrix which used positional matching since = was not used. Sounds like a fair summary of what Erik said, but it is subtly wrong. R has lazy evaluation of its arguments. There is nothing that forces the assignment to be evaluated and to pass the result into the function. On the contrary, the assignment takes place when the function evaluates the argument. For example: R rm(list=ls(all=TRUE)) R ls() character(0) R foo- function(x, y){ + if (x 3) cat(y, \n) + x} R foo(4, bar- 5) 5 [1] 4 R ls() [1] bar foo R bar [1] 5 R foo(2, fubar- 6) [1] 2 R fubar Error: object 'fubar' not found R ls() [1] bar foo Could you explain what's happening here?! In the first case bar is created, but in the second fubar is not... Why is that? Am I missing something obvious? Usually the problem facing newbies is that they want to save keystrokes and so use = for assignment (also a potential pitfall although not as likely to mess you up as the choice to use the two-keystroke path for argument assignment). On the contrary, the opposite is also very likely. One of my favourite idioms is: plot(fm- lm(y~x, data=some.data)) to (1) fit a model, (2) assign the fitted model to an object and (3) look immediately at diagnostic plots. Students came to me and said that the code in the lab sheet didn't work and they were getting strange error messages about objects not being found. They reassured me that they had typed in exactly what was on the lab sheet. Of course, once I got to their computer and looked at their screen, it was clear that they had typed: plot(fm = lm(y~x, data=some.data)) It's not much more complicated to type it in two lines, but it's much clearer and safer! Cheers, Berwin == Full address Berwin A Turlach Tel.: +61 (8) 6488 3338 (secr) School of Maths and Stats (M019) +61 (8) 6488 3383 (self) The University of Western Australia FAX : +61 (8) 6488 1028 35 Stirling Highway Crawley WA 6009 e-mail: ber...@maths.uwa.edu.au Australia http://www.maths.uwa.edu.au/~berwin __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. Säugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calan...@uni-hamburg.de ** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Maximum Number of Rows in a Dataframe
Hello, Please post a sample of your code so people here can understand what you are trying to do. Michael On 4 December 2010 11:00, rushabhbm rushab...@gmail.com wrote: Guys, I am new to R so please excuse if I am not very clear. My problem is: I have a 'for' loop in which I am defining a Dataframe df with a SQL query. First iteration gives a df with 31 rows(that's correct), however next iterations also gives me max rows as 31. It's kinda stuck at that value. I might have to delete df(how to do that?) after every iteration or if there is any other solution. Thanks for your help, Rushabh -- View this message in context: http://r.789695.n4.nabble.com/Maximum-Number-of-Rows-in-a-Dataframe-tp3071950p3071950.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] testing and ploting t-distribution
Hello Bill, Have a look at the example at the bottom of the help page for ?qqplot Michael On 4 December 2010 11:19, 5...@queensu.ca wrote: Hi there, I am doing a test to see the the residual is distributed in the form of t-distribution and trying to plot the residuals and the t-distribution to compare in the graph. Cheers, Bill - Original Message - From: r-help-requ...@r-project.org Date: Wednesday, December 1, 2010 1:29 am Subject: confirm 1f8ffd55c611e2b0ed4ed4d2b79a8d9b30182a95 To: 5...@queensu.ca Mailing list subscription confirmation notice for mailing list R-help We have received a request from 129.132.148.130 for subscription of your email address, 5...@queensu.ca, to the r-help@r-project.org mailing list. To confirm that you want to be added to this mailinglist, simply reply to this message, keeping the Subject: header intact. Or visit this web page: https://stat.ethz.ch/mailman/confirm/r- help/1f8ffd55c611e2b0ed4ed4d2b79a8d9b30182a95 Or include the following line -- and only the following line -- in a message to r-help-requ...@r-project.org: confirm 1f8ffd55c611e2b0ed4ed4d2b79a8d9b30182a95 Note that simply sending a `reply' to this message should work from most mail readers, since that usually leaves the Subject: line in the right form (additional Re: text in the Subject: is okay). If you do not wish to be subscribed to this list, please simply disregard this message. If you think you are being maliciously subscribed to the list, or have any other questions, send them to r-help-ow...@r-project.org. [[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 specify a fixed intercept for linear model
The offset arg is your friend... x - 1:10 y - 42 + 2*x + rnorm(length(x), 0, 0.5) # we suspect the intercept might be 42 ! lm( y ~ 0 + x, offset=rep(42, length(x))) Michael On 4 December 2010 13:42, cborley87 christopher.bor...@students.plymouth.ac.uk wrote: Hi, Im fitting some simple linear models to data from the olympic running events with lm(Logtime~Logdistance), the models contain an intercept and one slope parameter. I have fitted a separate model for each years data and as the intercept changes very little between years i am trying to simplify the models so that they all have the same intercept (calculated from the mean of the previous intercepts). I have seen a few posts relating to zero intercepts but cannot figure out how to set to a specific value to the parameter. Any help would be greatly appreciated. Thanks. Chris. -- View this message in context: http://r.789695.n4.nabble.com/How-to-specify-a-fixed-intercept-for-linear-model-tp3072044p3072044.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] Arrange elements on a matrix according to rowSums + short 'apply' Q
Hi Aaron, Following up on Ivan's suggestion, if you want the column order to mirror the row order... mo - order(rowSums(MAT), decreasing=TRUE) MAT2 - MAT[mo, mo] Also, you don't need all those extra c() calls when creating inputData, just the outermost one. Regarding your second question, your statements... TMAT - apply(t(MAT), 2, function(X) X/sum(X)) TMAT - t(TMAT) is actually just a complicated way of doing this... TMAT - MAT / rowSums(MAT) You can confirm that by doing it your way and then this... TMAT == MAT / rowSums(MAT) ...and you should see a matrix of TRUE values Michael On 2 December 2010 20:43, Ivan Calandra ivan.calan...@uni-hamburg.de wrote: Hi, Here is a not so easy way to do your first step, but it works: MAT2 - cbind(MAT, rowSums(MAT)) MAT[order(MAT2[,6], decreasing=TRUE),] For the second, I don't know! HTH, Ivan Le 12/2/2010 09:46, Aaron Polhamus a écrit : Greetings, My goal is to create a Markov transition matrix (probability of moving from one state to another) with the 'highest traffic' portion of the matrix occupying the top-left section. Consider the following sample: inputData- c( c(5, 3, 1, 6, 7), c(9, 7, 3, 10, 11), c(1, 2, 3, 4, 5), c(2, 4, 6, 8, 10), c(9, 5, 2, 1, 1) ) MAT- matrix(inputData, nrow = 5, ncol = 5, byrow = TRUE) colnames(MAT)- c(A, B, C, D, E) rownames(MAT)- c(A, B, C, D, E) rowSums(MAT) I wan to re-arrange the elements of this matrix such that the elements with the largest row sums are placed to the top-left, in descending order. Does this make sense? In this case the order I'm looking for would be B, D, A, E, C Any thoughts? As an aside, here is the function I've written to construct the transition matrix. Is there a more elegant way to do this that doesn't involve a double transpose? TMAT- apply(t(MAT), 2, function(X) X/sum(X)) TMAT- t(TMAT) I tried the following: TMAT- apply(MAT, 1, function(X) X/sum(X)) But my the custom function is still getting applied over the columns of the array, rather than the rows. For a check try: rowSums(TMAT) colSums(TMAT) Row sums here should equal 1... Many thanks in advance, Aaron [[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. -- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. Säugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calan...@uni-hamburg.de ** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] missing values
And just to add to Ivan's comment, if you are using the rowSums or colSums functions with a matrix or data.frame they also have the na.rm argument. Michael On 1 December 2010 20:16, Ivan Calandra ivan.calan...@uni-hamburg.de wrote: Hi, (a) sum() and mean() have a na.rm argument that should be set to TRUE. (b) let's try with an example: x - c(1:5, NA, NA, 6:10, NA) x[is.na(x)] - 0 ## replace NAs by 0 HTH, Ivan Le 12/1/2010 10:00, Iasonas Lamprianou a écrit : Dear all, i have spent a lot of time trying to solve this problem, but I am sure that there must be a simple solution. So, as a last resort, I am coming back to you again. I have a dataset with some (almost random) values in many variables. Lets say that the dataset represents the scores of students to test questions. What I need to do is to sum the scores for each student. However, wherever there is a missing (NA) value, I cannot get the total score. How can I compute the total score and the average per question (a) by ignoring the missing responses, (b) by assuming that a missing response is a zero? Thank you for the response Dr. Iasonas Lamprianou Assistant Professor (Educational Research and Evaluation) Department of Education Sciences European University-Cyprus P.O. Box 22006 1516 Nicosia Cyprus Tel.: +357-22-713178 Fax: +357-22-590539 Honorary Research Fellow Department of Education The University of Manchester Oxford Road, Manchester M13 9PL, UK Tel. 0044 161 275 3485 iasonas.lampria...@manchester.ac.uk __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. Säugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calan...@uni-hamburg.de ** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Pass an operator to function
Here is one way... f - function(a, b, op===) { call - call(op, a, b) result - eval(call) # possibly do other stuff result } f(1, 2) [1] FALSE f(1, 2, ) [1] TRUE Michael On 1 December 2010 13:54, randomcz rando...@gmail.com wrote: Hi guys, How to pass an operator to a function. For example, test - function(a, , b) { return(ab) #the operator is passed as an argument } Thanks, -- View this message in context: http://r.789695.n4.nabble.com/Pass-an-operator-to-function-tp3066627p3066627.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] cross tabulate variables by subject id
Hi Marianne, How about this... ac.ad - unstack(dat1, choice ~ cond1:cond2)[, c(A.C, A.D)] acad.xtab - with(ac.ad, table(A.C, A.D)) Michael On 29 November 2010 20:18, Marianne Promberger marianne.promber...@kcl.ac.uk wrote: Dear list, I have data like this: dat1 - data.frame(subject=rep(1:10,2), cond1=rep(c(A,B),each=5), cond2=rep(c(C,D),each=10), choice=sample(0:1,10,replace=TRUE)) I would like to compare subjects' choice for (cond1==A cond2==C) vs (cond1==A cond2==D), using mcnemar.test The ?mcnemar.test example has the data in a matrix: Performance 2nd Survey 1st Survey Approve Disapprove Approve 794 150 Disapprove 86 570 So for my case, I need something like: Choice AC AD 0 1 0 ... 1 Where ... would be the sum of subjects who answered 0 or 1 to AC and/or AD respectively. I can get the first step by making an extra variable: dat1$condnew - paste(dat1$cond1,dat1$cond2,sep=) although I am sure there are more elegant ways, and especially, I am stumped how to fill in the cells of the table. Thanks, Marianne -- Marianne Promberger PhD, King's College London http://promberger.info R version 2.12.0 (2010-10-15) Ubuntu 9.04 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] List of influential points?
Hi Bill, Have a look at the influence.measures function... my.lm - lm( ... ) influence.measures( my.lm ) Hope this helps, Michael On 30 November 2010 00:13, Schwab,Wilhelm K bsch...@anest.ufl.edu wrote: Hello all, I fit a linear model to some data and used plot() to create diagnostic plots for the fit; I am having trouble reading the points that R is flagging as influential. Is there a way to get the list of influential points from the fit or its summary, etc.? Most likely, there are a few points appearing in almost the same place, making it difficult to read from the plots. Bill __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] weighted x variables with glm
Hi Wendy, In case you haven't see it, the glm function accepts an optional weights argument. Michael On 29 November 2010 09:42, Wendy Anderson newhorizonscand...@gmail.com wrote: I have a glm regression (quasi-poisson) of log(mu) on x but I have varying degrees of confidence in the x values, and can attach a numerical weighting to each. Can anyone help me with suggestions of how to analysise this. Is there an R package that would help? Wendy [[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] weighted x variables with glm
In case you haven't see it, the glm function accepts an optional weights argument. Thanks for the reply. But the philosopy behind weighting is the assumption of unequal variance in the y values. In normal regression one assumes that the x values are known without error Wendy Sorry Wendy - I posted my reply prior to engaging my brain (ie. didn't read your question properly). You're talking about Model II / major axis type methods. The smatr package might cater for what you're trying to do. Hope this helps (more). Michael __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] weighted x variables with glm
Hello again Wendy, Actually, the simex package is probably a more useful suggestion... http://www.stat.uni-muenchen.de/~helmut/Texte/Simex_Rnews.pdf Michael On 29 November 2010 13:55, Michael Bedward michael.bedw...@gmail.com wrote: In case you haven't see it, the glm function accepts an optional weights argument. Thanks for the reply. But the philosopy behind weighting is the assumption of unequal variance in the y values. In normal regression one assumes that the x values are known without error Wendy Sorry Wendy - I posted my reply prior to engaging my brain (ie. didn't read your question properly). You're talking about Model II / major axis type methods. The smatr package might cater for what you're trying to do. Hope this helps (more). Michael __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 do I subtract sequential values ?
On 29 November 2010 15:09, Joshua Wiley jwiley.ps...@gmail.com wrote: (I hope I'm like wine and get better with age or) Sigh, me too - but I suspect I'm heading more towards vinegar Michael __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] get list index
Hello Lorezo, Try this... order(sapply(mylist, min))[1] Michael On 26 November 2010 11:23, Lorenzo Cattarino l.cattar...@uq.edu.au wrote: Hi R-users, I have a list mylist - list(c(0.79, 0.92, 0.91, 0.86, 0.96, 0.96, 0.95, 0.94, 0.99), c(0.28, 0.45, 0.59, 0.69, 0.80, 0.87, 0.95, 0.94, 0.98), c(0.29, 0.39, 0.59, 0.69, 0.68, 0.80, 0.93, 0.95, 0.98)) Is there a way to find the index of the list element that contains the lowest value among all the other elements? As the lowest value in each element is the first, the question is actually how to find the lowest 'first' values among the list elements, and then get the index of that element. In my example the list element would be (because the value is 0.28): [[2]] [1] 0.28 0.45 0.59 0.69 0.80 0.87 0.95 0.94 0.98 and the position of course 2. I am looking for the index because I would like to subset the list afterwards (e.g. mylist[[2]]) and extract that element (i.e. the whole vector). Thanks for your help Lorenzo Lorenzo Cattarino PhD Candidate (Confirmed) Landscape Ecology and Conservation Group Centre for Spatial Environmental Research School of Geography, Planning and Environmental Management The University of Queensland Brisbane, Queensland, 4072, Australia Telephone 61-7-3365 4370, Mobile 0410884610 Email l.cattar...@uq.edu.au Internet http://www.gpem.uq.edu.au/cser http://www.gpem.uq.edu.au/cser [[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] Print to the cmd line in a script
Is this what you want ? printCount - function(N) { for (i in 1:N) { cat(i, \n) } } Depending on your platform, output from the print function may not appear until after your function has finished. You can try using flush.console() to give it a nudge... printCount - function(N) { for (i in 1:N) { print(i) flush.console() } } Michael On 24 November 2010 21:15, Joel joda2...@student.uu.se wrote: Hi Ive written a script that loopes throu some data, but what I want to do is to print to the cmd line how many times it has looped to the cmd line. And as it is just a counter from 1 this should be quite easy :P but ive tried things like print(counter) and so on but it dossent print anything to the cmd prompt, so what is the command to print from inside a script? -- View this message in context: http://r.789695.n4.nabble.com/Print-to-the-cmd-line-in-a-script-tp3057009p3057009.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] compare GLM coefficients
Hello Kayce, My (very basic) understanding is that you can't directly compare the coefficients across models that have different response variables, nor could you use AIC and similar metrics of model goodness of fit. Instead, I think you have to carefully define what you mean by reveal similar population trends. If you treat the model with the count response as your reference, and it predicts (for example) population decline of magnitude X over period T, then you can investigate to what extent this same trend is retrieved by the presence response model. But the specifics of the comparison(s) should be closely tied to the population behaviours / syndromes / critical points that you are most interested in. If there are multiple behaviours of interest you want to know to what extent the presence data perform as well as the count data for each of them. That's my general take on the style of the approach. Hopefully others here will have more detailed and knowledgable comments for you. Michael On 23 November 2010 17:20, Kayce anderson kayc...@gmail.com wrote: I have a data set of repeated abundance counts over time. I am investigating whether count data reduced to presence-absence (presence) data will reveal similar population trends. I am using a negative binomial distribution for the glm (package MASS) because the count data contains many zeros and extreme values. count and presence are annual sums for each metric. I have also included sampling effort (visits) as an independent variable because sampling varies between 29-33 visits per year. My models are: glm.nb(count ~ year + visits) and glm.nb(presence ~ year + visits) I would like to test whether the coefficients for year are significantly different between models. Please advise me on the best method to make such a comparison. Thank you, Kayce [[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] need smooth cdf lines
Ah, this looks like Australian data :) One simple way would be to use the lowess function and fiddle with the f parameter (like bandwidth). Michael On 22 November 2010 14:18, Roslina Zakaria zrosl...@yahoo.com wrote: Hi, I would like to overlap the cdf curve for observed and generated data Here is my code: plot(cdf,main =CDF of the sum for winter season-Hume,cex.axis=1.2,xlab=Rainfall (mm), xaxs=i,yaxs=i,col=c(black,red), lty=c(1,1),ylab=Cumulative probability, xlim=c(0,800),lwd=1) lines(ecdf(datobs)) legend(topright, legend = c(observed,fitted), col = c(black,red), pch=c(NA,NA), lty = c(1, 1), lwd=c(3,3),bty=n, pt.cex=2) It gives me the plots but it is not smooth. How do I adjust so that I will get two smooth cumulative density curves. Thank you for any help given. [[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] R help
Hi Ahmed, Does 'quadratic plateau' model refer to a chage-point or bent-cable regression with quadratic on one side and an asymptote on the other ? If so, you might like to look at the bentcableAR package. There is a background article here: http://faculty.washington.edu/gchiu/Articles/bentcable-jasa.pdf Michael On 22 November 2010 06:06, Ahmed Attia ahmedati...@gmail.com wrote: Dear All, I'm a beginner user in R and I would like to make a quadratic and plateau model in R. Can you help please with an example? Thanks so much -- Ahmed M. Attia Assistant Lecturer El-Khattara farm Station Agronomy Dept., Zgazig Univ., Egypt Visiting Scientist Haskell Agricultural laboratory Agronomy and Horticultural Dept., Univ. of Nebraska-Lincoln ahmeda...@zu.edu.eg aattiamoham...@unlnotes.unl.edu Cell phone: 4023604178 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] R help
Sorry, chage-point should be change-point On 22 November 2010 14:44, Michael Bedward michael.bedw...@gmail.com wrote: Hi Ahmed, Does 'quadratic plateau' model refer to a chage-point or bent-cable regression with quadratic on one side and an asymptote on the other ? If so, you might like to look at the bentcableAR package. There is a background article here: http://faculty.washington.edu/gchiu/Articles/bentcable-jasa.pdf Michael On 22 November 2010 06:06, Ahmed Attia ahmedati...@gmail.com wrote: Dear All, I'm a beginner user in R and I would like to make a quadratic and plateau model in R. Can you help please with an example? Thanks so much -- Ahmed M. Attia Assistant Lecturer El-Khattara farm Station Agronomy Dept., Zgazig Univ., Egypt Visiting Scientist Haskell Agricultural laboratory Agronomy and Horticultural Dept., Univ. of Nebraska-Lincoln ahmeda...@zu.edu.eg aattiamoham...@unlnotes.unl.edu Cell phone: 4023604178 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] R help
Hi Ahmed, Please reply via the list - you'll get better answers because most people there know more than me :) Here is a toy example that is similar to the example graph you sent me... # generate data: linear up to a change point, then plateau with constant std dev x - 20:70 y - rnorm(length(x), c(20:50 * 2, rep(100, 20)), 10) plot(x, y) library(segmented) # naive linear regression y.lm - lm(y ~ x) # convert to segmented (chage-point) regression with # initial change-point (psi) deliberately away from # known cp in artificial data y.seg - segmented(y.lm, seg.Z=~x, psi=40) Results are: Call: segmented.lm(obj = y.lm, seg.Z = ~x, psi = 40) Meaningful coefficients of the linear terms: (Intercept)x U1.x 11.0061.684 -1.688 Estimated Break-Point(s) psi1.x : 51.4 There is an article about the segmented package in Rnews vol 8/1: http://cran.r-project.org/doc/Rnews/Rnews_2008-1.pdf Hope this helps. Michael On 22 November 2010 15:03, Ahmed Attia ahmedati...@gmail.com wrote: Hi Dr Michael, Attached is an example for the linear plus plateua model but in SAS, this exactly what I need to do in R. Ahmed On Sun, Nov 21, 2010 at 7:44 PM, Michael Bedward michael.bedw...@gmail.com wrote: Hi Ahmed, Does 'quadratic plateau' model refer to a chage-point or bent-cable regression with quadratic on one side and an asymptote on the other ? If so, you might like to look at the bentcableAR package. There is a background article here: http://faculty.washington.edu/gchiu/Articles/bentcable-jasa.pdf Michael On 22 November 2010 06:06, Ahmed Attia ahmedati...@gmail.com wrote: Dear All, I'm a beginner user in R and I would like to make a quadratic and plateau model in R. Can you help please with an example? Thanks so much -- Ahmed M. Attia Assistant Lecturer El-Khattara farm Station Agronomy Dept., Zgazig Univ., Egypt Visiting Scientist Haskell Agricultural laboratory Agronomy and Horticultural Dept., Univ. of Nebraska-Lincoln ahmeda...@zu.edu.eg aattiamoham...@unlnotes.unl.edu Cell phone: 4023604178 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Ahmed M. Attia Assistant Lecturer El-Khattara farm Station Agronomy Dept., Zgazig Univ., Egypt Visiting Scientist Haskell Agricultural laboratory Agronomy and Horticultural Dept., Univ. of Nebraska-Lincoln ahmeda...@zu.edu.eg aattiamoham...@unlnotes.unl.edu Cell phone: 4023604178 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Splitting 3D matrix from for loop to generate/save 2D matrices
Hi Hana, Use the paste function to create your file names. for ( i in 1:dim(M)[1] ) save( M[i,,], file=paste(M_, i, .img, sep=) ) Alternatively, use the sprintf function to get names with leading zeroes for easier sorting of files: for (i in 1:dim(M)[1] ) save( M[i,,], file=sprintf(M_%03d.img, i) ) Michael On 22 November 2010 16:27, Hana Lee hana...@email.unc.edu wrote: Hi! I have a matrix called M with dimension (586,100,100). I would like to split and save this into 586 matrices with dimension 100 by 100. I have tried the following for loops but couldn't get it work.. l-dim(M)[1] for (i in (1:l)){ save(M[i,,],file = M_[i].img) } Can somebody help me with this? Thanks! Hana Lee [[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] (no subject)
Type data() to list the numerous example data sets included with the standard R distribution. Michael On 20 November 2010 20:42, Stephen Liu sati...@yahoo.com wrote: Hi folks, Please advise where can I down free data files for learning R? Google search brought me many, not easy for to screen. TIA B.R. Stephen L __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] (no subject)
On 20 November 2010 20:57, Stephen Liu sati...@yahoo.com wrote: Hi Michael, Thanks for your advice. data() only displays a list of files. Is there an easy to show the brief summary of files rather than calling each file. What sort of summary do you want ? The data() command should display names and a brief description of the type / source. Besides I need the files on Excel format/extension. Where can I find them? I don't use Excel - sorry. Michael __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 catch warnings
Sorry Alex, I don't quite follow what you want. What do you mean by it would be nicer if the execution is paused so to try to print more values ? Michael On 18 November 2010 21:11, Alaios ala...@yahoo.com wrote: Dear Michael, I would like to thank you for your reply. This line made things easier. One more question what If I want to halt or pause the program when a warning happens? Right now I get only a message printed but it would be nicer if the execution is paused so to try to print more values. Best REgards Alex --- On Thu, 11/18/10, Michael Bedward michael.bedw...@gmail.com wrote: From: Michael Bedward michael.bedw...@gmail.com Subject: Re: [R] How to catch warnings To: Alaios ala...@yahoo.com Cc: Rhelp r-help@r-project.org Date: Thursday, November 18, 2010, 1:26 AM Hi Alex, Something like this ? x - 1:4 y - list(good=2:5, bad=3:5) for (yy in y) { tryCatch( x - cbind(x, yy), warning=function(w) cat(problem values: , yy, \n) ) } Michael On 18 November 2010 03:19, Alaios ala...@yahoo.com wrote: Hello when my code executes I receive the message that were some warnings. I want to catch warning messages at run time so to print some local variables and try to understand why this warning happens. I searched on internet and I tried withCallingHandlers( which seems to work but as I used Rkward the result is awful. I get a prompt to copy paste a value only while at the same time my background window with my code is not anymore accessible. Actually I get the warning that In f[cbind(shweights[, 1], shweights[, 2])] * shweights[, ... : longer object length is not a multiple of shorter object length and what I want is when the warning appear to be able to print the value shweights[,1] shweights[,2] to see what is going on. Any easier way to do that? Best 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] How to catch warnings
Like this ? f - function() { # on first warning will print to console and return results so far out - c(1,2,3,4) extras - list(good=c(2,3,4,5), bad=c(9,9,9), ugly=c(9,9,9,9,9)) ok - TRUE for (ex in extras) { tryCatch(out - cbind(out, ex), warning=function(w) { cat(problem values:, ex, \n) ok - FALSE } ) if (!ok) break } out } Michael On 18 November 2010 22:14, Alaios ala...@yahoo.com wrote: The best would be the R when a warning stop. To issue a warnign at console stop the execution at the point it was and let your write commands in console to check what was going on. Is that possible? Regards Alex --- On Thu, 11/18/10, Michael Bedward michael.bedw...@gmail.com wrote: From: Michael Bedward michael.bedw...@gmail.com Subject: Re: [R] How to catch warnings To: Alaios ala...@yahoo.com Cc: Rhelp r-help@r-project.org Date: Thursday, November 18, 2010, 10:17 AM Sorry Alex, I don't quite follow what you want. What do you mean by it would be nicer if the execution is paused so to try to print more values ? Michael On 18 November 2010 21:11, Alaios ala...@yahoo.com wrote: Dear Michael, I would like to thank you for your reply. This line made things easier. One more question what If I want to halt or pause the program when a warning happens? Right now I get only a message printed but it would be nicer if the execution is paused so to try to print more values. Best REgards Alex --- On Thu, 11/18/10, Michael Bedward michael.bedw...@gmail.com wrote: From: Michael Bedward michael.bedw...@gmail.com Subject: Re: [R] How to catch warnings To: Alaios ala...@yahoo.com Cc: Rhelp r-help@r-project.org Date: Thursday, November 18, 2010, 1:26 AM Hi Alex, Something like this ? x - 1:4 y - list(good=2:5, bad=3:5) for (yy in y) { tryCatch( x - cbind(x, yy), warning=function(w) cat(problem values: , yy, \n) ) } Michael On 18 November 2010 03:19, Alaios ala...@yahoo.com wrote: Hello when my code executes I receive the message that were some warnings. I want to catch warning messages at run time so to print some local variables and try to understand why this warning happens. I searched on internet and I tried withCallingHandlers( which seems to work but as I used Rkward the result is awful. I get a prompt to copy paste a value only while at the same time my background window with my code is not anymore accessible. Actually I get the warning that In f[cbind(shweights[, 1], shweights[, 2])] * shweights[, ... : longer object length is not a multiple of shorter object length and what I want is when the warning appear to be able to print the value shweights[,1] shweights[,2] to see what is going on. Any easier way to do that? Best 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] How to catch warnings
Hi Alex, Something like this ? x - 1:4 y - list(good=2:5, bad=3:5) for (yy in y) { tryCatch( x - cbind(x, yy), warning=function(w) cat(problem values: , yy, \n) ) } Michael On 18 November 2010 03:19, Alaios ala...@yahoo.com wrote: Hello when my code executes I receive the message that were some warnings. I want to catch warning messages at run time so to print some local variables and try to understand why this warning happens. I searched on internet and I tried withCallingHandlers( which seems to work but as I used Rkward the result is awful. I get a prompt to copy paste a value only while at the same time my background window with my code is not anymore accessible. Actually I get the warning that In f[cbind(shweights[, 1], shweights[, 2])] * shweights[, ... : longer object length is not a multiple of shorter object length and what I want is when the warning appear to be able to print the value shweights[,1] shweights[,2] to see what is going on. Any easier way to do that? Best 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] Sampling problem
On 16 November 2010 16:10, wangwallace talentt...@gmail.com wrote: Michael, I really appreciate your help. but I got the following error message when I wan trying to run the function written by you: Error in out[i, ] - apply(help[, c(grp1 + 1, grp2 + 5)], 2, sample, 1) : number of items to replace is not a multiple of replacement length Did the data.frame or matrix you were sampling have the same general form as the example you posted previously ? Can you give me a small example that causes the error ? I am not quite sure why would this happen. As a novice of R, these functions are kinda complex for me. I am wondering if it is doable without using loops like that. I wasn't sure exactly what you wanted so the function was meant to be general and easy to modify. It is often possible to use constructs other than loops in R, though that doesn't mean the code will always be either faster or clearer. But you'll need to describe your requirements in more precise terms (short, clear examples are good) for folks here to suggest methods. Again, thank you so much!!! No worries. If you can provide an example that generates the error we should be able to get further. Michael __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] simulate survival data using median survival time
Hi Kere, Step 1 is choose an appropriate distribution :) Do you have one in mind ? Or are you interested in examining the effects of survival times having the same mean but generated with alternative distributions ? One ready-rolled alternative is the SimSurv method in package prodlim. To find (many) others you can use the sos package... library(sos) findFn(simulate survival data) Which returns 66 matches when I just tried it. Michael On 16 November 2010 18:04, Kere Klein k.kl...@uq.edu.au wrote: Dear All, I like to know how to simulate survival data using median (or mean) survival time. Any help will be greatly appreciated. Best wishes, Kere Kerenaftali Klein PhD| Biostatistician | Queensland Clinical Trials Biostatistics Centre The University of Queensland | School of Population Health | Building 33, Level 1| Princess Alexandra Hospital |Ipswich Road | Woolloongabba QLD 4102 | Australia Ph: +61 7 3176 3062| Fax: +61 7 3176 6826 | Email: k.kl...@uq.edu.au | Web: http://www.sph.uq.edu.au/qctbc __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Partition of a set
Hi Diana, Yes, this seems to be a little bug in the setparts function. The following is a modified version which should work for any x 0. You'll see I've just changed a couple of lines... setparts2 - function (x) { if (length(x) == 1) { if (x 1) stop(if single value, x must be = 1) else if (x == 1) out - matrix(1, 1, 1) else return(Recall(parts(x))) } if (is.matrix(x)) { out - apply(x, 2, setparts) if (is.list(out)) out - do.call(cbind, out) } else { x - sort(x[x 0], decreasing = TRUE) num.of.parts - factorial(sum(x))/(prod(c(factorial(x), factorial(table(x) out - .C(wrap, as.integer(x), as.integer(length(x)), ans = integer(sum(x) * num.of.parts), PACKAGE = partitions)$ans dim(out) - c(sum(x), num.of.parts) } return(as.partition(out)) } If that works OK you might like to contact the package author about it. Michael On 17 November 2010 01:59, Diana Tichy d.ti...@mathematik.uni-wuerzburg.de wrote: Hello Michael I am once again contacting you, since I found out, that your function listParts does not work for index set of length lower or equal than 2. This is not your fault, since the function setparts does not work correct for a value n= 2. But I do not know why. Do you have an answer for that problem? If not, do not spend to much time, since I can solve the problem by implementing list with all partitions of set {1,2} and {1}. (only two partitions for {1,2} ...) Best wishes Diana Am 12.11.2010 12:06, schrieb Michael Bedward: You're welcome Diana - glad to help Michael On 12 November 2010 22:00, Diana Tichy d.ti...@mathematik.uni-wuerzburg.de wrote: Hi Michael Wow, yes this is exactly what I need. I want to sum up over all partitions of a set of length n. Now I can use a for loop which calls the elements of the list which is returned by your function listParts! Hence, you helped me a lot with your experience! Thank you! Diana Am 12.11.2010 11:42, schrieb Michael Bedward: listParts- function(n) { # Generates a list of lists representing the partitions # of an integer n require(partitions) x- 1:n apply(setparts(n), 2, function(pp) tapply(x, pp, function(xx) list(xx))) } -- Diana Tichy (geb. Stöhr) Lehrstuhl für Statistik Institut für Mathematik Universität Würzburg Am Hubland 97074 Würzburg Raum R110, Mathematikgebäude Tel.: 0931-31 85027 d.ti...@mathematik.uni-wuerzburg.de http://statistik.mathematik.uni-wuerzburg.de/~tichy -- Diana Tichy (geb. Stöhr) Lehrstuhl für Statistik Institut für Mathematik Universität Würzburg Am Hubland 97074 Würzburg Raum R110, Mathematikgebäude Tel.: 0931-31 85027 d.ti...@mathematik.uni-wuerzburg.de http://statistik.mathematik.uni-wuerzburg.de/~tichy __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Population abundance, change point
Hi Nick, I've used MCMC to fit change point regressions to a variety of ecological data and prefer this approach to strucchange and similar because I feel I have more control over the model, ie. I find it easier to tailor the form of the model to biological / demographic processes. I also find the imputing of missing response data more straightforward with MCMC - at least when they are some distance from potential changepoint(s). Most recently I've used JAGS via the rjags package to do such models. As an example, here is a toy model that looks for a single change-point after which there is a change in slope (time trend) in the data model { # y[i] is population data # N is number of (regular) observations for (i in 1:N) { y[i] ~ dnorm(mu[i], tau) mu[i] - b0 + i*(b1 + b1cp*step(cp - i)) } # intercept b0 ~ dnorm(0, 1.0e-6) # pre-change slope b1 ~ dnorm(0, 1.0e-6) # post-change slope b1cp ~ dnorm(0, 1.0e-6) # change point location (time) cp ~ dunif(2, N-1) # variance (assumed equal either side of change) sd ~ dunif(0.01, 100) tau - pow(sd, -2) } The step() function returns 1 when its argument is positive, and 0 otherwise. As a result, the slope is b1 before the change (time = cp) and b1 + b1cp after the change. You can easily modify this model to, for instance... - assume 0 slope but different intercepts either side of the change - allow for change in variance; or variance proportional to mean etc. - impute missing response data Michael On 17 November 2010 09:30, Nicholas M. Caruso carus...@gmail.com wrote: I am trying to understand my population abundance data and am looking into analyses of change point to try and determine, at approximately what point do populations begin to change (either decline or increasing). Can anyone offer suggestions on ways to go about this? I have looked into bcp and strucchange packages but am not completely convinced that these are appropriate for my data. Here is an example of what type of data I have Year of survey (continuous variable) 1960 - 2009 (there are gaps in the surveys (e.g., there were no surveys from 2002-2004) Relative abundance of salamanders during the survey periods Thanks for your help, Nick -- Nicholas M Caruso Graduate Student CLFS-Biology 4219 Biology-Psychology Building University of Maryland, College Park, MD 20742-5815 -- I learned something of myself in the woods today, and walked out pleased for having made the acquaintance. [[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] Sampling problem
Hello, Is this what you want ? sampleX - function(X, nGrp1, nsamples) # X is matrix or data.frame with cols for two groups of variables # with grp1 in cols 2:5 and grp2 in cols 6:9 # # nGrp1 - number of variables to sample from group 1 # # nsamples - number of rows in output matrix if (nGrp1 = 4) stop(can't sample all group 1 variables) out - matrix(0, nsamples, nGrp1+1) for (i in 1:nsamples) { # choose grp1 vars to sample grp1 - sample(4, nGrp1) # choose complentary grp2 var to sample grp2 - sample((1:4)[-grp1], 1) # sample 1 value from each var out[i, ] - apply(X[,c(grp1+1, grp2+5)], 2, sample, 1) } out } Michael On 16 November 2010 07:59, wangwallace talentt...@gmail.com wrote: Hey, I am hoping someone can help me with a sampling question. I have a data frame of 8 variables (the first column is the subjects' id): SubID CSE1 CSE2 CSE3 CSE4 WSE1 WSE2 WSE3 WSE4 1 6 5 6 2 6 2 2 4 2 6 4 7 2 6 6 2 3 3 5 5 5 5 5 5 4 5 4 5 4 3 4 4 4 5 2 5 5 6 7 5 6 4 4 1 6 5 4 3 6 4 3 7 3 7 3 6 6 3 6 5 2 1 8 3 6 6 3 6 5 4 7 the 6 variables are categorized into two groups with CSE1, CSE2, CSE3, and CSE4 in one group and the rest in another group. sample(data[,2:4],2,replace=FALSE) CSE1 CSE2 1 6 5 2 6 4 3 5 5 4 5 4 5 5 6 6 5 4 7 3 6 8 3 6 Now I want to sample 1 column from another group of variables (i.e., WSE1, WSE2, WSE3, WSE4), but I want to restrict a vector I am going to sample from to only those columns that are not correspond to GROUP 1 variables I have sampled. That is, I want to sample a column from WSE3, WSE4 Columns corresponding to CSE1 and CSE2 (i.e., WSE1, WSE2) need to be dropped. How can I do this? what if I want to repeat this whole process (drawing 2 random columns from CSE1, CSE2, CSE3, and CSE4 first, AND then another random column from WSE1, WSE2, WSE3, and WSE4) for 1000 times. any ideas? Many thanks in advance!! -- View this message in context: http://r.789695.n4.nabble.com/Sampling-problem-tp3043804p3043804.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] How to set an argument such that a function treats it as missing?
Hello Marius, NULL is not the same as missing. You could something like this in various ways. Here are a couple... g - function(x) { if (missing(x)) { f() } else { f(x) } } or change f to detect null args g - function(x) { if (missing(x)) { x - NULL } f(x) } f - function(x) { if (missing(x) | is.null(x)) { // do something } } Michael On 13 November 2010 19:14, Marius Hofert m_hof...@web.de wrote: Dear expeRts, I would like to call a function f from a function g with or without an argument. I use missing() to check if the argument is given. If it is not given, can I set it to anything such that the following function call (to f) behaves as if the argument isn't given? It's probably best described by a minimal example (see below). The reason why I want to do this is, that I do not have to distinguish between the cases when the argument is given or not. By setting it to something (what?) in the latter case, I can use the same code in the subsequent part of the function. Cheers, Marius f - function(x) if(missing(x)) print(f: missing x) else print(x) g - function(x){ if(missing(x)){ print(g: missing x) x - NULL # I try to set it to something here such that... } f(x) # ... this call to f behaves like f() } g() # should print f: missing x (is this possible?) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 set an argument such that a function treats it as missing?
Or yet another way which is (I think) a bit closer to your requirement... f - function(x) { if (missing(x)) cat(x is missing \n) else cat(x was provided \n) } g - function(x) { if (missing(x)) fcall - call(f) else fcall - call(f, x) eval(fcall) } On 13 November 2010 20:24, Michael Bedward michael.bedw...@gmail.com wrote: Hello Marius, NULL is not the same as missing. You could something like this in various ways. Here are a couple... g - function(x) { if (missing(x)) { f() } else { f(x) } } or change f to detect null args g - function(x) { if (missing(x)) { x - NULL } f(x) } f - function(x) { if (missing(x) | is.null(x)) { // do something } } Michael On 13 November 2010 19:14, Marius Hofert m_hof...@web.de wrote: Dear expeRts, I would like to call a function f from a function g with or without an argument. I use missing() to check if the argument is given. If it is not given, can I set it to anything such that the following function call (to f) behaves as if the argument isn't given? It's probably best described by a minimal example (see below). The reason why I want to do this is, that I do not have to distinguish between the cases when the argument is given or not. By setting it to something (what?) in the latter case, I can use the same code in the subsequent part of the function. Cheers, Marius f - function(x) if(missing(x)) print(f: missing x) else print(x) g - function(x){ if(missing(x)){ print(g: missing x) x - NULL # I try to set it to something here such that... } f(x) # ... this call to f behaves like f() } g() # should print f: missing x (is this possible?) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Partition of a set
Hi Diana, Have a look at the setparts function in the partitions package. Michael On 12 November 2010 20:03, Diana d.ti...@mathematik.uni-wuerzburg.de wrote: Hi I am new on this forum. I am searching for a function in R which provides all partitions of a set, say for the set {1,2,3} you get {{1,2,3}} {1,{2,3}} {2,{1,3}} {3,{1,2}} {{1},{2},{3}} . The number of partitions of a set is given by Bellsche`s number. The number of possibilities of length (this is the number of subsets included in the partition) of the partition is given by the partition function. But I need a list containg all possible partitions of an index set {1,...,d}, d arbitrary. I have already looked at the FAQs but I did not found. Sorry. Can anybody help me? Thanks a lot! Diana Tichy -- View this message in context: http://r.789695.n4.nabble.com/Partition-of-a-set-tp3039291p3039291.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] Adding meta-data when creating objects. e.g: changing - so to (for example) add creation time - how-to and pros/cons?
Hi Ivan, I had already seen your solution (that does work). If you're right about the issue in my function, then the error message is confusing ('could not find the function get- '). Moreover, I assign()ed and get() x from the .GlobalEnv, so there shouldn't be a problem with scoping, right? Ah, my apologies. I didn't notice that you were passing in a character string as the x arg rather than a name as in my version. Consider this... # from prompt, create object and set attribute foo - 123 attr(foo, bar) - 42 # now this will work without error attr(get(foo), bar) # but this won't attr(get(foo), bar) - 99 It provokes the error: target of assignment expands to non-language object. This is a bit different to the error you get trying the same thing from within your function, but I'm guessing that both are caused by the same thing. Perhaps the get function call is not fully evaluated before the assignment is attempted in the attr function ? As I say, I'm just guessing here. It's just that R doesn't behave as I expect, so I'd like to understand where I'm wrong. Me too but I suspect that we'd probably have to delve into the sources to work it out - or better, hope that someone else here can tell us the answer :) Michael __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Partition of a set
Hi Diane, Does this do what you want ? listParts - function(n) { # Generates a list of lists representing the partitions # of an integer n require(partitions) x - 1:n apply(setparts(n), 2, function(pp) tapply(x, pp, function(xx) list(xx))) } Michael On 12 November 2010 21:15, Diana Tichy d.ti...@mathematik.uni-wuerzburg.de wrote: Hello Michael once again. Now I can use print.partition which tells me, how to generate, e.g., all possible 5 partitions of {1,2,3}. But this generation is still complicated. Does there already exist a function doing that for me, e.g. returns a list, where the first element contains the one-elemnt partion, the second to fourth the three 2-element-partions and the fifth the 3-element partition? Do you know or is it my job to implement this by myself (o.k. but time-consuming ..) Best wishes Diana Am 12.11.2010 10:32, schrieb Michael Bedward: Hi Diana, Have a look at the setparts function in the partitions package. Michael On 12 November 2010 20:03, Dianad.ti...@mathematik.uni-wuerzburg.de wrote: Hi I am new on this forum. I am searching for a function in R which provides all partitions of a set, say for the set {1,2,3} you get {{1,2,3}} {1,{2,3}} {2,{1,3}} {3,{1,2}} {{1},{2},{3}} . The number of partitions of a set is given by Bellsche`s number. The number of possibilities of length (this is the number of subsets included in the partition) of the partition is given by the partition function. But I need a list containg all possible partitions of an index set {1,...,d}, d arbitrary. I have already looked at the FAQs but I did not found. Sorry. Can anybody help me? Thanks a lot! Diana Tichy -- View this message in context: http://r.789695.n4.nabble.com/Partition-of-a-set-tp3039291p3039291.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. -- Diana Tichy (geb. Stöhr) Lehrstuhl für Statistik Institut für Mathematik Universität Würzburg Am Hubland 97074 Würzburg Raum R110, Mathematikgebäude Tel.: 0931-31 85027 d.ti...@mathematik.uni-wuerzburg.de http://statistik.mathematik.uni-wuerzburg.de/~tichy __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Vector
Fancy that... vector spam :) Michael On 12 November 2010 20:30, Jeff Musgrave j...@teamteach2.com wrote: Now you and your Vector Team can make more money. Offer your current client base a chance to buy and sell a product in high demand. An item that increases in value every day. Are you ready for this no-inventory, unique product? Call us at 800.945.3360 to find out more. Don’t let Vector control your income. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Populating then sorting a matrix and/or data.frame
All values in a matrix are the same type, so if you've set up a matrix with a character column then your numeric values will also be stored as character. That would explain why they are being converted to factors. It would also explain why your query isn't working. Michael On 11 November 2010 18:40, Noah Silverman n...@smartmediacorp.com wrote: That was a typo. It should have read: results[results$one 100,] It does still fail. There is ONE column that is text. So my guess is that R is seeing that and assuming that the entire data.frame should be factors. -N On 11/10/10 11:16 PM, Michael Bedward wrote: Hello Noah, If you set these names... names(results)- c(one, two, three) this won't work... results[results$c 100,] because you don't have a column called c (unless that's just a typo in your post). I tried making it a data.frame with foo- data.frame(results) But that converted all the numeric values to factors!!! Not sure what's going on there. If 'results' is a numeric matrix you should get a data.frame with numeric cols since under the hood this is just calling the as.data.frame function. Michael On 11 November 2010 16:02, Noah Silvermann...@smartmediacorp.com wrote: Hi, I have a process in R that produces a lot of output. My plan was to build up a matrix or data.frame row by row, so that I'll have a nice object with all the resulting data. I started with: results- matrix(ncol=3) names(results)- c(one, two, three) Then, when looping through the data: results- rbind(results, c(a,b,c)) This seems to work fine. BUT, my problem arises when I want to filter, sort, etc. I tried (thinking like a data.frame): results[results$c 100,] But that fails. I tried making it a data.frame with foo- data.frame(results) But that converted all the numeric values to factors!!! Which causes a whole mess of problems. Any ideas?? -N __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 get a specific named element in a nested list
Hi Friedericksen, This function will do it. No doubt there are more elegant ways :) rmatch - function(x, name) { pos - match(name, names(x)) if (!is.na(pos)) return(x[[pos]]) for (el in x) { if (class(el) == list) { out - getEl(el, name) if (!is.null(out)) return(out) } } } Michael On 11 November 2010 19:05, Friedericksen Hope friedericksen.h...@gmail.com wrote: Hello, I have a nested named list structure, like the following: x - list( list( list(df1,df2) list(df3, list(df4,df5)) list(df6,df7))) with df1...d7 as data frames. Every data frame is named. Is there a way to get a specific named element in x? so, for example, x[[c(df5)]] gives me the data frame 5? Thank you in advance! Best, Friedericksen __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Populating then sorting a matrix and/or data.frame
You can use rbind as in your original post, but if you've got a mix of character and numeric data start with a data.frame rather than a matrix. Michael On 11 November 2010 20:30, Noah Silverman n...@smartmediacorp.com wrote: That makes perfect sense. Since I need to build up the results table sequentially as I iterate through the data, how would you recommend it?? Thanks, -N On 11/11/10 12:03 AM, Michael Bedward wrote: All values in a matrix are the same type, so if you've set up a matrix with a character column then your numeric values will also be stored as character. That would explain why they are being converted to factors. It would also explain why your query isn't working. Michael On 11 November 2010 18:40, Noah Silvermann...@smartmediacorp.com wrote: That was a typo. It should have read: results[results$one 100,] It does still fail. There is ONE column that is text. So my guess is that R is seeing that and assuming that the entire data.frame should be factors. -N On 11/10/10 11:16 PM, Michael Bedward wrote: Hello Noah, If you set these names... names(results)- c(one, two, three) this won't work... results[results$c 100,] because you don't have a column called c (unless that's just a typo in your post). I tried making it a data.frame with foo- data.frame(results) But that converted all the numeric values to factors!!! Not sure what's going on there. If 'results' is a numeric matrix you should get a data.frame with numeric cols since under the hood this is just calling the as.data.frame function. Michael On 11 November 2010 16:02, Noah Silvermann...@smartmediacorp.com wrote: Hi, I have a process in R that produces a lot of output. My plan was to build up a matrix or data.frame row by row, so that I'll have a nice object with all the resulting data. I started with: results- matrix(ncol=3) names(results)- c(one, two, three) Then, when looping through the data: results- rbind(results, c(a,b,c)) This seems to work fine. BUT, my problem arises when I want to filter, sort, etc. I tried (thinking like a data.frame): results[results$c 100,] But that fails. I tried making it a data.frame with foo- data.frame(results) But that converted all the numeric values to factors!!! Which causes a whole mess of problems. Any ideas?? -N __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Adding meta-data when creating objects. e.g: changing - so to (for example) add creation time - how-to and pros/cons?
Hi Tal, Here's a way of doing the first bit... assign2 - function(x, ...) { xname - deparse(substitute(x)) assign(xname, ...) x - get(xname) attr(x, creation.time) - Sys.time() assign(xname, x, pos=.GlobalEnv) } Michael On 11 November 2010 20:37, Tal Galili tal.gal...@gmail.com wrote: My objective is to start having meta-data on objects that I create. For example, consider the following function: assign2 - function(x, ...) { assign(x, ...) attr(x, creation time) - Sys.time() x - x } assign2(x, 1:4) assign2 assigns to x the vector 1:4, and it then also adds the creation time of the object. (Hat tip goes to Peter Alspach for pointing me to the concept of adding meta data to an object using attr) But this function has several major limitations: 1) It will not work for any assignment other then x. For example assign2(y, 1:4) Doesn't work. How might this be fixed ? 2) This function will probably need to also search the parent environment if the variable already exists. If it does, then there should be a update date instead of creation date. But for that to work, I'll need a solution for problem 1. 3) How will this handle a case when we are updating only a subset of the items? (for example: assign2(x[1:2], 8:9) ) 4) My real intention is to somehow change the - operator (not simply the assign). I am unsure as to how to do that. 5) Are there any major pros/cons to the adding of such meta-data to objects? (for example, excessive overhead on memory/performance) 6) Is there already some system that knows how to do this in R (which I am simply ignorant about)? Thanks for following through, and for any suggestions/thoughts you might have. Best, Tal Contact Details:--- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) -- [[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] Adding meta-data when creating objects. e.g: changing - so to (for example) add creation time - how-to and pros/cons?
As a hack you could do this... assign(=, assign2) Michael On 11 November 2010 21:30, Barry Rowlingson b.rowling...@lancaster.ac.uk wrote: On Thu, Nov 11, 2010 at 9:37 AM, Tal Galili tal.gal...@gmail.com wrote: 4) My real intention is to somehow change the - operator (not simply the assign). I am unsure as to how to do that. 5) Are there any major pros/cons to the adding of such meta-data to objects? (for example, excessive overhead on memory/performance) I had a go at doing (4) a few years back. The major problem I had was that if you do: y - 1:10 x - y with a - operator that sets a timestamp then: identical(x,y) is FALSE. I implemented timestamping by adding an attribute to objects during assigment by modifying the C source, and then lots and lots of R's tests failed during build because identical things were no longer identical. Might be better to store your metadata in a separate object, .metadata in the global env perhaps? Then just do: .metadata[[name_of_thing]] = list(modified=Sys.time()) in your modified assign. Performance will only be a problem if your program is doing nothing else except fiddle with metadata, I reckon. Your program does do something useful, doesn't it? Barry __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 get a specific named element in a nested list
On 11 November 2010 21:08, Janko Thyson janko.thy...@ku-eichstaett.de wrote: Could it be that you forgot to supply 'getEL()'? Or do I have to use some package to make it available? Oops - no. The problem was me stupidly renaming the function without modifying the code. Try this instead... rmatch - function(x, name) { pos - match(name, names(x)) if (!is.na(pos)) return(x[[pos]]) for (el in x) { if (class(el) == list) { out - rmatch(el, name) if (!is.null(out)) return(out) } } } Sorry about that. Michael -Ursprüngliche Nachricht- Von: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Im Auftrag von Michael Bedward Gesendet: Donnerstag, 11. November 2010 10:56 An: friedericksen.h...@gmail.com Cc: r-h...@stat.math.ethz.ch Betreff: Re: [R] How to get a specific named element in a nested list Hi Friedericksen, This function will do it. No doubt there are more elegant ways :) rmatch - function(x, name) { pos - match(name, names(x)) if (!is.na(pos)) return(x[[pos]]) for (el in x) { if (class(el) == list) { out - getEl(el, name) if (!is.null(out)) return(out) } } } Michael On 11 November 2010 19:05, Friedericksen Hope friedericksen.h...@gmail.com wrote: Hello, I have a nested named list structure, like the following: x - list( list( list(df1,df2) list(df3, list(df4,df5)) list(df6,df7))) with df1...d7 as data frames. Every data frame is named. Is there a way to get a specific named element in x? so, for example, x[[c(df5)]] gives me the data frame 5? Thank you in advance! Best, Friedericksen __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] create a pairwise coocurrence matrix
On 12 November 2010 02:21, David Winsemius dwinsem...@comcast.net wrote: The fastest and easiest solution is t(A) %*% A That is really elegant. (Wish I could remember my linear algebra lessons as well from forty years ago.) I checked it against the specified output and found that with one exception that the OP had planned for the diagonal to be filled with zeroes. So that could be completed by a simple modification: temp - t(A) %*% A diag(temp) - 0 temp Excellent solution ! Small is beautiful :) Michael __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Adding meta-data when creating objects. e.g: changing - so to (for example) add creation time - how-to and pros/cons?
Hi Ivan, It doesn't work because there is no object called x in the function's local scope. Try this... function(x, ...) { xname - deparse(substitute(x)) assign(xname, ...) x - get(xname) attr(x, creation.time) - Sys.time() assign(xname, x, pos=.GlobalEnv) } Michael On 11 November 2010 22:17, Ivan Calandra ivan.calan...@uni-hamburg.de wrote: Hi, I have tried this (just to practice): assign2 - function(x, ...){ assign(x, ..., envir=.GlobalEnv) attr(get(x, envir=.GlobalEnv), creation.time) - Sys.time() } assign2(y, 1:4) Error in attr(get(x), creation.time) - Sys.time() : could not find function get- Why doesn't it work? If I remove the attr() part, identical(y, get(y)) returns TRUE, so why attr() cannot work with it? Thanks in advance for the clarification, Ivan Le 11/11/2010 11:16, Michael Bedward a écrit : Hi Tal, Here's a way of doing the first bit... assign2- function(x, ...) { xname- deparse(substitute(x)) assign(xname, ...) x- get(xname) attr(x, creation.time)- Sys.time() assign(xname, x, pos=.GlobalEnv) } Michael On 11 November 2010 20:37, Tal Galilital.gal...@gmail.com wrote: My objective is to start having meta-data on objects that I create. For example, consider the following function: assign2- function(x, ...) { assign(x, ...) attr(x, creation time)- Sys.time() x- x } assign2(x, 1:4) assign2 assigns to x the vector 1:4, and it then also adds the creation time of the object. (Hat tip goes to Peter Alspach for pointing me to the concept of adding meta data to an object using attr) But this function has several major limitations: 1) It will not work for any assignment other then x. For example assign2(y, 1:4) Doesn't work. How might this be fixed ? 2) This function will probably need to also search the parent environment if the variable already exists. If it does, then there should be a update date instead of creation date. But for that to work, I'll need a solution for problem 1. 3) How will this handle a case when we are updating only a subset of the items? (for example: assign2(x[1:2], 8:9) ) 4) My real intention is to somehow change the - operator (not simply the assign). I am unsure as to how to do that. 5) Are there any major pros/cons to the adding of such meta-data to objects? (for example, excessive overhead on memory/performance) 6) Is there already some system that knows how to do this in R (which I am simply ignorant about)? Thanks for following through, and for any suggestions/thoughts you might have. Best, Tal Contact Details:--- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) -- [[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. -- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. Säugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calan...@uni-hamburg.de ** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Difficult doubt about choose distances randomly in a matrix with a probability of event
Hello Judit, The code below is a toy simulation function that takes as arguments a matrix representing the initial population, a dispersal kernel, global survival probability and max number of iterations to run. It doesn't implement exactly what you described in your post but if you study the code you should be able to use it as a starting point. WARNING - it was slapped together quickly so watch out for bugs. I spend a lot of time writing plant population simulation code in R (mainly for woodland tree species). Once you get started you will find that it's a very nice platform for working with both simple and complex models and / or prototyping models before moving them into another programming language. You might also want to post on the r-sig-ecology list. Have fun. Michael plantSim - function(pop, dispKernel, dispOrigin=NULL, nseed=5, survival=1.0, niter=100) { # Arguments: # pop - matrix for initial population map # (1 is occupied cell; 0 is vacant cell) # # dispKernel - matrix of probabilities for dispersal kernel # # dispOrigin - vector of two integers identifying the source cell of the dispersal kernel; # if null, the centre cell will be used # # nseed - number of seeds to disperse from each individual (constant) # # survival - individual probability of survival per time step (constant) # # niter - maximum number of iterations to run # # Returns a matrix with cols for time, number of individuals, number of successful dispersals, # number of deaths result - matrix(0, nrow=niter+1, ncol=4) colnames(result) - c(time, N, dispersals, deaths) result[1, ] - c(0, sum(pop == 1), 0, 0) dnr - nrow(dispKernel) dnc - ncol(dispKernel) if (is.null(dispOrigin)) dispOrigin - c(ceiling(dnr/2), ceiling(dnc)/2) MAPROWS - nrow(pop) MAPCOLS - ncol(pop) for (iter in 1:niter) { ndeaths - 0 ndisp - 0 occupied - which(pop == 1, arr.ind=TRUE) nstart - nrow(occupied) # check for extinction if (nrow(occupied) == 0) { # trim results matrix and finish early result - result[1:iter, ] break } for (iocc in 1:nrow(occupied)) { cell - occupied[iocc, ] # test survival if (runif(1) = survival) { # mortality pop[cell[1], cell[2]] - 0 ndeaths - ndeaths + 1 } else { # survived - do seed dispersal for (iseed in 1:nseed) { # choose a dispersal kernel cell repeat { row - sample(dnr, 1) col - sample(dnc, 1) if (runif(1) dispKernel[row, col]) { destOffset - c(row - dispOrigin[1], col - dispOrigin[2]) break } } destCell - cell + destOffset # check cell is in bounds - if not the seed is lost # (modify this to whatever boundary conditions you want) if (destCell[1] = 1 destCell[1] = MAPROWS destCell[2] = 1 destCell[2] = MAPCOLS) { # cell becomes occupied if vacant if (pop[destCell[1], destCell[2]] == 0) { pop[destCell[1], destCell[2]] - 1 ndisp - ndisp + 1 } } } } } # end of time step reporting result[iter+1, ] - c(iter, nstart-ndeaths+ndisp, ndisp, ndeaths) } # return results result } On 11 November 2010 05:19, Barroso, Judit judit.barr...@exchange.montana.edu wrote: I would like to build a model in R to simulate the seed dispersal by one plant. The plant produced 5 seeds and the probability of falling inside the eight closest space was 0.8 and in the next space 0.2 and in the rest space 0: 0 0 0 0 0 0 0.2 0.2 0.2 0.2 0.2 0 0.2 0.8 0.8 0.8 0.2 0 0.2 0.8 1 0.8 0.2 0 0.2 0.8 0.8 0.8 0.2 0 0.2 0.2 0.2 0.2 0.2 0 0 0 0 0 0 0 So I do not know how to program in R to choose these 5 places (randomly) knowing the probability of event. I have built a matrix that each value has assigned the probability of mortality (0.7) except when there is a plant (1) so, the 5 seeds that felt in each place around that plant would have to be multiplied by 0.7 and if the result was 1 then we would have a new individual for the next run. 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 1 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 I am trying to do loops but I am a beginner and I am very lost. Could anyone say me what code should I use? Thank you very much, Judit Barroso [[alternative HTML version deleted]] __ R-help@r-project.org mailing list
Re: [R] Populating then sorting a matrix and/or data.frame
Hello Noah, If you set these names... names(results) - c(one, two, three) this won't work... results[results$c 100,] because you don't have a column called c (unless that's just a typo in your post). I tried making it a data.frame with foo - data.frame(results) But that converted all the numeric values to factors!!! Not sure what's going on there. If 'results' is a numeric matrix you should get a data.frame with numeric cols since under the hood this is just calling the as.data.frame function. Michael On 11 November 2010 16:02, Noah Silverman n...@smartmediacorp.com wrote: Hi, I have a process in R that produces a lot of output. My plan was to build up a matrix or data.frame row by row, so that I'll have a nice object with all the resulting data. I started with: results - matrix(ncol=3) names(results) - c(one, two, three) Then, when looping through the data: results - rbind(results, c(a,b,c)) This seems to work fine. BUT, my problem arises when I want to filter, sort, etc. I tried (thinking like a data.frame): results[results$c 100,] But that fails. I tried making it a data.frame with foo - data.frame(results) But that converted all the numeric values to factors!!! Which causes a whole mess of problems. Any ideas?? -N __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] arrays of arrays
Hello Sachin, You have a ragged array and you can easily store this as a list of vectors... x - list(c(0,0,1,1), c(1,3,5), 4, c(7, -1, 8, 9, 10, 6)) The only gotcha with this is that you will then need to use double brackets for the first index when retrieving values (single brackets will return a vector wrapped in a list)... e.g. x[[2]][3] gives 5 You can query vector length with double brackets... e.g. length( x[[2]] ) gives 3 Hope this helps, Michael On 10 November 2010 16:00, sachinthaka.abeyward...@allianz.com.au wrote: Hi Erik, Thanks for replying. Only problem with that is that each row has 5 elements (or 5 columns). I want varying number of columns as shown in my example. x- 0 0 1 1 1 3 5 4 7 -1 8 9 10 6 Regards, Sachin p.s. sorry about corporate notice. --- Please consider the environment before printing this email --- Allianz - Best General Insurance Company of the Year 2010* Allianz - General Insurance Company of the Year 2009+ * Australian Banking and Finance Insurance Awards + Australia and New Zealand Insurance Industry Awards This email and any attachments has been sent by Allianz Australia Insurance Limited (ABN 15 000 122 850) and is intended solely for the addressee. It is confidential, may contain personal information and may be subject to legal professional privilege. Unauthorised use is strictly prohibited and may be unlawful. If you have received this by mistake, confidentiality and any legal privilege are not waived or lost and we ask that you contact the sender and delete and destroy this and any other copies. In relation to any legal use you may make of the contents of this email, you must ensure that you comply with the Privacy Act (Cth) 1988 and you should note that the contents may be subject to copyright and therefore may not be reproduced, communicated or adapted without the express consent of the owner of the copyright. Allianz will not be liable in connection with any data corruption, interruption, delay, computer virus or unauthorised access or amendment to the contents of this email. If this email is a commercial electronic message and you would prefer not to receive further commercial electronic messages from Allianz, please forward a copy of this email to unsubscr...@allianz.com.au with the word unsubscribe in the subject header. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] concatenating a string to a column
You want the paste command (cat is for printing to the console)... A$period - paste(A$period, 0, sep=) Michael On 10 November 2010 16:09, sachinthaka.abeyward...@allianz.com.au wrote: Hi All, Suppose I want to concatenate a zero to all the values to a column called period in data frame A. I want to do the following but the following command actually deletes the entire column altogether. A$period-cat(A$period,0,sep=); Any help would be appreciated. Thanks, Sachin p.s. sorry about the corporate notice. --- Please consider the environment before printing this email --- Allianz - Best General Insurance Company of the Year 2010* Allianz - General Insurance Company of the Year 2009+ * Australian Banking and Finance Insurance Awards + Australia and New Zealand Insurance Industry Awards This email and any attachments has been sent by Allianz Australia Insurance Limited (ABN 15 000 122 850) and is intended solely for the addressee. It is confidential, may contain personal information and may be subject to legal professional privilege. Unauthorised use is strictly prohibited and may be unlawful. If you have received this by mistake, confidentiality and any legal privilege are not waived or lost and we ask that you contact the sender and delete and destroy this and any other copies. In relation to any legal use you may make of the contents of this email, you must ensure that you comply with the Privacy Act (Cth) 1988 and you should note that the contents may be subject to copyright and therefore may not be reproduced, communicated or adapted without the express consent of the owner of the copyright. Allianz will not be liable in connection with any data corruption, interruption, delay, computer virus or unauthorised access or amendment to the contents of this email. If this email is a commercial electronic message and you would prefer not to receive further commercial electronic messages from Allianz, please forward a copy of this email to unsubscr...@allianz.com.au with the word unsubscribe in the subject header. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] arrays of arrays
Hi Sachin, That's OK - you don't need to know the dimensions up front and you can add new vectors, or elements to an existing vector, as required. # empty list to start with X - list() # we get a vector v1 - c(1, 2, 3, 4, 5) # add it to the ragged array X - c(X, list(v1)) # get another couple of vectors and add them as well v2 - c(9, 8, 7, 6) v3 - c(2, 4, 6, 8) X - c(X, list(v2, v3)) # add some more elements to the first vector in X - note double brackets to access # the vector directly X[[1]] - c(X[[1]], 4, 3, 2, 1) Michael On 10 November 2010 16:46, sachinthaka.abeyward...@allianz.com.au wrote: Hi Michael, Thanks for that. Its a starting point I guess. But what if I didn't know the length of the outer vector is? (i.e. all dimensions are variable). Or for that matter I don't actually know what the initial dimensions are going to be. All of it is created within a for loop. I was hoping for something like a C++ vector, where you can specify vectorvectorint myarray. I'm sure lists is definitely the way to go, but not sure how to implement it. Cheers, Sachin --- Please consider the environment before printing this email --- Allianz - Best General Insurance Company of the Year 2010* Allianz - General Insurance Company of the Year 2009+ * Australian Banking and Finance Insurance Awards + Australia and New Zealand Insurance Industry Awards This email and any attachments has been sent by Allianz Australia Insurance Limited (ABN 15 000 122 850) and is intended solely for the addressee. It is confidential, may contain personal information and may be subject to legal professional privilege. Unauthorised use is strictly prohibited and may be unlawful. If you have received this by mistake, confidentiality and any legal privilege are not waived or lost and we ask that you contact the sender and delete and destroy this and any other copies. In relation to any legal use you may make of the contents of this email, you must ensure that you comply with the Privacy Act (Cth) 1988 and you should note that the contents may be subject to copyright and therefore may not be reproduced, communicated or adapted without the express consent of the owner of the copyright. Allianz will not be liable in connection with any data corruption, interruption, delay, computer virus or unauthorised access or amendment to the contents of this email. If this email is a commercial electronic message and you would prefer not to receive further commercial electronic messages from Allianz, please forward a copy of this email to unsubscr...@allianz.com.au with the word unsubscribe in the subject header. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] jags error message
Hi James, The following is probably more an expression of empathy than direct help :) I've used rjags quite a bit but have often found it to be very picky about the formulation of the model. Scripts that run without problem under WinBUGS (for instance) provoke errors with jags and more often than not the error message is uninformative or misleading. Sometimes just fiddling with the syntax a little allows the model to run. One thing that will generate the message you report is if you have a model where values being sampled for one or more parameters become too small to distinguish from zero. For instance, I've struck this problem with ordinal response models where the sampled probabilities of category membership could be tiny. My hack work-around was to use nested min and max functions to limit the range of the sampled values. Michael On 10 November 2010 02:01, Maas James Dr (MED) j.m...@uea.ac.uk wrote: Could anyone give me some clues as to the best way to debug this error message? I think it is from the passing of variables back to R from the jags function which does Bayesian fitting. The curious part for me is that the error messages seem random, yet the input data are always the same. Any suggestions most welcome. Thanks J Compiling model graph Resolving undeclared variables Allocating nodes Graph Size: 1604 |++| 100% |**| 100% Error : NA/NaN/Inf in foreign function call (arg 1) In addition: There were 15 warnings (use warnings() to see them) === Dr. Jim Maas University of East Anglia [[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] Simple Function
If you want to assign to a variable in your workspace, rather than a local variable in your function, you can use the - operator (double headed arrow) like this... mat - function(i) { for (k in i:10) { y[k] - k+1 f[k] - y[k-1] / 2 } } Type ?- for the help page. Michael On 10 November 2010 18:09, rnick nikos.rachma...@gmail.com wrote: Hi guys, Very new to R and your help would be highly appreciated for the following problem. I am trying to create a simple function which registers values within an array through a for loop. These are the steps I have followed: 1) Declared 2 global matrices 2) Create function mat() with i as an input 3) constructed the for loop 4) called mat(2) The problem is that when i try to get y[4] and f[5] the output is: [1] NA my concern is that i am not addressing any of the following topics: 1) definition of global variable 2) the argument does not go through the for loop 3) the matrices definition is not correct 4) the function do not return any values 4) other.. My code is listed below: y=c(NA) f=c(NA) mat-function(i) { for (k in i:10) { y[k]=k+1 f[k]=y[k-1]/2 } } mat(2) Any thoughts or recommendations would be highly appreciated. Thanks in advance, N -- View this message in context: http://r.789695.n4.nabble.com/Simple-Function-tp3035585p3035585.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] conditional probability
Hello Jumlong, For Normal distribution see the help page for pnorm. For dealing with unknown (empirical) distributions, look at ecdf. Hope this helps Michael On 8 November 2010 16:29, Jumlong Vongprasert jumlong.u...@gmail.com wrote: Dear all I have problem with calculate probability, I have data x1,...,x10, I want to calculate probability x11 given x1,...,x10 with two conditions. 1. x is normal 2. unknow distribution How I can do this. Many Thanks. Jumlong -- Jumlong Vongprasert Assist, Prof. Institute of Research and Development Ubon Ratchathani Rajabhat University Ubon Ratchathani THAILAND 34000 [[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] : unusual combinations of categorical data
Perhaps just use the ftable function to generate a flat contingency table and look for counts below some threshold. Michael On 9 November 2010 09:25, Alan Chalk alan.ch...@gcc.rsagroup.com wrote: Regarding unusual combinations of factors in categorical data. Are there any R packages that can be used to identify the outliers i.e. unusual combinations in categorical datasets ? Thanks. Notice of Confidentiality This transmission contains information that may be confidential and that may also be privileged. Unless you are the intended recipient of the message (or authorised to receive it for the intended recipient) you may not copy, forward, or otherwise use it, or disclose it or its contents to anyone else. If you have received this transmission in error please notify us immediately and delete it from your system. RSA Insurance Group plc. Registered in England No. 2339826. The Registered Office is 9th Floor, One Plantation Place, 30 Fenchurch Street, London EC3M 3BD [[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] Question related to combination and the corresponding probability
Don't know if this is efficient but I think it works... yn - c(Y, N) X - expand.grid(x1=yn, x2=yn, x3=yn, x4=yn) Yp - c(0.6, 0.5, 0.8, 0.9) X$prob - apply(X, 1, function(x) cumprod(ifelse(x == Y, Yp, 1-Yp))[length(x)]) Michael On 9 November 2010 17:05, Kate Hsu yhsu.rh...@gmail.com wrote: Dear r users, I have 4 variables x1,x2,x3,x4 and each one has two levels, for example Y and N. For x1: prob(Y)=0.6, prob(N)=0.4; For x2: prob(Y)=0.5, prob(N)=0.5; For x3: prob(Y)=0.8, prob(N)=0.2; For x4: prob(Y)=0.9, prob(N)=0.1; Therefore, the sample space for (x1, x2, x3, x4)={, YYYN, YYNY,..} (16 possible combination) and the corresponding probabilities are {(0.6)(0.5)(0.8)(0.9), (0.6)(0.5)(0.8)(0.1), (0.6)(0.5)(0.2)(0.9),...} I want to create a matrix includes all possible outcomes and the corresponding probabilities. That is A= (0.6)(0.5)(0.8)(0.9) YYYN (0.6)(0.5)(0.8)(0.1) YYNY (0.6)(0.5)(0.2)(0.9) . . . . . . Any efficient way to do this? Thanks, Kate [[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] (no subject)
Hello, One approach would be to fit your distribution using MCMC with, for example, the rjags package. Then you can use the zeroes trick or ones trick to implement your new distribution as described here... http://mathstat.helsinki.fi/openbugs/data/Docu/Tricks.html You will find a summary of Bayesian / MCMC packages here... http://cran.r-project.org/web/views/Bayesian.html Of these, rjags is the only one I've used directly so I can't comment on which would be easiest. Hopefully others here can offer advice. Michael On 5 November 2010 00:25, Roes Da r0ez...@gmail.com wrote: hello,i'm roesda from indonesia I have trouble when they have to perform parameter estimation by MLE method using the R programming.because, the distribution that will be used instead of not like the distribution that already known distributions such as gamma distribution, Poisson or binomial. the distribution that i would estimate the parameters are the joint distribution between the negative binomial distribution and Lindley. how do I translate it in R if the distribution is still new as I mentioned? i hope everyone can help me. thank you very much Simak Baca secara fonetik [[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] Closing unreferenced result sets in dbi / RSQLite
Hi Andreas, Try this... # forget to assign result set dbSendQuery(con, select * from df) # retrieve the result set just created rs - dbListResults(con)[[1]] Then you can do dbClearResult or whatever. Michael On 4 November 2010 19:56, Andreas Borg andreas.b...@unimedizin-mainz.de wrote: Hello R-help members, I have one problem with the database interface dbi (more specifically, I work with RSQLite). Consider the following example, which writes a test table to a temporary SQLite database and sends a query to read from it: library(RSQLite) df - as.data.frame(matrix(runif(4), nrow=2, ncol=2)) drv - dbDriver(SQLite) con - dbConnect(drv) dbWriteTable(con, df, df) dbSendQuery(con, select * from df) In the last line I forgot to assign the DBIResult object returned by dbSendQuery() to a variable, which happens from time to time when I work interactively. The following attempt to correct the mistake: res - dbSendQuery(con, select * from df) fails because the orphaned result set from the preceeding call is still active. Consequently, I have to close the connection to keep on working, which is especially annoying when working with a temporary data base where everything is discarded on disconnection. Is there any way to create a new reference to the pending result set or to close result sets which are not bound to a variable? Thanks for any suggestion, Andreas -- Andreas Borg Medizinische Informatik UNIVERSITÄTSMEDIZIN der Johannes Gutenberg-Universität Institut für Medizinische Biometrie, Epidemiologie und Informatik Obere Zahlbacher Straße 69, 55131 Mainz www.imbei.uni-mainz.de Telefon +49 (0) 6131 175062 E-Mail: b...@imbei.uni-mainz.de Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und löschen Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail und der darin enthaltenen Informationen ist nicht gestattet. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Bayesian constrained regression method?
Hello Jim, Please reply to the list - you'll have a much better chance of getting useful suggestions. OK so some addition info. I know each of the X2 is in (0,1). Is there any method available? I don't think that's sufficient to estimate b, at least not in my experience of fitting Bayesian models with MCMC. To get any sort of precise posterior for b I think you would need to know that, for instance, X2 is correlated with X1 in some way, or that it can be described by a particular Beta distribution etc. I'd be happy to be corrected by others here who know much more than I do but if the best prior you can come up with for X2 is uniform in (0,1) I think you have insufficient information to proceed. Michael On 24 October 2010 09:28, Jim Silverton jim.silver...@gmail.com wrote: I am trying to estimate the parameter b. I have Y and X1 which I know and they are both random. However, I also have X2 which I don't know and is also random. I want to estimat b from the model: Y = b*X1 + ( 1 - b ) * X2 so my constraints areCan anyone offer some suggestions. The values of Y and X1 are both pvalues so they are constrained in (0,1). OK so some addition info. I know each of the X2 is in (0,1). Is there any method available? Jim On Sat, Oct 23, 2010 at 8:31 AM, Michael Bedward michael.bedw...@gmail.com wrote: Hi Jim, You don't mention whether you have any prior information regarding X2 that can be used to constrain values imputed for it. I think you will need some because without it values sampled for b and X2 respectively will just see-saw against each other. Michael On 22 October 2010 18:37, Jim Silverton jim.silver...@gmail.com wrote: Hello everyone, I am trying to estimate the parameter b. I have Y and X1 which I know and they are both random. However, I also have X2 which I don't know and is also random. I want to estimat b from the model: Y = b*X1 + ( 1 - b ) * X2 Can anyone offer some suggestions. The values of Y and X1 are both pvalues so they are constrained in (0,1). -- Thanks, Jim. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Thanks, 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] Bayesian constrained regression method?
Hi Jim, You don't mention whether you have any prior information regarding X2 that can be used to constrain values imputed for it. I think you will need some because without it values sampled for b and X2 respectively will just see-saw against each other. Michael On 22 October 2010 18:37, Jim Silverton jim.silver...@gmail.com wrote: Hello everyone, I am trying to estimate the parameter b. I have Y and X1 which I know and they are both random. However, I also have X2 which I don't know and is also random. I want to estimat b from the model: Y = b*X1 + ( 1 - b ) * X2 Can anyone offer some suggestions. The values of Y and X1 are both pvalues so they are constrained in (0,1). -- Thanks, Jim. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Two-Way Joining or Clustering
Hi Muhammad, Have a look at the biclust package... http://cran.r-project.org/web/packages/biclust/index.html Michael On 21 October 2010 18:00, Muhammad Yaseen myaseen...@gmail.com wrote: *Hi Folks,* * * *I want to do two-way joining or clustering as described in STATISTICA website *http://www.statsoft.com/textbook/cluster-analysis/#twotwo. *Any suggestion and/or help will be highly appreciated. Thanks* * * *Regards!* -- * Muhammad Yaseen * [[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] Clustering with ordinal data
Hello Steve, I've been asked to help evaluate a vegetation data set, specifically to examine it for community similarity. The initial problem I see is that the data is ordinal. At best this only captures a relative ranking of abundance and ordinal ranks are assigned after data collection. Just about every vegetation survey ever conducted uses either presence absence or ordinal data collection (e.g. Braun-Blanquet scores or importance scores from nested quadrats). A large number of distance metrics are in the literature to deal with such data. As well as Phil's suggestion you should definitely look at the vegan package which contains a good selection of these metrics plus numerous functions frequently used in classification and ordination of veg data. Michael On 20 October 2010 05:14, steve_fried...@nps.gov wrote: Hello I've been asked to help evaluate a vegetation data set, specifically to examine it for community similarity. The initial problem I see is that the data is ordinal. At best this only captures a relative ranking of abundance and ordinal ranks are assigned after data collection. I've been trying to find a procedure in R that can handle ordinal based classification and so far have not found one. Does one exist ? If there is one, which package supports this type of analysis and what is the function ? Thanks in advance. Steve Steve Friedman Ph. D. Spatial Statistical Analyst Everglades and Dry Tortugas National Park 950 N Krome Ave (3rd Floor) Homestead, Florida 33034 steve_fried...@nps.gov Office (305) 224 - 4282 Fax (305) 224 - 4147 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Random assignment
Hi John, The word species attracted my attention :) Like Dennis, I'm not sure I understand your idea properly. In particular, I don't see what you need the simulation for. If family F has Fn species, your random expectation is that p * Fn of them will be at risk (p = 0.0748). The variance on that expectation will be p * (1-p) * Fn. If you do your simulation that's the result you'll get. Perhaps to initial identify families with disproportionate observed extinction rates all you need is the dbinom function ? Michael On 15 October 2010 22:29, John Haart anothe...@me.com wrote: Hi Denis and list Thanks for this , and sorry for not providing enough information First let me put the study into a bit more context : - I know the number of species at risk in each family, what i am asking is Is risk random according to family or do certain families have a disproportionate number of at risk species? My idea was to randomly allocate risk to the families based on the criteria below (binomial(nspecies, 0.0748)) and then compare this to the true data and see if there was a significant difference. So in answer to your questions, (assuming my method is correct !) Is this over all families, or within a particular family? If the former, why does a distinction of family matter? Within a particular family - this is because i am looking to see if risk in the observed data set is random in respect to family so this will provide the baseline to compare against. I guess you've stated the p, but what's the n? The number of species in each family? This varies largely, for instance i have some families that are monotypic (with 1 species) and then i have other families with 100+ species Assuming you have multiple families, do you want separate simulations per family, or do you want to do some sort of weighting (perhaps proportional to size) over all families? I am assuming i want some sort of weighting. This is because i am wanting to calculate the number of species expected to be at risk in EACH family under the random binomial distribution ( assuming every species has a 7.48% chance of being at risk. Thanks John On 15 Oct 2010, at 11:19, Dennis Murphy wrote: Hi: I don't believe you've provided quite enough information just yet... On Fri, Oct 15, 2010 at 2:22 AM, John Haart anothe...@me.com wrote: Dear List, I am doing some simulation in R and need basic help! I have a list of animal families for which i know the number of species in each family. I am working under the assumption that a species has a 7.48% chance of being at risk. Is this over all families, or within a particular family? If the former, why does a distinction of family matter? I want to simulate the number of species expected to be at risk under a random binomial distribution with 10,000 randomizations. I guess you've stated the p, but what's the n? The number of species in each family? If you're simulating on a family by family basis, then it would seem that a binomial(nspecies, 0.0748) distribution would be the reference. Assuming you have multiple families, do you want separate simulations per family, or do you want to do some sort of weighting (perhaps proportional to size) over all families? The latter is doable, but it would require a two-stage simulation: one to randomly select a family and then to randomly select a species. Dennis I am relatively knew to this field and would greatly appreciate a idiot-proof response, I.e how should the data be entered into R? I was thinking of using read.table, header = T, where the table has F = Family Name, and SP = Number of species in that family? John __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[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] Random assignment
Hi John, I haven't read that particular paper but in answer to your question... So if i do this for all the families it will be the same as doing the simulation experiment outline in the method above? Yes :) Michael On 15 October 2010 23:18, John Haart anothe...@me.com wrote: Hi Michael, Thanks for this - the reason i am following this approach is that it appeared in a paper i was reading, and i thought it was a interesting angle to take The paper is Vamosi Wilson, 2008. Nonrandom extinction leads to elevated loss of angiosperm evolutionary history. Ecology Letters, (2008) 11: 1047–1053. and the specific method i am following states :- We calculated the number of species expected to be at risk in each family under a random binomial distribution in 10 000 randomizations [generated using R version 2.6.0 (R Development Team 2007)] assuming every species has a 7.48% chance of being at risk. I guess the reason i am doing the simulation is because i am not hugely statistically minded and the paper was asking the same question i am interested in answering :). So following your approach - if family F has Fn species, your random expectation is that p * Fn of them will be at risk (p = 0.0748). The variance on that expectation will be p * (1-p) * Fn. Family f = Bromeliaceae , with Fn = 80, p=0.0748 random expectation = p*Fn = (0.0748*80) = 5.984 variance = p * (1-p) * Fn = (0.0748*0.9252) *80 = 5.5363968 So the random expectation is that the Bromeliaceae will have 6 species at risk, if risk is assigned randomly? So if i do this for all the families it will be the same as doing the simulation experiment outline in the method above? Thanks John On 15 Oct 2010, at 12:49, Michael Bedward wrote: Hi John, The word species attracted my attention :) Like Dennis, I'm not sure I understand your idea properly. In particular, I don't see what you need the simulation for. If family F has Fn species, your random expectation is that p * Fn of them will be at risk (p = 0.0748). The variance on that expectation will be p * (1-p) * Fn. If you do your simulation that's the result you'll get. Perhaps to initial identify families with disproportionate observed extinction rates all you need is the dbinom function ? Michael On 15 October 2010 22:29, John Haart anothe...@me.com wrote: Hi Denis and list Thanks for this , and sorry for not providing enough information First let me put the study into a bit more context : - I know the number of species at risk in each family, what i am asking is Is risk random according to family or do certain families have a disproportionate number of at risk species? My idea was to randomly allocate risk to the families based on the criteria below (binomial(nspecies, 0.0748)) and then compare this to the true data and see if there was a significant difference. So in answer to your questions, (assuming my method is correct !) Is this over all families, or within a particular family? If the former, why does a distinction of family matter? Within a particular family - this is because i am looking to see if risk in the observed data set is random in respect to family so this will provide the baseline to compare against. I guess you've stated the p, but what's the n? The number of species in each family? This varies largely, for instance i have some families that are monotypic (with 1 species) and then i have other families with 100+ species Assuming you have multiple families, do you want separate simulations per family, or do you want to do some sort of weighting (perhaps proportional to size) over all families? I am assuming i want some sort of weighting. This is because i am wanting to calculate the number of species expected to be at risk in EACH family under the random binomial distribution ( assuming every species has a 7.48% chance of being at risk. Thanks John On 15 Oct 2010, at 11:19, Dennis Murphy wrote: Hi: I don't believe you've provided quite enough information just yet... On Fri, Oct 15, 2010 at 2:22 AM, John Haart anothe...@me.com wrote: Dear List, I am doing some simulation in R and need basic help! I have a list of animal families for which i know the number of species in each family. I am working under the assumption that a species has a 7.48% chance of being at risk. Is this over all families, or within a particular family? If the former, why does a distinction of family matter? I want to simulate the number of species expected to be at risk under a random binomial distribution with 10,000 randomizations. I guess you've stated the p, but what's the n? The number of species in each family? If you're simulating on a family by family basis, then it would seem that a binomial(nspecies, 0.0748) distribution would be the reference. Assuming you have multiple families, do you want separate simulations per family, or do you
Re: [R] Random assignment
Hello again John, I was going to suggest that you just use qbinom to generate the expected number of extinctions. For example, for the family with 80 spp the central 95% expectation is: qbinom(c(0.025, 0.975), 80, 0.0748) which gives 2 - 11 spp. If you wanted to do look across a large number of families you'd need to deal with multiple comparison error but as a quick first look it might be helpful. However, I've just got a copy of teh paper and it seems that the authors are calculating something different to a simple binomial expecation: they are differentiating between high-risk (red listed) and low-risk species within a family. They state that this equation (expressed here in R-ese)... choose(N, R) * p^R * b^(N - R) ...gives the probabilitiy of an entire family becoming extinct, where N is number of spp in family; R is number of those that are red listed; p is extinction probability for red list spp (presumably over some period but I haven't read the paper properly yet); b is extinction probability for other spp. Then, in their simulations they hold b constant but play around with a range of values for p. So this sounds a bit different to what you originally posted as your objective (?) Michael On 15 October 2010 22:49, Michael Bedward michael.bedw...@gmail.com wrote: Hi John, The word species attracted my attention :) Like Dennis, I'm not sure I understand your idea properly. In particular, I don't see what you need the simulation for. If family F has Fn species, your random expectation is that p * Fn of them will be at risk (p = 0.0748). The variance on that expectation will be p * (1-p) * Fn. If you do your simulation that's the result you'll get. Perhaps to initial identify families with disproportionate observed extinction rates all you need is the dbinom function ? Michael On 15 October 2010 22:29, John Haart anothe...@me.com wrote: Hi Denis and list Thanks for this , and sorry for not providing enough information First let me put the study into a bit more context : - I know the number of species at risk in each family, what i am asking is Is risk random according to family or do certain families have a disproportionate number of at risk species? My idea was to randomly allocate risk to the families based on the criteria below (binomial(nspecies, 0.0748)) and then compare this to the true data and see if there was a significant difference. So in answer to your questions, (assuming my method is correct !) Is this over all families, or within a particular family? If the former, why does a distinction of family matter? Within a particular family - this is because i am looking to see if risk in the observed data set is random in respect to family so this will provide the baseline to compare against. I guess you've stated the p, but what's the n? The number of species in each family? This varies largely, for instance i have some families that are monotypic (with 1 species) and then i have other families with 100+ species Assuming you have multiple families, do you want separate simulations per family, or do you want to do some sort of weighting (perhaps proportional to size) over all families? I am assuming i want some sort of weighting. This is because i am wanting to calculate the number of species expected to be at risk in EACH family under the random binomial distribution ( assuming every species has a 7.48% chance of being at risk. Thanks John On 15 Oct 2010, at 11:19, Dennis Murphy wrote: Hi: I don't believe you've provided quite enough information just yet... On Fri, Oct 15, 2010 at 2:22 AM, John Haart anothe...@me.com wrote: Dear List, I am doing some simulation in R and need basic help! I have a list of animal families for which i know the number of species in each family. I am working under the assumption that a species has a 7.48% chance of being at risk. Is this over all families, or within a particular family? If the former, why does a distinction of family matter? I want to simulate the number of species expected to be at risk under a random binomial distribution with 10,000 randomizations. I guess you've stated the p, but what's the n? The number of species in each family? If you're simulating on a family by family basis, then it would seem that a binomial(nspecies, 0.0748) distribution would be the reference. Assuming you have multiple families, do you want separate simulations per family, or do you want to do some sort of weighting (perhaps proportional to size) over all families? The latter is doable, but it would require a two-stage simulation: one to randomly select a family and then to randomly select a species. Dennis I am relatively knew to this field and would greatly appreciate a idiot-proof response, I.e how should the data be entered into R? I was thinking of using read.table, header = T, where the table has F = Family Name, and SP = Number
Re: [R] (no subject)
Hello Julia, I'm afraid your code had multiple problems: variables declared but not used, incorrect or unnecessary use of the c function, out-of-bounds subscripts and overwriting of result objects. Rather than point them all out in detail I've modified your code so that it works (see below). Please study this code and compare it to your original code. This will help you to understand how to do things more simply and reliably in R. Don't be discouraged - it takes practice :) Note, I didn't know whether you really wanted to retain the resultx and resultb matrices. I've left them in there just in case. Michael bidding.simulation - function(nsim=10, N=200, I=5) { set.seed(180185) tau - seq(0.48, 0.52, 0.001) tau.mid - which(tau == 0.5) Ntau - length(tau) mresultx - matrix(-99, nrow=I*N, ncol=nsim) mresultb - matrix(-99, nrow=I*N, ncol=nsim) h - seq(0.001, 0.020, 0.001) Mb0 - matrix(0, nrow=nsim, ncol=Ntau) Mb1 - matrix(0, nrow=nsim, ncol=Ntau) colnames(Mb1) - colnames(Mb0) - paste(tau, tau, sep=.) Mhb0 - matrix(0, nrow=nsim, ncol=tau.mid - 1) Mhb1 - matrix(0, nrow=nsim, ncol=tau.mid - 1) colnames(Mhb1) - colnames(Mhb0) - paste(width, tau[(tau.mid - 1):1] - tau[(tau.mid + 1):Ntau], sep=.) for (i in 1:nsim){ mu - runif(I*N) mx - rep(runif(N), I) b0 - rep(1, I*N) #function for private cost cost - b0+b0*mx+mu #bidding strategy bid - mx+((I+1)/I)+((I-1)/I)*mu mresultb[,i] - bid mresultx[,i] - mx qf - rq(formula = bid ~ mx, tau = tau) coefs - coef(qf) Mb0[i, ] - coefs[1, ] Mb1[i, ] - coefs[2, ] Mhb0[i, ] - coefs[1, (tau.mid - 1):1] - coefs[1, (tau.mid+1):Ntau] Mhb1[i, ] - coefs[2, (tau.mid - 1):1] - coefs[2, (tau.mid+1):Ntau] } # return results as a list list(Mb0=Mb0, Mb1=Mb1, Mhb0=Mhb0, Mhb1=Mhb1, mresultx=mresultx, mresultb=mresultb) } On 14 October 2010 05:37, Julia Lira julia.l...@hotmail.co.uk wrote: Dear all, I have just sent an email with my problem, but I think no one can see the red part, beacuse it is black. So, i am writing again the codes: rm(list=ls()) #remove almost everything in the memory set.seed(180185) nsim - 10 mresultx - matrix(-99, nrow=1000, ncol=nsim) mresultb - matrix(-99, nrow=1000, ncol=nsim) N - 200 I - 5 taus - c(0.480:0.520) h - c(1:20/1000) alpha1 - c(1:82) aeven1 - alpha[2 * 1:41] aodd1 - alpha[-2 * 1:41] alpha2 - c(1:40) aeven2 - alpha2[2 * 1:20] #Create an object to hold results. M - matrix(0, ncol=82, nrow=nsim) Mhb0 - matrix(0, ncol=20, nrow=nsim) Mhb1 - matrix(0, ncol=20, nrow=nsim) Mchb0 - matrix(0, ncol=20, nrow=nsim) Mchb1 - matrix(0, ncol=20, nrow=nsim) for (i in 1:nsim){ # make a matrix with 5 cols of N random uniform values u - replicate( 5, runif(N, 0, 1) ) # fit matrix u in another matrix of 1 column mu - matrix(u, nrow=1000, ncol=1) # make auction-specific covariate x - runif(N, 0, 1) mx - matrix(rep(x,5), nrow=1000, ncol=1) b0 - matrix(rep(c(1),1000), nrow=1000, ncol=1) #function for private cost cost - b0+b0*mx+mu #bidding strategy bid - mx+((I+1)/I)+((I-1)/I)*mu mresultb[,i] - bid mresultx[,i] - mx qf - rq(formula = mresultb[,i] ~ mresultx[,i], tau= 480:520/1000) # Storing result and does not overwrite prior values M[i, ] - coef(qf) QI - (1-0.5)/(I-1) M50b0 - M[,41] M50b1 - M[,42] Mb0 - matrix(M[,aodd1], nrow=nsim, ncol=20) Mb1 - matrix(M[,aeven1], nrow=nsim, ncol=20) for (t in aeven2){ Mhb0[,t] - M[,(41+t)]-M[,(41-t)] Mhb1[,t] - M[,(42+t)]-M[,(42-t)] } } The problem is in the part: for (t in aeven2){ Mhb0[,t] - M[,(41+t)]-M[,(41-t)] Mhb1[,t] - M[,(42+t)]-M[,(42-t)] } Since I want the software to subtract from column (41+t) of matrix called M the column (41-t), in such a way that the matrix Mhb0 will show me the result for each t organized by columns. Does anybody know what exactly I am doing wrong? Thanks in advance! Julia [[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] spatial partition
Some quick ideas... One very easy way would be to round them all to integer degrees and remove the duplicates - or even just let the duplicates overwrite each other in the plot. A step up from that would be to create a matrix at some resolution (e.g. 180 x 360 for a 1 degree global grid) and count the number of points that fall into each matrix cell. Then plot those points with symbol color or size to represent frequency categories. If you want to get fancy and look for natural spatial clusters of points in your data there are a number of packages that can help you: http://cran.r-project.org/web/views/Spatial.html Hope this helps. Michael On 14 October 2010 19:32, ogbos okike ogbos.ok...@gmail.com wrote: Hi everybody, I have a huge longitude and latitude data. I have used raster package to get the since of its global distribution. But I wish to display the information using a few points on the world map. The data is of the form: 95.2156 0.8312 -65.3236 -3.3851 -65.2364 -3.2696 -65.2349 -3.2679 164.7025 -17.6404 148.8214 -4.8285 -67.6568 2.6477 -73.4833 -0.2350 40.9587 -16.8655 -61.6474 8.1179 93.3401 -0.2755 119.9011 -17.1733 -69.7640 1.1245 -149.3088 -20.0035 177.8753 -3.4200 -67.5590 3.0133 -21.9331 15.4684 120.2656 -17.6166 165.9368 -17.3888 164.7335 -17.6953 -74.0017 -1.8623 -71.1195 -3.3562 130.1496 -11.5775 I am thinking of a way of handling that data in such a way that they can be represented by fewer points when plotted on the map. I have tried using factor method but didn't get any way. Thank you for any idea Best Ogbos [[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] compare histograms
Hi Rainer, Great - many thanks for that. Yes, I'm using OSX I initially tried to use install.packages to get get a pre-built binary of earthmovdist from Rforge, but it failed with... In getDependencies(pkgs, dependencies, available, lib) : package ‘earthmovdist’ is not available When I tried installing with type=source this was also failing. However, after reading your post I looked at the error messages properly and it turned out to be a trivial problem. The .First function defined in my .Rprofile was printing some text to the console with cat() which was being incorrectly picked up by the package build as if it was a makefile argument. When I commented out the call to cat the package installed successfully. I haven't had this problem installing other packages from source so I think there must be a little problem with your setup (?) Now that it's installed I look forward to trying it out shortly. Thanks again. Michael On 15 October 2010 03:17, Rainer M Krug r.m.k...@gmail.com wrote: On Thu, Oct 14, 2010 at 3:15 AM, Michael Bedward michael.bedw...@gmail.com wrote: Hi Juan, Yes, you can use EMD to quantify the difference between any pair of histograms regardless of their shape. The only constraint, at least the way that I've done it previously, is to have compatible bins. The original application of EMD was to compare images based on colour histograms which could have all sorts of shapes. I looked at the package that Dennis alerted me to on RForge but unfortunately it seems to be inactive No - well, it depends how you define inactive: the functionality we wanted to include is included, therefore no further development was necessary. and the nightly builds are broken. I've downloaded the source code and will have a look at it sometime in the next few days. Thanks for alerting us - we will look into that. But just don't use the nightly builds, as they are not different to the last release. Just download the package for your system (I assume Windows or mac, as I just installed from source without problems under Linux). Let me know if it doesn't work, Cheers, Rainer Meanwhile, let me know if you want a copy of my own code. It uses the lpSolve package. Michael On 14 October 2010 08:46, Juan Pablo Fededa jpfed...@gmail.com wrote: Hi Michael, I have the same challenge, can you use this earth movers distance it to compare bimodal distributions? Thanks cheers, Juan On Wed, Oct 13, 2010 at 4:39 AM, Michael Bedward michael.bedw...@gmail.com wrote: Just to add to Greg's comments: I've previously used 'Earth Movers Distance' to compare histograms. Note, this is a distance metric rather than a parametric statistic (ie. not a test) but it at least provides a consistent way of quantifying similarity. It's relatively easy to implement the metric in R (formulating it as a linear programming problem). Happy to dig out the code if needed. Michael On 13 October 2010 02:44, Greg Snow greg.s...@imail.org wrote: That depends a lot on what you mean by the histograms being equivalent. You could just plot them and compare visually. It may be easier to compare them if you plot density estimates rather than histograms. Even better would be to do a qqplot comparing the 2 sets of data rather than the histograms. If you want a formal test then the ks.test function can compare 2 datasets. Note that the null hypothesis is that they come from the same distribution, a significant result means that they are likely different (but the difference may not be of practical importance), but a non-significant test could mean they are the same, or that you just do not have enough power to find the difference (or the difference is hard for the ks test to see). You could also use a chi-squared test to compare this way. Another approach would be to use the vis.test function from the TeachingDemos package. Write a small function that will either plot your 2 histograms (density plots), or permute the data between the 2 groups and plot the equivalent histograms. The vis.test function then presents you with an array of plots, one of which is the original data and the rest based on permutations. If there is a clear meaningful difference in the groups you will be able to spot the plot that does not match the rest, otherwise it will just be guessing (might be best to have a fresh set of eyes that have not seen the data before see if they can pick out the real plot). -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- project.org] On Behalf Of solafah bh Sent: Monday, October 11, 2010 4:02 PM To: R help mailing list Subject: [R] compare
Re: [R] Program BFAST
Hello, Looking at the bfast help page is says that the output component of the returned object is a list where the elements correspond to results for each iteration of the fitting algorithm. Michael On 13 October 2010 15:58, CALEF ALEJANDRO RODRIGUEZ CUEVAS alejandro.rodriguez.cue...@gmail.com wrote: Hi everybody. I'm using BFAST program and I have some data to be analized (named META.csv for example). When I call BFAST in the following way: meta - read.csv(C:/META.csv) meta.ts-ts(meta, frequency=12, start = c(2005,9)) dis-frequency(meta.ts) / length(meta.ts) meta.sc-bfast(meta.ts, h=dis, max.iter=1) meta.sc$output The last statement has the option of write it in the next fashion: meta.sc$output[[1]] or meta.sc$output[[2]] which have associated meta.sc$output[[1]]$bp.Vt or meta.sc$output[[2]]$bp.Vt respectively...However I really don't understand what's the difference (if such exists) in choosing [[1]] or [[2]] I've noticed that they show different results but I don't understan which is correct or wrong and when to choose any of them. thanks a lot for your unvaluable help. Regards [[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] vertical kites in KiteChart (plotrix)
Hello Ellen, First up I think you can use reshape to get your data into a form that kiteChart will work with... # assuming your matrix or data.frame is called X Xwide - reshape(X, timevar=depth, idvar=distance, direction=wide) # replace NAs with 0 (don't think kiteChart likes NA) Xwide[ is.na(Xwide) ] - 0 kiteChart(Xwide) I haven't considered all of your plot requirements here but hopefully this will get you started. Michael On 13 October 2010 19:11, elpape ellen.p...@gmail.com wrote: Dear everyone, I would like to create a kite chart in which I plot densities (width of the vertical kites) in relation to sediment depth (on reversed y-axis) for 6 different locations (Distances from seep site, on x-axis on top of the plot). The dataset I would like to use is: Distance_from_seep_site Sedimentdepth Density 1100 0 107.8 1100 1 264.6 1100 2 284.2 1100 3 470.4 1100 4 58.8 100 0 98 100 1 176.4 100 2 499.8 100 3 548.8 100 4 401.8 100 5 107.8 10 0 51.3 10 1 22.8 10 2 79.8 10 3 68.4 10 4 17.1 10 5 5.7 10 6 17.1 5 0 188.1 5 1 267.9 5 2 376.2 5 3 233.7 5 4 165.3 5 8 5.7 5 9 5.7 2 0 74.1 2 1 102.6 2 2 85.5 2 3 91.2 2 4 34.2 2 5 5.7 2 6 11.4 2 8 11.4 2 10 28.5 2 11 22.8 0 0 461.7 0 1 273.6 0 2 79.8 0 3 68.4 0 4 34.2 0 5 22.8 0 6 51.3 0 8 68.4 0 9 39.9 0 11 22.8 I have tried to rearrange the data, but I do not seem to get the output that I desire... Can anyone help me? Thank you so much! Ellen -- View this message in context: http://r.789695.n4.nabble.com/vertical-kites-in-KiteChart-plotrix-tp2993295p2993295.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] vertical kites in KiteChart (plotrix)
Oops, sorry, I left out a step in that last post After replace NAs with 0 in Xwide... # use distance col as row names rownames( Xwide ) - Xwide[ , 1 ] Xwide - Xwide[ , -1 ] kiteChart( Xwide ) On 13 October 2010 19:49, Michael Bedward michael.bedw...@gmail.com wrote: Hello Ellen, First up I think you can use reshape to get your data into a form that kiteChart will work with... # assuming your matrix or data.frame is called X Xwide - reshape(X, timevar=depth, idvar=distance, direction=wide) # replace NAs with 0 (don't think kiteChart likes NA) Xwide[ is.na(Xwide) ] - 0 kiteChart(Xwide) I haven't considered all of your plot requirements here but hopefully this will get you started. Michael On 13 October 2010 19:11, elpape ellen.p...@gmail.com wrote: Dear everyone, I would like to create a kite chart in which I plot densities (width of the vertical kites) in relation to sediment depth (on reversed y-axis) for 6 different locations (Distances from seep site, on x-axis on top of the plot). The dataset I would like to use is: Distance_from_seep_site Sedimentdepth Density 1100 0 107.8 1100 1 264.6 1100 2 284.2 1100 3 470.4 1100 4 58.8 100 0 98 100 1 176.4 100 2 499.8 100 3 548.8 100 4 401.8 100 5 107.8 10 0 51.3 10 1 22.8 10 2 79.8 10 3 68.4 10 4 17.1 10 5 5.7 10 6 17.1 5 0 188.1 5 1 267.9 5 2 376.2 5 3 233.7 5 4 165.3 5 8 5.7 5 9 5.7 2 0 74.1 2 1 102.6 2 2 85.5 2 3 91.2 2 4 34.2 2 5 5.7 2 6 11.4 2 8 11.4 2 10 28.5 2 11 22.8 0 0 461.7 0 1 273.6 0 2 79.8 0 3 68.4 0 4 34.2 0 5 22.8 0 6 51.3 0 8 68.4 0 9 39.9 0 11 22.8 I have tried to rearrange the data, but I do not seem to get the output that I desire... Can anyone help me? Thank you so much! Ellen -- View this message in context: http://r.789695.n4.nabble.com/vertical-kites-in-KiteChart-plotrix-tp2993295p2993295.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.