6

I am a R-Beginner, but I want to use it for some spatial statistics. I want to check the relation between the occurrence of ill persons and the existence of water in a area of interest. To give you a more specific use case, I've created some example datasets:

Firstly I want to plot them in a map including a basemap. This already works!

persons_dataframe = read.csv("persons.csv", header = TRUE)
city_dataframe = read.csv("city.csv", header = TRUE)
water_dataframe = read.csv("water.csv", header = TRUE)
# plot them on a map
# load used packages
library(RgoogleMaps)
library(ggplot2)
library(ggmap)
library(sp)

persons_ggplot2 <- persons_dataframe
city_ggplot2 <- city_dataframe
water_ggplot2 <- water_dataframe
gc <- geocode('new york, usa')
center <- as.numeric(gc)  
G <- ggmap(get_googlemap(center = center, color = 'bw', scale = 1, zoom = 10, maptype = "terrain", frame=T), extent="device")
G1 <- G + geom_point(aes(x=POINT_X, y=POINT_Y ),data=city_dataframe, shape = 22, color="black", fill = "yellow", size = 4) + geom_point(aes(x=POINT_X, y=POINT_Y ),data=persons_dataframe, shape = 8, color="red", size=2.5) + geom_point(aes(x=POINT_X, y=POINT_Y ),data=water_ggplot2, color="blue", size=1)
plot(G1)

This has the following output: R Plot

No I want to create something like a cluster map. I've already looked for some useful packages and I've ended with the packages SpatialEpi and DCluser. Both provide some functions, which might be useful.

But at this point I don't know, how to proceed. Some open questions:

  1. Is it right that I need polygons to cluster the dataset? I've read it in the SpatialEpi pdf but I only have these point datasets.
  2. Additionally I don't know, if the fact that all points are located on only three locations might be a problem?!

Do you have a suggestions how to continue with my work?

  • Is there anyone, who has an idea? – schlomm Mar 3 '14 at 20:03
  • which point layer do you want to do clustering (person, city,..)? – Farid Cheraghi Oct 1 '15 at 16:43
1

Here is solution with mclust (model based clustering). To cluster the Persons table into two seperate clusters.

R Script

require(mclust)
require(sp)

data =read.csv(file = "person.txt")

data.xy <- cbind(data$POINT_X, data$POINT_Y) 

patternBIC <- mclustBIC(data.xy) 

patternModel <- summary(patternBIC, data.xy)
print(patternModel)

n <- patternModel$G
cond_probs <- lapply(1:n, function(i) patternModel$z[,i])
names(cond_probs) <- paste0("cond_prob", 1:n)

q <- quantile(patternModel$uncertainty, probs = c(0.75, 0.95))
q7595 <- sapply(patternModel$uncertainty, function(u){
  r <- if (u <= q[1]) 1 else 0
  if(u <= q[2] && r == 0) r <- 2
  if(u > q[2] && r == 0) r <- 3
  stopifnot(r != 0)
  r - 1
})

result <- data.frame(data,
          cluster = patternModel$classification,
          class_uncert = patternModel$uncertainty,
          sym_ucert=q7595,
          cond_probs)

par("mfrow"=c(1,1))
plot(result$POINT_X,result$POINT_Y,col=result$cluster)

persons.txt looks like:

city,ill,computer,POINT_X,POINT_Y
City A,yes,0,-73.996786,40.720813
City A,yes,0,-73.996786,40.720813
City A,yes,0,-73.996786,40.720813
City A,yes,0,-73.996786,40.720813
City A,yes,0,-73.996786,40.720813
City A,yes,0,-73.996786,40.720813
City A,yes,1,-73.996786,40.720813
City A,yes,0,-73.996786,40.720813
City A,yes,0,-73.996786,40.720813
City A,yes,0,-73.996786,40.720813
City A,yes,0,-73.996786,40.720813
City A,yes,0,-73.996786,40.720813
City A,yes,1,-73.996786,40.720813
City A,yes,0,-73.996786,40.720813
City A,yes,0,-73.996786,40.720813
City A,yes,0,-73.996786,40.720813
City A,yes,0,-73.996786,40.720813
City B,yes,0,-74.172237,40.732196
City B,yes,0,-74.172237,40.732196
City B,yes,0,-74.172237,40.732196
City B,yes,0,-74.172237,40.732196
City B,yes,0,-74.172237,40.732196
City B,yes,1,-74.172237,40.732196
City B,yes,0,-74.172237,40.732196
City B,yes,0,-74.172237,40.732196
City B,yes,0,-74.172237,40.732196
City C,yes,0,-74.197274,40.625130
City C,yes,0,-74.197274,40.625130
City C,yes,0,-74.197274,40.625130
City C,yes,0,-74.197274,40.625130
City C,yes,0,-74.197274,40.625130
City C,yes,0,-74.197274,40.625130
City C,yes,0,-74.197274,40.625130
City C,yes,0,-74.197274,40.625130
City C,yes,0,-74.197274,40.625130
City C,yes,0,-74.197274,40.625130
City C,yes,0,-74.197274,40.625130
City C,yes,0,-74.197274,40.625130
City C,yes,0,-74.197274,40.625130
City C,yes,0,-74.197274,40.625130
City C,yes,0,-74.197274,40.625130
City C,yes,0,-74.197274,40.625130
City C,yes,0,-74.197274,40.625130

Make sure both files are in the same directory and you set the working directory to that of your R script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.