0% found this document useful (0 votes)
99 views

Coding Analisis Spasial

The document discusses spatial modeling and analysis in R. It loads necessary packages, imports shapefile and data, creates queen and rook spatial weight matrices, performs global regression, diagnostics for residual independence and spatial dependence, and finally fits a spatial autoregressive (SAR) lag model.

Uploaded by

Rochmanto_Hani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

Coding Analisis Spasial

The document discusses spatial modeling and analysis in R. It loads necessary packages, imports shapefile and data, creates queen and rook spatial weight matrices, performs global regression, diagnostics for residual independence and spatial dependence, and finally fits a spatial autoregressive (SAR) lag model.

Uploaded by

Rochmanto_Hani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

analisis_spasial.

R
Anuraga

Wed Oct 18 15:17:52 2017


# PEMODELAN SPASIAL DENGAN MENGGUNAKAN R
#Package yang digunakan
library("spdep")

## Loading required package: sp

## Loading required package: Matrix

library("maptools")

## Checking rgeos availability: TRUE

# dataset yang digunakan di drive E


setwd("E:/R Project's/R-Anuraga/PETA JATIM 2017 AKB")
mr <- readShapeSpatial("jatim.shp")

## Warning: use rgdal::readOGR or sf::st_read

## Warning: use rgdal::readOGR or sf::st_read

data <- read.csv("dataakb.csv")


names(mr)

## [1] "POLY_ID" "KODE_KAB" "NAMA_KAB" "KODE_PROP" "NAMA_PROP"


## [6] "Y" "X1" "X2" "X3" "X4"
## [11] "X5" "X6" "X7"

plot(mr, main = "Jawa Timur", axes = TRUE)


#bobot queen contiguity
queen.w <- poly2nb(mr, row.names = mr$KABKOTNO, queen = TRUE)
summary(queen.w)

## Neighbour list object:


## Number of regions: 38
## Number of nonzero links: 140
## Percentage nonzero weights: 9.695291
## Average number of links: 3.684211
## Link number distribution:
##
## 1 2 3 4 5 6 7 8 9
## 8 6 5 7 2 6 2 1 1
## 8 least connected regions:
## 15 17 18 19 20 21 22 37 with 1 link
## 1 most connected region:
## 32 with 9 links

#standarisasi matrik bobot queen


queen.wl <- nb2listw(queen.w, style = "W")
summary(queen.wl)

## Characteristics of weights list object:


## Neighbour list object:
## Number of regions: 38
## Number of nonzero links: 140
## Percentage nonzero weights: 9.695291
## Average number of links: 3.684211
## Link number distribution:
##
## 1 2 3 4 5 6 7 8 9
## 8 6 5 7 2 6 2 1 1
## 8 least connected regions:
## 15 17 18 19 20 21 22 37 with 1 link
## 1 most connected region:
## 32 with 9 links
##
## Weights style: W
## Weights constants summary:
## n nn S0 S1 S2
## W 38 1444 38 26.78738 169.3149

# bobot rook contiguity


rook.w <- poly2nb(mr, row.names = mr$NAME, queen = FALSE)
summary(rook.w)

## Neighbour list object:


## Number of regions: 38
## Number of nonzero links: 136
## Percentage nonzero weights: 9.418283
## Average number of links: 3.578947
## Link number distribution:
##
## 1 2 3 4 5 6 8
## 8 6 6 6 2 8 2
## 8 least connected regions:
## 15 17 18 19 20 21 22 37 with 1 link
## 2 most connected regions:
## 28 32 with 8 links

#Plot matrik bobot W Queen and Rook


plot(mr, border = "black")
plot.queen <- plot(queen.w, coordinates(mr), add = TRUE, col = "red")
plot.queen

## NULL

plot.rook <- plot(rook.w, coordinates(mr), add = TRUE, col = "blue")


plot.rook

## NULL

#regresi global
ols <- lm( y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7, data=data)
summary(ols)

##
## Call:
## lm(formula = y ~ x1 + x2 + x3 + x4 + x5 + x6 + x7, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.0832 -3.7265 -0.4836 4.6464 10.1558
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 82.90172 20.22625 4.099 0.000291 ***
## x1 -0.32816 0.14451 -2.271 0.030492 *
## x2 -7.98427 1.99576 -4.001 0.000381 ***
## x3 -1.24091 2.01743 -0.615 0.543132
## x4 0.26703 0.20319 1.314 0.198743
## x5 -0.15276 0.12646 -1.208 0.236501
## x6 -0.20455 0.19224 -1.064 0.295783
## x7 0.01538 0.03691 0.417 0.679882
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.852 on 30 degrees of freedom
## Multiple R-squared: 0.8468, Adjusted R-squared: 0.8111
## F-statistic: 23.7 on 7 and 30 DF, p-value: 1.402e-10

ols1 <- lm( y ~ x1 + x2, data=data)


summary(ols1)

##
## Call:
## lm(formula = y ~ x1 + x2, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.3751 -4.8687 -0.3345 5.0513 10.8337
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 99.66099 5.68896 17.518 < 2e-16 ***
## x1 -0.53179 0.09553 -5.567 2.88e-06 ***
## x2 -6.01590 1.83710 -3.275 0.00239 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.969 on 35 degrees of freedom
## Multiple R-squared: 0.8141, Adjusted R-squared: 0.8034
## F-statistic: 76.62 on 2 and 35 DF, p-value: 1.636e-13

library(gvlma)
gvmodel <- gvlma(ols1)
summary(gvmodel)

##
## Call:
## lm(formula = y ~ x1 + x2, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.3751 -4.8687 -0.3345 5.0513 10.8337
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 99.66099 5.68896 17.518 < 2e-16 ***
## x1 -0.53179 0.09553 -5.567 2.88e-06 ***
## x2 -6.01590 1.83710 -3.275 0.00239 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.969 on 35 degrees of freedom
## Multiple R-squared: 0.8141, Adjusted R-squared: 0.8034
## F-statistic: 76.62 on 2 and 35 DF, p-value: 1.636e-13
##
##
## ASSESSMENT OF THE LINEAR MODEL ASSUMPTIONS
## USING THE GLOBAL TEST ON 4 DEGREES-OF-FREEDOM:
## Level of Significance = 0.05
##
## Call:
## gvlma(x = ols1)
##
## Value p-value Decision
## Global Stat 2.090408 0.7191 Assumptions acceptable.
## Skewness 0.007742 0.9299 Assumptions acceptable.
## Kurtosis 1.459612 0.2270 Assumptions acceptable.
## Link Function 0.586122 0.4439 Assumptions acceptable.
## Heteroscedasticity 0.036932 0.8476 Assumptions acceptable.

# pemeriksaan asumsi residal independen


library(car)
durbinWatsonTest(ols1)

## lag Autocorrelation D-W Statistic p-value


## 1 -0.31072 2.455267 0.154
## Alternative hypothesis: rho != 0

par(mfrow=c(2,2))
plot(ols1)
#regresi spasial
globalmorans <- lm.morantest(ols1, listw = queen.wl, alternative =
"two.sided")
globalmorans

##
## Global Moran I for regression residuals
##
## data:
## model: lm(formula = y ~ x1 + x2, data = data)
## weights: queen.wl
##
## Moran I statistic standard deviate = 3.2135, p-value = 0.001311
## alternative hypothesis: two.sided
## sample estimates:
## Observed Moran I Expectation Variance
## 0.34391177 -0.05510886 0.01541865

ujilagrange <- lm.LMtests(ols1,queen.wl,test=c("all"))


lagrange <- t(sapply(ujilagrange, function(x) c(x$statistic, x$parameter,
x$p.value)))
colnames(lagrange) <- c("Statistic", "df", "p-value")
printCoefmat(lagrange)

## Statistic df p-value
## LMerr 6.3757 1.0000 0.0116
## LMlag 5.0488 1.0000 0.0246
## RLMerr 2.3817 1.0000 0.1228
## RLMlag 1.0547 1.0000 0.3044
## SARMA 7.4305 2.0000 0.0243

print(ujilagrange)

##
## Lagrange multiplier diagnostics for spatial dependence
##
## data:
## model: lm(formula = y ~ x1 + x2, data = data)
## weights: queen.wl
##
## LMerr = 6.3757, df = 1, p-value = 0.01157
##
##
## Lagrange multiplier diagnostics for spatial dependence
##
## data:
## model: lm(formula = y ~ x1 + x2, data = data)
## weights: queen.wl
##
## LMlag = 5.0488, df = 1, p-value = 0.02464
##
##
## Lagrange multiplier diagnostics for spatial dependence
##
## data:
## model: lm(formula = y ~ x1 + x2, data = data)
## weights: queen.wl
##
## RLMerr = 2.3817, df = 1, p-value = 0.1228
##
##
## Lagrange multiplier diagnostics for spatial dependence
##
## data:
## model: lm(formula = y ~ x1 + x2, data = data)
## weights: queen.wl
##
## RLMlag = 1.0547, df = 1, p-value = 0.3044
##
##
## Lagrange multiplier diagnostics for spatial dependence
##
## data:
## model: lm(formula = y ~ x1 + x2, data = data)
## weights: queen.wl
##
## SARMA = 7.4305, df = 2, p-value = 0.02435

# 1. Spatial SAR
lagakb <- lagsarlm(ols1, listw=queen.wl, data=data, type="lag")
summary(lagakb, correlation = FALSE, Nagelkerke = TRUE, Hausman=TRUE,
adj.se=TRUE)

##
## Call:lagsarlm(formula = ols1, data = data, listw = queen.wl, type = "lag")
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.688292 -4.025453 -0.061897 4.191491 11.534906
##
## Type: lag
## Coefficients: (asymptotic standard errors)
## Estimate Std. Error t value Pr(>|z|)
## (Intercept) 80.86898 10.87578 7.4357 1.039e-13
## x1 -0.41763 0.10407 -4.0129 5.999e-05
## x2 -5.82716 1.73895 -3.3510 0.0008053
##
## Rho: 0.20727, LR test value: 4.6634, p-value: 0.030812
## Asymptotic standard error: 0.10588
## t-value: 1.9576, p-value: 0.050276
## Wald statistic: 4.1607, p-value: 0.041372
##
## Log likelihood: -117.9157 for lag model
## ML residual variance (sigma squared): 28.672, (sigma: 5.3547)
## Nagelkerke pseudo-R-squared: 0.83554
## Number of observations: 38
## Number of parameters estimated: 5
## AIC: 245.83, (AIC for lm: 248.49)
## LM test for residual autocorrelation
## test value: 3.1451, p-value: 0.076154

#BP test
bptest.sarlm(lagakb)

##
## studentized Breusch-Pagan test
##
## data:
## BP = 4.2674, df = 2, p-value = 0.1184

?nb2listw

## starting httpd help server ...

## done

You might also like