[R] recursive function on a structured list of lists (dendrogram)
Dear all, I have been trying the following without avail and would be very grateful for any help. From a dendrogram (recursive list of lists with some structure), I would like to obtain some information of the component lists and of the enclosing list at the same time. In dendrogram-speech I basically would like the label of the leaf and the height of the enclosing branch. A dendrogram example (from the help file of stats::dendrogram), and some functions showing how it is structured: hc - hclust(dist(USArrests), ave) dend1 - as.dendrogram(hc) plot(dend1) str(dend1) Similarly to dendrapply(), I tried o recursively obtain from the tree a list including, for each member (leaf) the height of the list containing it. However, I fail to fully grasp how the 'recursiveness' is made within the function saving both elements at the leaf and branch levels. For reference the dendrapply function is as follows: function (X, FUN, ...) { FUN - match.fun(FUN) if (!inherits(X, dendrogram)) stop('X' is not a dendrogram) Napply - function(d) { r - FUN(d, ...) if (!is.leaf(d)) { if (!is.list(r)) r - as.list(r) if (length(r) (n - length(d))) r[seq_len(n)] - vector(list, n) r[] - lapply(d, Napply) } r } Napply(X) } I essentially don't manage to 'save' the height of a branch (a list of lists) so that it can be used at the next iterations for adding to the leafs there. Many thanks for any guidance on how to recursively implement a function. Kind regards, Ivan [[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] Recursive function calls
My apologies, I know that this is not a new problem, but I'm not sure how to find the answer I want to recursively loop over an object and trim trailing white space. When I use this function on a list of data.frame I get output like this: [1] c(\ many spaces \, \ many spaces \) c(\ many spaces \, \ many spaces \) What should I do to recombine the results? If anyone has a good way to search for this type of question, that would be appreciated. I tried rseek.org with recursive, but after wading though all the rpart references I didn't find something that seemed to help with this problem. Thank you very much. trim - function(x) { if(length(x)1) sapply(x[1], trim) gsub(^[[:space:]]+|[[:space:]]+$, , x) } tempobj = ' many spaces ' tempvec = c(tempobj, tempobj) templist = list(tempvec, tempvec) tempdf = data.frame(x = tempvec, y = tempvec) trim(tempobj) trim(tempvec) trim(templist) trim(tempdf) Thank you, Gene Leynes _ *Data Scientist* *Mobile: 312-498-7702 **http://www.linkedin.com/in/geneleynes * http://goog_598053156*http://geneorama.com/ http://geneorama.com/%20* [[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] Recursive function calls
Recursively loop over an object is a pretty meaningless phrase, since it depends entirely on the structure of the object. For example, a character vector is an object, and there is no need for any sort of recursion to do what you want for it. The following regex example trims trailing spaces (see ?regex for an exact definition). Adapt it to whatever structure you like, probably with apply type functions. x - c( ab ,ab \t ,\t\t) x [1]abab \t\t\t sub([[:space:]]+$,,x) [1]ab ab But note also that (e.g. S3) methods and is.list or is.recursive may be useful in a more general approach, something like (where deSpace(x) is a function with the above sub() expression): nospace - function(x){ if(is.atomic(x))deSpace(x) else lapply(x, function(y)Recall(y)) ##?Recall for recursion } Note that this is completely untested, probably fails miserably and isn't what you want anyway, ... ;-) Good luck! Cheers, Bert On Fri, Aug 3, 2012 at 9:12 AM, Gene Leynes gley...@gmail.com wrote: My apologies, I know that this is not a new problem, but I'm not sure how to find the answer I want to recursively loop over an object and trim trailing white space. When I use this function on a list of data.frame I get output like this: [1] c(\ many spaces \, \ many spaces \) c(\ many spaces \, \ many spaces \) What should I do to recombine the results? If anyone has a good way to search for this type of question, that would be appreciated. I tried rseek.org with recursive, but after wading though all the rpart references I didn't find something that seemed to help with this problem. Thank you very much. trim - function(x) { if(length(x)1) sapply(x[1], trim) gsub(^[[:space:]]+|[[:space:]]+$, , x) } tempobj = ' many spaces ' tempvec = c(tempobj, tempobj) templist = list(tempvec, tempvec) tempdf = data.frame(x = tempvec, y = tempvec) trim(tempobj) trim(tempvec) trim(templist) trim(tempdf) Thank you, Gene Leynes _ *Data Scientist* *Mobile: 312-498-7702 **http://www.linkedin.com/in/geneleynes * http://goog_598053156*http://geneorama.com/ http://geneorama.com/%20* [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Recursive function calls
Note that this is a common enough case that Hadley provides for it with the str_trim() function in his stringr package. Best, Michael On Fri, Aug 3, 2012 at 12:02 PM, Bert Gunter gunter.ber...@gene.com wrote: Recursively loop over an object is a pretty meaningless phrase, since it depends entirely on the structure of the object. For example, a character vector is an object, and there is no need for any sort of recursion to do what you want for it. The following regex example trims trailing spaces (see ?regex for an exact definition). Adapt it to whatever structure you like, probably with apply type functions. x - c( ab ,ab \t ,\t\t) x [1]abab \t\t\t sub([[:space:]]+$,,x) [1]ab ab But note also that (e.g. S3) methods and is.list or is.recursive may be useful in a more general approach, something like (where deSpace(x) is a function with the above sub() expression): nospace - function(x){ if(is.atomic(x))deSpace(x) else lapply(x, function(y)Recall(y)) ##?Recall for recursion } Note that this is completely untested, probably fails miserably and isn't what you want anyway, ... ;-) Good luck! Cheers, Bert On Fri, Aug 3, 2012 at 9:12 AM, Gene Leynes gley...@gmail.com wrote: My apologies, I know that this is not a new problem, but I'm not sure how to find the answer I want to recursively loop over an object and trim trailing white space. When I use this function on a list of data.frame I get output like this: [1] c(\ many spaces \, \ many spaces \) c(\ many spaces \, \ many spaces \) What should I do to recombine the results? If anyone has a good way to search for this type of question, that would be appreciated. I tried rseek.org with recursive, but after wading though all the rpart references I didn't find something that seemed to help with this problem. Thank you very much. trim - function(x) { if(length(x)1) sapply(x[1], trim) gsub(^[[:space:]]+|[[:space:]]+$, , x) } tempobj = ' many spaces ' tempvec = c(tempobj, tempobj) templist = list(tempvec, tempvec) tempdf = data.frame(x = tempvec, y = tempvec) trim(tempobj) trim(tempvec) trim(templist) trim(tempdf) Thank you, Gene Leynes _ *Data Scientist* *Mobile: 312-498-7702 **http://www.linkedin.com/in/geneleynes * http://goog_598053156*http://geneorama.com/ http://geneorama.com/%20* [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Recursive function calls
Hello, This seems to work. trim2 - function(x) { if(is.atomic(x)) gsub(^[[:space:]]+|[[:space:]]+$, , x) else sapply(x, function(y) trim2(y)) } # Tests trim2(tempobj) trim2(tempvec) trim2(templist) trim2(tempdf) # Extra test templistlist - list(templist, list(tempobj, tempdf)) trim2(templistlist) Note, however, that the df is not returned as a df: class(trim2(tempdf)) [1] matrix Hope this helps, Rui Barradas Em 03-08-2012 17:12, Gene Leynes escreveu: My apologies, I know that this is not a new problem, but I'm not sure how to find the answer I want to recursively loop over an object and trim trailing white space. When I use this function on a list of data.frame I get output like this: [1] c(\ many spaces \, \ many spaces \) c(\ many spaces \, \ many spaces \) What should I do to recombine the results? If anyone has a good way to search for this type of question, that would be appreciated. I tried rseek.org with recursive, but after wading though all the rpart references I didn't find something that seemed to help with this problem. Thank you very much. trim - function(x) { if(length(x)1) sapply(x[1], trim) gsub(^[[:space:]]+|[[:space:]]+$, , x) } tempobj = ' many spaces ' tempvec = c(tempobj, tempobj) templist = list(tempvec, tempvec) tempdf = data.frame(x = tempvec, y = tempvec) trim(tempobj) trim(tempvec) trim(templist) trim(tempdf) Thank you, Gene Leynes _ *Data Scientist* *Mobile: 312-498-7702 **http://www.linkedin.com/in/geneleynes * http://goog_598053156*http://geneorama.com/ http://geneorama.com/%20* [[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] Recursive function calls
On Fri, Aug 3, 2012 at 12:19 PM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, This seems to work. trim2 - function(x) { if(is.atomic(x)) gsub(^[[:space:]]+|[[:space:]]+$, , x) else sapply(x, function(y) trim2(y)) } Using sapply is a bit dangerous here. Compare: trim2(list(c(a, b), c(c, d))) # [,1] [,2] # [1,] a c # [2,] b d trim2(list(c(a, b), c(c, d, e))) # [[1]] # [1] a b # # [[2]] # [1] c d e which I think is rather undesirable behaviour. sapply is suitable for interactive use, but you should never use it inside a function because you don't know what sort of data structure you'll get back. I think it's also a bit unsafe to accept any type of input - you're generally better off being explicit. This leads to trim3: trim3 - function(x) { if (is.character(x)) { gsub(^[[:space:]]+|[[:space:]]+$, , x) } else if (is.list(x)) { lapply(x, trim3) } else { warning(Invalid input: , paste(class(x), sep = /)) x } } trim2(list(c(a, b), c(c, d))) trim3(list(c(a, b), c(c, d))) But then the function isn't extensible for new types of input, which suggests an S3 implementation: trim4 - function(x) UseMethod(trim4) trim4.character - function(x) gsub(^[[:space:]]+|[[:space:]]+$, , x) trim4.list - function(x) lapply(x, trim4) trim4.default - function(x) { warning(Invalid input) x } Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Recursive function calls
Rui, Yes, that's exactly it, thanks!! I wouldn't have thought of the is.atomic. I was going after a series of tests for dimension and is.list. @ R. Michael Weylandt Good point! Also, thanks for mentioning stringr. I always forget about that one. It's nice that R keeps the base function list short enough that you can look at it, but it would be nice to have a few more convenience functions included, especially ones that mirror common functions, like trim On Fri, Aug 3, 2012 at 12:19 PM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, This seems to work. trim2 - function(x) { if(is.atomic(x)) gsub(^[[:space:]]+|[[:space:]**]+$, , x) else sapply(x, function(y) trim2(y)) } # Tests trim2(tempobj) trim2(tempvec) trim2(templist) trim2(tempdf) # Extra test templistlist - list(templist, list(tempobj, tempdf)) trim2(templistlist) Note, however, that the df is not returned as a df: class(trim2(tempdf)) [1] matrix Hope this helps, Rui Barradas Em 03-08-2012 17:12, Gene Leynes escreveu: My apologies, I know that this is not a new problem, but I'm not sure how to find the answer I want to recursively loop over an object and trim trailing white space. When I use this function on a list of data.frame I get output like this: [1] c(\ many spaces \, \ many spaces \) c(\ many spaces \, \ many spaces \) What should I do to recombine the results? If anyone has a good way to search for this type of question, that would be appreciated. I tried rseek.org with recursive, but after wading though all the rpart references I didn't find something that seemed to help with this problem. Thank you very much. trim - function(x) { if(length(x)1) sapply(x[1], trim) gsub(^[[:space:]]+|[[:space:]**]+$, , x) } tempobj = ' many spaces ' tempvec = c(tempobj, tempobj) templist = list(tempvec, tempvec) tempdf = data.frame(x = tempvec, y = tempvec) trim(tempobj) trim(tempvec) trim(templist) trim(tempdf) Thank you, Gene Leynes __**___ *Data Scientist* *Mobile: 312-498-7702 **http://www.linkedin.com/in/**geneleyneshttp://www.linkedin.com/in/geneleynes * http://goog_598053156*http:/**/geneorama.com/ http://geneorama.com/ http://geneorama.com/%20* [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Recursive function calls
On Fri, Aug 3, 2012 at 3:36 PM, Gene Leynes gley...@gmail.com wrote: Rui, Yes, that's exactly it, thanks!! I wouldn't have thought of the is.atomic. I was going after a series of tests for dimension and is.list. One other helpful function that I haven't seen anyone mention this far is ? rapply [= recursive apply] Best, 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] Recursive function calls
It's nice that R keeps the base function list short enough that you can look at it, but it would be nice to have a few more convenience functions included, especially ones that mirror common functions, like trim sum(sapply(search(), function(x) length(ls(x [1] 2376 Over two thousand objects seems like rather a lot to me... Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Recursive function calls
Ah yes, good point. this was easy enough to write, it doesn't lose dimensions, and there's no unnecessary complexity... unless you're passing in data frames, in which you'll have to recombine them. trim = function(x) gsub(^[[:space:]]+|[[:space:]]+$, , x) trimmer = function(x){ if(is.list(x)){ rapply(x, trim, how='replace') }else{ trim(x) } } tempobj = ' many spaces ' tempvec = c(tempobj, tempobj) templist = list(tempvec, tempvec) tempdf = data.frame(x = tempvec, y = tempvec) trimmer(tempobj) trimmer(tempvec) trimmer(templist) trimmer(tempdf) Thank you, Gene Leynes _ *Data Scientist* *Mobile: 312-498-7702 **http://www.linkedin.com/in/geneleynes * http://goog_598053156*http://geneorama.com/ http://geneorama.com/%20* On Fri, Aug 3, 2012 at 4:14 PM, R. Michael Weylandt michael.weyla...@gmail.com wrote: On Fri, Aug 3, 2012 at 3:36 PM, Gene Leynes gley...@gmail.com wrote: Rui, Yes, that's exactly it, thanks!! I wouldn't have thought of the is.atomic. I was going after a series of tests for dimension and is.list. One other helpful function that I haven't seen anyone mention this far is ? rapply [= recursive apply] Best, 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.
Re: [R] Recursive function calls
On Fri, Aug 3, 2012 at 5:41 PM, Gene Leynes gley...@gmail.com wrote: Ah yes, good point. this was easy enough to write, it doesn't lose dimensions, and there's no unnecessary complexity... unless you're passing in data frames, in which you'll have to recombine them. trim = function(x) gsub(^[[:space:]]+|[[:space:]]+$, , x) trimmer = function(x){ if(is.list(x)){ rapply(x, trim, how='replace') }else{ trim(x) } } Possibly, though I suppose it's a matter of judgement, even more straightforward to just make your input into a list and don't special case: trimmer - function(x){rapply(list(x), function(x) gsub(^[[:space:]]+|[[:space:]]+$, , x), how = replace)[[1]]} Note the [[1]] to undo the list() call we added. Best, Michael tempobj = ' many spaces ' tempvec = c(tempobj, tempobj) templist = list(tempvec, tempvec) tempdf = data.frame(x = tempvec, y = tempvec) trimmer(tempobj) trimmer(tempvec) trimmer(templist) trimmer(tempdf) Thank you, Gene Leynes _ Data Scientist Mobile: 312-498-7702 http://www.linkedin.com/in/geneleynes http://geneorama.com/ On Fri, Aug 3, 2012 at 4:14 PM, R. Michael Weylandt michael.weyla...@gmail.com wrote: On Fri, Aug 3, 2012 at 3:36 PM, Gene Leynes gley...@gmail.com wrote: Rui, Yes, that's exactly it, thanks!! I wouldn't have thought of the is.atomic. I was going after a series of tests for dimension and is.list. One other helpful function that I haven't seen anyone mention this far is ? rapply [= recursive apply] Best, 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.
[R] recursive function - finding connections
Dear all, I'm having some problems getting my recursive function to work. At first I though that maybe my data was too big and I increase option(expressions=5). Then I thought that I would try it on some smaller data. Still not working. :( I would have thought there should be a function for this already, so any suggestions are welcomed for other methods. I did try igraph but couldn't get cliques() to give anything useful. Also a quick play with hclust and cut, again nothing too useful. Basically the function is trying to find uniquely connected subgraphs. So the sub-network is only connected by itself and not to other nodes. If everything is connected then the list (connectedList) should be length of 1 and have every index in the 1st slot. cheers, Paul findconnection-function(mat, cutoff){ toList-function(mat, connectList, cutoff, i, idx){ idx-which(mat[,idx] cutoff) if(length(idx) = 1){ connectList[[i]]-idx for(z in 1:length(idx)){ connectList-toList(mat, connectList, cutoff, i, idx[z]) } }else{ return(connectList) } } connectList-list() for(i in 1:ncol(mat)){ connectList-toList(mat, connectList, cutoff, i, i) } return(unique(connectList)) } foomat-matrix(sample(c(1,0.5,0), 100, replace=T), nrow=10) ## example data foomat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0.0 0.5 0.0 0.5 0.5 0.0 0.5 1.0 0.5 0.0 [2,] 0.0 1.0 1.0 0.0 0.0 1.0 0.0 1.0 0.5 1.0 [3,] 1.0 1.0 1.0 1.0 0.5 0.0 0.5 0.5 0.5 0.5 [4,] 0.0 0.5 0.0 0.0 0.5 0.5 0.5 0.0 1.0 0.0 [5,] 0.5 0.5 1.0 1.0 0.5 1.0 1.0 0.5 0.5 0.5 [6,] 0.0 0.5 0.0 0.5 0.5 0.5 0.5 0.5 1.0 1.0 [7,] 1.0 1.0 0.0 1.0 0.0 0.5 1.0 1.0 0.5 0.5 [8,] 0.5 1.0 0.0 0.5 1.0 0.0 1.0 0.0 0.0 0.0 [9,] 0.0 0.5 0.0 0.0 0.5 0.0 0.5 0.0 0.5 0.5 [10,] 1.0 1.0 0.5 1.0 0.0 1.0 0.0 0.0 0.0 0.5 pb-findconnection(foomat, 0.01) Error: C stack usage is too close to the limit Error during wrapup: __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] recursive function - finding connections
Sorry bad example. My data is undirected. It's a correlation matrix so probably better to look at something like: foomat-cor(matrix(rnorm(100), ncol=10)) foomat mine are pvalues from the correlation but same idea. On 14 Jul 2011, at 11:23, Erich Neuwirth wrote: cliques only works for undirected graphs. Your matrix is not symmetric, therefore the graph is directed. On 7/14/2011 8:53 AM, Benton, Paul wrote: Dear all, I'm having some problems getting my recursive function to work. At first I though that maybe my data was too big and I increase option(expressions=5). Then I thought that I would try it on some smaller data. Still not working. :( I would have thought there should be a function for this already, so any suggestions are welcomed for other methods. I did try igraph but couldn't get cliques() to give anything useful. Also a quick play with hclust and cut, again nothing too useful. Basically the function is trying to find uniquely connected subgraphs. So the sub-network is only connected by itself and not to other nodes. If everything is connected then the list (connectedList) should be length of 1 and have every index in the 1st slot. cheers, Paul findconnection-function(mat, cutoff){ toList-function(mat, connectList, cutoff, i, idx){ idx-which(mat[,idx] cutoff) if(length(idx) = 1){ connectList[[i]]-idx for(z in 1:length(idx)){ connectList-toList(mat, connectList, cutoff, i, idx[z]) } }else{ return(connectList) } } connectList-list() for(i in 1:ncol(mat)){ connectList-toList(mat, connectList, cutoff, i, i) } return(unique(connectList)) } foomat-matrix(sample(c(1,0.5,0), 100, replace=T), nrow=10) ## example data foomat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0.0 0.5 0.0 0.5 0.5 0.0 0.5 1.0 0.5 0.0 [2,] 0.0 1.0 1.0 0.0 0.0 1.0 0.0 1.0 0.5 1.0 [3,] 1.0 1.0 1.0 1.0 0.5 0.0 0.5 0.5 0.5 0.5 [4,] 0.0 0.5 0.0 0.0 0.5 0.5 0.5 0.0 1.0 0.0 [5,] 0.5 0.5 1.0 1.0 0.5 1.0 1.0 0.5 0.5 0.5 [6,] 0.0 0.5 0.0 0.5 0.5 0.5 0.5 0.5 1.0 1.0 [7,] 1.0 1.0 0.0 1.0 0.0 0.5 1.0 1.0 0.5 0.5 [8,] 0.5 1.0 0.0 0.5 1.0 0.0 1.0 0.0 0.0 0.0 [9,] 0.0 0.5 0.0 0.0 0.5 0.0 0.5 0.0 0.5 0.5 [10,] 1.0 1.0 0.5 1.0 0.0 1.0 0.0 0.0 0.0 0.5 pb-findconnection(foomat, 0.01) Error: C stack usage is too close to the limit Error during wrapup: __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] recursive function - finding connections
Hi Paul, I assume you are using the argument cutoff to specify the p-value below which nodes are considered connected and above which they are not connected. I would use single linkage hierarchical clustering. If you have two groups of nodes and any two nodes between the groups are connected (i.e. have adjacency =1 or dissimilarity 0), then the groups have dissimilarity 0. If no two nodes between the two groups are connected, you will get dissimilarity 1. Thus you can use any tree cut height between 0 and 1 to get the clusters that correspond to connected. For large data you will need a large computer to hold your distance matrix, but you must have observed that already. subgraphs = function(mat, cut) { disconnected = matcut # Change the inequality if necessary tree = hclust(as.dist(disconnected), method = single) clusters = cutree(tree, h = 0.5) # Clusters is already the answer, but you want it in a different format, so we reformat it. nClusters = max(clusters) connectedList = list(); for (c in 1:nClusters) connectedList[[c]] = which(clusters==c) connectedList } Try it and see if this does what you want. HTH Peter On Thu, Jul 14, 2011 at 4:12 PM, Benton, Paul hpaul.bento...@imperial.ac.uk wrote: Sorry bad example. My data is undirected. It's a correlation matrix so probably better to look at something like: foomat-cor(matrix(rnorm(100), ncol=10)) foomat mine are pvalues from the correlation but same idea. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] recursive function - finding connections
One more thing - for large data sets, the packages flashClust and fastcluster provide much faster hierarchical clustering that (at least for flashClust which I'm the maintainer of) give the exact same results. Simply insert a library(flashClust) before you call the function and your code will run much faster. Peter On Thu, Jul 14, 2011 at 4:58 PM, Peter Langfelder peter.langfel...@gmail.com wrote: Hi Paul, I assume you are using the argument cutoff to specify the p-value below which nodes are considered connected and above which they are not connected. I would use single linkage hierarchical clustering. If you have two groups of nodes and any two nodes between the groups are connected (i.e. have adjacency =1 or dissimilarity 0), then the groups have dissimilarity 0. If no two nodes between the two groups are connected, you will get dissimilarity 1. Thus you can use any tree cut height between 0 and 1 to get the clusters that correspond to connected. For large data you will need a large computer to hold your distance matrix, but you must have observed that already. subgraphs = function(mat, cut) { disconnected = matcut # Change the inequality if necessary tree = hclust(as.dist(disconnected), method = single) clusters = cutree(tree, h = 0.5) # Clusters is already the answer, but you want it in a different format, so we reformat it. nClusters = max(clusters) connectedList = list(); for (c in 1:nClusters) connectedList[[c]] = which(clusters==c) connectedList } Try it and see if this does what you want. HTH Peter On Thu, Jul 14, 2011 at 4:12 PM, Benton, Paul hpaul.bento...@imperial.ac.uk wrote: Sorry bad example. My data is undirected. It's a correlation matrix so probably better to look at something like: foomat-cor(matrix(rnorm(100), ncol=10)) foomat mine are pvalues from the correlation but same idea. -- Sent from my Linux computer. Way better than iPad :) __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] recursive function
Hi, I created a function for obtaining the normal cumulative distribution (I know all this already exists in R, I just wanted to verify my understanding of it). below is the code I came up with. cdf-function(x) { erf-function(x) { # approximation to the error function (erf) of the # normal cumulative distribution function # from Winitzki (2008) a - 0.147 partc - 1+a*x^2 partb - 4/pi+a*x^2 parta - -x^2*(partb/partc) erf - sqrt(1-exp(parta)) erf } # cumulative density function cdf- 1/2*(1+erf(x/sqrt(2))) cdf } The erf(x) produces positive values whatever the sign of x. Instead of obtaining the expected sigmoid shape of the erf() I end up with a positive values V shape. erf(x) should be negative for negative values of x. I figure I need some form of conditional statement in the above code but all my attempts at using if and for failed miserably. Clearly, I don't understand how to use them properly in R. If anyone could point me in the right direction as on how to code conditional events in R that would be much appreciated. many thanks in advance Pierre-Olivier [[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] recursive function
(1) What has this to do with recursion? (2) You probably need to use ifelse(). I believe that this is (in effect) an FAQ. cheers, Rolf Turner On 20/05/11 07:42, Tremblay, Pierre-Olivier wrote: Hi, I created a function for obtaining the normal cumulative distribution (I know all this already exists in R, I just wanted to verify my understanding of it). below is the code I came up with. cdf-function(x) { erf-function(x) { # approximation to the error function (erf) of the # normal cumulative distribution function # from Winitzki (2008) a- 0.147 partc- 1+a*x^2 partb- 4/pi+a*x^2 parta- -x^2*(partb/partc) erf- sqrt(1-exp(parta)) erf } # cumulative density function cdf- 1/2*(1+erf(x/sqrt(2))) cdf } The erf(x) produces positive values whatever the sign of x. Instead of obtaining the expected sigmoid shape of the erf() I end up with a positive values V shape. erf(x) should be negative for negative values of x. I figure I need some form of conditional statement in the above code but all my attempts at using if and for failed miserably. Clearly, I don't understand how to use them properly in R. If anyone could point me in the right direction as on how to code conditional events in R that would be much appreciated. many thanks in advance __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] recursive function
Perhaps this is useful: x=c(-2,0,2) sign(x)*abs(x) [1] -2 0 2 -- Robert W. Baer, Ph.D. Professor of Physiology Kirksville College of Osteopathic Medicine A. T. Still University of Health Sciences 800 W. Jefferson St. Kirksville, MO 63501 660-626-2322 FAX 660-626-2965 -- Robert W. Baer, Ph.D. Professor of Physiology Kirksville College of Osteopathic Medicine A. T. Still University of Health Sciences 800 W. Jefferson St. Kirksville, MO 63501 660-626-2322 FAX 660-626-2965 -- From: Tremblay, Pierre-Olivier potremb...@pharmanet.com Sent: Thursday, May 19, 2011 2:42 PM To: r-help@r-project.org Subject: [R] recursive function Hi, I created a function for obtaining the normal cumulative distribution (I know all this already exists in R, I just wanted to verify my understanding of it). below is the code I came up with. cdf-function(x) { erf-function(x) { # approximation to the error function (erf) of the # normal cumulative distribution function # from Winitzki (2008) a - 0.147 partc - 1+a*x^2 partb - 4/pi+a*x^2 parta - -x^2*(partb/partc) erf - sqrt(1-exp(parta)) erf } # cumulative density function cdf- 1/2*(1+erf(x/sqrt(2))) cdf } The erf(x) produces positive values whatever the sign of x. Instead of obtaining the expected sigmoid shape of the erf() I end up with a positive values V shape. erf(x) should be negative for negative values of x. I figure I need some form of conditional statement in the above code but all my attempts at using if and for failed miserably. Clearly, I don't understand how to use them properly in R. If anyone could point me in the right direction as on how to code conditional events in R that would be much appreciated. many thanks in advance Pierre-Olivier [[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] recursive function
Dear list, I have the following problem, what i'm trying to do is to built a function which does the following calculationg in a recursive way: I have a data frame more or less like this: variableyear DELTA EC01 2006/ EC01 2007 10 EC01 20085 EC01 20099 And then I have at time 2009 a variable called R_EC01(2009)=5 What I have to do is to construct the R_EC01 time series by starting from the 2009 value: R_EC01(2008)=R_EC01(2009)-DELTA(2009) R_EC01(2007)=R_EC01(2008)-DELTA(2008) R_EC01(2006)=R_EC01(2007)-DELTA(2007) In terms of number, the results that i should get are: R_EC01(2008)=5-9=-4 R_EC01(2007)=-4-5=-9 R_EC01(2006)=-9-10=-19 so my data frame should looks like this SERIESYEAR value R_EC01 2006 -19 R_EC012007 -9 R_EC012008 -4 R_EC01 2009 5 Anyone Knows hot to do it?? My dataframe is not set as a time series... Thanks a lot!!! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] recursive function
Hi! Do you mean something like this (df is your original data frame): --- cut here --- df1-df df1[[1]]-paste(R,df[[1]],sep=_) colnames(df1)-c(SERIES,YEAR,value) df1$value[ df1$YEAR==2009 ]-5 for (i in c(2009:2007)) { df1$value[ df1$YEAR==(i-1) ]-( df1$value[ df1$YEAR==i ]-df$DELTA[ df$year==i ] ) } --- cut here --- Now the output: df1 SERIES YEAR value 1 R_EC01 2006 -19 2 R_EC01 2007-9 3 R_EC01 2008-4 4 R_EC01 2009 5 Please let me know if you were looking for a more general approach suitable for larger data frames with e.g. several variable classes (EC01, EC02 etc.) Kind regards, Kimmo -- University of Turku, Finland Dep. of Political Science and Contemporary history __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] recursive function
Try this: transform(x, DELTA = NULL, value = rev(c(5, 5 - cumsum(rev(DELTA[-1]) On Mon, Jun 14, 2010 at 12:29 PM, n.via...@libero.it n.via...@libero.itwrote: Dear list, I have the following problem, what i'm trying to do is to built a function which does the following calculationg in a recursive way: I have a data frame more or less like this: variableyear DELTA EC01 2006/ EC01 2007 10 EC01 20085 EC01 20099 And then I have at time 2009 a variable called R_EC01(2009)=5 What I have to do is to construct the R_EC01 time series by starting from the 2009 value: R_EC01(2008)=R_EC01(2009)-DELTA(2009) R_EC01(2007)=R_EC01(2008)-DELTA(2008) R_EC01(2006)=R_EC01(2007)-DELTA(2007) In terms of number, the results that i should get are: R_EC01(2008)=5-9=-4 R_EC01(2007)=-4-5=-9 R_EC01(2006)=-9-10=-19 so my data frame should looks like this SERIESYEAR value R_EC01 2006 -19 R_EC012007 -9 R_EC012008 -4 R_EC01 2009 5 Anyone Knows hot to do it?? My dataframe is not set as a time series... Thanks a lot!!! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[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] recursive function, how to avoid list structure in return value
I've written a recursive function to extract the members of an individual cluster within a hierarchical clustering. I have something that works, but the return value has a list structure I don't like. I know how to work around with 'unlist()' but I suspect the function could be fixed. Can anyone show me show? Thanks, Jenny Bryan Demo of my problem -- (Note: although my question has nothing to do with hierarchical clustering per se, my example does assume knowledge of the 'merge' object.) ## faking the key aspects of an hclust object myClust - list(merge = rbind(c(-1, -2), c(-3, -4), c(2, -5), c(1, 3), c(4, -6)), height = 1:5, order = 1:6) ## plot the example / fake tree stats:::plot.hclust(myClust, hang = -1) ## recursive function to extract members of a cluster ## 'sapply' version clMembFun1 - function(x) { if(x 0) { -x } else { sapply(1:2, function(j) clMembFun1(myClust$merge[x,j])) } } Here's a transcript of using clMembFun: ## trivial case of cluster = 2 singletons is OK clMembFun1(1) [1] 1 2 str(clMembFun1(1)) # num vector num [1:2] 1 2 ## case of cluster that contains a cluster -- list clMembFun1(3) [[1]] [1] 3 4 [[2]] [1] 5 str(clMembFun1(3)) List of 2 $ : num [1:2] 3 4 $ : num 5 ## now the list also has 2D matrix structure clMembFun1(4) [,1] [,2] [1,] 1Numeric,2 [2,] 25 str(clMembFun1(4)) List of 4 $ : num 1 $ : num 2 $ : num [1:2] 3 4 $ : num 5 - attr(*, dim)= int [1:2] 2 2 ## and it just gets worse clMembFun1(5) [[1]] [,1] [,2] [1,] 1Numeric,2 [2,] 25 [[2]] [1] 6 str(clMembFun1(5)) List of 2 $ :List of 4 ..$ : num 1 ..$ : num 2 ..$ : num [1:2] 3 4 ..$ : num 5 ..- attr(*, dim)= int [1:2] 2 2 $ : num 6 I know one workaround is to 'unlist' the return value: ## post hoc fix unlist(clMembFun1(3)) [1] 3 4 5 unlist(clMembFun1(4)) [1] 1 2 3 4 5 unlist(clMembFun1(5)) [1] 1 2 3 4 5 6 But can the function itself be fixed/improved? I also tried using a 'for' loop instead of 'sapply' but that suffered from fatal problems (maybe I didn't implement correctly?). __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] recursive function help SOLVED (sort of)
Well, it turns out to be very simple - just insert a Vectorize between integrate and function(x). However, the special cases where C[i,j]==1 make the actual code quite messy. It matches pmvnorm {mvtnorm} and pmnorm {mnormt} quite well. And the recursive method is incredibly slow for higher dimensions. And still some cases blow up or don't converge. So, never mind. It looked clever, but I would recommend pmvnorm for speed and accuracy, even though it is non-deterministic for higher dimensions. One little note: for the bivariate, this method (without recursion) is as accurate as the existing methods and a bit faster than pmvnorm. -- David -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: Tuesday, February 19, 2008 3:08 PM To: r-help@r-project.org Subject: [R] recursive function help I'm trying to implement a recursive function using integrate, and I suspect I need a Vectorize somewhere, but I can't suss it out. Any help would be appreciated. I've tried traceback() and various debugging ideas to no avail (most likely due to my inexperience with these tools.) Here's what I have. Nk - function(m, C) { if (length(m) 1) { rho - C[1, -1] Rmat - C[-1, -1] B - diag(1/sqrt(1 - rho*rho)) %*% (-rho %*% t(rho) + Rmat) %*% diag(1/sqrt(1 - rho*rho)) integrate( function(x) dnorm(x) * Nk((m[-1] - rho*x)/sqrt(1 - rho*rho), B), -10, m[1] )$value } else { pnorm(m[1]) } } my example is x2 - c(0.0781292, -1.6403152) sigma2 - matrix(c(1, -0.5054781, -0.5054781, 1), nrow=2) Nk(x2, sigma2) Error in integrate(function(x) dnorm(x) * Nk((m[-1] - rho * x)/sqrt(1 - : non-finite function value All the pieces work outside of the function, and the integrand is finite as far as I can see. [Yes, this is a recursive function for multivariate cumulative normal. It seems to match (so far for k=2 without recursion) the existing R functions from packages mvtnorm and mnormt. It is from D. Cassimon, et al. Closed-form valuation of American call options on stocks paying multiple dividends. Finance Research Letters 4 (2007) 33-48.] Thank you to anyone who can shed some light on this. David L. Reiner, PhD Rho Trading Securities, LLC __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] recursive function help
I'm trying to implement a recursive function using integrate, and I suspect I need a Vectorize somewhere, but I can't suss it out. Any help would be appreciated. I've tried traceback() and various debugging ideas to no avail (most likely due to my inexperience with these tools.) Here's what I have. Nk - function(m, C) { if (length(m) 1) { rho - C[1, -1] Rmat - C[-1, -1] B - diag(1/sqrt(1 - rho*rho)) %*% (-rho %*% t(rho) + Rmat) %*% diag(1/sqrt(1 - rho*rho)) integrate( function(x) dnorm(x) * Nk((m[-1] - rho*x)/sqrt(1 - rho*rho), B), -10, m[1] )$value } else { pnorm(m[1]) } } my example is x2 - c(0.0781292, -1.6403152) sigma2 - matrix(c(1, -0.5054781, -0.5054781, 1), nrow=2) Nk(x2, sigma2) Error in integrate(function(x) dnorm(x) * Nk((m[-1] - rho * x)/sqrt(1 - : non-finite function value All the pieces work outside of the function, and the integrand is finite as far as I can see. [Yes, this is a recursive function for multivariate cumulative normal. It seems to match (so far for k=2 without recursion) the existing R functions from packages mvtnorm and mnormt. It is from D. Cassimon, et al. Closed-form valuation of American call options on stocks paying multiple dividends. Finance Research Letters 4 (2007) 33-48.] Thank you to anyone who can shed some light on this. David L. Reiner, PhD Rho Trading Securities, LLC __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Recursive Function
It is possible to place two functions in a recursive function Main results so as to simultaneously? [[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.