[R] generating symmetric matrices
Greetings, I have a seemingly simple task which I have not been able to solve today. I want to construct a symmetric matrix of arbtriray size w/o using loops. The following I thought would do it: p - 6 Rmat - diag(p) dat.cor - rnorm(p*(p-1)/2) Rmat[outer(1:p, 1:p, )] - Rmat[outer(1:p, 1:p, )] - dat.cor However, the problem is that the matrix is filled by column and so the resulting matrix is not symmetric. I'd be grateful for any adive and/or solutions. Gregory - [[alternative HTML version deleted]] __ 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.
[R] Constructing correlation matrices
Greetings, I have a seemingly simple task which I have not been able to solve today and I checked all of the help archives on this and have been unable to find anything useful. I want to construct a symmetric matrix of arbtriray size w/o using loops. The following I thought would do it: p - 6 Rmat - diag(p) dat.cor - rnorm(p*(p-1)/2) Rmat[outer(1:p, 1:p, )] - Rmat[outer(1:p, 1:p, )] - dat.cor However, the problem is that the matrix is filled by column and so the resulting matrix is not symmetric. I'd be grateful for any adive and/or solutions. Gregory - [[alternative HTML version deleted]] __ 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] generating symmetric matrices
Douglas Bates [EMAIL PROTECTED] wrote: On 7/27/07, Gregory Gentlemen wrote: Greetings, I have a seemingly simple task which I have not been able to solve today. I want to construct a symmetric matrix of arbtriray size w/o using loops. The following I thought would do it: p - 6 Rmat - diag(p) dat.cor - rnorm(p*(p-1)/2) Rmat[outer(1:p, 1:p, )] - Rmat[outer(1:p, 1:p, )] - dat.cor However, the problem is that the matrix is filled by column and so the resulting matrix is not symmetric. Could you provide more detail on the properties of the symmetric matrices that you would like to generate? It seems that you are trying to generate correlation matrices. Is that the case? Do you wish the matrices to be a random sample from a specific distribution. If so, what distribution? Yes, my goal is to generate correlation matrices whose entries have been sampled independently from a normal with a specified mean and variance. Thanks for the help. Greg - [[alternative HTML version deleted]] __ 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.
[R] getting the smoother matrix from smooth.spline
Can anyone tell me the trick for obtaining the smoother matrix from smooth.spline when there are non-unique values for x. I have the following code but, of course, it only works when all values of x are unique. ## get the smoother matrix (x having unique values smooth.matrix = function(x, df){ n = length(x); A = matrix(0, n, n); for(i in 1:n){ y = rep(0, n); y[i]=1; yi = smooth.spline(x, y, df=df)$y; A[,i]= yi; } (A+t(A))/2; } Thanks for any assistance, Gregory - - Get a sneak peak at messages with a handy reading pane. [[alternative HTML version deleted]] __ 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
[R] multiple operations on indexed columns
Hi guys, Im struggling with this: I have a dataset in which I have 9 measures and for each measure id like to compute the kendall tau correlation using cor.test with each of 20 covariates. Is there any systematic way to do this without writing a for loop? Thanks in advance, Gregory Gentlemen - [[alternative HTML version deleted]] __ 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
[R] factorizing many columns of a dataframe
Hi guys, I have a large number of columns of a dataframe that I want to apply a common factorization to; the columns are all numeric and the factorization collapses some of these values into common groups (labels). How can I do this systematically? Is there an analog to sapply that can pass columns as vectors to FUN? Thanks in advance, Gregory Gentlemen - [[alternative HTML version deleted]] __ 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
[R] using integrate with optimize nested in the integration
Hi guys im having a problem getting R to numerically integrate for some function, say f(bhat)*optimize(G(bhat)), over bhat. Where id like to integrate this over some finite range, so that here as we integrate over bhat optimize would return a different optimum. For instance consider this simple example for which I cannot get R to return the desired result: f - function(bhat) exp(bhat) g - function(bhat) optimize(f,c(0,15),maximum=TRUE)$maximum*bhat integrate(g,lower=0,upper=5) which returns: 187.499393759 with absolute error 2.1e-12 However this is an approximation of : 15*(5^2/2 - 0)=187.5, not what I intended on getting. Its not identifying that f is a function of bhat ... any advice or ways I can get integrate to not treat this as a constant? Any help is appreciated. Gregoy Gentlemen __ [[alternative HTML version deleted]] __ 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
Re: [R] using integrate with optimize nested in the integration
Thanks for the prompt reply. Your right, that was a weak example. Consider this one though: f - function(n,x) (x-2.5)^2*n g - function(y) optimize(f,c(0,15), maximum=TRUE,x=y)$maximum*y then if you try: integrate(g,lower=0,upper=5) it produces: Error in optimize(f, c(0, 15), maximum = TRUE, x = y) : invalid function value in 'optimize' Any ideas? My problem is a similar more complex function in which optimizaiton depends on the value of the integrator. Huntsinger, Reid [EMAIL PROTECTED] wrote: In your example, f is a function, but optimize(f,c(0,15),maximum=TRUE)$maximum is just a number (the point at which f reaches its maximum value). I'm not sure what you want, but if you had say f - function(x,y) x^3 + yx + 1 and defined g - function(y) optimize(f,c(0,5),maximum=TRUE,y)$maximum then g(t) is the x-value at which the function f(x,t) (over x in (0,5)) reaches its maximum for this fixed t. That could then be integrated. Reid Huntsinger -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Gregory Gentlemen Sent: Thursday, July 28, 2005 3:58 PM To: r-help@stat.math.ethz.ch Subject: [R] using integrate with optimize nested in the integration Hi guys im having a problem getting R to numerically integrate for some function, say f(bhat)*optimize(G(bhat)), over bhat. Where id like to integrate this over some finite range, so that here as we integrate over bhat optimize would return a different optimum. For instance consider this simple example for which I cannot get R to return the desired result: f - function(bhat) exp(bhat) g - function(bhat) optimize(f,c(0,15),maximum=TRUE)$maximum*bhat integrate(g,lower=0,upper=5) which returns: 187.499393759 with absolute error 2.1e-12 However this is an approximation of : 15*(5^2/2 - 0)=187.5, not what I intended on getting. Its not identifying that f is a function of bhat ... any advice or ways I can get integrate to not treat this as a constant? Any help is appreciated. Gregoy Gentlemen __ [[alternative HTML version deleted]] __ 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 -- -- __ [[alternative HTML version deleted]] __ 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
[R] Question about creating unique factor labels with the factor function
Hi guys, I ran into a problem of not being able to create unique labels when creating a factor. Consider an example below: hb - factor(c(1,1,1,2,2,2,3,3,3), levels=c(1,2,3),labels=c(1,1,2)) hb [1] 1 1 1 1 1 1 2 2 2 Levels: 1 1 2 unique(hb) [1] 1 1 2 Levels: 1 1 2 How come there are three unique levels, I thought this would only create one unique level? unique(as.ordered(hb)) [1] 1 2 Levels: 1 1 2 Is as.ordered the only solution? Thanks in advance, Greg __ [[alternative HTML version deleted]] __ 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
Re: [R] optimization problem in R ... can this be done?
Spencer: Thank you for the helpful suggestions. I have another question following some code I wrote. The function below gives a crude approximation for the x of interest (that value of x such that g(x,n) is less than 0 for all n). # // btilda optimize g(n,x) for some fixed x, and then approximately finds that g(n,x) #such that abs(g(n*,x)=0 // btilda - function(range,len) { # range: over which to look for x bb - seq(range[1],range[2],length=len) OBJ - sapply(bb,function(x) {fixed - c(x,100,692,50,1600,v1,217227); return(optimize(g,c(1,1000),maximum=TRUE,tol=0.001,x=fixed)$objective)}) tt - data.frame(b=bb,obj=OBJ) tt$absobj - abs(tt$obj) d - tt[order(tt$absobj),][1:3,] return(as.vector(d)) } For instance a run of btilda(c(20.55806,20.55816),1) returns: b objabsobj 5834 20.55812-0.00049428480.0004942848 5833 20.55812 0.00117154330.0011715433 5835 20.55812-0.00216011400.0021601140 My question is how to improve the precision of b (which is x) here. It seems that seq(20.55806,20.55816,length=1 ) is only precise to 5 or so digits, and thus, is equivalent for numerous succesive values. How can I get around this? Spencer Graves [EMAIL PROTECTED] wrote: Part of the R culture is a statement by Simon Blomberg immortalized in library(fortunes) as, This is R. There is no if. Only how. I can't see now how I would automate a complete solution to your problem in general. However, given a specific g(x, n), I would start by writing a function to use expand.grid and contour to make a contour plot of g(x, n) over specified ranges for x = seq(0, x.max, length=npts) and n = seq(0, n.max, npts) for a specified number of points npts. Then I'd play with x.max, n.max, and npts until I got what I wanted. With the right choices for x.max, n.max, and npts, the solution will be obvious from the plot. In some cases, nothing more will be required. If I wanted more than that, I would need to exploit further some specifics of the problem. For that, permit me to restate some of what I think I understood of your specific problem: (1) For fixed n, g(x, n) is monotonically decreasing in x0. (2) For fixed x, g(x, n) has only two local maxima, one at n=0 (or n=eps0, esp arbitrarily small) and the other at n2(x), say, with a local minimum in between at n1(x), say. With this, I would write functions to find n1(x) and n2(x) given x. I might not even need n1(x) if I could figure out how to obtain n2(x) without it. Then I'd make a plot with two lines (using plot and lines) of g(x, 0) and g(x, n2(x)) vs. x. By the time I'd done all that, if I still needed more, I'd probably have ideas about what else to do. hope this helps. spencer graves Gregory Gentlemen wrote: Im trying to ascertain whether or not the facilities of R are sufficient for solving an optimization problem I've come accross. Because of my limited experience with R, I would greatly appreciate some feedback from more frequent users. The problem can be delineated as such: A utility function, we shall call g is a function of x, n ... g(x,n). g has the properties: n 0, x lies on the real line. g may take values along the real line. g is such that g(x,n)=g(-x,n). g is a decreasing function of x for any n; for fixed x, g(x,n) is smooth and intially decreases upon reaching an inflection point, thereafter increasing until it reaches a maxima and then declinces (neither concave nor convex). My optimization problem is to find the largest positive x such that g(x,n) is less than zero for all n. In fact, because of the symmetry of g around x, we need only consider x 0. Such an x does exists in this problem, and of course g obtains a maximum value of 0 at some n for this value of x. my issue is writing some code to systematically obtain this value. Is R capable of handling such a problem? (i.e. through some sort of optimization fucntion, or some sort of grid search with the relevant constraints) Any suggestions would be appreciated. Gregory Gentlemen [EMAIL PROTECTED] The following is a sketch of an optimization problem I need to solve. __ [[alternative HTML version deleted]] __ 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 -- Spencer Graves, PhD Senior Development Engineer PDF Solutions, Inc. 333 West San Carlos Street Suite 700 San Jose, CA 95110, USA [EMAIL PROTECTED] www.pdf.com Tel: 408-938-4420 Fax: 408-280-7915 __ [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting
[R] optimization problem in R ... can this be done?
Im trying to ascertain whether or not the facilities of R are sufficient for solving an optimization problem I've come accross. Because of my limited experience with R, I would greatly appreciate some feedback from more frequent users. The problem can be delineated as such: A utility function, we shall call g is a function of x, n ... g(x,n). g has the properties: n 0, x lies on the real line. g may take values along the real line. g is such that g(x,n)=g(-x,n). g is a decreasing function of x for any n; for fixed x, g(x,n) is smooth and intially decreases upon reaching an inflection point, thereafter increasing until it reaches a maxima and then declinces (neither concave nor convex). My optimization problem is to find the largest positive x such that g(x,n) is less than zero for all n. In fact, because of the symmetry of g around x, we need only consider x 0. Such an x does exists in this problem, and of course g obtains a maximum value of 0 at some n for this value of x. my issue is writing some code to systematically obtain this value. Is R capable of handling such a problem? (i.e. through some sort of optimization fucntion, or some sort of grid search with the relevant constraints) Any suggestions would be appreciated. Gregory Gentlemen [EMAIL PROTECTED] The following is a sketch of an optimization problem I need to solve. __ [[alternative HTML version deleted]] __ 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