尽管进行了在线搜索并咨询了Baddeley和Rubak的《空间点模式:R的方法和应用》,但我仍难以将点模式转换为超帧。我对R和空间统计不熟悉。任何帮助将非常感激! 我的情况: 我有一个来自GIS的点shapefile和一个多边形shapefile。点shapefile包含x y坐标以及许多分组变量,协变量和响应变量。 多边形shapefile包含点所在的绘图坐标,并包含“绘图ID”列。
我需要基于每个绘图内以及绘图间的几个因素来表征和分析点模式。注意:该图是实验单位。基于阅读,我得出结论,超帧是最用户友好的分析方法。 例如,这是我如何想象超帧:
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
以上超帧可能用于按颜色对点模式进行分组,以分析点模式中的差异。
我通过将单个图及其相关点设置为子图,成功地将shapefile的简化版本转换为超帧。这是代码:
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
但是,当我使用循环对多个绘图应用相同的过程时,超帧仅包含来自单个绘图的信息。这是多个绘图的代码:
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
}
调查zm2
str(zm2) # Although all plots print above, "str" shows only the first
#plot
View(zm2)#Contains only the points of the first plot
转换为超帧
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
如何在超帧中包含所有图?
答案 0 :(得分:0)
是的,您需要将数据安排在超帧中进行分析。该超帧的每一行将包含一个实验单位的所有数据,也就是说,超帧的每一行将包含一个点图案,包括其边界多边形。
但是,在您的帖子的第一个显示框中(标题为“作为示例,这里是我想象的超帧”),每一行都包含单个点的数据。那不是你想要的。此框中的数据可以表示为data.frame
,而您的首要任务是将其分为包含每个点模式的数据的组。
假设您已经建立了一个data.frame
,其中包含在第一个显示的框中绘制的所有数据。称为df
。首先,我们根据PlotID
变量将该数据帧分为几个数据帧:
dflist <- split(df, PlotID)
结果是一个列表,其每个元素是一个数据框,其中df[[i]]
包含第i
个点图案的坐标数据。
接下来,您要将这些数据框与相应的边界多边形匹配。假设您已经收集
边界多边形作为列表blist
,其中blist[[i]]
是第i
个多边形。为了匹配坐标和边界,
plist <- mapply(as.ppp, X=dflist, W=blist, SIMPLIFY=FALSE)
结果应为点模式列表(所有其他变量,例如Sex
,Colour
将是附加到这些模式的“标记”)。从此列表中,您可以构建超帧,例如
H <- hyperframe(X=plist)
但是您将需要在超帧中添加更多列以适合有趣的模型。
答案 1 :(得分:0)
Adrian Baddeley的答案将我引向了正确的方向,但是在代码生效之前,我的数据框必须重新组织。 解决方案:
#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)