r-causal
R Wrapper for Tetrad Library
Note 2023-03-06: This version of RCausal uses an older version of Tetrad from at least 5 years ago. However, we have updated our Python integration to a much better version--see https://github.com/cmu-phil/py-tetrad. Updating our R integration is one of the next projects we will take up.
News 2023-04-05: We have put forward a proposal to replace the r-causal functionality using the py-tetrad functionality, here: https://github.com/cmu-phil/py-tetrad/tree/main/pytetrad/R. The installation procedure for this is still somewhat complicated, and we will try to simplify it. If you try it and and have difficulties, please let us know. Once you have it installed, it is very easy and intuitive to use.
By the way, rcausal has not been maintained for some time now, as the tireless maintainer has since moved on to different work :-)... but going back through some of the issues posted for r-causal gives some hints as to additional functionality that pytetrad/R should have. We'll try to get caught up.
R Library Requirement
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")
- Install r-causal from github:
library(devtools)
install_github("bd2kccd/r-causal")
Example
Continuous Dataset
library(rcausal)
data("charity") #Load the charity dataset
tetradrunner.getAlgorithmDescription(algoId = 'fges')
tetradrunner.getAlgorithmParameters(algoId = 'fges',scoreId = 'fisher-z')
#Compute FGES search
tetradrunner <- tetradrunner(algoId = 'fges',df = charity,scoreId = 'fisher-z',
dataType = 'continuous',alpha=0.1,faithfulnessAssumed=TRUE,maxDegree=-1,verbose=TRUE)
tetradrunner$nodes #Show the result's nodes
tetradrunner$edges #Show the result's edges
Discrete Dataset
library(rcausal)
data("audiology") #Load the charity dataset
tetradrunner.getAlgorithmParameters(algoId = 'fges',scoreId = 'bdeu')
#Compute FGES search
tetradrunner <- tetradrunner(algoId = 'fges',df = audiology,scoreId = 'bdeu',dataType = 'discrete',
alpha=0.1,faithfulnessAssumed=TRUE,maxDegree=-1,verbose=TRUE)
tetradrunner$nodes #Show the result's nodes
tetradrunner$edges #Show the result's edges
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)
Plot a DOT graph
library(DOT)
graph_dot <- tetradrunner.tetradGraphToDot(tetradrunner$graph)
dot(graph_dot)