-
Notifications
You must be signed in to change notification settings - Fork 12
/
sample_r_code10.R
54 lines (44 loc) · 1.87 KB
/
sample_r_code10.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
## This file shows how to Tetrad searches for continuous data interactively
## in R using the TetradSearch class.
##
## Please make your own copy of this R file if you want to make sure your
## changes don't get overwritten by future `git pull's.
##
## For purposes of these example scripts, we will assume that in RStudio one
## has loaded the py-tetrad directory as the project, so that the project
## directory is the py-tetrad/pytetrad directory. For your own scripts, these
## paths can be adjusted.
if (!requireNamespace("here", quietly = TRUE)) {
install.packages("here")
}
library(here)
project_root <- here()
setwd(project_root)
library(reticulate)
data <- read.table("./resources/airfoil-self-noise.continuous.txt", header=TRUE)
## The read.table function will read decimal columns as real ('numeric')
## and integer columns as discrete. When passing data from R into Python,
## integer columns will still be interpreted as discrete, so we have to
## specify in the data frame for this data in columns 1-5 are to be interpreted
## as continuous (i.e., 'numeric'); some of them are integer columns.
i <- c(1, 6)
data[ , i] <- apply(data[ , i], 2, function(x) as.numeric(x))
## Make a TetradSearch object.
source_python("tools/TetradSearch.py")
ts <- TetradSearch(data)
## Set some knowledge--we know pressure should be the endogenous variable
## here, so why not help the search out? (It's interesting of course to
## see what searches can get this right without the help.)
ts$add_to_tier(1, "Frequency")
ts$add_to_tier(1, "Attack")
ts$add_to_tier(1, "Chord")
ts$add_to_tier(1, "Velocity")
ts$add_to_tier(1, "Displacement")
ts$add_to_tier(2, "Pressure")
## Run the search and return the graph in PCALG format
ts$use_sem_bic(penalty_discount=2)
ts$use_fisher_z(0.05)
ts$run_ica_lingam()
print(ts$get_bhat())
ts$run_ica_lingd(threshold_w = 0.0001, threshold_b = 1)
print(ts$get_stable_bhats())