Материал готовится,

пожалуйста, возвращайтесь позднее

пожалуйста, возвращайтесь позднее

In Part II of the workshop, we look at the different types of spatial data in spatstat (point

patterns, windows, pixel images, etc).

We explain how to read data into the package and

manipulate these data types.

5

Objects, classes and methods in R

The tutorial examples above have used some of the ‘object-oriented’ features of R. It is very

useful to know a little about how these work.

5.1

Classes in R

R is an ‘object-oriented’ language. A dataset with some kind of structure on it (e.g. a contingency

table, a time series, a point pattern) is treated as a single ‘object’.

For example, R includes a dataset sunspots which is a time series containing monthly sunspot

counts from 1749 to 1983. This dataset can be manipulated as if it were a single object:

> plot(sunspots)

> summary(sunspots)

> X class(sunspots)

[1] "ts"

Standard operations, such as printing, plotting, or calculating the sample mean, are defined

separately for each class of object.

For example, typing plot(sunspots) invokes the generic command plot. Now sunspots is

an object of class "ts" representing a time series, and there is a special “method” for plotting

time series, called plot.ts. So the system executes plot.ts(sunspots). It is said that the plot

command is “dispatched” to the method plot.ts. The plot method for time series produces a

display that is sensible for time series, with axes properly annotated.

Tip:

to find out how to modify the plot for an object of class "foo", consult

help(plot.foo) rather than help(plot).

5.2

Classes in spatstat

To handle point pattern datasets and related data, the spatstat package defines the following

important classes of objects:

ppp: planar point pattern

owin: spatial region (‘observation window’)

im: pixel image

psp: pattern of line segments

tess: tessellation

(there are also other classes for specialised use, such as pp3 for three-dimensional point

patterns, ppx for multidimensional space-time point patterns, and hyperframe).

Most of the functionality in spatstat works on such objects. To use this functionality, you’ll

need to read your raw data into R and then convert it into an object of the appropriate format.

In particular spatstat has methods for plot, print and summary for each of these classes.

For example, the plot method for point patterns, plot.ppp, ensures that the x and y scales

are equal, and does various other things that are sensible when plotting a spatial point pattern

rather than just a list of (x,y) pairs

Exercise 1 Find out how to modify the command plot(swedishpines) so that the title reads

“Swedish Pines data” and the points are represented by plus-signs instead of circles.

When you type print(swedishpines) or just swedishpines, this invokes the generic com-

mand print, which dispatches to the method print.ppp, which prints some sensible information

about the point pattern swedishpines at the terminal.

> swedishpines

planar point pattern: 71 points

window: rectangle = [0, 96] x [0, 100] units (one unit = 0.1 metres)

The generic command summary is meant to provide basic summary statistics for a dataset.

When you type summary(swedishpines) this is dispatched to the method summary.ppp, which

computes a sensible set of summary statistics for a point pattern, and prints them at the terminal.

> summary(swedishpines)

Planar point pattern: 71 points

Average intensity 0.0074 points per square unit (one unit = 0.1 metres)

patterns, windows, pixel images, etc).

We explain how to read data into the package and

manipulate these data types.

5

Objects, classes and methods in R

The tutorial examples above have used some of the ‘object-oriented’ features of R. It is very

useful to know a little about how these work.

5.1

Classes in R

R is an ‘object-oriented’ language. A dataset with some kind of structure on it (e.g. a contingency

table, a time series, a point pattern) is treated as a single ‘object’.

For example, R includes a dataset sunspots which is a time series containing monthly sunspot

counts from 1749 to 1983. This dataset can be manipulated as if it were a single object:

> plot(sunspots)

> summary(sunspots)

> X class(sunspots)

[1] "ts"

Standard operations, such as printing, plotting, or calculating the sample mean, are defined

separately for each class of object.

For example, typing plot(sunspots) invokes the generic command plot. Now sunspots is

an object of class "ts" representing a time series, and there is a special “method” for plotting

time series, called plot.ts. So the system executes plot.ts(sunspots). It is said that the plot

command is “dispatched” to the method plot.ts. The plot method for time series produces a

display that is sensible for time series, with axes properly annotated.

Tip:

to find out how to modify the plot for an object of class "foo", consult

help(plot.foo) rather than help(plot).

5.2

Classes in spatstat

To handle point pattern datasets and related data, the spatstat package defines the following

important classes of objects:

ppp: planar point pattern

owin: spatial region (‘observation window’)

im: pixel image

psp: pattern of line segments

tess: tessellation

(there are also other classes for specialised use, such as pp3 for three-dimensional point

patterns, ppx for multidimensional space-time point patterns, and hyperframe).

Most of the functionality in spatstat works on such objects. To use this functionality, you’ll

need to read your raw data into R and then convert it into an object of the appropriate format.

In particular spatstat has methods for plot, print and summary for each of these classes.

For example, the plot method for point patterns, plot.ppp, ensures that the x and y scales

are equal, and does various other things that are sensible when plotting a spatial point pattern

rather than just a list of (x,y) pairs

Exercise 1 Find out how to modify the command plot(swedishpines) so that the title reads

“Swedish Pines data” and the points are represented by plus-signs instead of circles.

When you type print(swedishpines) or just swedishpines, this invokes the generic com-

mand print, which dispatches to the method print.ppp, which prints some sensible information

about the point pattern swedishpines at the terminal.

> swedishpines

planar point pattern: 71 points

window: rectangle = [0, 96] x [0, 100] units (one unit = 0.1 metres)

The generic command summary is meant to provide basic summary statistics for a dataset.

When you type summary(swedishpines) this is dispatched to the method summary.ppp, which

computes a sensible set of summary statistics for a point pattern, and prints them at the terminal.

> summary(swedishpines)

Planar point pattern: 71 points

Average intensity 0.0074 points per square unit (one unit = 0.1 metres)

Загрузка...

Выбрать следующее задание

Ты добавил

Выбрать следующее задание

Ты добавил