-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim_data.R
More file actions
80 lines (52 loc) · 1.89 KB
/
sim_data.R
File metadata and controls
80 lines (52 loc) · 1.89 KB
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
set.seed(123)
# Simulate effects:
# --------------------------------
bl.type = sample(c("Linear", "Spline with degree 3", "Polynomial"))
bl.names = c("GenderMale", "Income", "Height", "Longitude", "Latitude", "RiskGroup",
"GenderFemale", "Age", "Duration", "Wealth", "Depth", "Pressure", "Temperature",
"City", "Country", "Experience", "Education", "Travel")
bl.names = paste0(sample(bl.type, length(bl.names), TRUE), " ",bl.names)
bl = sample(bl.names, 1500, TRUE)
# Write .js file to load the base-learner:
mylines = c(
"function getBlearner ()",
"{",
paste0(" return [", paste( paste0("\"", bl, "\""), collapse = ", "), "];"),
"}"
)
writeLines(mylines, con = "js/blearner.js")
# Simulate data depending on learner:
# -------------------------------------------
dir = "plot_data/effects"
simLinear = function () {
betas = runif(2, -4, 4)
x = runif(100, -10, 10)
x = seq(from = min(x), to = max(x), length.out = 100)
y = cbind(1, x) %*% betas
# plot(x, y)
return (list(x, y))
}
simPoly = function () {
betas = runif(3, -4, 4)
x = runif(100, -10, 10)
x = seq(from = min(x), to = max(x), length.out = 100)
y = cbind(1, x, x^2) %*% betas
# plot(x, y)
return (list(x, y))
}
simSplines = function () {
betas = runif(5, -2, 2)
x = runif(100, -2, 2)
x = seq(from = min(x), to = max(x), length.out = 100)
y = runif(1, 0, 10) + cbind(x, x^2, x^3, x^4, x^5) %*% betas
# plot(x, y)
return (list(x, y))
}
for (i in unique(bl)) {
file.name = stringr::str_replace_all(string = i, pattern = " ", replacement = "")
if (grepl("Linear", i)) { df.temp = simLinear() }
if (grepl("Spline with degree 3", i)) { df.temp = simSplines() }
if (grepl("Polynomial", i)) { df.temp = simPoly() }
df.temp = data.frame(x = df.temp[[1]], y = df.temp[[2]])
write.csv(df.temp, file = paste0(dir, "/", file.name, ".csv"), row.names = FALSE)
}