-
Notifications
You must be signed in to change notification settings - Fork 1
/
AllRmdScripts.R
182 lines (146 loc) · 4.34 KB
/
AllRmdScripts.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#RScript - please enter your code here !
#Generate a point pattern object
library (spatstat)
# some arbitrary coordinates in [0,1]
x <- runif(20)
y <- runif(20)
# the following are equivalent
X <- ppp(x, y, c(0,1), c(0,1))
X <- ppp(x, y)
X <- ppp(x, y, window=owin(c(0,1),c(0,1)))
# specify that the coordinates are given in metres
X <- ppp(x, y, c(0,1), c(0,1), unitname=c("metre","metres"))
plot(X)
#Generate an observation (sample region) window
w <- owin(c(0,1), c(0,1))
# the unit square
w <- owin(c(10,20), c(10,30), unitname=c("foot","feet"))
# a rectangle of dimensions 10 x 20 feet
# with lower left corner at (10,10)
plot(w)
# polygon (diamond shape)
w <- owin(poly=list(x=c(0.5,1,0.5,0),y=c(0,1,2,1)))
w <- owin(c(0,1), c(0,2), poly=list(x=c(0.5,1,0.5,0),y=c(0,1,2,1)))
# polygon with hole
ho <- owin(poly=list(list(x=c(0,1,1,0), y=c(0,0,1,1)),
list(x=c(0.6,0.4,0.4,0.6), y=c(0.2,0.2,0.4,0.4))))
plot(ho)
#Create and plot example dataset
data(swedishpines)
X<-swedishpines
plot(X)
#Nearest neighbour
summary(X)
nearest<-nndist(X)
summary(nearest)
#Finds the nearest neighbour of each point in a point pattern and plot.
data(cells)
m <- nnwhich(cells)
m2 <- nnwhich(cells, k=2)
#Plot nearest neighbour links
b <- cells[m]
plot(cells)
arrows(cells$x, cells$y, b$x, b$y, angle=15, length=0.15, col="red")
#Density plot
plot(density(swedishpines, 10))
#Contour plot
contour(density(X,10), axes=FALSE)
#From a particle analysis
library(spatstat)
imageSizeX<-256
imageSizeY<-254
#Plot is visualized in ImageJ (Java) coordinates!
X<- ppp(Particles$X, Particles$Y, c(0,imageSizeX), c(0,imageSizeY))
plot(x = 1, y = 1,xlim=c(0,imageSizeX),ylim=c(imageSizeY,0), type = "n", main = "blobs.gif", asp = 1, axes = F, xlab = "x", ylab = "y")
plot(X,axes=TRUE,xlim=c(1,imageSizeX),ylim=c(imageSizeY,1),add = T)
axis(1)
axis(2, las = 2)
#Plot as overlay
imMat<-t(imageMatrix)
im<-as.im(imMat, owin(xrange=c(0,imageSizeX), yrange=c(0,imageSizeY)))
#Plot is visualized in ImageJ (Java) coordinates!
plot(x = 1, y = 1,xlim=c(0,imageSizeX),ylim=c(imageSizeY,0), type = "n", main = "", asp = 1, axes = F, xlab = "x", ylab = "y")
plot.im(im,add=T)
plot(X,xlim=c(0,imageSizeX),ylim=c(imageSizeY,0),add = T)
#Create window from spatialPolygons object
library(maptools)
library(spatstat)
polWin<-as(spatialPolygons, "owin")
plot(polWin)
#Create ppp object with window and points
X<- ppp(Particles$X, Particles$Y,window=polWin)
plot(X)
# marks
m <- sample(1:2, 20, replace=TRUE)
m <- factor(m, levels=1:2)
X <- ppp(x, y, c(0,1), c(0,1), marks=m)
plot(X)
#Create marks from SpatialPointsDataframe
library(maptools)
library(spatstat)
spatialPointsDF<-as(spatialPointsDataFrame, "ppp")
plot(spatialPointsDF)
summary(spatialPointsDF)
#Covariates
data(bei)
slope <- bei.extra$grad
par(mfrow = c(1, 2))
plot(bei)
plot(slope)
#Quadrat count:
data(bei)
Z <- bei.extra$grad
b <- quantile(Z, probs = (0:4)/4)
Zcut <- cut(Z, breaks = b, labels = 1:4)
V <- tess(image = Zcut)
plot(V)
plot(bei, add = TRUE, pch = "+")
#Tesselated:
qb <- quadratcount(bei, tess = V)
plot(qb)
#Estimate of intensity
plot(rhohat(bei, slope))
#Quadrat count
Q <- quadratcount(X, nx = 4, ny = 3)
Q
plot(X)
plot(Q, add = TRUE, cex = 2)
#Ripley’s K function
K <- Kest(X)
plot(K)
#L function
L <- Lest(X)
plot(L, main = "L function")
#Envelope
ENV <- envelope(Y = swedishpines, fun = Kest, nsim = 40)
plot(ENV)
ENVL <- envelope(Y = swedishpines, fun = Lest, nsim = 40)
plot(ENVL)
#Line patterns
linePattern <- psp(runif(10), runif(10), runif(10), runif(10), window=owin())
plot(linePattern)
#Dirichlet Tessalation
X <- runifpoint(42)
plot(dirichlet(X))
plot(X, add=TRUE)
#Point patterns in 3D
threeDPpp <- pp3(runif(10), runif(10), runif(10), box3(c(0,1)))
plot(threeDPpp)
#Nearest neighbour measurements in 3D
nndist(threeDPpp)
#Ripleys K in 3D
threeDPppKest <- K3est(threeDPpp)
plot(threeDPppKest)
#Point pattern example on a Linear Network
data(simplenet)
Xlin <- rpoislpp(5, simplenet)
plot(Xlin)
linearK(Xlin)
linearK(Xlin, correction="none")
#Fit a point process model to an observed point pattern.
# fit the stationary Poisson process
# to point pattern 'nztrees'
data(nztrees)
fitted<-ppm(nztrees)
modelFitted<-rmh(fitted)
plot(modelFitted, main="Fitted Model")