Hey,
I found this commads on the net to do what I need... but I am not good
enough at programing.
There are any easier option?
***
Hi Sharon,
I was just working yesterday to reconfigure my data so that it includes
matched pair identifiers. I did not take my data to the matched pair level
of analysis, but what I did should get you part of the way there. From
where I leave off, importing the data into SAS and then transforming the
data from long to wide (
http://www.ats.ucla.edu/stat/sas/modules/longtowide_data.htm) might be a
reasonable approach. Hope this helps.
Bridget
Here's what I did, with the help of my colleague Dan Nordlund, to attach
matched pair identifiers to my dataset (shown here using the example
lalonde dataset):
> data(lalonde)
> rownames(lalonde)<-1:614
> lalonde$id_num<-1:614
> head(lalonde)
treat age educ black hispan married nodegree re74 re75 re78 id_num
1 1 37 11 1 0 1 1 0 0 9930.0460 1
2 1 22 9 0 1 0 1 0 0 3595.8940 2
3 1 30 12 1 0 0 0 0 0 24909.4500 3
4 1 27 11 1 0 0 1 0 0 7506.1460 4
5 1 33 8 1 0 0 1 0 0 289.7899 5
6 1 22 9 1 0 0 1 0 0 4056.4940 6
> m.out<-matchit(treat ~ re74 + re75 + educ + black + hispan + age,
data=lalonde, method="nearest", ratio=1,
+ exact= c("age","educ"))
> m.out
Call:
matchit(formula = treat ~ re74 + re75 + educ + black + hispan +
age, data = lalonde, method = "nearest", ratio = 1, exact = c("age",
"educ"))
Sample sizes:
Control Treated
All 429 185
Matched 118 118
Unmatched 311 67
Discarded 0 0
>
>
>
First, I generate a large dataset of all person-level observations that
went into the matching process, with the essential output: id_numbers,
treatment identifiers, and weights.
> # large datafile
> a<-data.frame(lalonde$id_num,m.out$treat,m.out$weights)
> colnames(a)<-c("id_num","trt","weights")
> head(a)
id_num trt weights
1 1 1 1
2 2 1 0
3 3 1 1
4 4 1 0
5 5 1 1
6 6 1 1
>
Then, I reconfigure the "match.matrix" information so that it is a data
frame with variables in numeric format, identifying treated units and
matched control units, and a match number. I drop non-matched treatment
units, so the maximum match number here should correspond with the number
of matched treatment units.
> # datafile of matches
> b<-as.data.frame(m.out$match.matrix)
> colnames(b)<-c("matched_unit")
> b$matched_unit<-as.numeric(as.character(b$matched_unit))
> b$treated_unit<-as.numeric(rownames(b))
> #now delete matches=na
> c<-b[!is.na(b$matched_unit),]
> c$match_num<-1:dim(c)[1]
> head(c)
matched_unit treated_unit match_num
1 269 1 1
3 350 3 2
5 222 5 3
6 247 6 4
7 460 7 5
9 515 9 6
> head(m.out$match.matrix)
1
1 "269"
2 NA
3 "350"
4 NA
5 "222"
6 "247"
>
Last, I attach the match numbers to the large data-set of person
observations as follows. NA is the match number for treatment units with
no matched controls, and for controls that were not matched to any
treatment units.
> # attach match number to large datafile
> a[c$matched_unit,4]<-c$match_num
> a[c$treated_unit,4]<-c$match_num
> colnames(a)[4]<-"match_num"
> head(a)
id_num trt weights match_num
1 1 1 1 1
2 2 1 0 NA
3 3 1 1 2
4 4 1 0 NA
5 5 1 1 3
6 6 1 1 4
>
A couple of checks to see that the data reconfiguration is working as
planned:
> # check match numbers
> a[!is.na(a$match_num) & a$match_num==1,]
id_num trt weights match_num
1 1 1 1 1
269 269 0 1 1
> a[!is.na(a$match_num) & a$match_num==2,]
id_num trt weights match_num
3 3 1 1 2
350 350 0 1 2
> a[!is.na(a$match_num) & a$match_num==3,]
id_num trt weights match_num
5 5 1 1 3
222 222 0 1 3
> table(a$match_num)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
19 20 21
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
40 41 42
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
61 62 63
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
82 83 84
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
104 105
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2
106 107 108 109 110 111 112 113 114 115 116 117 118
2 2 2 2 2 2 2 2 2 2 2 2 2
2016-09-21 18:00 GMT+02:00 <matchit-request(a)lists.gking.harvard.edu>:
> Send Matchit mailing list submissions to
> matchit(a)lists.gking.harvard.edu
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://lists.gking.harvard.edu/mailman/listinfo/matchit
> or, via email, send a message with subject or body 'help' to
> matchit-request(a)lists.gking.harvard.edu
>
> You can reach the person managing the list at
> matchit-owner(a)lists.gking.harvard.edu
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Matchit digest..."
>
>
> Today's Topics:
>
> 1. Does it have an option to save the number of matched pairs???
> (Andreu Ferrero)
> 2. Re: Does it have an option to save the number of matched
> pairs??? (Gary King)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Wed, 21 Sep 2016 14:40:36 +0200
> From: Andreu Ferrero <fromnorden(a)gmail.com>
> To: matchit(a)lists.gking.harvard.edu
> Subject: [matchit] Does it have an option to save the number of
> matched pairs???
> Message-ID:
> <CAH2QH=eTLP4UBAwxfCs0EXjYGu1bXMd5EhpOEzVbB3Dsco6KXw@mail.
> gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> Dear Authors,
>
> Recently I am using quite a lot the R-package that you build.
> It is great, but I have a question.
> Does it have an option to save the number of matched pairs???
> I was looking into "match.data()" function, but I did not find it...
>
> I think this point may be important, beacuse one may use "condicional
> logistic regression models" after matching's process.
>
> I hope you have time to answer me.
>
> Have a good day,
>
> Andrew
>
> --
> Andreu Ferrero Gregori
>
Dear Authors,
Recently I am using quite a lot the R-package that you build.
It is great, but I have a question.
Does it have an option to save the number of matched pairs???
I was looking into "match.data()" function, but I did not find it...
I think this point may be important, beacuse one may use "condicional
logistic regression models" after matching's process.
I hope you have time to answer me.
Have a good day,
Andrew
--
Andreu Ferrero Gregori