[R] boundary check

2010-09-24 Thread Feng Li
Dear R,

I have a covariates matrix with 10 observations,  e.g.

 X - matrix(rnorm(50), 10, 5)
 X
 [,1][,2][,3][,4]   [,5]
 [1,]  0.24857135  0.30880745 -1.44118657  1.10229027  1.0526010
 [2,]  1.24316806  0.36275370 -0.40096866 -0.24387888 -1.5324384
 [3,] -0.33504014  0.42996246  0.03902479 -0.84778875 -2.4754644
 [4,]  0.06710229  1.01950917 -0.09325091 -0.03222811  0.4127816
 [5,] -0.13619141  1.33143821 -0.79958805  2.08274102  0.6901768
 [6,] -0.45060357  0.19348831 -1.23793647 -0.72440163  0.5057326
 [7,] -1.20740516  0.20231086  1.15584485  0.8170 -1.2719855
 [8,] -1.81166284 -0.07913113 -0.91080581 -0.34774436  0.9552182
 [9,]  0.19131383  0.14980569 -0.37458224 -0.09371273 -1.7667203
[10,] -0.85159276 -0.66679528  1.63019340  0.56920196 -2.4049600

And I define a boundary of X:  The smallest ball that nests all the
observations of X. I wish to check if a particular point x_i

 x_i - matrix(rnorm(5), 1, 5)
 x_i
   [,1]  [,2]   [,3]  [,4]  [,5]
[1,] -0.1525543 0.4606419 -0.1011011 -1.557225 -1.035694

is inside the boundary of X or not. I know it's easy to do it with 1-D or
2-D, but I don't knot how to manage it when the dimension is large.

Can someone give a hint? Thanks in advance!


Feng

-- 
Feng Li
Department of Statistics
Stockholm University
106 91 Stockholm, Sweden
http://feng.li/

[[alternative HTML version deleted]]

__
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] boundary check

2010-09-24 Thread Robin Hankin
Hello

convex hulls in large numbers of dimensions are hard.

For your problem, though, one can tell whether a given
point is inside or outside by using linear programming:


 X - matrix(rnorm(50), 10, 5)
 x_i - matrix(rnorm(5), 1, 5)
 isin.chull
function(candidate,p,plot=FALSE,give.answers=FALSE,
  ...){
  if(plot){
plot(p,...)
p(candidate[1],candidate[2], pch=16)
  }
  n - nrow(p) # number of points
  d - ncol(p) # number of dimensions

  p - t(sweep(p,2,candidate))
  jj - simplex(a=rep(1,n),A3=rbind(p,1),b3=c(0*candidate,1))
  if(give.answers){
return(jj)
  }  else {
return((jj$solved = 0)  all(jj$soln1))
  }
}
 isin.chull(x_i,X)
[1] FALSE



(we can discuss offline; I'll summarize)


HTH

rksh


On 24/09/10 10:44, Feng Li wrote:
 Dear R,

 I have a covariates matrix with 10 observations,  e.g.

   
 X - matrix(rnorm(50), 10, 5)
 X
 
  [,1][,2][,3][,4]   [,5]
  [1,]  0.24857135  0.30880745 -1.44118657  1.10229027  1.0526010
  [2,]  1.24316806  0.36275370 -0.40096866 -0.24387888 -1.5324384
  [3,] -0.33504014  0.42996246  0.03902479 -0.84778875 -2.4754644
  [4,]  0.06710229  1.01950917 -0.09325091 -0.03222811  0.4127816
  [5,] -0.13619141  1.33143821 -0.79958805  2.08274102  0.6901768
  [6,] -0.45060357  0.19348831 -1.23793647 -0.72440163  0.5057326
  [7,] -1.20740516  0.20231086  1.15584485  0.8170 -1.2719855
  [8,] -1.81166284 -0.07913113 -0.91080581 -0.34774436  0.9552182
  [9,]  0.19131383  0.14980569 -0.37458224 -0.09371273 -1.7667203
 [10,] -0.85159276 -0.66679528  1.63019340  0.56920196 -2.4049600

 And I define a boundary of X:  The smallest ball that nests all the
 observations of X. I wish to check if a particular point x_i

   
 x_i - matrix(rnorm(5), 1, 5)
 x_i
 
[,1]  [,2]   [,3]  [,4]  [,5]
 [1,] -0.1525543 0.4606419 -0.1011011 -1.557225 -1.035694

 is inside the boundary of X or not. I know it's easy to do it with 1-D or
 2-D, but I don't knot how to manage it when the dimension is large.

 Can someone give a hint? Thanks in advance!


 Feng

   


-- 
Robin K. S. Hankin
Uncertainty Analyst
University of Cambridge
19 Silver Street
Cambridge CB3 9EP
01223-764877

__
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] boundary check

2010-09-24 Thread Michael Bedward
Hello,

If an N-dimensional convex hull fits your idea of a smallest ball
then you could try the convhulln function in the geometry package.

For testing if a new point is inside a previously derived hull, one
brute force approach is to rbind the new point to your data, generate
a new hull and see if it is the same as the previous one.

I've only used convhulln in low dimensions so I don't know how
efficient it is when N is large.

Hope this helps.
Michael


On 24 September 2010 19:44, Feng Li feng...@stat.su.se wrote:
 Dear R,

 I have a covariates matrix with 10 observations,  e.g.

 X - matrix(rnorm(50), 10, 5)
 X
             [,1]        [,2]        [,3]        [,4]       [,5]
  [1,]  0.24857135  0.30880745 -1.44118657  1.10229027  1.0526010
  [2,]  1.24316806  0.36275370 -0.40096866 -0.24387888 -1.5324384
  [3,] -0.33504014  0.42996246  0.03902479 -0.84778875 -2.4754644
  [4,]  0.06710229  1.01950917 -0.09325091 -0.03222811  0.4127816
  [5,] -0.13619141  1.33143821 -0.79958805  2.08274102  0.6901768
  [6,] -0.45060357  0.19348831 -1.23793647 -0.72440163  0.5057326
  [7,] -1.20740516  0.20231086  1.15584485  0.8170 -1.2719855
  [8,] -1.81166284 -0.07913113 -0.91080581 -0.34774436  0.9552182
  [9,]  0.19131383  0.14980569 -0.37458224 -0.09371273 -1.7667203
 [10,] -0.85159276 -0.66679528  1.63019340  0.56920196 -2.4049600

 And I define a boundary of X:  The smallest ball that nests all the
 observations of X. I wish to check if a particular point x_i

 x_i - matrix(rnorm(5), 1, 5)
 x_i
           [,1]      [,2]       [,3]      [,4]      [,5]
 [1,] -0.1525543 0.4606419 -0.1011011 -1.557225 -1.035694

 is inside the boundary of X or not. I know it's easy to do it with 1-D or
 2-D, but I don't knot how to manage it when the dimension is large.

 Can someone give a hint? Thanks in advance!


 Feng

 --
 Feng Li
 Department of Statistics
 Stockholm University
 106 91 Stockholm, Sweden
 http://feng.li/

        [[alternative HTML version deleted]]

 __
 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.


__
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] boundary check

2010-09-24 Thread baptiste auguie
Hi,

I remember a discussion we had on this list a few months ago for a
better way to decide if a point is inside a convex hull. It eventually
lead to a R function in this post,

http://tolstoy.newcastle.edu.au/R/e8/help/09/12/8784.html

I don't know if it was included in the geometry package in the end.

HTH,

baptiste


On 24 September 2010 12:44, Michael Bedward michael.bedw...@gmail.com wrote:
 Hello,

 If an N-dimensional convex hull fits your idea of a smallest ball
 then you could try the convhulln function in the geometry package.

 For testing if a new point is inside a previously derived hull, one
 brute force approach is to rbind the new point to your data, generate
 a new hull and see if it is the same as the previous one.

 I've only used convhulln in low dimensions so I don't know how
 efficient it is when N is large.

 Hope this helps.
 Michael


 On 24 September 2010 19:44, Feng Li feng...@stat.su.se wrote:
 Dear R,

 I have a covariates matrix with 10 observations,  e.g.

 X - matrix(rnorm(50), 10, 5)
 X
             [,1]        [,2]        [,3]        [,4]       [,5]
  [1,]  0.24857135  0.30880745 -1.44118657  1.10229027  1.0526010
  [2,]  1.24316806  0.36275370 -0.40096866 -0.24387888 -1.5324384
  [3,] -0.33504014  0.42996246  0.03902479 -0.84778875 -2.4754644
  [4,]  0.06710229  1.01950917 -0.09325091 -0.03222811  0.4127816
  [5,] -0.13619141  1.33143821 -0.79958805  2.08274102  0.6901768
  [6,] -0.45060357  0.19348831 -1.23793647 -0.72440163  0.5057326
  [7,] -1.20740516  0.20231086  1.15584485  0.8170 -1.2719855
  [8,] -1.81166284 -0.07913113 -0.91080581 -0.34774436  0.9552182
  [9,]  0.19131383  0.14980569 -0.37458224 -0.09371273 -1.7667203
 [10,] -0.85159276 -0.66679528  1.63019340  0.56920196 -2.4049600

 And I define a boundary of X:  The smallest ball that nests all the
 observations of X. I wish to check if a particular point x_i

 x_i - matrix(rnorm(5), 1, 5)
 x_i
           [,1]      [,2]       [,3]      [,4]      [,5]
 [1,] -0.1525543 0.4606419 -0.1011011 -1.557225 -1.035694

 is inside the boundary of X or not. I know it's easy to do it with 1-D or
 2-D, but I don't knot how to manage it when the dimension is large.

 Can someone give a hint? Thanks in advance!


 Feng

 --
 Feng Li
 Department of Statistics
 Stockholm University
 106 91 Stockholm, Sweden
 http://feng.li/

        [[alternative HTML version deleted]]

 __
 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.


 __
 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.


__
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] boundary check

2010-09-24 Thread Feng Li
Thanks. I agree with you that the speed and memory issues might be
(actually is) a big problem for big dimensions. It is interesting to
know to solve this by using linear programming. Buy the way, it seems
a potential bug in your function if you try this


 X - matrix(rnorm(50), 10, 5)
 x_i-X[1,,drop=FALSE]
 isin.chull(x_i,X)
Error in A.out[, basic] - iden(M) : subscript out of bounds

Or I must be wrong somewhere.


Feng


On Sep 24, 12:39 pm, Robin Hankin rk...@cam.ac.uk wrote:
 Hello

 convex hulls in large numbers of dimensions are hard.

 For your problem, though, one can tell whether a given
 point is inside or outside by using linear programming:

  X - matrix(rnorm(50), 10, 5)
  x_i - matrix(rnorm(5), 1, 5)
  isin.chull

 function(candidate,p,plot=FALSE,give.answers=FALSE,
   ...){
   if(plot){
     plot(p,...)
     p(candidate[1],candidate[2], pch=16)
   }
   n - nrow(p) # number of points
   d - ncol(p) # number of dimensions

   p - t(sweep(p,2,candidate))
   jj - simplex(a=rep(1,n),A3=rbind(p,1),b3=c(0*candidate,1))
   if(give.answers){
     return(jj)
   }  else {
     return((jj$solved = 0)  all(jj$soln1))
   }

 }
  isin.chull(x_i,X)
 [1] FALSE

 (we can discuss offline; I'll summarize)

 HTH

 rksh

 On 24/09/10 10:44, Feng Li wrote:



  Dear R,

  I have a covariates matrix with 10 observations,  e.g.

  X - matrix(rnorm(50), 10, 5)
  X

               [,1]        [,2]        [,3]        [,4]       [,5]
   [1,]  0.24857135  0.30880745 -1.44118657  1.10229027  1.0526010
   [2,]  1.24316806  0.36275370 -0.40096866 -0.24387888 -1.5324384
   [3,] -0.33504014  0.42996246  0.03902479 -0.84778875 -2.4754644
   [4,]  0.06710229  1.01950917 -0.09325091 -0.03222811  0.4127816
   [5,] -0.13619141  1.33143821 -0.79958805  2.08274102  0.6901768
   [6,] -0.45060357  0.19348831 -1.23793647 -0.72440163  0.5057326
   [7,] -1.20740516  0.20231086  1.15584485  0.8170 -1.2719855
   [8,] -1.81166284 -0.07913113 -0.91080581 -0.34774436  0.9552182
   [9,]  0.19131383  0.14980569 -0.37458224 -0.09371273 -1.7667203
  [10,] -0.85159276 -0.66679528  1.63019340  0.56920196 -2.4049600

  And I define a boundary of X:  The smallest ball that nests all the
  observations of X. I wish to check if a particular point x_i

  x_i - matrix(rnorm(5), 1, 5)
  x_i

             [,1]      [,2]       [,3]      [,4]      [,5]
  [1,] -0.1525543 0.4606419 -0.1011011 -1.557225 -1.035694

  is inside the boundary of X or not. I know it's easy to do it with 1-D or
  2-D, but I don't knot how to manage it when the dimension is large.

  Can someone give a hint? Thanks in advance!

  Feng

 --
 Robin K. S. Hankin
 Uncertainty Analyst
 University of Cambridge
 19 Silver Street
 Cambridge CB3 9EP
 01223-764877

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

__
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] boundary check

2010-09-24 Thread Keith Jewell
Hi,

Below Baptiste's message I attach  the R code and the .Rd documentation I 
treated as 'final', it may be slightly different from that in the Dec 2009 
post.

I did submit if for inclusion in the geometry package, but last time I 
checked it wasn't there.

I have found (and others have reported via private e-mail) that high 
dimensional data sets cause crashes (R exits with no warnings or messages). 
I tentatively believe this is a 'bug' in convhulln, but tracking it takes me 
to a complicated package and compiled code. It isn't a problem for me, so I 
can't make time to follow it up.

hth.

Keith J

baptiste auguie baptiste.aug...@googlemail.com wrote in message 
news:aanlktikf3+higwyhwyxeu2brwqs0x9mtywz9xzyq_...@mail.gmail.com...
Hi,

I remember a discussion we had on this list a few months ago for a
better way to decide if a point is inside a convex hull. It eventually
lead to a R function in this post,

http://tolstoy.newcastle.edu.au/R/e8/help/09/12/8784.html

I don't know if it was included in the geometry package in the end.

HTH,

baptiste
-
inhull - function(testpts, calpts, hull=convhulln(calpts), 
tol=mean(mean(abs(calpts)))*sqrt(.Machine$double.eps)) {
#
# R implementation of the Matlab code by John D'Errico 04 Mar 2006 (Updated 
30 Oct 2006)
# downloaded from 
http://www.mathworks.com/matlabcentral/fileexchange/10226-inhull
# with some modifications and simplifications
#
# Efficient test for points inside a convex hull in n dimensions
#
#% arguments: (input)
#%  testpts - nxp array to test, n data points, in p dimensions
#%   If you have many points to test, it is most efficient to
#%   call this function once with the entire set.
#%
#%  calpts - mxp array of vertices of the convex hull, as used by
#%   convhulln.
#%
#%  hull - (OPTIONAL) tessellation (or triangulation) generated by convhulln
#%   If hull is left empty or not supplied, then it will be
#%   generated.
#%
#%  tol - (OPTIONAL) tolerance on the tests for inclusion in the
#%   convex hull. You can think of tol as the distance a point
#%   may possibly lie outside the hull, and still be perceived
#%   as on the surface of the hull. Because of numerical slop
#%   nothing can ever be done exactly here. I might guess a
#%   semi-intelligent value of tol to be
#%
#% tol = 1.e-13*mean(abs(calpts(:)))
#%
#%   In higher dimensions, the numerical issues of floating
#%   point arithmetic will probably suggest a larger value
#%   of tol.
#%
# In this R implementation default 
tol=mean(mean(abs(calpts)))*sqrt(.Machine$double.eps)
#
# VALUE: Matlab returns a vector of TRUE (inside/on) or FALSE (outside)
#   This R implementation returns an integer vector of length n
#   1 = inside hull
#  -1 = inside hull
#   0 = on hull (to precision indicated by tol)
#
# ensure arguments are matrices (not data frames) and get sizes
   calpts - as.matrix(calpts)
   testpts - as.matrix(testpts)
   p - ncol(calpts)   # columns in calpts
   cx - nrow(testpts)  # rows in testpts
   nt - nrow(hull)# number of simplexes in hull
# find normal vectors to each simplex
   nrmls - matrix(NA, nt, p) # predefine each nrml as NA, 
degenerate
   degenflag - matrix(TRUE, nt, 1)
   for (i in  1:nt) {
nullsp - t(Null(t(calpts[hull[i,-1],] - 
matrix(calpts[hull[i,1],],p-1,p, byrow=TRUE
if (dim(nullsp)[1] == 1) { nrmls[i,] - nullsp
   degenflag[i] - FALSE}}
# Warn of degenerate faces, and remove corresponding normals
   if(sum(degenflag)  0) warning(sum(degenflag), degenerate faces in 
convex hull)
   nrmls - nrmls[!degenflag,]
   nt - nrow(nrmls)
# find center point in hull, and any (1st) point in the plane of each 
simplex
   center = colMeans(calpts)
   a - calpts[hull[!degenflag,1],]
# scale normal vectors to unit length and ensure pointing inwards
   nrmls - nrmls/matrix(apply(nrmls, 1, function(x) sqrt(sum(x^2))), nt, p)
   dp - sign(apply((matrix(center, nt, p, byrow=TRUE)-a) * nrmls, 1, sum))
   nrmls - nrmls*matrix(dp, nt, p)
# if  min across all faces of dot((x - a),nrml) is
#  +ve then x is inside hull
#  0   then x is on hull
#  -ve then x is outside hull
# Instead of dot((x - a),nrml)  use dot(x,nrml) - dot(a, nrml)
   aN - diag(a %*% t(nrmls))
   val - apply(testpts %*% t(nrmls) - matrix(aN, cx, nt, byrow=TRUE), 
1,min)
# code  values inside 'tol' to zero, return sign as integer
   val[abs(val)  tol] - 0
   as.integer(sign(val))
}

\name{inhull}
\alias{inhull}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{
Test if one set of N-D points is inside the convex hull defined by another 
set
}
\description{
R implementation of the Matlab code by John D'Errico 04 Mar 2006 (Updated 30 
Oct 2006)
downloaded from 

Re: [R] boundary check

2010-09-24 Thread Robin Hankin
Feng

thanks for this.  The problem you report is
reproducible; it originates from simplex()
of the boot packge.

It is ultimately due to
the fact that x_i is precisely *on* the convex hull,
which is evidently causing problems.  I'll
investigate it.

In the short term, you can break the degeneracy:

 isin.chull(X[1,]+1e-10,X)
[1] FALSE


but I don't know if that is acceptable to you.

best

rksh



On 24/09/10 13:17, Feng Li wrote:
 Thanks. I agree with you that the speed and memory issues might be
 (actually is) a big problem for big dimensions. It is interesting to
 know to solve this by using linear programming. Buy the way, it seems
 a potential bug in your function if you try this


   
 X - matrix(rnorm(50), 10, 5)
 x_i-X[1,,drop=FALSE]
 isin.chull(x_i,X)
 
 Error in A.out[, basic] - iden(M) : subscript out of bounds

 Or I must be wrong somewhere.


 Feng


 On Sep 24, 12:39 pm, Robin Hankin rk...@cam.ac.uk wrote:
   
 Hello

 convex hulls in large numbers of dimensions are hard.

 For your problem, though, one can tell whether a given
 point is inside or outside by using linear programming:

 
 X - matrix(rnorm(50), 10, 5)
 x_i - matrix(rnorm(5), 1, 5)
 isin.chull
   
 function(candidate,p,plot=FALSE,give.answers=FALSE,
   ...){
   if(plot){
 plot(p,...)
 p(candidate[1],candidate[2], pch=16)
   }
   n - nrow(p) # number of points
   d - ncol(p) # number of dimensions

   p - t(sweep(p,2,candidate))
   jj - simplex(a=rep(1,n),A3=rbind(p,1),b3=c(0*candidate,1))
   if(give.answers){
 return(jj)
   }  else {
 return((jj$solved = 0)  all(jj$soln1))
   }

 }
 
 isin.chull(x_i,X)
   
 [1] FALSE

 (we can discuss offline; I'll summarize)

 HTH

 rksh

 On 24/09/10 10:44, Feng Li wrote:



 
 Dear R,
   
 
 I have a covariates matrix with 10 observations,  e.g.
   
 
 X - matrix(rnorm(50), 10, 5)
 X
 
 
  [,1][,2][,3][,4]   [,5]
  [1,]  0.24857135  0.30880745 -1.44118657  1.10229027  1.0526010
  [2,]  1.24316806  0.36275370 -0.40096866 -0.24387888 -1.5324384
  [3,] -0.33504014  0.42996246  0.03902479 -0.84778875 -2.4754644
  [4,]  0.06710229  1.01950917 -0.09325091 -0.03222811  0.4127816
  [5,] -0.13619141  1.33143821 -0.79958805  2.08274102  0.6901768
  [6,] -0.45060357  0.19348831 -1.23793647 -0.72440163  0.5057326
  [7,] -1.20740516  0.20231086  1.15584485  0.8170 -1.2719855
  [8,] -1.81166284 -0.07913113 -0.91080581 -0.34774436  0.9552182
  [9,]  0.19131383  0.14980569 -0.37458224 -0.09371273 -1.7667203
 [10,] -0.85159276 -0.66679528  1.63019340  0.56920196 -2.4049600
   
 
 And I define a boundary of X:  The smallest ball that nests all the
 observations of X. I wish to check if a particular point x_i
   
 
 x_i - matrix(rnorm(5), 1, 5)
 x_i
 
 
[,1]  [,2]   [,3]  [,4]  [,5]
 [1,] -0.1525543 0.4606419 -0.1011011 -1.557225 -1.035694
   
 
 is inside the boundary of X or not. I know it's easy to do it with 1-D or
 2-D, but I don't knot how to manage it when the dimension is large.
   
 
 Can someone give a hint? Thanks in advance!
   
 
 Feng
   
 --
 Robin K. S. Hankin
 Uncertainty Analyst
 University of Cambridge
 19 Silver Street
 Cambridge CB3 9EP
 01223-764877

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


-- 
Robin K. S. Hankin
Uncertainty Analyst
University of Cambridge
19 Silver Street
Cambridge CB3 9EP
01223-764877

__
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] boundary check

2010-09-24 Thread Greg Snow
You did not originally define ball, the other respondents have discussed 
using a convex hull, but here is another approach:

Use ball to mean sphere (or technically hypersphere) and find the sphere with 
the smallest radius that contains all the points, optim or other optimizers 
could be programmed to do this (or an approximation that may be good enough is 
to use the means as the center and the distance to the furthest point as the 
radius).

Then finding if a new point is within the sphere is just a matter of computing 
the Euclidean distance from the new point to the center and comparing that to 
the radius.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
 project.org] On Behalf Of Feng Li
 Sent: Friday, September 24, 2010 3:44 AM
 To: r-help@r-project.org
 Subject: [R] boundary check
 
 Dear R,
 
 I have a covariates matrix with 10 observations,  e.g.
 
  X - matrix(rnorm(50), 10, 5)
  X
  [,1][,2][,3][,4]   [,5]
  [1,]  0.24857135  0.30880745 -1.44118657  1.10229027  1.0526010
  [2,]  1.24316806  0.36275370 -0.40096866 -0.24387888 -1.5324384
  [3,] -0.33504014  0.42996246  0.03902479 -0.84778875 -2.4754644
  [4,]  0.06710229  1.01950917 -0.09325091 -0.03222811  0.4127816
  [5,] -0.13619141  1.33143821 -0.79958805  2.08274102  0.6901768
  [6,] -0.45060357  0.19348831 -1.23793647 -0.72440163  0.5057326
  [7,] -1.20740516  0.20231086  1.15584485  0.8170 -1.2719855
  [8,] -1.81166284 -0.07913113 -0.91080581 -0.34774436  0.9552182
  [9,]  0.19131383  0.14980569 -0.37458224 -0.09371273 -1.7667203
 [10,] -0.85159276 -0.66679528  1.63019340  0.56920196 -2.4049600
 
 And I define a boundary of X:  The smallest ball that nests all the
 observations of X. I wish to check if a particular point x_i
 
  x_i - matrix(rnorm(5), 1, 5)
  x_i
[,1]  [,2]   [,3]  [,4]  [,5]
 [1,] -0.1525543 0.4606419 -0.1011011 -1.557225 -1.035694
 
 is inside the boundary of X or not. I know it's easy to do it with 1-D
 or
 2-D, but I don't knot how to manage it when the dimension is large.
 
 Can someone give a hint? Thanks in advance!
 
 
 Feng
 
 --
 Feng Li
 Department of Statistics
 Stockholm University
 106 91 Stockholm, Sweden
 http://feng.li/
 
   [[alternative HTML version deleted]]
 
 __
 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.

__
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.