-
Notifications
You must be signed in to change notification settings - Fork 6
/
hhhsensfns.R
139 lines (109 loc) · 4.07 KB
/
hhhsensfns.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
specpars.ps <- function(dat, covs, dropcov, type = NULL, outcome, treatment,
fitfunction = glm, thecaliper = NULL, fmcontrol = NULL, fmmaker = NULL, ...) {
## outcome, treatment, dropcov are character names of variables in dat
## covs is a vector of character names of covariates
## dat is a data frame
## type says whether the covariate is a factor (2) or numeric (1)
## fmfmla is either psdist or psdist+caliper(...)
require(lmtest)
require(sandwich)
if (is.null(type)) {
type <- 1
}
## New dataset without the cov
newdat <- dat[, -grep(dropcov, names(dat))]
newcovs <- covs[ -grep(dropcov, covs)]
if (is.null(fmmaker)) {
## Make distances without the cov
newfmla <- reformulate(newcovs, response = treatment)
psmod <- fitfunction(newfmla, data = newdat, ...)
psdist <- match_on(psmod, data = newdat)
if (!is.null(thecaliper)) {
psdist <- psdist + caliper(psdist, thecaliper)
}
## Do matching
if (is.null(fmcontrol)) {
thefm <- fullmatch(psdist, data = newdat)
}
else {
fmcontrol[["x"]] <- psdist
fmcontrol[["data"]] <- newdat
thefm <- do.call("fullmatch", fmcontrol)
}
} else {
thefm <- fmmaker(newdat, newcovs, treatment = treatment, ...)
}
dat[names(thefm), "thefm"] <- thefm
mod2fmla <- reformulate(c(treatment, "thefm"), response = outcome)
mod1fmla <- reformulate(c(treatment, "thefm", dropcov), response = outcome)
## Fit calibration models:
## trtmod = lm(infhrs~postbomb+thefm+dat[,dropcov], data=dat)
trtmod <- lm(mod1fmla, data = dat)
r.with <- summary(trtmod)$r.squared
b.add <- coef(trtmod)[treatment]
se.b.add <- coeftest(trtmod, vcov. = vcovHC(trtmod, type = "HC2"))[treatment, "Std. Error"]
## trtmod2 = lm(infhrs~postbomb+thefm, data=dat)
trtmod2 <- lm(mod2fmla, data = dat)
r.without <- summary(trtmod2)$r.square
b <- coef(trtmod2)[treatment]
se.b <- coeftest(trtmod2, vcov. = vcovHC(trtmod2, type = "HC2"))[treatment, "Std. Error"]
r.par <- (r.with - r.without) / (1 - r.without)
if (type == 1) {
bigmodfmla <- reformulate(c(dropcov, "thefm"), response = treatment)
## bigmod = lm(postbomb~dat[,dropcov]+thefm, data=dat)
bigmod <- lm(bigmodfmla, data = dat)
t.w <- coeftest(bigmod, vcov. = vcovHC(bigmod, type = "HC2"))[dropcov, "t value"]
df <- bigmod$df
}
if (type == 2) {
smmodfmla <- reformulate(c("thefm"), response = treatment)
smmod <- lm(smmodfmla, data = dat)
bigmodfmla <- reformulate(c(dropcov, "thefm"), response = treatment)
## bigmod = lm(postbomb~dat[,dropcov]+thefm, data=dat)
bigmod <- lm(bigmodfmla, data = dat)
Fw <- ((deviance(smmod) - deviance(bigmod)) / (deviance(bigmod) / df.residual(bigmod)))
df <- summary(bigmod)$df[2]
k <- length(levels(dat[, dropcov])) - 1
t.w <- sqrt((k * df / (df + 1 - k)) * Fw)
}
specpars <- cbind(r.par, t.w, b, se.b, df, b.add, se.b.add)
return(specpars)
}
make.ci <- function(dat, specpars, covnm, tquant, r.par) {
T <- abs(as.numeric(specpars["t.w", covnm]))
k <- length(levels(dat[, covnm])) - 1
df <- specpars["df", covnm]
g <- (T^2 * (df + k)) / (T^2 * (df + k) + tquant^2 * (T^2 + df))
if (r.par <= g) {
ci <- cor2(dat, specpars, covnm, tquant, r.par)
}
if (r.par > g) {
ci <- cor1(dat, specpars, covnm, tquant)
}
return(ci)
}
cor2 <- function(dat, specpars, covnm, tquant, r.par) {
T <- abs(as.numeric(specpars["t.w", covnm]))
df <- specpars["df", covnm]
b <- specpars["b", covnm]
se.b <- specpars["se.b", covnm]
adj1 <- T * sqrt(r.par)
adj2 <- tquant * sqrt(((T^2) + df) / (df - 1)) * sqrt(1 - r.par)
adj <- adj1 + adj2
lb <- b - adj * se.b
ub <- b + adj * se.b
ci <- cbind(lb, ub)
return(ci)
}
cor1 <- function(dat, specpars, covnm, tquant) {
T <- abs(as.numeric(specpars["t.w", covnm]))
df <- specpars["df", covnm]
b <- specpars["b", covnm]
se.b <- specpars["se.b", covnm]
k <- as.numeric(length(names(dat)) - 1)
adj <- sqrt((T^2) + (tquant^2) * (((T^2) + df - k + 1) / (df - k)))
lb <- b - adj * se.b
ub <- b + adj * se.b
ci <- cbind(lb, ub)
return(ci)
}