-
Notifications
You must be signed in to change notification settings - Fork 2
/
acro.R
152 lines (134 loc) · 4.24 KB
/
acro.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
library(reticulate) # import Python modules
library(admiraldev)
library(png)
library(grid)
acro <- import("acro")
ac <- acro$ACRO()
acro_crosstab <- function(index, columns, values=NULL, aggfunc=NULL)
{
"ACRO crosstab"
table = ac$crosstab(index, columns, values=values, aggfunc=aggfunc)
return(table)
}
acro_table <- function(index, columns, dnn=NULL, deparse.level=0, ...)
{
"ACRO crosstab without aggregation function"
if (is.null(dnn)) {
if (deparse.level == 0) {
row_names <- list("")
col_names <- list("")
} else if (deparse.level == 1) {
tryCatch({
index_symbol <- assert_symbol(substitute(index))
row_names <- list(deparse(index_symbol))},
error = function(e) {
row_names <- list("")
})
tryCatch({
column_symbol <- assert_symbol(substitute(columns))
col_names <- list(deparse(column_symbol))},
error = function(e) {
col_names <- list("")
})
} else if (deparse.level == 2) {
row_names <- list(deparse((substitute(index))))
col_names <- list(deparse(substitute(columns)))
}
}
else {
row_names <- list(dnn[1])
col_names <- list(dnn[2])
}
table <- ac$crosstab(index, columns, rownames=row_names, colnames=col_names)
# Check for any unused arguments
if (length(list(...)) > 0) {
warning("Unused arguments were provided: ", paste0(names(list(...)), collapse = ", "), "\n", "To find more help about the function use: acro_help(\"acro_table\")\n")
}
return(table)
}
acro_pivot_table <- function(data, values=NULL, index=NULL, columns=NULL, aggfunc="mean")
{
"ACRO pivot table"
table = ac$pivot_table(data, values=values, index=index, columns=columns, aggfunc=aggfunc)
return(table)
}
acro_lm <- function(formula, data)
{
"ACRO linear model"
model = ac$olsr(formula, data)
model$summary()
}
acro_glm <- function(formula, data, family)
{
"ACRO logit/probit model"
if (family == "logit") {
model = ac$logitr(formula, data)
} else if (family == "probit") {
model = ac$probitr(formula, data)
} else {
stop("Invalid family. Options = {'logit', 'probit'}");
}
model$summary()
}
acro_hist <- function(data, column, breaks=10, freq=TRUE, col=NULL, filename="histogram.png"){
"ACRO histogram"
histogram = ac$hist(data=data, column=column, bins=breaks, density=freq, color=col, filename=filename)
# Load the saved histogram
image <- readPNG(histogram)
grid.raster(image)
}
acro_surv_func <- function(time, status, output, filename="kaplan-meier.png"){
"Estimates the survival function. Produce either a plot of table"
results = ac$surv_func(time=time, status=status, output=output, filename=filename)
if (output=="plot"){
# Load the saved survival plot
image <- readPNG(results[[2]])
grid.raster(image)
}
}
acro_rename_output <- function(old, new)
{
"Rename an output"
ac$rename_output(old, new)
}
acro_remove_output <- function(name)
{
"Remove an output"
ac$remove_output(name)
}
acro_add_comments <- function(name, comment)
{
"Add comments to an output"
ac$add_comments(name, comment)
}
acro_custom_output <- function(filename, comment=NULL)
{
"Add an unsupported output"
ac$custom_output(filename, comment)
}
acro_add_exception <- function(name, reason)
{
"Add an exception request to an output"
ac$add_exception(name, reason)
}
acro_print_outputs <- function()
{
"Prints outputs to console"
ac$print_outputs()
}
acro_finalise <- function(path, ext)
{
"Write outputs to file"
ac$finalise(path, ext)
}
acro_help <- function(command, family="")
{
"Get the help documentation of the functions"
# mapping the r commands to their corresponding python commands
command_map <- c("acro_table" = "crosstab", "acro_lm" = "olsr",
"acro_glm" = ifelse(family %in% c("", "logit"), "logitr", "probitr"))
# get the command
command <- ifelse(command %in% names(command_map), command_map[command], sub("acro_", "", command))
command <- ac[[command]]
py_help(command)
}