如何创建一个包含点组的超帧,每个点都与 R 中的唯一 window 相关联
How to create a hyperframe containing groups of points with marks each associated with a unique window in R
尽管进行了在线搜索并咨询了 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
如何在超帧中包含所有图?
是的,您需要将数据排列在超帧中进行分析。这个超帧的每一行都将包含一个实验单元的所有数据——也就是说,超帧的每一行都将包含一个点模式,包括它的边界多边形。
但是,在您发布的第一个显示框中(标题 "As an example here's how I imagine the hyperframe"),每一行都包含一个点的数据。那不是你想要的。此框中的数据可以表示为 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
将 "marks" 附加到这些模式)。从此列表中,您可以构建超帧,例如
H <- hyperframe(X=plist)
但您需要在超框架中添加更多列以适应有趣的模型。
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)
尽管进行了在线搜索并咨询了 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
如何在超帧中包含所有图?
是的,您需要将数据排列在超帧中进行分析。这个超帧的每一行都将包含一个实验单元的所有数据——也就是说,超帧的每一行都将包含一个点模式,包括它的边界多边形。
但是,在您发布的第一个显示框中(标题 "As an example here's how I imagine the hyperframe"),每一行都包含一个点的数据。那不是你想要的。此框中的数据可以表示为 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
将 "marks" 附加到这些模式)。从此列表中,您可以构建超帧,例如
H <- hyperframe(X=plist)
但您需要在超框架中添加更多列以适应有趣的模型。
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)