-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsparse_mat.R
161 lines (142 loc) · 5.18 KB
/
sparse_mat.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
# Copyright 2006-14 by Roger Bivand
#
as.spam.listw <- function(listw) {
if (requireNamespace("spam", quietly = TRUE)) {
#if (!require(spam)) stop("spam not available")
N <- length(listw$neighbours)
W_sn <- listw2sn(listw)
rpts <- as.integer(cumsum(c(1, card(listw$neighbours))))
W <- new("spam", entries=W_sn$weights, colindices=W_sn$to,
rowpointers=rpts, dimension=as.integer(c(N, N)))
stopifnot(spam::validate_spam(W))
return(W)
} else stop("spam not available")
}
listw2U_spam <- function(lw) 0.5 * (lw + t(lw))
setAs("listw", "CsparseMatrix", function(from) {as(as_dgRMatrix_listw(from), "CsparseMatrix")})
setAs("listw", "RsparseMatrix", function(from) {as_dgRMatrix_listw(from)})
setAs("listw", "symmetricMatrix", function(from) {as_dsTMatrix_listw(from)})
as_dgRMatrix_listw <- function(listw) {
if(!inherits(listw, "listw")) stop("not a listw object")
n <- length(listw$neighbours)
cardw <- card(listw$neighbours)
p0 <- as.integer(c(0, cumsum(cardw)))
scard <- sum(cardw)
z <- .Call("listw2dgR", listw$neighbours, listw$weights,
as.integer(cardw), as.integer(scard), PACKAGE="spatialreg")
res <- as(new("dgRMatrix", j=z[[1]], p=p0, Dim=as.integer(c(n, n)),
x=z[[2]]), "generalMatrix")
colnames(res) <- attr(listw$neighbours, "region.id")
rownames(res) <- colnames(res)
res
}
as_dsTMatrix_listw <- function(listw) {
if (!inherits(listw, "listw")) stop("not a listw object")
if (!is.symmetric.glist(listw$neighbours, listw$weights))
stop("not a symmetric matrix")
n <- length(listw$neighbours)
cardw <- card(listw$neighbours)
scard <- sum(cardw)
if (scard %% 2 != 0) stop("odd neighbours sum")
z <- .Call("listw2dsT", listw$neighbours, listw$weights,
as.integer(cardw), as.integer(scard/2), PACKAGE="spatialreg")
res <- as(new("dsTMatrix", i=z[[1]], j=z[[2]], Dim=as.integer(c(n, n)),
x=z[[3]]), "generalMatrix")
colnames(res) <- attr(listw$neighbours, "region.id")
rownames(res) <- colnames(res)
res
}
as_dsCMatrix_I <- function(n) {
if (n < 1) stop("matrix must have positive dimensions")
as(as(Diagonal(n), "symmetricMatrix"), "generalMatrix")
}
as_dsCMatrix_IrW <- function(W, rho) {
stopifnot(is(W, "symmetricMatrix"))
n <- dim(W)[1]
as_dsCMatrix_I(n) - rho * W
}
Jacobian_W <- function(W, rho) {
sum(2*log(diag(chol(as_dsCMatrix_IrW(W, rho)))))
}
listw2U_Matrix <- function(lw)
as(as(0.5 * (lw + t(lw)), "symmetricMatrix"), "CsparseMatrix")
powerWeights <- function(W, rho, order=250, X, tol=.Machine$double.eps^(3/5)) {
timings <- list()
.ptime_start <- proc.time()
n <- dim(W)[1]
dX <- dim(X)
if (dX[1] == n) side <- "R"
else if (dX[2] == n) side <- "L"
else stop("W and X non-conformant")
aW <- rho*W
if (side == "R") last <- aW %*% X
else last <- X %*% aW
acc <- X + last
conv <- FALSE
iter <- 1
series <- numeric(order)
while (iter < order) {
if (side == "R") {
last <- aW %*% last
acc <- acc + last
} else {
last <- last %*% aW
acc <- acc + last
}
# abs() added 2017-02-15, bug spotted by Yongwan Chun
series[iter] <- mean(abs(as(last, "matrix")))
if (series[iter] < tol) {
conv <- TRUE
break
}
iter <- iter+1
}
if (!conv) warning("not converged within order iterations")
timings[["make_power_sum"]] <- proc.time() - .ptime_start
attr(acc, "internal") <- list(series=series, order=order,
tol=tol, iter=iter, conv=conv)
attr(acc, "timings") <- do.call("rbind", timings)[, c(1, 3)]
acc
}
invIrM <- function(neighbours, rho, glist=NULL, style="W", method="solve",
feasible=NULL) {
if(!inherits(neighbours, "nb")) stop("Not a neighbours list")
invIrW(nb2listw(neighbours, glist=glist, style=style), rho=rho,
method=method, feasible=feasible)
}
invIrW <- function(x, rho, method="solve", feasible=NULL) {
if(inherits(x, "listw")) {
n <- length(x$neighbours)
V <- listw2mat(x)
} else if (inherits(x, "Matrix") || inherits(x, "matrix")) {
if (method == "chol" && all(t(x) == x))
stop("No Cholesky method for matrix or sparse matrix object")
n <- dim(x)[1]
V <- x
} else stop("Not a weights list or a Sparse Matrix")
if (is.null(feasible) || (is.logical(feasible) && !feasible)) {
e <- eigen(V, only.values = TRUE)$values
if (is.complex(e)) feasible <- 1/(range(Re(e)))
else feasible <- 1/(range(e))
if (rho <= feasible[1] || rho >= feasible[2])
stop(paste("Rho", rho, "outside feasible range:",
paste(feasible, collapse=":")))
}
if (method == "chol"){
if (x$style %in% c("W", "S") && !(spatialreg::can.be.simmed(x)))
stop("Cholesky method requires symmetric weights")
if (x$style %in% c("B", "C", "U") &&
!(is.symmetric.glist(x$neighbours, x$weights)))
stop("Cholesky method requires symmetric weights")
if (x$style %in% c("W", "S")) {
V <- listw2mat(listw2U(spatialreg::similar.listw(x)))
}
mat <- diag(n) - rho * V
res <- chol2inv(chol(mat))
} else if (method == "solve") {
mat <- diag(n) - rho * V
res <- solve(mat)
} else stop("unknown method")
attr(res, "call") <- match.call()
res
}