Skip to content

guhjy/r-causal

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

r-causal

R Wrapper for Tetrad Library

R Library Requirement

R >= 3.2.0, stringr, rJava, graph, [RBGL] (http://bioconductor.org/packages/release/bioc/html/RBGL.html), [Rgraphviz] (http://bioconductor.org/packages/release/bioc/html/Rgraphviz.html)

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
fges <- fges(df = charity, penaltydiscount = 2, maxDegree = -1,  
faithfulnessAssumed = TRUE, numOfThreads = 2, verbose = TRUE)    

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

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

Discrete Dataset

library(rcausal)
data("audiology")    #Load the charity dataset
#Compute FGES search
fges.discrete <- fges.discrete(df=audiology,structurePrior=1.0,samplePrior=1.0, 
maxDegree = -1, faithfulnessAssumed = TRUE, numOfThreads = 2,verbose = TRUE)
fges.discrete$parameters #Show the FGES Discrete's parameters
fges.discrete$datasets #Show the dataset
fges.discrete$nodes #Show the result's nodes
fges.discrete$edges #Show the result's edges
library(Rgraphviz)
plot(fges.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)
fges <- fges(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')
fges.discrete <- fges.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(fges.discrete$graphNEL)
plot(igraph)

Useful rJava Trouble-shooting Installation in Mac OS X Links

  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/

About

R Wrapper for Tetrad Library

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 99.5%
  • Other 0.5%