[R] Use correlation matrix to get values for a new data frame
Greg Snow
538280 at gmail.com
Wed Oct 30 01:28:27 CET 2013
I am not sure that I fully understand your question, but the mvrnorm
function in the MASS package may do what you want. It will generate
multivariate normal data with a specified correlation(covariance). If
that is not what you want then try to explain a bit more about what
you want your final result to be.
On Sun, Oct 27, 2013 at 2:46 PM, tobias schlager <tobebryant at me.com> wrote:
> Dear all,
>
> i am a bit stuck with a problem right now. Specifically, I have a correlation matrix, and a dataframe on which I want to project the values. However, I am not sure how I can do this.
> Here the calculations I made, I wanted to get a matrix that shows the values for each of the combinations of the ten variables in „d", so the new data is „newdat“. How might this be possible?
>
> Thanks a lot,
> Tobi
>
>
> x1 <- c(1,0,0,1,0,1,0,0,0,0)
> x2 <- c(0,1,0,0,0,1,0,0,0,1)
> x3 <- c(0,0,1,0,1,0,1,0,0,0)
> x4 <- c(1,0,0,1,0,0,0,0,1,0)
> x5 <- c(0,0,1,0,1,0,0,1,0,1)
> x6 <- c(1,1,0,0,0,1,0,0,0,0)
> x7 <- c(0,0,1,0,0,0,1,1,0,0)
> x8 <- c(0,0,0,0,1,0,1,1,0,0)
> x9 <- c(0,0,0,1,0,0,0,0,1,0)
> x10 <- c(0,1,0,0,1,0,0,0,0,1)
>
> x1c <- ifelse(x1==1, runif(4, -1, 1), 0);
> x2c <- ifelse(x2==1, runif(4, -1, 1), 0);
> x3c <- ifelse(x3==1, runif(4, -1, 1), 0);
> x4c <- ifelse(x4==1, runif(4, -1, 1), 0);
> x5c <- ifelse(x5==1, runif(4, -1, 1), 0);
> x6c <- ifelse(x6==1, runif(4, -1, 1), 0);
> x7c <- ifelse(x7==1, runif(4, -1, 1), 0);
> x8c <- ifelse(x8==1, runif(4, -1, 1), 0);
> x9c <- ifelse(x9==1, runif(4, -1, 1), 0);
> x10c <- ifelse(x10==1, runif(4, -1, 1), 0);
>
> c <- cbind(x1c,x2c,x3c,x4c,x5c,x6c,x7c,x8c,x9c,x10c); c
>
> c <- data.frame(c)
> c[1,1] <- 1; c[2,2] <- 1; c[3,3] <- 1; c[4,4] <- 1; c[5,5] <- 1; c[6,6] <- 1; c[7,7] <- 1; c[8,8] <- 1; c[9,9] <- 1; c[10,10] <- 1;
>
> # get symmetry
> c[2,1] <-c[1,2]; c[3,1] <-c[1,3]; c[4,1] <-c[1,4]; c[5,1] <-c[1,5]; c[6,1] <-c[1,6]; c[7,1] <-c[1,7]; c[8,1] <-c[1,8]; c[9,1] <-c[1,9]; c[10,1] <-c[1,10];
> c[3,2] <-c[2,3]; c[4,2] <-c[2,4]; c[5,2] <-c[2,5]; c[6,2] <-c[2,6]; c[7,2] <-c[2,7]; c[8,2] <-c[2,8]; c[9,2] <-c[2,9]; c[10,2] <-c[2,10];
> c[4,3] <-c[3,4]; c[5,3] <-c[3,5]; c[6,3] <-c[3,6]; c[7,3] <-c[3,7]; c[8,3] <-c[3,8]; c[9,3] <-c[3,9]; c[10,3] <-c[3,10];
> c[5,4] <-c[4,5]; c[6,4] <-c[4,6]; c[7,4] <-c[4,7]; c[8,4] <-c[4,8]; c[9,4] <-c[4,9]; c[10,4] <-c[4,10];
> c[6,5] <-c[5,6]; c[7,5] <-c[5,7]; c[8,5] <-c[5,8]; c[9,5] <-c[5,9]; c[10,5] <-c[5,10];
> c[7,6] <-c[6,7]; c[8,6] <-c[6,8]; c[9,6] <-c[6,9]; c[10,6] <-c[6,10];
> c[8,7] <-c[7,8]; c[9,7] <-c[7,9]; c[10,7] <-c[7,10];
> c[9,8] <-c[8,9]; c[10,8] <-c[8,10];
> c[10,9] <-c[9,10];
>
> d <- c (c[,1], c[,2], c[,3],c[,4],c[,5],c[,6],c[,7],c[,8],c[,9],c[,10])
>
>
> newdat <- expand.grid(x1c=c(1,0),x2c=c(1,0),x3c=c(1,0),x4c=c(1,0),x5c=c(1,0),x6c=c(1,0),x7c=c(1,0),x8c=c(1,0),x9c=c(1,0),x10c=c(1,0))
>
> ____________________________
> Dr. Tobias Schlager
> Projektleiter
>
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at 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.
>
--
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com
More information about the R-help
mailing list