# This is a simple example to illustrate makematch().
# Please see the better match in the documentation for
# tighten() before continuing with this example.
#
###########
# Tighten match from 1-3 to 1-2 to adjust for BMI.
# BMI might be affected by alcohol consumption, so
# the primary comparison did not adjust for it.
###########
# The example below illustrates mechanics.
# See the same example in tighten() for a
# simpler construction of a better match.
data(aHDLt)
z<-aHDLt$z #treatment indicator
# If you need to debug, it is helpful to have the same names for z
# and rownames for dat.
# These names are then used by functions that create distance matrices,
# including startcost(), addNearExact() and addMahal().
# If you use names, they will be checked for consistency and an error
# may result from incompatible names.
rownames(aHDLt)<-aHDLt$SEQN
names(z)<-aHDLt$SEQN
# Create a zero cost (i.e., distance) matrix for the left and right
# sides of the network.
left<-startcost(z)
right<-startcost(z)
left<-addNearExact(left,z,aHDLt$block) # Forces within-block matching.
# Prefer to retain within blocks people who are closest for age, education.
left<-addMahal(left,z,cbind(aHDLt$education,aHDLt$age))
# Try to balance the categories of BMI which are out of balance.
right<-addNearExact(right,z,aHDLt$ibmi,penalty=20)
m<-makematch(aHDLt,left,right,ncontrols=2,large=10)
# Tightened blocks
table(aHDLt$z)
table(m$z)
table(table(aHDLt$block))
table(table(m$block))
table(table(m$mset))
boxplot(m$bmi[m$z==1],m$bmi[m$z==0],
names=c("D","C"),ylab="BMI")
# Cost matrix for left side of network.
# Rows are treated, columns are control.
z[1:10]
round(left[1:5,1:4],1)
# Cost matrix for right side of network.
round(right[1:5,1:4],1)
Run the code above in your browser using DataLab