-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.Rmd
156 lines (113 loc) · 2.52 KB
/
test.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
---
title: "ACRO R Notebook"
output: html_notebook
---
```{r}
# install.packages("haven")
# install.packages("reticulate")
```
## Import Libraries
```{r}
library(haven) # read .dta
source("../acro.R") # ACRO
```
## Load Data
```{r}
data = read_dta("../data/test_data.dta")
data = as.data.frame(data)
data = zap_labels(data) # Stata data files include extra labels
head(data)
```
## Tables
### ACRO Crosstab
```{r}
index = data[, c("year")]
columns = data[, c("grant_type")]
values = data[, c("inc_grants")]
aggfunc = "mean"
table = acro_crosstab(index, columns, values=values, aggfunc=aggfunc)
```
```{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 = "grant_type"
values = "inc_grants"
aggfunc = list("mean", "std")
table = acro_pivot_table(data, values=values, index=index, aggfunc=aggfunc)
```
```{r}
table
```
## Linear Models
```{r}
# extract relevant columns
df = data[, c("inc_activity", "inc_grants", "inc_donations", "total_costs")]
# drop rows with missing values
df = df[complete.cases(df), ]
# formula to fit
formula = "inc_activity ~ inc_grants + inc_donations + total_costs"
```
### Fit Linear Model
```{r}
model = lm(formula=formula, data=df)
summary(model)
```
### ACRO Linear Model
```{r}
acro_lm(formula=formula, data=df)
```
## Logit/Probit Models
```{r}
# extract relevant columns
df = data[, c("survivor", "inc_activity", "inc_grants", "inc_donations", "total_costs")]
# drop rows with missing values
df = df[complete.cases(df), ]
# convert survivor to numeric
df = transform(df, survivor = as.numeric(survivor))
# formula to fit
formula = "survivor ~ inc_activity + inc_grants + inc_donations + total_costs"
```
### 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")
```
## Finalise
```{r}
acro_finalise("RTEST", "json")
```
## Use the help function
```{r}
acro_help("acro_pivot_table")
```