Home > Blockchain >  Error in correlation with interaction in R
Error in correlation with interaction in R

Time:01-07

this is my dataset:

Variable_1 <- c(3, 5, 6, 9, 12)
Variable_2 <- c(5, 5, 7, 6, 10)
Variable_3 <- c(0, 1, 3, 3, 5)
Variable_4 <- c(6, 7, 5, 10, 10)

X <- data.frame(Variable_1, Variable_2, Variable_3, Variable_4)

I want to calculate the pairwise correlation of these variables, with interaction with a new variable (x). However, when I do this with my codes, the following error appears:

with(data.frame(x = rnorm(5)),
cor(model.matrix(~ . * x, X)))


> 1: In terms.formula(object, data = data) :
      'varlist' has changed (from nvar=4) to new 5 after EncodeVars() -- should no longer happen!
>    2: In terms.formula(formula, data = data) :
      'varlist' has changed (from nvar=4) to new 5 after EncodeVars() -- should no longer happen!
>    3: In cor(model.matrix(~. * x, X)) : standard deviation is zero

Why does it happen? And how can I fix this?

CodePudding user response:

If you add x to the X data instead of using with in a separate data frame, that solves the errors 1 and 2. Warning 3 about the SD being 0 is because the model formula you have includes an intercept term, we can use 0 or - 1 in the formula to not include an intercept.

cbind(X, x = rnorm(5)) |> 
  model.matrix(object = ~ . * x - 1) |> 
  cor()
#               Variable_1 Variable_2  Variable_3  Variable_4          x Variable_1:x
# Variable_1    1.00000000  0.8524929  0.94311913  0.82929841 -0.6392038   0.03777884
# Variable_2    0.85249292  1.0000000  0.91532579  0.48178766 -0.6351861  -0.31367519
# Variable_3    0.94311913  0.9153258  1.00000000  0.60163630 -0.6868033  -0.04758412
# Variable_4    0.82929841  0.4817877  0.60163630  1.00000000 -0.3359712   0.29232862
# x            -0.63920377 -0.6351861 -0.68680333 -0.33597124  1.0000000   0.56683001
# Variable_1:x  0.03777884 -0.3136752 -0.04758412  0.29232862  0.5668300   1.00000000
# Variable_2:x -0.53418365 -0.5306942 -0.54239657 -0.30656762  0.9766116   0.66241818
# Variable_3:x  0.45817294  0.1534868  0.51245819  0.35902826 -0.1084688   0.70426363
# Variable_4:x -0.33930292 -0.5650112 -0.47714884  0.06483919  0.8915007   0.83829795
#              Variable_2:x Variable_3:x Variable_4:x
# Variable_1    -0.53418365   0.45817294  -0.33930292
# Variable_2    -0.53069420   0.15348678  -0.56501121
# Variable_3    -0.54239657   0.51245819  -0.47714884
# Variable_4    -0.30656762   0.35902826   0.06483919
# x              0.97661158  -0.10846875   0.89150066
# Variable_1:x   0.66241818   0.70426363   0.83829795
# Variable_2:x   1.00000000   0.06749953   0.89434134
# Variable_3:x   0.06749953   1.00000000   0.20684320
# Variable_4:x   0.89434134   0.20684320   1.00000000
  • Related