-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathsimple_example.R
106 lines (83 loc) · 2.25 KB
/
simple_example.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
## try using built-in jcache
options(java.parameters = "-Xmx1500m")
library(bartMachine)
n = 500
x = 1 : n; y = x + rnorm(n)
bart_machine = build_bart_machine(as.data.frame(x), y, serialize = TRUE)
save.image("test_bart_machine.RData")
q("no")
#close R, open R
R
options(java.parameters = "-Xmx1500m")
library(bartMachine)
load("test_bart_machine.RData")
x = 1 : n
predict(bart_machine, as.data.frame(x))
##how big is this stuff?
data_names = names(bart_machine)
sizes = matrix(NA, ncol = 1, nrow = length(data_names))
rownames(sizes) = data_names
for (i in 1 : length(data_names)){
sizes[i, ] = object.size(bart_machine[[data_names[i]]]) / 1e6
}
t(t(sizes[order(-sizes), ]))
q("no")
####ensure no more memory leak
R
options(java.parameters = "-Xmx1000m")
library(bartMachine)
x = 1 : 100; y = x + rnorm(100)
for (i in 1 : 10000){
bart_machine = build_bart_machine(as.data.frame(x), y)
}
q("no")
## If it helps, this may
#get some data
R
options(java.parameters = "-Xmx1000m")
library(bartMachine)
library(MASS)
data(Boston)
X = Boston
y = X$medv
X$medv = NULL
Xtrain = X[1 : (nrow(X) / 2), ]
ytrain = y[1 : (nrow(X) / 2)]
Xtest = X[(nrow(X) / 2 + 1) : nrow(X), ]
ytest = y[(nrow(X) / 2 + 1) : nrow(X)]
set_bart_machine_num_cores(4)
bart_machine = build_bart_machine(Xtrain, ytrain,
num_trees = 200,
num_burn_in = 300,
num_iterations_after_burn_in = 1000,
use_missing_data = TRUE,
debug_log = TRUE,
verbose = TRUE)
bart_machine
plot_y_vs_yhat(bart_machine)
yhat = predict(bart_machine, Xtest)
q("no")
options(java.parameters = "-Xmx1500m")
library(bartMachine)
data("Pima.te", package = "MASS")
X <- data.frame(Pima.te[, -8])
y <- Pima.te[, 8]
bart_machine = bartMachine(X, y)
bart_machine
table(y, predict(bart_machine, X, type = "class"))
raw_node_data = extract_raw_node_data(bart_machine, g = 37)
raw_node_data[[17]]
options(java.parameters = "-Xmx20000m")
library(bartMachine)
set_bart_machine_num_cores(10)
set.seed(1)
data(iris)
iris2 = iris[51 : 150, ] #do not include the third type of flower for this example
iris2$Species = factor(iris2$Species)
X = iris2[ ,1:4]
y = iris2$Species
bart_machine = bartMachine(X, y, num_trees = 50, seed = 1)
bart_machine
##make probability predictions on the training data
p_hat = predict(bart_machine, iris2[ ,1:4])
p_hat