[R] Finding cases in one subset that are closet to another subset

2009-12-02 Thread Peter Flom
Good afternoon

Running R2.10.0 on Windows

I have a data frame that includes (among much else) a factor (In_2006) and a 
continuous variable (math_3_4).  I would like to find the 2 cases for In_2006 = 
0 that are closest to each case where In_2006 = 1.

My data looks like

 In_2006 math_3_4
 0 55.1
 1 51.6
 1 18.1
 1 26.6
 1 14.1
 1  9.6
 1 48.9
 1 12.9
 0 63.0
 0 51.8

etc. for several hundred rows.

I would like a new data frame that has all the cases where In_2006 = 1, and 
those cases of In_2006 that are closest to those cases

Thanks in advance

Peter

Peter L. Flom, PhD
Statistical Consultant
Website: www DOT peterflomconsulting DOT com
Writing; http://www.associatedcontent.com/user/582880/peter_flom.html
Twitter:   @peterflom

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Finding cases in one subset that are closet to another subset

2009-12-02 Thread Chuck Cleland
On 12/2/2009 3:01 PM, Peter Flom wrote:
 Good afternoon
 
 Running R2.10.0 on Windows
 
 I have a data frame that includes (among much else) a factor (In_2006) and a 
 continuous variable (math_3_4).  I would like to find the 2 cases for In_2006 
 = 0 that are closest to each case where In_2006 = 1.
 
 My data looks like
 
  In_2006 math_3_4
  0 55.1
  1 51.6
  1 18.1
  1 26.6
  1 14.1
  1  9.6
  1 48.9
  1 12.9
  0 63.0
  0 51.8
 
 etc. for several hundred rows.
 
 I would like a new data frame that has all the cases where In_2006 = 1, and 
 those cases of In_2006 that are closest to those cases

Hi Peter:

  How about using one of the various matching packages (MatchIt,
optmatch, Matching)?  For example, something like this:


DF - data.frame(X = rbinom(200, 1, .1), Y = runif(200))

library(MatchIt)

DF.match - matchit(X ~ Y, data=DF, method='optimal', ratio=2)

DF[c(rownames(DF.match$match.matrix), c(DF.match$match.matrix)),]


hope this helps,

Chuck

 Thanks in advance
 
 Peter
 
 Peter L. Flom, PhD
 Statistical Consultant
 Website: www DOT peterflomconsulting DOT com
 Writing; http://www.associatedcontent.com/user/582880/peter_flom.html
 Twitter:   @peterflom
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code. 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Finding cases in one subset that are closet to another subset

2009-12-02 Thread David Winsemius


On Dec 2, 2009, at 3:01 PM, Peter Flom wrote:


Good afternoon

Running R2.10.0 on Windows

I have a data frame that includes (among much else) a factor  
(In_2006) and a continuous variable (math_3_4).  I would like to  
find the 2 cases for In_2006 = 0 that are closest to each case where  
In_2006 = 1.




You ask for closest which would imply a singleton, but you also ask  
for cases (plural). Define closest, please.



My data looks like

In_2006 math_3_4
0 55.1
1 51.6
1 18.1
1 26.6
1 14.1
1  9.6
1 48.9
1 12.9
0 63.0
0 51.8

etc. for several hundred rows.

I would like a new data frame that has all the cases where In_2006 =  
1, and those cases of In_2006 that are closest to those cases


Thanks in advance

Peter

Peter L. Flom, PhD
Statistical Consultant
Website: www DOT peterflomconsulting DOT com
Writing; http://www.associatedcontent.com/user/582880/peter_flom.html
Twitter:   @peterflom

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Finding cases in one subset that are closet to another subset

2009-12-02 Thread Peter Flom
David Winsemius dwinsem...@comcast.net wrote

On Dec 2, 2009, at 3:01 PM, Peter Flom wrote:

 Good afternoon

 Running R2.10.0 on Windows

 I have a data frame that includes (among much else) a factor  
 (In_2006) and a continuous variable (math_3_4).  I would like to  
 find the 2 cases for In_2006 = 0 that are closest to each case where  
 In_2006 = 1.


You ask for closest which would imply a singleton, but you also ask  
for cases (plural). Define closest, please.


I would like the 2 closest cases  that is, for each case in one set, the 
two that are closest in the other set.

Sorry if I was not clear

Peter

Peter L. Flom, PhD
Statistical Consultant
Website: www DOT peterflomconsulting DOT com
Writing; http://www.associatedcontent.com/user/582880/peter_flom.html
Twitter:   @peterflom

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Finding cases in one subset that are closet to another subset

2009-12-02 Thread Peter Flom
Thanks Chuck

I did not know about that MatchIt.   I will check it out

Peter

-Original Message-
From: Chuck Cleland cclel...@optonline.net
Sent: Dec 2, 2009 3:47 PM
To: Peter Flom peterflomconsult...@mindspring.com
Cc: r help r-help@r-project.org
Subject: Re: [R] Finding cases in one subset that are closet to another subset

On 12/2/2009 3:01 PM, Peter Flom wrote:
 Good afternoon
 
 Running R2.10.0 on Windows
 
 I have a data frame that includes (among much else) a factor (In_2006) and a 
 continuous variable (math_3_4).  I would like to find the 2 cases for 
 In_2006 = 0 that are closest to each case where In_2006 = 1.
 
 My data looks like
 
  In_2006 math_3_4
  0 55.1
  1 51.6
  1 18.1
  1 26.6
  1 14.1
  1  9.6
  1 48.9
  1 12.9
  0 63.0
  0 51.8
 
 etc. for several hundred rows.
 
 I would like a new data frame that has all the cases where In_2006 = 1, and 
 those cases of In_2006 that are closest to those cases

Hi Peter:

  How about using one of the various matching packages (MatchIt,
optmatch, Matching)?  For example, something like this:


DF - data.frame(X = rbinom(200, 1, .1), Y = runif(200))

library(MatchIt)

DF.match - matchit(X ~ Y, data=DF, method='optimal', ratio=2)

DF[c(rownames(DF.match$match.matrix), c(DF.match$match.matrix)),]


hope this helps,

Chuck

 Thanks in advance
 
 Peter
 
 Peter L. Flom, PhD
 Statistical Consultant
 Website: www DOT peterflomconsulting DOT com
 Writing; http://www.associatedcontent.com/user/582880/peter_flom.html
 Twitter:   @peterflom
 
 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code. 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894


Peter L. Flom, PhD
Statistical Consultant
Website: www DOT peterflomconsulting DOT com
Writing; http://www.associatedcontent.com/user/582880/peter_flom.html
Twitter:   @peterflom

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.