R Spatstat

0 views
Skip to first unread message

Madox Valdivia

unread,
Aug 4, 2024, 6:26:39 PM8/4/24
to apasarad
Ifyou are new to spatstat take a look at the resourcespage for places to get started. For help take a lookat the help page and check out the FAQ pageto see if somebody else has had a similar problem.

The spatstat package (Baddeley, Turner, and Rubak 2022) can be used for statistical analysis of spatial point patterns.In spatstat, spatial point patterns are represented with objects of class ppp that contain the event locations with possibly associated marks, and the observation window where the events occur.Here, we show how to use spatstat to create ppp objects representing spatial point patterns, and how to transform ppp objects to sf objects to be able to work with the data using packages such as sf.


In spatstat, spatial point patterns are represented as objects of class ppp (planar point pattern). To create a ppp object, we use the ppp() function passing the vectors x and y with the event coordinates, and the observation window which is of class owin.


For example, here we create a spatial point pattern of 100 randomly generated points in the region \([0, 1] \times [0, 2]\).First, we use the owin() function to create an object of class owin with the observation window \([0, 1] \times [0, 2]\) passing the ranges of the horizontal and vertical axes.


An alternative way of simulating a spatial point pattern of independent uniform random points in a given region is by using the runifpoint() function.The arguments of runifpoint() include the number n of points and the window of class owin where the point pattern is simulated. For example, the previous pattern could have been generated using X


Marks denoting associated information of events can be set with marks() or %mark%. For example, Figure 18.2 shows the previous point pattern X where we add a mark with a numeric value to each of the events.


Note that the definition of an observation window that represents the study region of the point pattern needs to be carefully specified, as it affects the visualization and analysis of the data, and possibly the conclusions obtained.For example, if the previous point pattern was thought to be observed in the window \([0, 5] \times [0, 5]\), the data would appear as a cluster in the bottom left corner of the window instead of randomly in \([0, 1] \times [0, 2]\).Note that this situation, depicted in Figure 18.3, would change the interpretation of the data.


The inside.owin() function can be used to test whether a set of points lie within a particular observation window.For example, we can identify the points in the point pattern X that are inside the unit square by passing the points and the observation window as follows:


In some situations, we may be interested in transforming an object of class ppp to an object of class sf to be able to manipulate and visualize the data using other packages such as sf.Here, we show how to use the st_as_sf() function from sf to transformthe longleaf data from sptatstat which contains the locations and sizes of longleaf pine trees, from ppp to sf class.


First, we create a data frame containing the coordinates and the marks of the ppp object.Then, we create a sf object by using the st_as_sf() function of sf passing the data frame and specifying the name of the columns that contain the event coordinates in argument coords.The window of the point pattern can also be converted to sf with st_as_sf(Window(X)).


We can also convert a sf object to a ppp object with as.ppp() by providing the point coordinates and the observation window.For example, here we obtain the coordinates of the sf object in matrix form with st_coordinates(), and consider the observation window as the bounding box of the data which can be obtained with st_bbox().Marks of the points can be set with marks() or %mark%.Figure 18.5 shows the plot of the ppp object obtained.


In case we wish to use a polygon of class sf as observation window,we can use as.owin() to transform the sf object to an owin object. Note that the sf object needs to be in a projected coordinate reference system.Here, we show an example on how to create a spatial point pattern with the boundary of Brazil as observation window.First, we obtain the Brazil map with the rnaturalearth package.Then, we use st_transform() to transform the map to projection EPSG 29172 (UTM zone 22N).Finally, we use runifpoint() to generate 100 independent uniform random points within the observation window (Figure 18.5).


new error messageI have updated the package spatstat, which is divided into various packages. All of them are updated except one, spatstat.data. I have tried install.packages("spatstat.data") and update.packages("spatstat.data")


Defines types of spatial data on a linear network and provides functionality for geometrical operations, data analysis and modelling of data on a linear network, in the 'spatstat' family of packages. Contains definitions and support for linear networks, including creation of networks, geometrical measurements, topological connectivity, geometrical operations such as inserting and deleting vertices, intersecting a network with another object, and interactive editing of networks. Data types defined on a network include point patterns, pixel images, functions, and tessellations. Exploratory methods include kernel estimation of intensity on a network, K-functions and pair correlation functions on a network, simulation envelopes, nearest neighbour distance and empty space distance, relative risk estimation with cross-validated bandwidth selection. Formal hypothesis tests of random pattern (chi-squared, Kolmogorov-Smirnov, Monte Carlo, Diggle-Cressie-Loosmore-Ford, Dao-Genton, two-stage Monte Carlo) and tests for covariate effects (Cox-Berman-Waller-Lawson, Kolmogorov-Smirnov, ANOVA) are also supported.Parametric models can be fitted to point pattern data using the function lppm() similar to glm(). Only Poisson models are implemented so far. Models may involve dependence on covariates and dependence on marks. Models are fitted by maximum likelihood.Fitted point process models can be simulated, automatically. Formal hypothesis tests of a fitted model are supported (likelihood ratio test, analysis of deviance, Monte Carlo tests) along with basic tools for model selection (stepwise(), AIC()) and variable selection (sdr). Tools for validating the fitted model include simulation envelopes, residuals, residual plots and Q-Q plots, leverage and influence diagnostics, partial residuals, and added variable plots.Random point patterns on a network can be generated using a variety of models.


Apart from two-dimensional point patterns and point processes,spatstat also supports point patterns in three dimensions, point patterns in multidimensional space-time,point patterns on a linear network,patterns of line segments in two dimensions, and spatial tessellations and random sets in two dimensions.


The models may include spatial trend,dependence on covariates, and complicated interpoint interactions.Models are specified by a formula in the R language, and are fitted usinga function analogous to lm and glm.Fitted models can be printed, plotted, predicted, simulated and so on.


The spatstat package includes over 50 datasets,which can be useful when learning the package.Type demo(data) to see plots of all datasetsavailable in the package.Type vignette('datasets') for detailed background informationon these datasets, and plots of each dataset.


For information on converting your data into spatstat format,read Chapter 3 of Baddeley, Rubak and Turner (2015).This chapter is available free online, as one of the sample chaptersat the book companion website,


The breakup has been done in such a way that the user should not noticeany difference. Source code that worked with the old spatstatpackage should work with the new spatstat family.Code that is documented in our books, journal articles and vignettesshould still work.


When you install spatstat, the sub-packages listed above are alsoinstalled. Then if you load the spatstat package by typinglibrary(spatstat), the other sub-packages listed above willautomatically be loaded or imported.


See the Vignette Summary of recent updates,installed with spatstat, which describes the main changesto spatstat since the book (Baddeley, Rubak and Turner, 2015)was published. It is accessible as vignette('updates').


A colour map is a mechanism for associating colours with data.It can be regarded as a function, mapping data to colours.Using a colourmap object in a plot commandensures that the mapping from numbers to colours isthe same in different plots.


Summary statistics for a marked point pattern:A marked point pattern is represented by an object Xof class "ppp" with a component X$marks.The entries in the vector X$marks may be numeric, complex,string or any other atomic type. For numeric marks, there are thefollowing functions:


Cluster process models (with homogeneous or inhomogeneous intensity)and Cox processes can be fitted by the function kppm.Its result is an object of class "kppm".The fitted model can be printed, plotted, predicted, simulatedand updated.


Poisson point processes are the simplest models for point patterns.A Poisson model assumes that the points are stochasticallyindependent. It may allow the points to have a non-uniform spatialdensity. The special case of a Poisson process with a uniformspatial density is often called Complete Spatial Randomness.


Poisson point processes are included in the more general class of Gibbs pointprocess models. In a Gibbs model, there is interactionor dependence between points. Many different types of interactioncan be specified.


Pixel-based spatial logistic regression is an alternativetechnique for analysing spatial point patternsthat is widely used in Geographical Information Systems.It is approximately equivalent to fitting a Poisson point processmodel.

3a8082e126
Reply all
Reply to author
Forward
0 new messages