A bit new to R and I'm working on something a bit more challenging than I am used to- so here's whats going on: Data inputs: 9 different shapefiles (.shp) of different point locations (lat, long) shapefile bounding box (lat/long corner points (14, 1) (15,1) (14, 2) (15,2)) 1 .csv of lat/long coordinates for points Goal: a- I want to randomly drop x number of points in a study area within the bounding box b- Then I want to add the .csv points to the mix c- Measure the distances from all these points to each of the 9 shapefiles locations, one at a time and save the results in a table or list d- run an regression analysis on the table or list created (I have this code all set to go) e- I want to save the outputs of the model in a new table (I have this code also) f- Run this whole process again x number of times and save x number of outputs in the table in order of creation Any help on steps a, b,c, or f would be appreciated M. -- View this message in context: http://r.789695.n4.nabble.com/Randomized-Points-in-space-saving-model-results-tp3922612p3922612.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt
2011-Oct-20 21:56 UTC
[R] Randomized Points in space/ saving model results
Not doing much work with spacial stats or shapefiles, I can't help in too much detail, but here are some R commands that might help for each part: a. # This will help you pick random points within your bounded box runif2d <- function(n, xmin, xmax, ymin, ymax){ stopifnot(all(xmax > xmin, ymax > min)) y <- runif(n, ymin, ymax) x <- runif(n, xmin, xmax) cbind(x,y) } b. take the output of runif2d (which gives a nx2 matrix) and cbind() the csv points on as well c. getdist <- function(inpoints, refpoints) { # Takes in two 2 column matrices representing (x,y) coordinates and returns a matrix with all the distance pairs between them stopifnot(all(is.matrix(inpoints), is.matrix(refpoints), dim(inpoints)[2L] == 2L, dim(refpoints)[2L] == 2L)) d <- matrix(nrow = nrow(inpoints), ncol = nrow(refpoints)) for (i in seq_along(refpoints)) { d[, i] <- rowSums(inpoints-refpoints[i,]^2) } d } f. Wrap everything in a function and use replicate() Do these help? Michael On Thu, Oct 20, 2011 at 1:32 PM, magono <nroyal01 at gmail.com> wrote:> A bit new to R and I'm working on something a bit more challenging than I am > used to- so here's whats going on: > > Data inputs: ?9 different shapefiles (.shp) of different point locations > (lat, long) > ? ? ? ? ? ? ? ? ? shapefile bounding box ?(lat/long corner points (14, 1) > (15,1) (14, 2) (15,2)) > ? ? ? ? ? ? ? ? ? 1 .csv of lat/long coordinates for points > > > > Goal: ?a- I want to randomly drop x number of points in a study area within > the bounding box > ? ? ? ? b- Then I want to add the .csv points to the mix > ? ? ? ? c- Measure the distances from all these points to each of the 9 > shapefiles locations, one at a time and save the results in a table or list > ? ? ? ? d- run an regression analysis on the table or list created (I have > this code all set to go) > ? ? ? ? e- I want to save the outputs of the model in a new table ?(I have > this code also) > ? ? ? ? f- ?Run this whole process again x number of times and save x > number of outputs in the table in order of creation > > Any help on steps a, b,c, or f would be appreciated > > M. > > > -- > View this message in context: http://r.789695.n4.nabble.com/Randomized-Points-in-space-saving-model-results-tp3922612p3922612.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
On 21/10/11 06:32, magono wrote:> A bit new to R and I'm working on something a bit more challenging than I am > used to- so here's whats going on: > > Data inputs: 9 different shapefiles (.shp) of different point locations > (lat, long) > shapefile bounding box (lat/long corner points (14, 1) > (15,1) (14, 2) (15,2)) > 1 .csv of lat/long coordinates for points > > > > Goal: a- I want to randomly drop x number of points in a study area within > the bounding box > b- Then I want to add the .csv points to the mix > c- Measure the distances from all these points to each of the 9 > shapefiles locations, one at a time and save the results in a table or list > d- run an regression analysis on the table or list created (I have > this code all set to go) > e- I want to save the outputs of the model in a new table (I have > this code also) > f- Run this whole process again x number of times and save x > number of outputs in the table in order of creation > > Any help on steps a, b,c, or f would be appreciated.I believe that the tools available in the "spatstat" package would be of use to you. Read the vignette "Handling shapefiles in the spatstat package" to find out how to convert shapefile objects to objects of classes with which "spatstat" can deal. cheers, Rolf Turner
Thank you both for your help. If I have any further questions after I give this a go I'll get back to you. This was much appreciated. M. -- View this message in context: http://r.789695.n4.nabble.com/Randomized-Points-in-space-saving-model-results-tp3922612p3954834.html Sent from the R help mailing list archive at Nabble.com.
Ok, to michael Looking over what you posted I'm thinking this is the way forward but my lack of experience using functions is getting in the way of accomplishing the task. Examining the outputs into the functions and putting in my own info is giving me a headache. Here's my thoughts at each step marked with ``````- runif2d <- function(n = 100, xmin = 1, xmax = 2, ymin = 14, ymax = 15){ ```````put in my bounding box coordinates above stopifnot(all(xmax > xmin, ymax > min)) y <- runif(n, ymin, ymax) x <- runif(n, xmin, xmax) ````````shall I enter the bounding box info again above in the y= and x= ? cbind(x,y) } `````Say I want to examine the resulting points created, how can I go about this? getdist <- function(inpoints, refpoints) { ``````` which of these is my .csv of predetermined coordinates to add to the mix and which of these points to the generated points above? # Takes in two 2 column matrices representing (x,y) coordinates # and returns a matrix with all the distance pairs between them stopifnot(all(is.matrix(inpoints), is.matrix(refpoints), dim(inpoints)[2L] == 2L, dim(refpoints)[2L] == 2L)) d <- matrix(nrow = nrow(inpoints), ncol = nrow(refpoints)) for (i in seq_along(refpoints)) { d[, i] <- rowSums(inpoints-refpoints[i,]^2) } d } `````Again, I want to see the matrix created above with all the points and distances, how do I call that up (I know this must be simple, but I think since I can't get the whole shabang to work, I'm having trouble finding the right command to look at the results.) Thanks so much by the way, you have an enviable command of R. -M. -- View this message in context: http://r.789695.n4.nabble.com/Randomized-Points-in-space-saving-model-results-tp3922612p3954873.html Sent from the R help mailing list archive at Nabble.com.