Skip to content

Instantly share code, notes, and snippets.

@pietromarchesi
Created June 6, 2017 10:36
Show Gist options
  • Save pietromarchesi/4df83a359be43bfa1da69891ddb5c4c2 to your computer and use it in GitHub Desktop.
Save pietromarchesi/4df83a359be43bfa1da69891ddb5c4c2 to your computer and use it in GitHub Desktop.
library(psych)
"
Analyze the difference between change and initial value.
Source:
Tu, Y. K. (2016). Testing the relation between percentage change
and baseline value. Scientific reports, 6.
"
YuKangTu <- function(x, y){
# ratio of the coefficients of variation
cv_ratio = (sd(x) / mean(x)) / (sd(y) / mean(y))
# observed correlation
rho = cor(x, (y-x)/x)
# correlation under the null hypothesis
rho_H0 = cor(x, (y - x) / x) + sqrt((1 - cor(x, y)/2))
# z-test
z = (fisherz(rho) - fisherz(rho_H0)) / sqrt(1 / (length(x)-3))
p_val = pnorm(z) / 2
return(c(cv_ratio, z, p_val))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment