forked from SoerenPannier/emdi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remdiplus.ado
72 lines (62 loc) · 2.33 KB
/
Remdiplus.ado
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
* stata wrapper for EMDIplus
* depends on package rcall
cap program drop Remdiplus
program define Remdiplus
syntax namelist, smp_data(string) pop_data(string) weights(string) pop_weight(string) smp_domains(string) pop_domains(string) threshold(real) [l(int 100) b(int 100) mse(string) transformation(string) cpus(int 1) seed(int 1234) savexls(string) saveobject(string) interval(string) scale_bootvar(string)]
* set defaults
if "`MSE'"=="" {
local MSE="TRUE"
}
if "`transformation'"=="" {
local transformation "no"
}
if "`savexls'"=="" {
local saving "EMDI_results.xlsx"
}
if "`saveobject'"=="" {
local saveobject "EMDI"
}
if "`scale_bootvar'"=="" {
local scale_bootvar "NULL"
}
local pop_data : subinstr local pop_data "\" "/", all
local smp_data : subinstr local smp_data "\" "/", all
local savexls : subinstr local savexls "\" "/", all
local saveobject : subinstr local saveobject "\" "/", all
preserve
use `smp_data', replace
* output model to `modelfile', a textfile R can read
tempname modelfile
gettoken yvar xvars : namelist
local formula "`yvar' ~ "
foreach var of varlist `xvars' {
local formula "`formula' + `var'"
}
local formula : subinstr local formula "+ " ""
dis "`formula'"
cap file close R
file open R using "`modelfile'.txt", write replace
file write R "`formula'" _n
file close R
local working_dir : pwd
local working_dir : subinstr local working_dir "\" "/", all
restore
rcall debug: library("emdiplus"); ///
library("haven"); ///
library("openxlsx"); ///
library("nlme"); ///
library("R.utils"); ///
povline <- `threshold'; ///
setwd("`working_dir'"); ///
pop <- as.data.frame(read_dta("`pop_data'")); ///
smp <- as.data.frame(read_dta("`smp_data'")); ///
model <- read.delim("`modelfile'.txt", header = FALSE, sep = "\t"); ///
model <- as.formula(as.character(model[1,1])); ///
ebp_results <- ebp(fixed = model,pop_data = pop, ///
pop_domains = "`pop_domains'", smp_data = smp, smp_domains = "`smp_domains'", ///
threshold = `threshold', L = `l', B = `b', MSE = `mse', transformation = "`transformation'", ///
na.rm = TRUE, cpus = `cpus', seed=`seed', weights = "`weights'", pop_weights = "`pop_weight'"); ///
write.excel(ebp_results, file = "`savexls'", indicator = "Poverty", ///
MSE = `mse', CV = `mse', split = FALSE); ///
save(ebp_results,file="`saveobject'")
end