0

I am working on simulation of data, and this is just the first step of what I am trying to do. I am first simulating data based on a correlation matrix (as this is what I want). I used the R code found on a blog :

# The variables will each have a mean of 0 and a variance of 1. After you have the     data, you can 
# apply a linear transformation to get any mean and variance that you want.

rmat <- matrix(c(1.0, .50, -.50, .50, 1.0, .30, -.50, .30, 1.0), nrow = 3, byrow = T)
mu <- c(0,0,0)
library(MASS)
mat <- mvrnorm(100, Sigma = rmat, mu = mu, empirical = TRUE) 
# If empirical = FALSE, the correlations will be approx.

cat ("The intercorrelation matrix is = '\n'")
print(cor(mat))

I have two questions - (1) can I just use a covariance matrix here instead?

2) the author has written "apply a linear transformation to get any mean and variance that you want". I need the data to have a particular mean and S.D...it is probably a trivial exercise , but can someone help me how I can transform this data which has mean 0 and SD 1 to what is desired? So is there an easy way to scale mat variables to have the desired mean and S.D?

Thanks for the help!

kristen
  • 81
  • 5
  • 1
    Please do not expect readers to check another webpage in order to understand your question. As such, your question cannot be answered: describe your issues with the other solution and where you are stuck. – Xi'an Mar 17 '15 at 17:28
  • You should find the answer in the linked thread. Please read it. If you still have a question afterwards, come back here & edit your Q to state what you've learned & what you still need to understand. Then we can provide the information you need without simply duplicated material elsewhere that already didn't help you. – gung - Reinstate Monica Mar 17 '15 at 17:51
  • thanks. I have pasted the code. Thanks for linking the thread. My question is a very simple one compared to whats done there. Is there a quick way to linearly transform 'mat' variables to have the desired mean and variance in R? thanks – kristen Mar 17 '15 at 18:13
  • Simulating from the covariance matrix (& w/ given mean vector) won't make any real difference. The generated data won't exactly match what you want, but they will be closer. If you need them to match exactly, you'll still have to transform in the same way. – gung - Reinstate Monica Mar 17 '15 at 18:14
  • The method you would use is the same one listed there. There is no 'simpler' way than that. – gung - Reinstate Monica Mar 17 '15 at 18:15
  • Can I simply do a X1 = c(((X1 - mean(X1))/sd(X1))*66.5+40.9) where 66.5 and 40.9 are the new S.D and mean? ....also can I just use scale and sweep? – kristen Mar 18 '15 at 04:56
  • Also .. why are we doing this xScaled[i,] = scale(x) in the linked thread? Is it okay to ask here if I dont understand something? sorry to bother you . I am kinda stuck! – kristen Mar 18 '15 at 04:59

0 Answers0