r-causal

R Wrapper for Tetrad Library

R Library Requirement

R >= 3.2.0, stringr, rJava, graph, RBGL, Rgraphviz

Docker

As an alternative to installing the library and getting rJava working with your installation (i.e., does not work well on mac) we have a Docker image

Installation

  • Install the R library requirements:
install.packages("stringr")
install.packages("rJava")
## try http:// if https:// URLs are not supported
source("https://bioconductor.org/biocLite.R") 
biocLite("graph")
biocLite("RBGL")
biocLite("Rgraphviz") # For plotting graph
  • Install r-causal from github:
library(devtools)
install_github("bd2kccd/r-causal")

Example

Continuous Dataset

library(rcausal)
data("charity")   #Load the charity dataset

#Compute FGES search
fgs <- fgs(df = charity, penaltydiscount = 2, maxDegree = -1,  
faithfulnessAssumed = TRUE, numOfThreads = 2, verbose = TRUE)    

fgs$parameters #Show the FGES's parameters
fgs$datasets #Show the dataset
fgs$nodes #Show the result's nodes
fgs$edges #Show the result's edges

library(Rgraphviz)
plot(fgs$graphNEL) #Plot the causal model

Discrete Dataset

library(rcausal)
data("audiology")    #Load the charity dataset
#Compute FGES search
fgs.discrete <- fgs.discrete(df=audiology,structurePrior=1.0,samplePrior=1.0, 
maxDegree = -1, faithfulnessAssumed = TRUE, numOfThreads = 2,verbose = TRUE)
fgs.discrete$parameters #Show the FGES Discrete's parameters
fgs.discrete$datasets #Show the dataset
fgs.discrete$nodes #Show the result's nodes
fgs.discrete$edges #Show the result's edges
library(Rgraphviz)
plot(fgs.discrete$graphNEL) #Plot the causal model

Prior Knowledge

Create PriorKnowledge Object

forbid <- list(c('TangibilityCondition','Impact')) # List of forbidden directed edges
require <- list(c('Sympathy','TangibilityCondition')) # List of required directed edges
forbiddenWithin <- c('TangibilityCondition','Imaginability')
class(forbiddenWithin) <- 'forbiddenWithin' # Make this tier forbidden within
temporal <- list(forbiddenWithin, c('Sympathy','AmountDonated'),c('Impact')) # List of temporal node tiers
prior <- priorKnowledge(forbiddirect = forbid, requiredirect = require, addtemporal = temporal)
fgs <- fgs(df = charity, penaltydiscount = 2, depth = -1, ignoreLinearDependence = TRUE, 
heuristicSpeedup = TRUE, numOfThreads = 2, verbose = TRUE, priorKnowledge = prior)

Load Knowledge File

# knowledge file: audiology.prior
# /knowledge
# forbiddirect
# class tymp
# class age_gt_60
# class notch_at_4k
# 
# requiredirect
# history_noise class
#
# addtemporal
# 0* bser late_wave_poor tymp notch_at_4k o_ar_c ar_c airBoneGap air bone o_ar_u airBoneGap
# 1 history_noise history_dizziness history_buzzing history_roaring history_recruitment history_fluctuating history_heredity history_nausea
# 2 class

prior <- priorKnowledgeFromFile('audiology.prior')
fgs.discrete <- fgs.discrete(df=audiology,structurePrior=1.0,samplePrior=1.0, 
depth = -1, heuristicSpeedup = TRUE, numOfThreads = 2,verbose = TRUE, priorKnowledge = prior)

Convert Rgraphviz to igraph one

library(igraph)
igraph <- igraph.from.graphNEL(fgs.discrete$graphNEL)
plot(igraph)
  1. http://stackoverflow.com/questions/26948777/how-can-i-make-rjava-use-the-newer-version-of-java-on-osx/32544358#32544358
  2. http://andrewgoldstone.com/blog/2015/02/03/rjava/