Re: [R] Distance matrix for Spatial Filtering {spdep}

2014-11-09 Thread Roger Bivand
Erica Cseko Nolasco ecnolasco at gmail.com writes:

 
 Hi all,
 
 I´m working on a species distribution modeling, and I want to build
 eigenvectors to represent my spatial varuable. I´m trying to use
 SpatialFiltering(spdep) for it, but I´m having problems to create the nb
 object. My weights would be a truncated pairwise distance matrix that I was
 pretty able to build. However, I´m going out resources to create a nb
 object. Or it doesn´t match the matrix or the R-Studio crashes. Here is the
 code I´m trying:

You have not provided a reproducible example, it usually helps. Also always
run code that fails in R-Studio outside it, as this frees memory and aids
debugging (nothing between you and R). In addition, we know nothing of your
platform, I guess Windows (G: isn't common elsewhere). Your posting is HTML
which is discouraged.

 

  matriz3=earth.dist(coor1, dist=T)

This function takes a matrix of geographical coordinates, and returns a
triangular matrix of distances (n*(n-1)), hence the length:

 
  length(matriz3)
 
 [1] 12110581
 
  ###truncating the distance matrix
 
  matriz3[matriz3=230]=230
  ###creating the nb object to link to the distance matrix
 
  library(spdep)
 
 coord=read.table(coordinates16.txt,sep= , header=T)
 
  coo=cbind(coord[,1],coord[,2])
 
  length(coo[,1])
 [1] 4922
 
 nb=dnearneigh(coo,0, 1, longlat=T) ###large nb (4922 elements,92.9Mb)
 
  nb2listw(nb,glist=matriz3,style=W)
 
 *Error in nb2listw(nb, glist = matriz3, style = W) : glist wrong length*
 

The documentation of nb2listw says that the glist argument is a list, not a
triangular matrix. In addition, it is highly unlikely that you expect the
spatial autocorrelation to increase in distance. The examples for the
nb2listw function show how to construct truncated inverse distance weights
using km distances, here with a cutoff at 230km and using geographical
coordinates:

nb - dnearneigh(coo, 0, 230, longlat=TRUE)
dists - nbdists(nb, coo, longlat=TRUE)
glist - lapply(dists, function(x) 1/x)
lw - nb2listw(nb, glist, style=W)

The use of functions to create nb objects is described in vignette(nb) in
the spdep package.

...
 
 *R session aborted. R encoutered a fatal error. The session was terminated*
 

Such things have happened before with R-Studio as a front end when Windows
platforms have run out of memory, but do not repeat outside R-Studio
(reported but no action taken that I know of).

  nb=knearneigh(coo,500,longlat=T) ###tentativa de criar nb
 
 *Error in knearneigh(coo, 500, longlat = T) : too many ties in knearneigh*
 

Almost certainly your choice of k=500 is erroneous, and also suggests that
you are flailing around without a clear grasp of what you should be doing.

1) until the problem is resolved, drop R-Studio; once resolved outside
R-Studio, you may go back to using it, but beware of memory problems.

2) construct the list of weights object as shown (maybe modify the threshold
if too many observations have no neighbour)

3) think carefully about the use of SpatialFiltering - is your response
continuous? You have about 5000 observations, so you will be operation on
5000x5000 matrices in order to construct the Moran eigenvectors, and will be
doing a brute force search for combinations of these eigenvectors to add to
your model. Are you sure that the model you are starting from is
well-specified (included variables and their functional forms)? If not, you
risk including many eigenvectors that simply mop up other misspecifications.
This search will be very time-consuming.

Consider using the R-sig-geo list, which may be more appropriate for this
kind of question.

Roger

...
 
 Any ideas would help a lot! Thanks in advance
 
 *Erica Csekö Nolasco*

__
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] distance matrix from metaMDS

2014-08-29 Thread Cara Fiore
Yes I looked at this and I tried metaMDSdist but got an error and for some
reason I didn't try metaMDSredist which seems to be the right thing. So the
main thing I was confused about was what to call dist() on -i.e., getting
the correct ordinal distance, and then if I assume that the NMDS scores are
the coordinates, which I believe they are, then how do I call dist() on one
column? But, I just found the answer in a translation from matlab to R -
you have to use drop=FALSE (and hopefully I am calling dist() on the right
thing)

euc.dist.axis1=dist(NMDS2[,1, drop=FALSE], method=euclidean)


Maybe this is obvious to other folks but just in case there is anyone like
me out there I figured I'd write back. Thanks for the info I have never
written to this list before because I always found what I needed online. I
appreciate your help and patience.


Cara



On Thu, Aug 28, 2014 at 10:19 PM, David L Carlson dcarl...@tamu.edu wrote:

 Don't the functions metaMDSdist() and metaMDSredist() that are documented
 on the metaMDS manual page give you the distance matrix? If you want to
 compute the distances based on a single axis, you could use vegdist().

 David C

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
 On Behalf Of Cara Fiore
 Sent: Thursday, August 28, 2014 7:02 PM
 To: r-help@r-project.org
 Subject: [R] distance matrix from metaMDS

 Dear R users,

 I would like to access the distance matrix generated by metaMDS as well as
 use the dist function to calculate the euclidean distance for each axis in
 the NMDS. I am having trouble finding a way to access these variables and
 any help is greatly appreciated.

 For the distance matrix I know I could just calculate the bray-curtis
 distance but it would be nice to know how to get it from the NMDS function.
 For the euclidean distance, the only thing I can find within metaMDS is
 the score function but there must be some way for me to call on/access the
 ordination distance for one axis right?

 The reason for this is I would like to do something like the stressplot
 function but for each axis.

 Thank you,
 Cara

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


[[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] distance matrix from metaMDS

2014-08-28 Thread David L Carlson
Don't the functions metaMDSdist() and metaMDSredist() that are documented on 
the metaMDS manual page give you the distance matrix? If you want to compute 
the distances based on a single axis, you could use vegdist(). 

David C

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Cara Fiore
Sent: Thursday, August 28, 2014 7:02 PM
To: r-help@r-project.org
Subject: [R] distance matrix from metaMDS

Dear R users,

I would like to access the distance matrix generated by metaMDS as well as use 
the dist function to calculate the euclidean distance for each axis in the 
NMDS. I am having trouble finding a way to access these variables and any help 
is greatly appreciated.

For the distance matrix I know I could just calculate the bray-curtis distance 
but it would be nice to know how to get it from the NMDS function.
For the euclidean distance, the only thing I can find within metaMDS is the 
score function but there must be some way for me to call on/access the 
ordination distance for one axis right?

The reason for this is I would like to do something like the stressplot 
function but for each axis.

Thank you,
Cara

[[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] distance matrix and hclustering

2012-07-30 Thread Jean V Adams
Look at the examples for the functions dist() and hclust(),
?dist
?hclust

Jean


eliza botto eliza_bo...@hotmail.com wrote on 07/30/2012 01:43:55 AM:
 
 Dear R Users,i am very new to R. I want your help on an issue 
 regarding distance matrix and cluster analysis
 i had discharge data of 4 rivers(a,b,c,d) in 4 vectors each having 364 
values
  dput(qmu)structure(list(a = c(0.26, 0.25, 0.25, 0.25, 0.24, 0.23, 
 0.22, 0.21, 0.21, 0.21, 0.2, 0.19, 0.19, 0.19, 0.19, 0.18, 0.18, 0.
 18, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.
 17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.18, 0.19, 0.19, 0.19, 0.2, 0.21,
 0.21, 0.21, 0.21, 0.21, 0.21, 0.19, 0.19, 0.18, 0.17, 0.17, 0.15, 0.
 18, 0.2, 0.21, 0.2, 0.19, 0.19, 0.19, 0.2, 0.24, 0.3, 0.3, 0.3, 0.
 32, 0.34, 0.42, 0.46, 0.48, 0.67, 0.82, 0.79, 0.73, 0.69, 0.67, 0.
 67, 0.66, 0.64, 0.61, 0.58, 0.56, 0.55, 0.55, 0.55, 0.52, 0.49, 0.
 48, 0.51, 0.53, 0.52, 0.49, 0.48, 0.48, 0.46, 0.46, 0.44, 0.43, 0.
 43, 0.41, 0.48, 0.55, 0.57, 0.55, 0.56, 0.6, 0.64, 0.67, 0.73, 0.84,
 0.94, 1.09, 1.24, 1.28, 1.19, 1.11, 1, 0.92, 0.86, 0.79, 0.76, 0.76,
 0.76, 0.76, 0.92, 0.98, 1.03, 1.03, 1.03, 1.03, 1.07, 1.11, 1.24, 1.
 44, 2.12, 3.26, 15, 9.45, 5.07, 4.59, 3.5, 2.84, 2.54, 2.57, 3.01, 
 2.32, 2.32, 2.97, 2.92, 3.88, 4.76, 5.99, 3.74, 2.92, 2.65, 2.57, 2.
 97, 3.4, 4.13, 4.31, 3.89, 3.45, 3.01, 2.88, 2!
  .5, 2.29, 2.39, 2.25, 2.02, 1.87, 1.87, 2.54, 2.69, 2.76, 3.18, 3.
 74, 4.59, 4.76, 4.36, 6.56, 5.07, 3.84, 3.55, 3.84, 3.84, 5.49, 5.
 32, 3.74, 3.31, 3.4, 3.26, 3.09, 2.69, 2.54, 2.46, 2.39, 2.25, 2.22,
 2.22, 2.25, 2.29, 2.22, 2.18, 2.05, 2.18, 2.39, 2.18, 2.29, 2.11, 1.
 81, 1.6, 1.44, 1.41, 1.32, 1.37, 1.37, 1.65, 2.31, 2.25, 1.68, 1.41,
 1.26, 1.15, 3.28, 1.93, 1.6, 1.53, 1.28, 1.13, 1.03, 1.03, 1.03, 1.
 03, 1, 0.96, 0.92, 0.87, 0.82, 0.79, 0.76, 0.73, 0.7, 0.67, 0.64, 0.
 64, 0.61, 0.61, 0.61, 1.76, 1.19, 1.24, 1.37, 1.68, 2.39, 2.05, 1.
 78, 1.58, 1.41, 1.39, 1.5, 1.41, 1.32, 1.19, 1.11, 1.02, 1.07, 4.57,
 1.96, 1.68, 1.5, 1.37, 1.24, 1.11, 1.03, 0.96, 0.94, 2.93, 2.88, 2.
 92, 2.76, 2.02, 1.71, 1.5, 1.37, 1.22, 1.09, 1, 0.94, 0.87, 0.81, 0.
 76, 0.73, 0.7, 0.67, 0.61, 0.58, 0.57, 0.55, 0.53, 0.51, 0.48, 0.47,
 0.44, 0.43, 0.43, 0.41, 0.41, 0.38, 0.4, 0.4, 0.42, 0.42, 0.41, 0.
 46, 0.53, 0.55, 0.52, 0.49, 0.51, 0.53, 0.55, 0.7, 1.03, 1.03, 1.17,
 1.24, 1.19, 1.11, 1.03, 0.98, 0.92, 0.84,!
   0.79, 0.75, 0.7, 0.67, 0.61, 0.58, 0.56, 0.56, 0.55, 0.53, 0.51, 0.48
 , 0.46, 0.43, 0.41, 0.38, 0.37, 0.35, 0.34, 0.32, 0.31, 0.3, 0.29, 
 0.28, 0.27, 0.25, 0.26, 0.24, 0.23, 0.22, 0.22, 0.21, 0.21, 0.21), b
 = c(0.19, 0.19, 0.19, 0.18, 0.18, 0.18, 0.17, 0.17, 0.17, 0.17, 0.
 16, 0.17, 0.17, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.
 15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.14, 0.14, 0.
 14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.
 14, 0.14, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.16, 0.16, 0.
 17, 0.17, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.19, 0.
 21, 0.21, 0.21, 0.22, 0.23, 0.24, 0.24, 0.23, 0.24, 0.24, 0.25, 0.
 25, 0.25, 0.28, 0.29, 0.29, 0.3, 0.31, 0.31, 0.34, 0.41, 0.46, 0.51,
 0.57, 0.61, 0.64, 0.67, 0.7, 0.76, 0.82, 0.86, 1.05, 1.24, 1.05, 0.
 94, 0.92, 0.9, 0.86, 0.82, 0.76, 0.76, 0.76, 0.78, 0.82, 0.9, 1.07, 
 1.76, 3.13, 3.64, 3.45, 3.01, 2.39, 2.02, 1.87, 2.11, 2.02, 1.78, 1.
 63, 1.53, 1.63, 4.84, 12.5, 8.11, 3.89, 2.73, 2.11, 1.96, 3.17, 2.
 65, 2.54, 3.01, 3.31, 3.6, 3.36, 2.76, 2.39, 2.11, 2.!
  25, 2.08, 1.99, 2.11, 2.36, 3.13, 7.16, 5.39, 5.52, 5.32, 4.25, 3.
 45, 3.26, 3.18, 3.74, 4.35, 5.79, 5.45, 4.42, 3.84, 3.36, 2.84, 2.
 39, 3.84, 3.18, 3.22, 2.97, 2.73, 2.65, 2.92, 4.33, 3.01, 3.01, 3.
 26, 3.09, 3.6, 3.64, 4.05, 4.25, 4.48, 3.69, 3.74, 3.6, 3.18, 2.76, 
 4.11, 2.92, 2.69, 2.73, 2.69, 3.93, 2.69, 2.18, 2.52, 2.69, 1.99, 2.
 57, 1.81, 1.55, 1.44, 1.37, 1.28, 1.19, 1.19, 1.03, 1.03, 1, 0.94, 
 0.89, 0.87, 0.86, 0.86, 2.3, 1.55, 1.19, 1.11, 1.5, 1.39, 1.22, 1.
 24, 1.07, 1.02, 0.96, 0.92, 1.34, 1.15, 1.03, 2.06, 1.76, 1.3, 1.15,
 1.05, 0.98, 0.92, 0.89, 0.84, 0.81, 0.76, 0.73, 1.59, 5.2, 3.01, 2.
 05, 1.65, 1.68, 5.29, 2.73, 1.96, 1.6, 1.41, 1.28, 1.15, 1.11, 1.13,
 1.09, 1.03, 6.99, 10.6, 5.39, 3.45, 2.5, 1.87, 1.68, 1.78, 1.53, 1.
 41, 1.3, 1.17, 1.05, 0.98, 0.92, 0.9, 0.87, 0.86, 0.82, 0.78, 0.75, 
 0.72, 0.67, 0.82, 1.6, 0.89, 0.94, 0.96, 0.92, 0.87, 0.82, 0.79, 0.
 75, 0.7, 0.67, 0.64, 0.61, 0.58, 0.56, 0.53, 0.51, 0.48, 0.47, 0.46,
 0.43, 0.41, 0.41, 0.68, 16.3, 17.2, 6.05, 3.6!
  9, 2.92, 2.25, 1.87, 1.63, 1.46, 1.32, 1.19, 1.07, 1, 0.94, 0.89, 0.87
 , 0.86, 0.81, 0.76, 0.73, 0.7, 0.7, 0.7, 0.7, 0.7, 0.67, 0.67, 0.66,
 0.64, 0.61, 0.58, 0.56, 0.55, 0.53, 0.51, 0.48, 0.46, 0.44, 0.43, 0.
 43, 0.41, 0.4, 0.38, 0.37, 0.36, 0.35, 0.34, 0.33, 0.32, 0.31, 0.31,
 0.3, 0.3, 0.29, 0.29, 0.28, 0.27, 0.27), c = c(0.27, 0.25, 0.25, 0.
 25, 0.24, 0.24, 0.23, 0.22, 0.22, 0.21, 0.21, 0.21, 0.21, 0.21, 0.2,
 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.
 19, 0.19, 0.19, 0.18, 

Re: [R] distance matrix and hclustering

2012-07-30 Thread David L Carlson
For basic hierarchical clustering use

groups - hclust(dist(qmu))

But there are many options for function dist() and for function hclust() and
these will affect the results. For basic plotting use

plot(groups)

But as you will see, there are too many cases for the labels to be legible.
Hierarchical clustering gives you from 1 to ncases groups. You have to
decide where to cut the dendrogram to define a particular number of
clusters. Since you are clustering on a single value, you may find kmeans
clustering to be more useful You must specify the number of groups in
advance, but you can try several group sizes to see what makes the most
sense for your data.

--
David L Carlson
Associate Professor of Anthropology
Texas AM University
College Station, TX 77843-4352



 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of eliza botto
 Sent: Monday, July 30, 2012 1:44 AM
 To: r-help@r-project.org; smartpink...@yahoo.com
 Subject: [R] distance matrix and hclustering
 
 
 Dear R Users,i am very new to R. I want your help on an issue regarding
 distance matrix and cluster analysis
 i had discharge data of 4 rivers(a,b,c,d) in 4 vectors each having 364
 values
  dput(qmu)structure(list(a = c(0.26, 0.25, 0.25, 0.25, 0.24, 0.23,
 0.22, 0.21, 0.21, 0.21, 0.2, 0.19, 0.19, 0.19, 0.19, 0.18, 0.18, 0.18,
 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17,
 0.17, 0.17, 0.17, 0.17, 0.18, 0.19, 0.19, 0.19, 0.2, 0.21, 0.21, 0.21,
 0.21, 0.21, 0.21, 0.19, 0.19, 0.18, 0.17, 0.17, 0.15, 0.18, 0.2, 0.21,
 0.2, 0.19, 0.19, 0.19, 0.2, 0.24, 0.3, 0.3, 0.3, 0.32, 0.34, 0.42,
 0.46, 0.48, 0.67, 0.82, 0.79, 0.73, 0.69, 0.67, 0.67, 0.66, 0.64, 0.61,
 0.58, 0.56, 0.55, 0.55, 0.55, 0.52, 0.49, 0.48, 0.51, 0.53, 0.52, 0.49,
 0.48, 0.48, 0.46, 0.46, 0.44, 0.43, 0.43, 0.41, 0.48, 0.55, 0.57, 0.55,
 0.56, 0.6, 0.64, 0.67, 0.73, 0.84, 0.94, 1.09, 1.24, 1.28, 1.19, 1.11,
 1, 0.92, 0.86, 0.79, 0.76, 0.76, 0.76, 0.76, 0.92, 0.98, 1.03, 1.03,
 1.03, 1.03, 1.07, 1.11, 1.24, 1.44, 2.12, 3.26, 15, 9.45, 5.07, 4.59,
 3.5, 2.84, 2.54, 2.57, 3.01, 2.32, 2.32, 2.97, 2.92, 3.88, 4.76, 5.99,
 3.74, 2.92, 2.65, 2.57, 2.97, 3.4, 4.13, 4.31, 3.89, 3.45, 3.01, 2.88,
 2!
  .5, 2.29, 2.39, 2.25, 2.02, 1.87, 1.87, 2.54, 2.69, 2.76, 3.18, 3.74,
 4.59, 4.76, 4.36, 6.56, 5.07, 3.84, 3.55, 3.84, 3.84, 5.49, 5.32, 3.74,
 3.31, 3.4, 3.26, 3.09, 2.69, 2.54, 2.46, 2.39, 2.25, 2.22, 2.22, 2.25,
 2.29, 2.22, 2.18, 2.05, 2.18, 2.39, 2.18, 2.29, 2.11, 1.81, 1.6, 1.44,
 1.41, 1.32, 1.37, 1.37, 1.65, 2.31, 2.25, 1.68, 1.41, 1.26, 1.15, 3.28,
 1.93, 1.6, 1.53, 1.28, 1.13, 1.03, 1.03, 1.03, 1.03, 1, 0.96, 0.92,
 0.87, 0.82, 0.79, 0.76, 0.73, 0.7, 0.67, 0.64, 0.64, 0.61, 0.61, 0.61,
 1.76, 1.19, 1.24, 1.37, 1.68, 2.39, 2.05, 1.78, 1.58, 1.41, 1.39, 1.5,
 1.41, 1.32, 1.19, 1.11, 1.02, 1.07, 4.57, 1.96, 1.68, 1.5, 1.37, 1.24,
 1.11, 1.03, 0.96, 0.94, 2.93, 2.88, 2.92, 2.76, 2.02, 1.71, 1.5, 1.37,
 1.22, 1.09, 1, 0.94, 0.87, 0.81, 0.76, 0.73, 0.7, 0.67, 0.61, 0.58,
 0.57, 0.55, 0.53, 0.51, 0.48, 0.47, 0.44, 0.43, 0.43, 0.41, 0.41, 0.38,
 0.4, 0.4, 0.42, 0.42, 0.41, 0.46, 0.53, 0.55, 0.52, 0.49, 0.51, 0.53,
 0.55, 0.7, 1.03, 1.03, 1.17, 1.24, 1.19, 1.11, 1.03, 0.98, 0.92, 0.84,!
   0.79, 0.75, 0.7, 0.67, 0.61, 0.58, 0.56, 0.56, 0.55, 0.53, 0.51, 0.48
 , 0.46, 0.43, 0.41, 0.38, 0.37, 0.35, 0.34, 0.32, 0.31, 0.3, 0.29,
 0.28, 0.27, 0.25, 0.26, 0.24, 0.23, 0.22, 0.22, 0.21, 0.21, 0.21), b =
 c(0.19, 0.19, 0.19, 0.18, 0.18, 0.18, 0.17, 0.17, 0.17, 0.17, 0.16,
 0.17, 0.17, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15,
 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.14, 0.14, 0.14, 0.14, 0.14,
 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.15, 0.15,
 0.15, 0.15, 0.15, 0.15, 0.15, 0.16, 0.16, 0.17, 0.17, 0.18, 0.18, 0.18,
 0.18, 0.18, 0.18, 0.18, 0.18, 0.19, 0.21, 0.21, 0.21, 0.22, 0.23, 0.24,
 0.24, 0.23, 0.24, 0.24, 0.25, 0.25, 0.25, 0.28, 0.29, 0.29, 0.3, 0.31,
 0.31, 0.34, 0.41, 0.46, 0.51, 0.57, 0.61, 0.64, 0.67, 0.7, 0.76, 0.82,
 0.86, 1.05, 1.24, 1.05, 0.94, 0.92, 0.9, 0.86, 0.82, 0.76, 0.76, 0.76,
 0.78, 0.82, 0.9, 1.07, 1.76, 3.13, 3.64, 3.45, 3.01, 2.39, 2.02, 1.87,
 2.11, 2.02, 1.78, 1.63, 1.53, 1.63, 4.84, 12.5, 8.11, 3.89, 2.73, 2.11,
 1.96, 3.17, 2.65, 2.54, 3.01, 3.31, 3.6, 3.36, 2.76, 2.39, 2.11, 2.!
  25, 2.08, 1.99, 2.11, 2.36, 3.13, 7.16, 5.39, 5.52, 5.32, 4.25, 3.45,
 3.26, 3.18, 3.74, 4.35, 5.79, 5.45, 4.42, 3.84, 3.36, 2.84, 2.39, 3.84,
 3.18, 3.22, 2.97, 2.73, 2.65, 2.92, 4.33, 3.01, 3.01, 3.26, 3.09, 3.6,
 3.64, 4.05, 4.25, 4.48, 3.69, 3.74, 3.6, 3.18, 2.76, 4.11, 2.92, 2.69,
 2.73, 2.69, 3.93, 2.69, 2.18, 2.52, 2.69, 1.99, 2.57, 1.81, 1.55, 1.44,
 1.37, 1.28, 1.19, 1.19, 1.03, 1.03, 1, 0.94, 0.89, 0.87, 0.86, 0.86,
 2.3, 1.55, 1.19, 1.11, 1.5, 1.39, 1.22, 1.24, 1.07, 1.02, 0.96, 0.92,
 1.34, 1.15, 1.03, 2.06, 1.76, 1.3, 1.15, 1.05, 0.98, 0.92, 0.89, 0.84,
 0.81, 0.76, 0.73, 1.59, 5.2, 3.01, 2.05, 1.65, 1.68, 5.29, 

Re: [R] distance matrix

2011-05-05 Thread csrabak

Em 5/5/2011 07:34, antu escreveu:

Hello all,

I am wondering if there is anyway to create distance matrix for replicated
data

for example,

I have a data like

sample  pop id  var1var2var3var4
1.1 1   a   1   1   0   1
1.2 1   a   0   0   1   0
1.3 1   a   1   1   0   1
2.1 2   b   0   0   1   0
2.2 2   b   1   1   1   1
2.3 2   b   0   1   0   0
2.4 2   b   1   0   1   1
3.1 3   a   0   1   0   1
3.2 3   a   1   1   1   0
3.3 3   a   0   0   0   0


dist(data[,c(4:7)] gives the distance of samples, but I also need the
distances of pop ie (1,2,3) and also id (a,b) how can I achieve this??

Just a doubt: does the idea of comparing (ordering) the variable pop and 
id make sense? Or expressed in more direct way: what would a distance 
between b and a mean (same for the pop labels)?


__
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] distance matrix

2011-05-05 Thread antu
I don't know whether I understood your question, but 1.1, 1.2 , 1.3 all are
subsample of 1 , so, rather than comparing 1000 subsample, comparison of 20
pop level makes more sense in my case. 

thanks for query

-


Ananta Acharya
Graduate Student
--
View this message in context: 
http://r.789695.n4.nabble.com/distance-matrix-tp3498033p3498890.html
Sent from the R help mailing list archive at Nabble.com.

__
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] distance matrix?

2010-06-28 Thread John Ramey
x - 0:10
y - t(replicate(11, 0:10))
abs(sweep(y, 1, x))

Hope this helps.

On Mon, Jun 28, 2010 at 5:21 AM, clips10 m.mcquil...@lancaster.ac.uk wrote:

 I have a vector 0 to 10 and want to create a matrix with the differences
 between the numbers in it for instance:

        0   1   2   3   4   5   6   7   8   9   10

 0       0   1   2   3   4   5   6   7   8   9   10
 1      1   0   1   2   3   4   5   6   7    8    9
 2
 3
 4
 5
 6
 7
 8
 9
 10

 Etc etc. So that the matrix is filled with the differences between in
 absolute value so there are no negatives.

 Any ideas?

 Thanks

 --
 View this message in context: 
 http://r.789695.n4.nabble.com/distance-matrix-tp2270722p2270722.html
 Sent from the R help mailing list archive at Nabble.com.

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




-- 
John A. Ramey, M.S.
Ph.D. Candidate
Department of Statistics
Baylor University
http://www.ramhiser.com

__
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] distance matrix?

2010-06-28 Thread Kjetil Halvorsen
 abs(outer(1:10, 1:10, FUN=-))
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]012345678 9
 [2,]101234567 8
 [3,]210123456 7
 [4,]321012345 6
 [5,]432101234 5
 [6,]543210123 4
 [7,]654321012 3
 [8,]765432101 2
 [9,]876543210 1
[10,]987654321 0


Kjetil

On Mon, Jun 28, 2010 at 5:44 PM, John Ramey johnra...@gmail.com wrote:
 x - 0:10
 y - t(replicate(11, 0:10))
 abs(sweep(y, 1, x))

 Hope this helps.

 On Mon, Jun 28, 2010 at 5:21 AM, clips10 m.mcquil...@lancaster.ac.uk wrote:

 I have a vector 0 to 10 and want to create a matrix with the differences
 between the numbers in it for instance:

        0   1   2   3   4   5   6   7   8   9   10

 0       0   1   2   3   4   5   6   7   8   9   10
 1      1   0   1   2   3   4   5   6   7    8    9
 2
 3
 4
 5
 6
 7
 8
 9
 10

 Etc etc. So that the matrix is filled with the differences between in
 absolute value so there are no negatives.

 Any ideas?

 Thanks

 --
 View this message in context: 
 http://r.789695.n4.nabble.com/distance-matrix-tp2270722p2270722.html
 Sent from the R help mailing list archive at Nabble.com.

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




 --
 John A. Ramey, M.S.
 Ph.D. Candidate
 Department of Statistics
 Baylor University
 http://www.ramhiser.com

 __
 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] distance matrix as text file - how to import?

2008-04-09 Thread Hans-Joerg Bibiko
 On Tue, Apr 8, 2008 at 1:50 PM, Hans-Jörg Bibiko [EMAIL PROTECTED]  
 wrote:
 I was sent a text file containing a distance matrix à la:

 1
 2 3
 4 5 6

Thanks a lot for your hints.

At the end all hints ends up more or less in my stony way to do it.

Let me summarize it.

The clean way is to initialize a matrix containing my distance matrix  
and generate a dist object by using as.dist(mat).
Fine. But how to read the text data (triangular) into a matrix?

#1 approach - using 'read.table'

mat = read.table(test.txt, fill=T)

The problem here is that the first line doesn't contain the correct  
number of columns of my matrix, thus 'read.table' sets the number of  
columns to 5 as default.
Ergo I have to know the number of columns (num_cols) in beforehand in  
order to do this:

mat = read.table(test.txt, fill=T, col.names=rep('', num_cols))

Per definitionem the last line of test.txt contains the correct  
number of columns.
On a UNIX/Mac you can do the following:

num_cols - as.numeric(system(tail -n 1 'test.txt' | wc - 
w,intern=TRUE))

In other words, read the last line of 'test.txt' and count the number  
of words if the delimiter is a space. Or one could use 'readLines' and  
split the last array element to get num_cols.

#2 approach - using 'scan()'

mat = matrix(0, num_cols, num_cols)
mat[row(mat) = col(mat)] - scan(test.txt)

But this also leads to my problem:
1
2 4
3 5 6

instead of
1
2 3
4 5 6

 one solution 

The approach #2 has two advantages: it's faster than read.table AND I  
can calculate num_cols. The only problem is the correct order. But  
this is solvable via: reading the data into the upper triangle and  
transpose the matrix

mat - matrix(0, num_cols, num_cols)
mat[row(mat) = col(mat)] - scan(test.txt)
mat - t(mat)


Next. If I know that my text file really contains a distance matrix  
(i.e. the diagonals have been removed) then I can do the following:

data - scan(test.txt)
num_cols - (1 + sqrt(1 + 8*length(data)))/2 - 1
mat - matrix(0, num_cols, num_cols)
mat[row(mat) = col(mat)] - data
mat - t(mat)

#Finally to get a 'dist' object:

mat - rbind(0, mat)
mat - cbind(mat, 0)
dobj - as.dist(mat)


Again, thanks a lot!

--Hans

__
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] distance matrix as text file - how to import?

2008-04-08 Thread Paul Johnson
On Tue, Apr 8, 2008 at 1:50 PM, Hans-Jörg Bibiko [EMAIL PROTECTED] wrote:
 Dear all,

  I have -hopefully- a tiny problem.

  I was sent a text file containing a distance matrix à la:

  1
  2 3
  4 5 6



Try this! I put your test data in text.txt and voila:


mat - matrix(0, 3,3)

mat[row(mat) = col(mat)] - scan(test.txt)


I found this Idea after RSiteSearch(scan triangular) led to this
item as the very first link:

http://finzi.psych.upenn.edu/R/Rhelp02a/archive/22369.html


PJ


-- 
Paul E. Johnson
Professor, Political Science
1541 Lilac Lane, Room 504
University of Kansas

__
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] distance matrix

2008-01-20 Thread Erin Hodgess
Suppose you have:

 x - runif(25)
 y - rnorm(25)
 x.df - data.frame(x,y)
 dist(x.df)


The default is euclidean distance.

Hope this helps!

Sincerely,
Erin


On Jan 20, 2008 2:41 PM, WCD [EMAIL PROTECTED] wrote:

 Hello, I want do get the point to point distance matrix.
 Suppose, I have a data frame where the first column is X and second column
 is Y coordinate of points.
 I define a function dst(x1,y1,x2,y2) to claculate distance between two
 points. But I don't know how to tell R to calculate dst for every two points
 and create a matrix of these distances.

 Can you give me a little guidance, please? I am quite new to R and
 programming.
 Thanks for every advice. Filip Kral.

 --
 View this message in context: 
 http://www.nabble.com/distance-matrix-tp14986595p14986595.html
 Sent from the R help mailing list archive at Nabble.com.

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




-- 
Erin Hodgess
Associate Professor
Department of Computer and Mathematical Sciences
University of Houston - Downtown
mailto: [EMAIL PROTECTED]

__
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] Distance matrix in SpDep-package

2007-10-16 Thread Roger Bivand
Elke Moons elke.moons at uhasselt.be writes:

 
 Hello everybody,
 
 I would like to use the SpDep-package (especially the Local Moran index
 analysis and the Getis-Ord statistics) in R for analysing my data. However,
 I don't have x-y coordinates, but my data is in a distance matrix format. Is
 it possible to use the SpDep package with predefined distances as well
 instead of letting the program determining the distance itself by the
 function dnearneigh based on x-y coordinates. Or is there another package
 that can transform a distance matrix into fake x-y coordinates, so that
 the SpDep-package can work with it?

If you use the mat2listw() function in spdep on (slices of) your matrix, 
you can get to usable listw objects. If the distance matrix is full 
(that is all sites to all sites), you could impose very large values on
distances which fall outside a given slice, say outside 0-100m, to create a
listw object corresponding to a dnearneigh(xy, 0, 100) neighbours list. 
See the mat2listw help page for more details:

 The function converts a square spatial weights matrix to a weights
 list  object, optionally adding region IDs from the row names of
 the matrix, as a  sequence of numbers 1:nrow(x), or as given as an
 argument.


Note that the input matrix is expected to contain spatial weights, so 
inverse distances would be closer than distances in their native metric. 
You should also look at the glist= argument to nb2listw() if you want
generalised spatial weights.


Roger Bivand


 
 Thank you very much for your answer.
 
 Kind regards,
 
 Elke Moons
 
 __
 
 Elke Moons, PhD
 
 Transportation Research Institute/
 
 Instituut voor Mobiliteit (IMOB)
 
 Universiteit Hasselt
 
 Wetenschapspark 5, bus 6
 
 3590 Diepenbeek
 
 Belgium
 
 Tel. +32-11- 26.91.26
 
 Fax. +32-11-26.91.99
 
 E-mail:  mailto:elke.moons at uhasselt.be elke.moons at uhasselt.be
 
   [[alternative HTML version deleted]]
 
 __
 R-help at 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.