Created
March 28, 2016 17:00
-
-
Save jebyrnes/f0a16e3274aba35f376f to your computer and use it in GitHub Desktop.
A simulation to look at how distance based correlation can work with D-sep tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############# | |
#' @title Fisher's C and Distance Correlation | |
#' | |
#' @author Jarrett Byrnes | |
#' | |
#' @description A simulation to look at | |
#' how distance based correlation can work with | |
#' D-sep tests | |
#' | |
############# | |
#for data manipulation | |
library(dplyr) | |
#install.packages("energy") #for dcor | |
library(energy) | |
#sample size | |
n <- 100 | |
#Make a data frame with two nonlinear relationships | |
test_data_frame <- data.frame(a = runif(n,20,40)) %>% | |
mutate(b = rnorm(n, 2*a, 5), | |
c = rgamma(n, rate = 10/exp(0.2*a), shape=10), | |
d = rgamma(n, rate=1/exp(0.01*b-0.005*c), shape=1)) | |
#visualize | |
pairs(test_data_frame) | |
#The model | |
mod1 <- lm(b ~ a, data=test_data_frame) | |
mod2 <- glm(c ~ a, family=Gamma(link="log"), data=test_data_frame) | |
mod3 <- glm(d ~ b + c, family=Gamma(link="log"), data=test_data_frame) | |
#look at results | |
summary(mod1) | |
summary(mod2) | |
summary(mod3) | |
#classic basis set test | |
test1_classic <- lm(b ~ a + c, data=test_data_frame) | |
test1a_classic <- glm(c ~ a + b, family=Gamma(link="log"), data=test_data_frame) | |
test2_classic <- glm(d ~ b + c + a, family=Gamma(link="log"), data=test_data_frame) | |
#testing the basis set with distance based covariance | |
test1 <- dcov.test(residuals(mod1),test_data_frame$c) | |
test1a <- dcov.test(residuals(mod2), test_data_frame$b) | |
test2 <- dcov.test(residuals(mod3), test_data_frame$a) | |
#compare at the results | |
coef(summary(test1_classic)) | |
coef(summary(test1a_classic)) | |
test1 | |
test1a | |
########################## | |
#Fisher's C | |
fishers_c <- -2*(log(test1$p.value) + log(test2$p.value)) | |
fishers_c | |
#The test | |
pchisq(fishers_c, 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment