简体   繁体   中英

How to calculate (in R) the range in the difference of ranges to check if 0 is within this range?

I am studying the relationship of one Variable Y with two possible explanatory variables X1 and X2 . All my variables are continuous. Furthermore, Y has a non-normal distribution, so I decided to evaluate separately the relationship of X1 and X2 with Y using the correlation coefficient of Spearman. I get a correlation coefficient called rho with a Confidence Interval CI (= a range) for either Y~X1 ( rho1 ) and Y~X2 ( rho2 ). I want to calculate the range of possible differences between rho1 and rho2 , and check if 0 is within this range.

As an example:

rho1 <- range(0.90,0.92)
rho2 <- range(0.91,0.93)

Range

0.00  0.03     # The difference between `rho1` and `rho2` is between 0 and 

Another example:

rho1 <- range(0.85,0.88)
rho2 <- range(0.89,0.91)

Range

0.01  0.06    

I want to calculate the range of possible differences between rho1 and rho2 to check, as I said, if 0 is within this range and then say that the strength of the relationship of rho1 and rho2 with Y is likely the shame. In this example, the range of possible differences is:

Do you know how to calculate the mentioned range?

I think I had misunderstood the question earlier. We need to check if any value in a vector is between other value in the another vector.

get_difference_values <- function(x, y) {
  if(any(dplyr::between(x, min(y), max(y))))
    c(0, max(max(x) - min(y), max(y) - min(x)))
  else
    range(abs(outer(x, y, `-`)))
}

rho1 <- range(0.90,0.92)
rho2 <- range(0.91,0.93)
get_difference_values(rho1, rho2)
#[1] 0.00 0.03

rho1 <- range(0.85,0.88)
rho2 <- range(0.89,0.91)
get_difference_values(rho1, rho2)  
#[1] 0.01 0.06 

Old answer

In the reproducible example I think what you need is seq and not range .

We can use outer to get difference between every element of rho1 , rho2 , get their absolute value and get the range .

rho1 <- seq(0.90,0.92, 0.01)
rho2 <- seq(0.91,0.93, 0.01)
range(abs(outer(rho1, rho2, `-`)))
#[1] 0.00 0.03


rho1 <- seq(0.85,0.88, 0.01)
rho2 <- seq(0.89,0.91, 0.01)

range(abs(outer(rho1, rho2, `-`)))
#[1] 0.01 0.06

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM