neigh <- matrix(ncol=5,nrow=5,)
neigh
neigh <- matrix(ncol=5,nrow=5,
c(0,1,1,0,1,
  0,1,1,0,1,
  1,1,1,1,1,
  0,0,1,))
neigh <- matrix(ncol=5,nrow=5,
c(0,1,1,0,1,
  1,0,1,0,1,
  1,1,0,1,1,
  0,0,1,0,1,
  1,1,1,1,0))
neigh
neigh.w <-
for(i in 1:nrow(neigh)){
neigh.1[i,] <- neigh[i,] / sum(neigh[i,])}
neigh.w <-
for(i in 1:nrow(neigh)){
neigh.w[i,] <- neigh[i,] / sum(neigh[i,])}
}
neigh.w <- matrix(ncol=5,nrow=5,0)
neigh.w
for(i in 1:nrow(neigh)){
neigh.w[i,] <- neigh[i,] / sum(neigh[i,])}
neigh.w
x <- c(1,3,4,5,3)
y <- c(3,4,3,1,1)
id <- c(1,2,3,4,5)
pnt
pnts <- cbind(t(id),t(x),t(y))
pnts
rbind(id,x,y)
pnt.dat <-data.frame(rbind(id,x,y))
pnt.dat
pnt.dat <-data.frame(t(rbind(x,y)),row.names=id)
pnt.dat
pnt <-data.frame(t(rbind(x,y)),row.names=id)
pnt
pnt$x[1]
pnt$x[2]
dist <- matrix(nrow=nrow(pnt),ncol=nrow(pnt),0)#
for(i in 1:nrow(pnt)){#
for(j in 1:nrow(pnt)){#
dist[i,j] <- sqrt((pnt$x[i]-pnt$x[j])^2+(pnt$y[i]-pnt$y[j])^2)
}}
dist
dist.w <- dist * neigh
dist.w
for(i in 1:nrow(neigh)){#
dist.w[i,] <- neigh[i,] / sum(neigh[i,])}
dist.w
neigh.rook <- matrix(ncol=nrow=9,c(#
0,1,0,1,0,0,0,0,0,#
1,0,1,0,1,0,0,0,0,#
0,1,0,0,0,1,0,0,0,#
1,0,0,0,1,0,1,0,0,#
0,1,0,1,0,1,0,1,0,#
0,0,1,0,1,0,0,0,1,#
0,0,0,1,0,0,0,1,0,#
0,0,0,0,1,0,1,0,1,#
0,0,0,0,0,1,0,1,0))
neigh.rook <- matrix(ncol=9,nrow=9,c(#
0,1,0,1,0,0,0,0,0,#
1,0,1,0,1,0,0,0,0,#
0,1,0,0,0,1,0,0,0,#
1,0,0,0,1,0,1,0,0,#
0,1,0,1,0,1,0,1,0,#
0,0,1,0,1,0,0,0,1,#
0,0,0,1,0,0,0,1,0,#
0,0,0,0,1,0,1,0,1,#
0,0,0,0,0,1,0,1,0))
neigh.rook
 att1 <- c(1,2,3,2,3,4,3,4,5)
N=9
moran1 <- matrix(ncol=N,nrow=N,0)
neigh.rook.w <- neigh.rook#
for(i in 1:N){#
neigh.rook.w[i,] <- neigh.rook[i,] / sum(neigh.rook[i,])}
neigh.rook.w
X <- c(1,2,3,2,3,4,3,4,5)
X.mean <- mean(X)
X.mean
moran1 <- matrix(ncol=N,nrow=N,0)#
#
for(i in 1:N){#
for(j in 1:N){#
moran1 <- neigh.rook.w[i,j] * (X[i] - X.mean) * (X[j] - X.mean)}}
moran1
N
moran1
moran1 <- matrix(ncol=N,nrow=N,0)
moran1
for(i in 1:N){#
for(j in 1:N){#
moran1[i,j] <- neigh.rook.w[i,j] * (X[i] - X.mean) * (X[j] - X.mean)}}
moran1
neigh.rook.w
X
MoranI <- N * moran1 / {sum(neigh.rook.w) * sum((X-X.mean)^2)}
MoranI
MoranI <- N * sum(moran1) / {sum(neigh.rook.w) * sum((X-X.mean)^2)}
MoranI
library(spdep)
data(afcon)
afcon
oid <- order(afcon$id)
oid
resI <- localmoran(afcon$totcon, nb2listw(paper.nb))
resI
printCoefmat(data.frame(resI[oid,], row.names=afcon$name[oid]),
check.names=FALSE)
hist(resI[,5])
printCoefmat(data.frame(resI[oid,], row.names=afcon$name[oid]),
check.names=FALSE)
hist(resI[,5])
totcon <-afcon$totcon
library(DCluster)
data(nc.sids)
sids<-data.frame(Observed=nc.sids$SID74)
sids<-cbind(sids, Expected=nc.sids$BIR74*sum(nc.sids$SID74)/sum(nc.sids$BIR74))
sids<-cbind(sids, x=nc.sids$x, y=nc.sids$y)
sids
sidsgam<-opgam(data=sids, radius=30, step=10, alpha=.002)
sidsgam
plot(sidsgam)
plot(sids$x, sids$y)
