# Use seattledmi, block-level panel data, to evaluate a crime intervention.
# Declare time-variant (outcome) and time-invariant variables for matching
cov.var <- c('TotalPop', 'BLACK', 'HISPANIC', 'Males_1521',
'HOUSEHOLDS', 'FAMILYHOUS', 'FEMALE_HOU', 'RENTER_HOU', 'VACANT_HOU')
match.out <- c('i_felony', 'i_misdemea', 'i_drugs', 'any_crime')
set.seed(99199) # for reproducibility
# Perform matching and estimation, without permutations or jackknife
# runtime: < 1 min
# \donttest{
sea1 <- microsynth(seattledmi,
idvar='ID', timevar='time', intvar='Intervention',
start.pre=1, end.pre=12, end.post=16,
match.out=match.out, match.covar=cov.var,
result.var=match.out, omnibus.var=match.out,
test='lower',
n.cores = min(parallel::detectCores(), 2))
# View results
summary(sea1)
plot_microsynth(sea1)
# }
if (FALSE) {
# Repeat matching and estimation, with permutations and jackknife
# Set permutations and jack-knife to very few groups (2) for
# quick demonstration only.
# runtime: ~30 min
sea2 <- microsynth(seattledmi,
idvar='ID', timevar='time', intvar='Intervention',
start.pre=1, end.pre=12, end.post=c(14, 16),
match.out=match.out, match.covar=cov.var,
result.var=match.out, omnibus.var=match.out,
test='lower',
perm=250, jack=TRUE,
result.file=file.path(tempdir(), 'ExResults2.xlsx'),
n.cores = min(parallel::detectCores(), 2))
# View results
summary(sea2)
plot_microsynth(sea2)
# Specify additional outcome variables for matching, which makes
# matching harder.
match.out <- c('i_robbery','i_aggassau','i_burglary','i_larceny',
'i_felony','i_misdemea','i_drugsale','i_drugposs','any_crime')
# Perform matching, setting check.feas = T and use.backup = T
# to ensure model feasibility
# runtime: ~40 minutes
sea3 <- microsynth(seattledmi,
idvar='ID', timevar='time', intvar='Intervention',
end.pre=12,
match.out=match.out, match.covar=cov.var,
result.var=match.out, perm=250, jack=0,
test='lower', check.feas=TRUE, use.backup = TRUE,
result.file=file.path(tempdir(), 'ExResults3.xlsx'),
n.cores = min(parallel::detectCores(), 2))
# Aggregate outcome variables before matching, to boost model feasibility
match.out <- list( 'i_robbery'=rep(2, 6), 'i_aggassau'=rep(2, 6),
'i_burglary'=rep(1, 12), 'i_larceny'=rep(1, 12),
'i_felony'=rep(2, 6), 'i_misdemea'=rep(2, 6),
'i_drugsale'=rep(4, 3), 'i_drugposs'=rep(4, 3),
'any_crime'=rep(1, 12))
# After aggregation, use.backup and cheack.feas no longer needed
# runtime: ~40 minutes
sea4 <- microsynth(seattledmi, idvar='ID', timevar='time',
intvar='Intervention', match.out=match.out, match.covar=cov.var,
start.pre=1, end.pre=12, end.post=16,
result.var=names(match.out), omnibus.var=names(match.out),
perm=250, jack = TRUE, test='lower',
result.file=file.path(tempdir(), 'ExResults4.xlsx'),
n.cores = min(parallel::detectCores(), 2))
# View results
summary(sea4)
plot_microsynth(sea4)
# Generate weights only (for four variables)
match.out <- c('i_felony', 'i_misdemea', 'i_drugs', 'any_crime')
# runtime: ~ 20 minutes
sea5 <- microsynth(seattledmi, idvar='ID', timevar='time',
intvar='Intervention', match.out=match.out, match.covar=cov.var,
start.pre=1, end.pre=12, end.post=16,
result.var=FALSE, perm=250, jack=TRUE,
n.cores = min(parallel::detectCores(), 2))
# View weights
summary(sea5)
# Generate results only
sea6 <- microsynth(seattledmi, idvar='ID', timevar='time',
intvar='Intervention',
start.pre=1, end.pre=12, end.post=c(14, 16),
result.var=match.out, test='lower',
w=sea5, result.file=file.path(tempdir(), 'ExResults6.xlsx'),
n.cores = min(parallel::detectCores(), 2))
# View results (including previously-found weights)
summary(sea6)
# Generate plots only
plot_microsynth(sea6, plot.var=match.out[1:2])
# Apply microsynth in the traditional setting of Synth
# Create macro-level (small n) data, with 1 treatment unit
set.seed(86879)
ids.t <- names(table(seattledmi$ID[seattledmi$Intervention==1]))
ids.c <- setdiff(names(table(seattledmi$ID)), ids.t)
ids.synth <- c(base::sample(ids.t, 1), base::sample(ids.c, 100))
seattledmi.one <- seattledmi[is.element(seattledmi$ID,
as.numeric(ids.synth)), ]
# Apply microsynth to the new macro-level data
# runtime: < 5 minutes
sea8 <- microsynth(seattledmi.one, idvar='ID', timevar='time',
intvar='Intervention',
start.pre=1, end.pre=12, end.post=16,
match.out=match.out[4],
match.covar=cov.var, result.var=match.out[4],
test='lower', perm=250, jack=FALSE,
check.feas=TRUE, use.backup=TRUE,
n.cores = min(parallel::detectCores(), 2))
# View results
summary(sea8)
plot_microsynth(sea8)
# Use microsynth to calculate propensity score-type weights
# Prepare cross-sectional data at time of intervention
seattledmi.cross <- seattledmi[seattledmi$time==16, colnames(seattledmi)!='time']
# Apply microsynth to find propensity score-type weights
# runtime: ~5 minutes
sea9 <- microsynth(seattledmi.cross, idvar='ID', intvar='Intervention',
match.out=FALSE, match.covar=cov.var, result.var=match.out,
test='lower', perm=250, jack=TRUE,
n.cores = min(parallel::detectCores(), 2))
# View results
summary(sea9)
}
Run the code above in your browser using DataLab