Re: [R] R-help Digest, Vol 46, Issue 27
On Wednesday 27 December 2006 06:00, [EMAIL PROTECTED] wrote: jingjiangyan I agree, you can use 'assign'. To be more explicit, you could use the following function. jingjiangyan - function(formula, data) { m - match.call() %,% - function(x,y)paste(x,y,sep=) d.nm - as.character(m$data) y.nm - as.character(formula[[2]]) x.nm - as.character(formula[[3]]) for(i in levels(data[[x.nm]])){ var.name - d.nm %,% . %,% i var.val - data[[y.nm]][data[[x.nm]]==i] cmd - var.name %,% - %,% var.val eval(cmd) assign(var.name, var.val, globalenv()) } } Next, assuming the data.frame listed in the previous posting, 'df' exists in your workspace, the call jingjiangyan(bb ~ aa, data=df) would produce the desired results. Cheers, Grant Izmirlian -- Հրանդ Իզմիրլյան __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/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] generate random numbers that sum up to 1
So, Alberto, you didn't see my post? If Y has d independent components that are gamma distributed with common rate and shapes A_1, A_2, ..., A_d, then X, given by the components of Y divided by their sum has distribution Dirichlet(A_1, A_2, ..., A_d). If you want Uniform on the d-simplex, then use A_1 = A_2 = ... = A_d = 1 (just as Duncan said) original message: Duncan Murdoch's definition is _the_ only one that I know. X is Uniform on A means E phi(X) = \int_A phi(x) dx / \int_A dx, so that the probability density is equal to 1/ \int_A dx everwhere on the set A. By the way, another way to simulate X ~ Dirichlet(A1, A2, ..., Ad) is to generate d independent gamma variables having equal rate parameter (doesn't matter, so why not 1) and shape parameters A1, A2, ..., Ad Then the vector of components divided by their sum is the desired Dirichlet: n - 10 d - 3 # for three numbers that add to one ( the unit simplex in R^3) A - rep(1, 3) # for uniform X - matrix(0, n, d) for (k in 1:3) X[,k] - rgamma(n, shape=A[k], rate=1) S - X %*% rep(1, d) Y - X/S Present example will simulate n independant 3 vectors, each having non-negative components summing to 1, and having a distribution assigning equal mass to every possible value. Changing d and the components of A will provide an arbitrary Dirichlet on the unit simplex in R^d Grant Izmirlian NCI Duncan Murdoch wrote Another definition of uniform is to have equal density for all possible vectors; the Dirichlet distribution with parameters (1,1,1) would give you that. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/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] generate random numbers that sum up to 1
Duncan Murdoch's definition is _the_ only one that I know. X is Uniform on A means E phi(X) = \int_A phi(x) dx / \int_A dx, so that the probability density is equal to 1/ \int_A dx everwhere on the set A. By the way, another way to simulate X ~ Dirichlet(A1, A2, ..., Ad) is to generate d independent gamma variables having equal rate parameter (doesn't matter, so why not 1) and shape parameters A1, A2, ..., Ad Then the vector of components divided by their sum is the desired Dirichlet: n - 10 d - 3 # for three numbers that add to one ( the unit simplex in R^3) A - rep(1, 3) # for uniform X - matrix(0, n, d) for (k in 1:3) X[,k] - rgamma(n, shape=A[k], rate=1) S - X %*% rep(1, d) Y - X/S Present example will simulate n independant 3 vectors, each having non-negative components summing to 1, and having a distribution assigning equal mass to every possible value. Changing d and the components of A will provide an arbitrary Dirichlet on the unit simplex in R^d Grant Izmirlian NCI Duncan Murdoch wrote Another definition of uniform is to have equal density for all possible vectors; the Dirichlet distribution with parameters (1,1,1) would give you that. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.