-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-nursery.Rmd
244 lines (174 loc) · 4.43 KB
/
test-nursery.Rmd
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
---
title: "ACRO R Notebook"
output: html_notebook
---
```{r}
# install.packages("haven")
# install.packages("reticulate")
# install.packages("farff")
# install.packages("survival")
```
## Import Libraries
```{r}
source("../acro.R") # ACRO
```
## Load Data
```{r}
data = farff::readARFF("../data/nursery.arff")
data = as.data.frame(data)
names(data)[names(data) == "class"] <- "recommend"
```
## Convert children to integers,
### replacing 'more' with random int from range 4-10
```{r}
unique(data$children)
#data$children <- sub("more","6",data$children)
unique(data$children)
data$children <-as.numeric(as.character(data$children))
unique(data$children)
data[is.na(data)] <- round(runif(sum(is.na(data)), min = 4, max = 10),0)
unique(data$children)
```
```{r}
head(data)
#sapply(data)
```
## Tables
### ACRO Crosstab
```{r}
index = data[, c("recommend")]
columns = data[, c("parents")]
values = data[, c("children")]
aggfunc = "mean"
table = acro_crosstab(index, columns, values=values, aggfunc=aggfunc)
```
```{r}
table
```
```{r}
index = data[, c("recommend")]
columns = data[, c("parents")]
table = acro_table(index, columns, dnn= c("recommend", "parents"), deparse.level=0)
```
```{r}
table
```
### Add Comments to Output
```{r}
acro_add_comments("output_0", "This is a crosstab on the nursery dataset.")
```
### ACRO Pivot Table
```{r}
index = "parents"
values = "children"
aggfunc = list("mean", "std")
table = acro_pivot_table(data, values=values, index=index, aggfunc=aggfunc)
```
```{r}
table
```
### ACRO histogram
```{r}
acro_hist(data, "children")
```
### ACRO survival analysis
```{r}
data(package = "survival")
# Load the lung dataset
data(lung)
#head(lung)
acro_surv_func(time=lung$time, status=lung$status, output ="plot")
```
```
# Regression examples using ACRO
Again there is an industry-standard package in python, this time called **statsmodels**.
- The examples below illustrate the use of the ACRO wrapper standard statsmodel functions
- Note that statsmodels can be called using an 'R-like' format (using an 'r' suffix on the command names)
- most statsmodels functiobns return a "results object" which has a "summary" function that produces printable/saveable outputs
### Start by manipulating the nursery data to get two numeric variables
- The 'recommend' column is converted to an integer scale
```{r}
data$recommend <- as.character(data$recommend)
data$recommend[which(data$recommend=="not_recom")] <- "0"
data$recommend[which(data$recommend=="recommend")] <- "1"
data$recommend[which(data$recommend=="very_recom")] <- "2"
data$recommend[which(data$recommend=="priority")] <- "3"
data$recommend[which(data$recommend=="spec_prior")] <- "4"
data$recommend <- as.numeric(data$recommend)
```
##
```{r}
# extract relevant columns
df = data[, c("recommend", "children")]
# drop rows with missing values
df = df[complete.cases(df), ]
# formula to fit
formula = "recommend ~ children"
```
### Fit Linear Model
```{r}
model = lm(formula=formula, data=df)
summary(model)
```
### ACRO Linear Model
```{r}
acro_lm(formula=formula, data=df)
```
### ACRO Probit/ Logit regression
This is an example of logit/probit regression using ACRO\
We use a different combination of variables from the original dataset.
```{r}
# extract relevant columns
df = data[, c("finance", "children")]
# drop rows with missing values
df = df[complete.cases(df), ]
# convert finance to numeric
df = transform(df, finance = as.numeric(finance))
# subtract 1 to make 1s and 2S into 0a and 1s
df$finance <- df$finance -1
# formula to fit
formula = "finance ~ children"
```
### Fit Logit Model
```{r}
model = glm(formula=formula, data=df, family=binomial(link="logit"))
summary(model)
```
### ACRO Logit Model
```{r}
acro_glm(formula=formula, data=df, family="logit")
```
### Fit Probit Model
```{r}
model = glm(formula=formula, data=df, family=binomial(link="probit"))
summary(model)
```
### ACRO Probit Model
```{r}
acro_glm(formula=formula, data=df, family="probit")
```
### Add Custom Output
```{r}
acro_custom_output("XandY.jpeg", "This output is an image showing the relationship between X and Y")
```
### Rename Output
```{r}
acro_rename_output("output_5", "xy_plot")
```
### Remove Output
```{r}
acro_remove_output("output_3")
```
### Display Outputs
```{r}
acro_print_outputs()
```
## Finalise
```{r}
#acro_finalise("RTEST", "xlsx")
acro_finalise("RTEST", "json")
```
## Get the documentation for crosstab function
```{r}
acro_help("acro_crosstab")
```