Skip to content
Prev 208060 / 398502 Next

Vectors with equal sd but different slope

Here is one approach:

library(MASS)

cor1 <- diag(2)
cor1[1,2] <- cor1[2,1] <- 0.5

cor2 <- cor1
cor2[1,2] <- cor2[2,1] <- 0.9

sd <- c(5,20)
mns <- c(50, 100)

cov1 <- diag(sd) %*% cor1 %*% diag(sd)
cov2 <- diag(sd) %*% cor2 %*% diag(sd)

one <- mvrnorm(100, mns, cov1, empirical=TRUE)
two <- mvrnorm(100, mns, cov2, empirical=TRUE)

cor(one)
cor(two)
apply(one,2,mean)
apply(two,2,mean)
apply(one,2,sd)
apply(two,2,sd)

plot(one, col='blue')
points(two, col='green')
abline( lm(one[,2] ~ one[,1]), col='blue' )
abline( lm(two[,2] ~ two[,1]), col='green' )


You can modify values to fit your situation better.

Hope this helps,