require(spdep)
data(boston)
samp <- sample( dim( boston.c )[ 1 ], 300)
d <- boston.c[ samp, ] ## Data at observed sites
y <- d[, "CMEDV"]
x <- d[,c("ZN", "LSTAT")]
xconst <- d[,c("CRIM", "NOX", "AGE", "DIS", "RAD", "TAX", "PTRATIO", "B", "RM")]
coords <- d[,c("LON", "LAT")]
d0 <- boston.c[-samp, ] ## Data at unobserved sites
y0 <- d0[, "CMEDV"]
x0 <- d0[,c("ZN", "LSTAT")]
xconst0 <- d0[,c("CRIM", "NOX", "AGE", "DIS", "RAD", "TAX", "PTRATIO", "B", "RM")]
coords0 <- d0[,c("LON", "LAT")]
meig <- meigen( coords = coords )
meig0 <- meigen0( meig = meig, coords0 = coords0 )
############ Spatial prediction ############
#### model with residual spatial dependence
mod <- resf(y=y, x=x, meig=meig)
pred0 <- predict0( mod = mod, x0 = x0, meig0 = meig0 )
pred0$pred[1:5,] # Predicted values
#### model with spatially varying coefficients (SVCs)
mod <- resf_vc(y=y, x=x, xconst=xconst, meig=meig )
pred0 <- predict0( mod = mod, x0 = x0, xconst0=xconst0, meig0 = meig0 )
pred0$pred[1:5,] # Predicted values
pred0$b_vc[1:5,] # SVCs
pred0$bse_vc[1:5,]# standard errors of the SVCs
pred0$t_vc[1:5,] # t-values of the SNVCs
pred0$p_vc[1:5,] # p-values of the SNVCs
plot(y0,pred0$pred[,1]);abline(0,1)
Run the code above in your browser using DataLab