

You can multiply the vector by β > 0 to obtain a vector whose norm is β. The algorithm in this article produces a centered vector that has unit length. Given a vector, x, and a correlation coefficient, ρ, find a vector, y, such that corr( x, y) = ρ.
#How do you create avector without a loop in r how to
This article shows how to create a vector that has a specified correlation with a given vector. The random guess does not need to be from the normal distribution. The Y variable is a random vector that was rescaled to have mean 100 and standard deviation 23, as above. The X variable is the same for each plot. For example, the following graph shows a panel of scatter plots for ρ = -0.75, -0.25, 0.25, and 0.75. Or you can repeat the process for a range of ρ values to visualize data that have a range of correlations. If you make a second call to the RANDFUN function, you can get another random vector that has the same properties. The correlation in the scatter plot is 0.678. The graph shows a scatter plot between x and the random vector, v. V = mean + std *y title "Correlation = 0.678" The following statements call the function for a vector, x, and requests a unit vector that has correlation ρ = 0.543 with x: The function detects this case and flips y, if necessary. However, if the guess is in the opposite direction from x, the algorithm will compute a vector, y, that has the opposite correlation. The purpose of the function is to project the guess onto the green cone in the figure. Flip the sign of y, if necessary */ if sign (y` *u) ^= sign(rho) then V2 = sqrt ( 1 - rho **2) * UnitVec(wPerp) Have lengths rho and sqrt(1-rho^2), respectively */ Where y (the hypotenuse) has unit length and the legs The requirement that cos(theta)=rho results in a right triangle Project z onto the span(u) and the orthog complement of span(u) */ The initial guess can be almostĪny vector that is not in span(x), orthog to span(x), and not in span(1) */ * Find a vector, y, such that corr(x,y) = rho. * create a unit vector in the direction of a column vector */ * center a column vector by subtracting its mean */ * Given a vector, x, and a correlation, rho, find y such that corr(x,y) = rho */ proc iml Finally, the function uses trigonometry and the fact that cos(θ) = ρ to return a unit vector that has the required correlation with x. The vector z is projected onto the span of u. The function centers and scales the vectors into the vectors u and z. The function CorrVec1 takes three arguments: the vector x, a correlation coefficient ρ, and an initial guess. The following program defines two helper functions (Center and UnitVec) and uses them to implement the projection algorithm.

It is straightforward to implement this projection in a matrix-vector language such as SAS/IML. The plane marked \(\mathbf^\perp\rVert\). This is shown graphically in the figure below.

Geometrically, you can choose any vector on a positive cone in the same direction as u, where the cone has angle θ and cos(θ)=ρ. Given a centered vector, u, there are infinitely-many vectors that have correlation ρ with u. You can scale and translate the vector to obtain other solutions. For simplicity, the SAS program in this article returns a centered unit vector. For any real number, α, and for any β > 0, the vector α + β y has the same correlation with x as y does.
