0

I am a novice to R and I can’t figure out a solution to my problem.

Imagine that I have created three normally distributed variables, x, y and z (n=100 in each with a mean of 0 and a sd of 1). The variables are independent from each other (r~0). Then I create two new variables xz and yz (i.e the interaction between x, z and y, z.).

Now assume that I want to keep x and y fixed and randomly sample from z (or create a new z) before the interactions are computed and, after every new random sample from z, compute the interactions and calculate the correlation coefficient between them (for the sake of argument lets say I want to do this 100 times) and print the 100 correlations to a new datafile for further analysis. How to I achieve this?

4

1 回答 1

0

不太确定这是您要的。给它一个检查。

x<-rnorm(100)
y<-rnorm(100)
z<-rnorm(100)
xz<-x*z
yz<-y*z

co<-data.frame(nrow=100, ncol=2)
for (i in 1:100){
  z[i]<-rnorm(1)
  xz[i]<-z[i]*x[i]
  yz[i]<-z[i]*y[i]
 co[i,]<-c(cor(z,x), cor(z,y))
}
于 2013-10-20T13:23:18.490 回答