Reputation: 15
I'm having difficulty converting point patterns to a hyperframes despite on-line searches and consulting Baddeley and Rubak's Spatial Point Patterns: Methodology and Applications with R. I'm new to R and spatial stats. Any help would be much appreciated! My situation: I have a point shapefile and a polygon shapefile from GIS. The point shapefile contains x y coordinates along with many grouping variables, covariates, and response variables. The polygon shapefile contains plot coordinates where the points are located within, and includes a Plot ID column.
I need to characterize and analyze point patterns based on several factors, both within each plot and between plots. Note: the plot is the experimental unit. Based on readings, I've concluded a hyperframe is the most user-friendly method for analysis. As an example here's how I imagine the hyperframe:
PlotID Point# X Coord Y Coord Color Size Sex Weight Growth
A 1 514514.5 3372057 Red Small Female 10 0.5
A 2 514484.2 3372062 Red Medium Male 14 0.6
A 3 514517.8 3372017 Red Large Female 12 0.6
B 1 524514.5 3372065 Blue Small Male 14 0.4
B 2 524484.2 3372067 Blue Small Male 16 0.3
B 3 524517.8 3372063 Blue Large Male 10 0.35
C 1 504514.5 3372041 Red Medium Female 10 0.7
C 2 504484.2 3372042 Red Large Female 12 0.4
C 3 504517.8 3372038 Red Small Male 16 0.6
D 1 504517.8 3372038 Blue Small Male 10 0.7
D 2 504517.8 3372038 Blue Medium Female 12 0.3
D 3 504517.8 3372038 Blue Small Male 16 0.6
The above hyperframe might be used to group point patterns by color to analyse differences in point patterns.
I successfully converted a simplified version of the shapefiles to a hyperframe by subsetting a single plot with its associated points. Here's the code:
library(sp)
library(spatstat)
library(shapefiles)
library(maptools)
library(rgdal)
x <- readShapeSpatial("Points_subset.shp") #creates a spatial points
#dataframe
x.data <- slot(x,"data") #columns of the data frame used as marks
p <- readShapeSpatial("Plot_subset") #creates spatial polygons df.
w <- as(as(p,"SpatialPolygons"),"owin") #assign the plot boundary as the
#window of the point pattern
y <- as(x, "SpatialPoints") #Assign point coordinates as spatial points
z <- as(y, "ppp") #Convert to class "ppp"
z <- z[w] #Assign the plot boundary as the window of the ppp
marks(z) <- x.data #Attach the data.frame of variables to the ppp.
plot(z) #Correctly produces 1 plot containing all points
However, when I apply the same process with multiple plots using a loop the hyperframe only includes information from a single plot. Here's code for multiple plots:
xm <- readShapeSpatial("Points_All.shp")
xm.data <- slot(xm,"data")
xn <- levels(unique(xm$PlotID)) #identify all plots
pm <- readShapeSpatial("Plots_All.shp")
for(i in 1:length(xn)) {
pm2 <- subset(pm, pm$PlotID == xn[i])
wm2 <- as(as(pm2,"SpatialPolygons"),"owin")#list of polygon windows
xm2 <- subset(xm, xm$PlotID == xn[i])
xm2.data <- subset(xm.data, xm.data$PlotID == xn[i])
ym <- as(xm2, "SpatialPoints")
zm2 <- as.ppp(coordinates(ym),wm2)
marks(zm2) <- xm2.data
unitname(zm) <- c("metre","metres")
plot(zm2, main=paste(xn[i])) #plots each plot's points with correct
#window
}
Investigate zm2
str(zm2) # Although all plots print above, "str" shows only the first
#plot
View(zm2)#Contains only the points of the first plot
Convert to a hyperframe
zm2.hyp <- as.hyperframe(zm2)
str(zm2.hyp) #as above, contains a row for each point of the first plot.
#hyperframe should include points for all plots
How do I include all plots in the hyperframe?
Upvotes: 1
Views: 496
Reputation: 15
Adrian Baddeley's answer led me in the right direction, but my dataframe had to be reorganized before the code worked. Solution:
#load points shapefile
xm <- readShapeSpatial("Points_All.shp")
#coerce spatialpointdataframe to dataframe
xm.df <- as.data.frame(xm)
#reorder df so X and Y data are the first columns as required for mapply
xm.df.d <- xm.df[,c(5,6,25,1:4,7:24)]
#Remove plotlevel data except plotID. Only point level data remains
xm.df.d <- xm.df.d[,-c(4,6,7,9,10)]
#Create list of dataframes with all point data based on Plotnam.
xm.df.l <- split(xm.df.d, f=xm.df.d$plotID)
#select plot level data from df. Combine plot level to the hyperframe
#later
plot.df <- xm.df[,c(3,6,9,10)]
plot.df <- unique(plot.df)
#check df length same as hyperframe length
nrow(plot.df)
#load plot polygons shapefile. Use as windows
pm <- readShapeSpatial("Plots_All")
#list of plots based on plotID
pm.l <- split(pm, pm$plotID)
#Coerce plots to owin type objects
pm.l.win <- lapply(pm.l, as.owin)
#A Baddeley, E Rubak, R Turner - 2015 pg 55-56 details the mapply procedure
#Note: procedure will not work unless X and Y coord data are the first 2
#columns of the df.
zml <- mapply(as.ppp, X = xm.df.l, W = pm.l.win,SIMPLIFY=FALSE)
H <- hyperframe(X=zml)
#combine the point pattern of the hyperframe with plot level data
#produces a hyperframe of ppp for each plot, with columns of plot level data
#such as Vegetation Type for each plot. Data for each point, such as tree
#height and species, are stored within the $marks
Final.hyp <- cbind.hyperframe(H,plot.df)
Upvotes: 0
Reputation: 1984
Yes, you will need to arrange the data in a hyperframe for analysis. Each row of this hyperframe will contain all the data for one experimental unit -- that is, each row of the hyperframe will contain a point pattern including its boundary polygon.
However, in the first displayed box in your posting (headed "As an example here's how I imagine the hyperframe") each row contains the data for a single point. That's not what you want. The data in this box can be represented as a data.frame
and your first task is to separate this into groups containing the data for each point pattern.
Suppose you have set up a data.frame
containing all the data sketched in that first displayed box. Call it df
. First we split this data frame into several data frames according to the PlotID
variable:
dflist <- split(df, PlotID)
The result is a list, each element of which is a data frame, withdf[[i]]
containing the coordinate data for the i
th point pattern.
Next you want to match these data frames with the corresponding boundary polygons. Suppose you've collected
the boundary polygons as a list blist
where blist[[i]]
is the i
th polygon. To match up the coordinates and the boundaries,
plist <- mapply(as.ppp, X=dflist, W=blist, SIMPLIFY=FALSE)
The result should be a list of point patterns (all the other variables such as Sex
, Colour
will be "marks" attached to these patterns). From this list, you can build your hyperframe, e.g
H <- hyperframe(X=plist)
but you'll need more columns in the hyperframe to fit interesting models.
Upvotes: 1