[R-sig-Geo] Defining neighbors base on same location

Tom Gottfried tom.gottfried at tum.de
Wed Apr 4 07:53:20 CEST 2012


Am 03.04.2012 17:43, schrieb Roosbeh Nowrouzian:
> Thanks for the hint. I tried the syntax but I am getting error:
> script:
> # reading data
> triplengthfile <- as.data.frame ( read.spss ( file =
> "triplengthfile.sav" ) )
> #Defining projection
> coordinates ( triplengthfile ) <- cbind ( triplengthfile $ longa,
> triplengthfile $ lata )
> coords <- cbind ( triplengthfile $ longa, triplengthfile $ lata )
> proj4string ( triplengthfile ) <- CRS ( "+proj=longlat +ellps=WGS84" )
>
> #projecting to Albers conical
> triplengthfile <- spTransform ( triplengthfile , CRS =
> CRS ( "+proj=aea +lat_1=24 +lat_2=31.5 +lat_0=24 +lon_0=-84 +x_0=400000
> +y_0=0 +ellps=GRS80 +units=m +no_defs" ) )
>
> #updating coords
> coords <- coordinates( triplengthfile )
> IDs <- rownames ( triplengthfile at data )
>
> #defining neighbors for Household members
> HHneighbor.knn <- mapply ( function (n,coord) knearneigh ( x=coord, k=n,
> longlat=FALSE, RANN=TRUE), HHNUM, lapply (1:nrow(coords),function(i)
> coords[i,] ))

HHneighbor.knn <- mapply ( function (n,coord) knearneigh ( x=coord, k=n, 
longlat=FALSE, RANN=TRUE), HHNUM, lapply(1:nrow(coords),function(i) 
coords[i,,drop=FALSE] ))

> which HHNUM is the number of HH members in matrix class (is defined for
> each observation(individual))
>
> But I am getting error:
> Error:
>
> Error in knearneigh(x = coord, k = n, longlat = FALSE, RANN = TRUE) :
> Data not in matrix form

The error message is quite unambiguous, but its cause might be 
confusing. "[" by default drops the dim-attribute, if a single row is 
selected from a matrix. See ?"[" and the above example how to overcome 
this (obviously it was, and still is untested).

Tom

> Appreciate you could help me solve this error.
> appreciate your help.
> Roosbeh Nowrouzian
> PhD student
> Department of Civil Engineering
> University of Florida
>
> On Wed, Mar 28, 2012 at 10:35 PM, Tom Gottfried <tom.gottfried at tum.de
> <mailto:tom.gottfried at tum.de>> wrote:
>
>     Hi,
>
>     Am 29.03.2012 01:08, schrieb Roosbeh Nowrouzian:
>
>         Hi list members:
>
>         Is there any way to define neighbors in R as any other observation
>         with distance to the corresponding point equal to 0.
>
>         I am trying to define neighbors base on the same location ( the same
>         Household members are linked to each other). I tried defining
>
>         weights base on fixed distance bounds:
>         neighbordistance.nb<- dnearneigh ( coords, d1=0, d2=0.00001,
>         row.names=IDs
>         )
>         But this function does not define other Household members
>         (distance=0) as
>         the neighbor.
>
>         When trying fixed number of neighbors:
>         neighbor3.knn<- knearneigh ( x=coords , k=3, longlat= FALSE,
>         RANN=TRUE )
>         This function defines other Household members(distance =0) as
>         neighbor but
>         the problem with this function is that each Household has different
>         Household number so different number of neighbors is required
>         for each
>         Household.
>
>
>     neighbor3.knn <-
>     mapply(function(n, coord) knearneigh(x=coord,
>     k=n, longlat=FALSE,RANN=TRUE),
>     ns, lapply(1:nrow(coords), function(i) coords[i,]))
>
>     Assuming you have a vector 'ns' with the number of household members
>     per household, this should give you a list with each element
>     defining the neighbours you desire. I hope I didn't misunderstand
>     your question.
>
>     HTH,
>     Tom
>
>         It would be kind of you letting me know if there is a way to define
>         neighbors base on zero distance with the corresponding point.
>         Appreciate it.
>
>         Roosbeh Nowrouzian
>         PhD student
>         Department of Civil Engineering
>         University of Florida
>
>         [[alternative HTML version deleted]]
>
>         _________________________________________________
>         R-sig-Geo mailing list
>         R-sig-Geo at r-project.org <mailto:R-sig-Geo at r-project.org>
>         https://stat.ethz.ch/mailman/__listinfo/r-sig-geo
>         <https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
>
>
>     --
>     Technische Universität München
>     Department für Pflanzenwissenschaften
>     Lehrstuhl für Grünlandlehre
>     Alte Akademie 12
>     85350 Freising / Germany
>     Phone: ++49 (0)8161 715324 <tel:%2B%2B49%20%280%298161%20715324>
>     Fax: ++49 (0)8161 713243 <tel:%2B%2B49%20%280%298161%20713243>
>     email: tom.gottfried at wzw.tum.de <mailto:tom.gottfried at wzw.tum.de>
>     http://www.wzw.tum.de/__gruenland <http://www.wzw.tum.de/gruenland>
>
>     _________________________________________________
>     R-sig-Geo mailing list
>     R-sig-Geo at r-project.org <mailto:R-sig-Geo at r-project.org>
>     https://stat.ethz.ch/mailman/__listinfo/r-sig-geo
>     <https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
>
>

-- 
Technische Universität München
Department für Pflanzenwissenschaften
Lehrstuhl für Grünlandlehre
Alte Akademie 12
85350 Freising / Germany
Phone: ++49 (0)8161 715324
Fax:   ++49 (0)8161 713243
email: tom.gottfried at wzw.tum.de
http://www.wzw.tum.de/gruenland



More information about the R-sig-Geo mailing list