Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Code used for my paper "A dimensao geografica das eleicoes brasileiras".
### preliminary stuff
setwd("/Users/thiagomarzagao/desktop/PROJECT")
library(foreign)
library(MASS)
library(car)
library(lmtest)
library(spdep)
library(sphet)
library(Matrix)
library(spgwr)
options(digits=5, scipen=1)
### importing the attributes table
meusdados<-read.csv(file="completedatabase.csv",head=TRUE)
attach(meusdados)
### importing weights matrices from GeoDa
weights.50km<-read.gwt2nb("wmfifty.gwt", region.id=CODE)
print(weights.50km)
weights.80km<-read.gwt2nb("wmeighty.gwt", region.id=CODE)
print(weights.80km)
weights.110km<-read.gwt2nb("wmoneten.gwt", region.id=CODE)
print(weights.110km)
weights.queen<-read.gal("wmqueen.gal", region.id=CODE)
print(weights.queen)
weights.rook<-read.gal("wmrook.gal", region.id=CODE)
print(weights.rook)
### converting weights matrices to listw format
weights.50km.listw<-nb2listw(weights.50km, zero.policy=TRUE)
print(weights.50km.listw, zero.policy=TRUE)
weights.80km.listw<-nb2listw(weights.80km, zero.policy=TRUE)
print(weights.80km.listw, zero.policy=TRUE)
weights.110km.listw<-nb2listw(weights.110km, zero.policy=TRUE)
print(weights.110km.listw, zero.policy=TRUE)
weights.queen.listw<-nb2listw(weights.queen, zero.policy=TRUE)
print(weights.queen.listw, zero.policy=TRUE)
weights.rook.listw<-nb2listw(weights.rook, zero.policy=TRUE)
print(weights.rook.listw, zero.policy=TRUE)
### preliminary tests of spatial autocorrelation
## computing Moran's I
pt1.50km.moran<-moran.test(pt1, weights.50km.listw, na.action=na.exclude, zero.policy=TRUE)
pt1.80km.moran<-moran.test(pt1, weights.80km.listw, na.action=na.exclude, zero.policy=TRUE)
pt1.110km.moran<-moran.test(pt1, weights.110km.listw, na.action=na.exclude, zero.policy=TRUE)
pt1.queen.moran<-moran.test(pt1, weights.queen.listw, na.action=na.exclude, zero.policy=TRUE)
pt1.rook.moran<-moran.test(pt1, weights.rook.listw, na.action=na.exclude, zero.policy=TRUE)
psdb1.50km.moran<-moran.test(psdb1, weights.50km.listw, na.action=na.exclude, zero.policy=TRUE)
psdb1.80km.moran<-moran.test(psdb1, weights.80km.listw, na.action=na.exclude, zero.policy=TRUE)
psdb1.110km.moran<-moran.test(psdb1, weights.110km.listw, na.action=na.exclude, zero.policy=TRUE)
psdb1.queen.moran<-moran.test(psdb1, weights.queen.listw, na.action=na.exclude, zero.policy=TRUE)
psdb1.rook.moran<-moran.test(psdb1, weights.rook.listw, na.action=na.exclude, zero.policy=TRUE)
pt2.50km.moran<-moran.test(pt2, weights.50km.listw, na.action=na.exclude, zero.policy=TRUE)
pt2.80km.moran<-moran.test(pt2, weights.80km.listw, na.action=na.exclude, zero.policy=TRUE)
pt2.110km.moran<-moran.test(pt2, weights.110km.listw, na.action=na.exclude, zero.policy=TRUE)
pt2.queen.moran<-moran.test(pt2, weights.queen.listw, na.action=na.exclude, zero.policy=TRUE)
pt2.rook.moran<-moran.test(pt2, weights.rook.listw, na.action=na.exclude, zero.policy=TRUE)
psdb2.50km.moran<-moran.test(psdb2, weights.50km.listw, na.action=na.exclude, zero.policy=TRUE)
psdb2.80km.moran<-moran.test(psdb2, weights.80km.listw, na.action=na.exclude, zero.policy=TRUE)
psdb2.110km.moran<-moran.test(psdb2, weights.110km.listw, na.action=na.exclude, zero.policy=TRUE)
psdb2.queen.moran<-moran.test(psdb2, weights.queen.listw, na.action=na.exclude, zero.policy=TRUE)
psdb2.rook.moran<-moran.test(psdb2, weights.rook.listw, na.action=na.exclude, zero.policy=TRUE)
bolsagdp.50km.moran<-moran.test(bolsagdp, weights.50km.listw, na.action=na.exclude, zero.policy=TRUE)
bolsagdp.80km.moran<-moran.test(bolsagdp, weights.80km.listw, na.action=na.exclude, zero.policy=TRUE)
bolsagdp.110km.moran<-moran.test(bolsagdp, weights.110km.listw, na.action=na.exclude, zero.policy=TRUE)
bolsagdp.queen.moran<-moran.test(bolsagdp, weights.queen.listw, na.action=na.exclude, zero.policy=TRUE)
bolsagdp.rook.moran<-moran.test(bolsagdp, weights.rook.listw, na.action=na.exclude, zero.policy=TRUE)
lgdpcap.50km.moran<-moran.test(lgdpcap, weights.50km.listw, na.action=na.exclude, zero.policy=TRUE)
lgdpcap.80km.moran<-moran.test(lgdpcap, weights.80km.listw, na.action=na.exclude, zero.policy=TRUE)
lgdpcap.110km.moran<-moran.test(lgdpcap, weights.110km.listw, na.action=na.exclude, zero.policy=TRUE)
lgdpcap.queen.moran<-moran.test(lgdpcap, weights.queen.listw, na.action=na.exclude, zero.policy=TRUE)
lgdpcap.rook.moran<-moran.test(lgdpcap, weights.rook.listw, na.action=na.exclude, zero.policy=TRUE)
illiteracy.50km.moran<-moran.test(illiteracy, weights.50km.listw, na.action=na.exclude, zero.policy=TRUE)
illiteracy.80km.moran<-moran.test(illiteracy, weights.80km.listw, na.action=na.exclude, zero.policy=TRUE)
illiteracy.110km.moran<-moran.test(illiteracy, weights.110km.listw, na.action=na.exclude, zero.policy=TRUE)
illiteracy.queen.moran<-moran.test(illiteracy, weights.queen.listw, na.action=na.exclude, zero.policy=TRUE)
illiteracy.rook.moran<-moran.test(illiteracy, weights.rook.listw, na.action=na.exclude, zero.policy=TRUE)
rural.50km.moran<-moran.test(rural, weights.50km.listw, na.action=na.exclude, zero.policy=TRUE)
rural.80km.moran<-moran.test(rural, weights.80km.listw, na.action=na.exclude, zero.policy=TRUE)
rural.110km.moran<-moran.test(rural, weights.110km.listw, na.action=na.exclude, zero.policy=TRUE)
rural.queen.moran<-moran.test(rural, weights.queen.listw, na.action=na.exclude, zero.policy=TRUE)
rural.rook.moran<-moran.test(rural, weights.rook.listw, na.action=na.exclude, zero.policy=TRUE)
nonadequate.50km.moran<-moran.test(nonadequate, weights.50km.listw, na.action=na.exclude, zero.policy=TRUE)
nonadequate.80km.moran<-moran.test(nonadequate, weights.80km.listw, na.action=na.exclude, zero.policy=TRUE)
nonadequate.110km.moran<-moran.test(nonadequate, weights.110km.listw, na.action=na.exclude, zero.policy=TRUE)
nonadequate.queen.moran<-moran.test(nonadequate, weights.queen.listw, na.action=na.exclude, zero.policy=TRUE)
nonadequate.rook.moran<-moran.test(nonadequate, weights.rook.listw, na.action=na.exclude, zero.policy=TRUE)
## displaying Moran's I
moran50km<-matrix(c(pt1.50km.moran$estimate[1],pt1.50km.moran$estimate[2],pt1.50km.moran$estimate[3],pt1.50km.moran$p.value,psdb1.50km.moran$estimate[1],psdb1.50km.moran$estimate[2],psdb1.50km.moran$estimate[3],psdb1.50km.moran$p.value,pt2.50km.moran$estimate[1],pt2.50km.moran$estimate[2],pt2.50km.moran$estimate[3],pt2.50km.moran$p.value,psdb2.50km.moran$estimate[1],psdb2.50km.moran$estimate[2],psdb2.50km.moran$estimate[3],psdb2.50km.moran$p.value,bolsagdp.50km.moran$estimate[1],bolsagdp.50km.moran$estimate[2],bolsagdp.50km.moran$estimate[3],bolsagdp.50km.moran$p.value,lgdpcap.50km.moran$estimate[1],lgdpcap.50km.moran$estimate[2],lgdpcap.50km.moran$estimate[3],lgdpcap.50km.moran$p.value,illiteracy.50km.moran$estimate[1],illiteracy.50km.moran$estimate[2],illiteracy.50km.moran$estimate[3],illiteracy.50km.moran$p.value,rural.50km.moran$estimate[1],rural.50km.moran$estimate[2],rural.50km.moran$estimate[3],rural.50km.moran$p.value,nonadequate.50km.moran$estimate[1],nonadequate.50km.moran$estimate[2],nonadequate.50km.moran$estimate[3],nonadequate.50km.moran$p.value),nrow=36,ncol=1)
moran80km<-matrix(c(pt1.80km.moran$estimate[1],pt1.80km.moran$estimate[2],pt1.80km.moran$estimate[3],pt1.80km.moran$p.value,psdb1.80km.moran$estimate[1],psdb1.80km.moran$estimate[2],psdb1.80km.moran$estimate[3],psdb1.80km.moran$p.value,pt2.80km.moran$estimate[1],pt2.80km.moran$estimate[2],pt2.80km.moran$estimate[3],pt2.80km.moran$p.value,psdb2.80km.moran$estimate[1],psdb2.80km.moran$estimate[2],psdb2.80km.moran$estimate[3],psdb2.80km.moran$p.value,bolsagdp.80km.moran$estimate[1],bolsagdp.80km.moran$estimate[2],bolsagdp.80km.moran$estimate[3],bolsagdp.80km.moran$p.value,lgdpcap.80km.moran$estimate[1],lgdpcap.80km.moran$estimate[2],lgdpcap.80km.moran$estimate[3],lgdpcap.80km.moran$p.value,illiteracy.80km.moran$estimate[1],illiteracy.80km.moran$estimate[2],illiteracy.80km.moran$estimate[3],illiteracy.80km.moran$p.value,rural.80km.moran$estimate[1],rural.80km.moran$estimate[2],rural.80km.moran$estimate[3],rural.80km.moran$p.value,nonadequate.80km.moran$estimate[1],nonadequate.80km.moran$estimate[2],nonadequate.80km.moran$estimate[3],nonadequate.80km.moran$p.value),nrow=36,ncol=1)
moran110km<-matrix(c(pt1.110km.moran$estimate[1],pt1.110km.moran$estimate[2],pt1.110km.moran$estimate[3],pt1.110km.moran$p.value,psdb1.110km.moran$estimate[1],psdb1.110km.moran$estimate[2],psdb1.110km.moran$estimate[3],psdb1.110km.moran$p.value,pt2.110km.moran$estimate[1],pt2.110km.moran$estimate[2],pt2.110km.moran$estimate[3],pt2.110km.moran$p.value,psdb2.110km.moran$estimate[1],psdb2.110km.moran$estimate[2],psdb2.110km.moran$estimate[3],psdb2.110km.moran$p.value,bolsagdp.110km.moran$estimate[1],bolsagdp.110km.moran$estimate[2],bolsagdp.110km.moran$estimate[3],bolsagdp.110km.moran$p.value,lgdpcap.110km.moran$estimate[1],lgdpcap.110km.moran$estimate[2],lgdpcap.110km.moran$estimate[3],lgdpcap.110km.moran$p.value,illiteracy.110km.moran$estimate[1],illiteracy.110km.moran$estimate[2],illiteracy.110km.moran$estimate[3],illiteracy.110km.moran$p.value,rural.110km.moran$estimate[1],rural.110km.moran$estimate[2],rural.110km.moran$estimate[3],rural.110km.moran$p.value,nonadequate.110km.moran$estimate[1],nonadequate.110km.moran$estimate[2],nonadequate.110km.moran$estimate[3],nonadequate.110km.moran$p.value),nrow=36,ncol=1)
moranrook<-matrix(c(pt1.rook.moran$estimate[1],pt1.rook.moran$estimate[2],pt1.rook.moran$estimate[3],pt1.rook.moran$p.value,psdb1.rook.moran$estimate[1],psdb1.rook.moran$estimate[2],psdb1.rook.moran$estimate[3],psdb1.rook.moran$p.value,pt2.rook.moran$estimate[1],pt2.rook.moran$estimate[2],pt2.rook.moran$estimate[3],pt2.rook.moran$p.value,psdb2.rook.moran$estimate[1],psdb2.rook.moran$estimate[2],psdb2.rook.moran$estimate[3],psdb2.rook.moran$p.value,bolsagdp.rook.moran$estimate[1],bolsagdp.rook.moran$estimate[2],bolsagdp.rook.moran$estimate[3],bolsagdp.rook.moran$p.value,lgdpcap.rook.moran$estimate[1],lgdpcap.rook.moran$estimate[2],lgdpcap.rook.moran$estimate[3],lgdpcap.rook.moran$p.value,illiteracy.rook.moran$estimate[1],illiteracy.rook.moran$estimate[2],illiteracy.rook.moran$estimate[3],illiteracy.rook.moran$p.value,rural.rook.moran$estimate[1],rural.rook.moran$estimate[2],rural.rook.moran$estimate[3],rural.rook.moran$p.value,nonadequate.rook.moran$estimate[1],nonadequate.rook.moran$estimate[2],nonadequate.rook.moran$estimate[3],nonadequate.rook.moran$p.value),nrow=36,ncol=1)
moranqueen<-matrix(c(pt1.queen.moran$estimate[1],pt1.queen.moran$estimate[2],pt1.queen.moran$estimate[3],pt1.queen.moran$p.value,psdb1.queen.moran$estimate[1],psdb1.queen.moran$estimate[2],psdb1.queen.moran$estimate[3],psdb1.queen.moran$p.value,pt2.queen.moran$estimate[1],pt2.queen.moran$estimate[2],pt2.queen.moran$estimate[3],pt2.queen.moran$p.value,psdb2.queen.moran$estimate[1],psdb2.queen.moran$estimate[2],psdb2.queen.moran$estimate[3],psdb2.queen.moran$p.value,bolsagdp.queen.moran$estimate[1],bolsagdp.queen.moran$estimate[2],bolsagdp.queen.moran$estimate[3],bolsagdp.queen.moran$p.value,lgdpcap.queen.moran$estimate[1],lgdpcap.queen.moran$estimate[2],lgdpcap.queen.moran$estimate[3],lgdpcap.queen.moran$p.value,illiteracy.queen.moran$estimate[1],illiteracy.queen.moran$estimate[2],illiteracy.queen.moran$estimate[3],illiteracy.queen.moran$p.value,rural.queen.moran$estimate[1],rural.queen.moran$estimate[2],rural.queen.moran$estimate[3],rural.queen.moran$p.value,nonadequate.queen.moran$estimate[1],nonadequate.queen.moran$estimate[2],nonadequate.queen.moran$estimate[3],nonadequate.queen.moran$p.value),nrow=36,ncol=1)
moran1table<-cbind(moran50km,moran80km,moran110km,moranrook,moranqueen)
colnames(moran1table)<-c('50km','80km','110km','rook','queen')
rownames(moran1table)<-c('PT1','expectation','variance','p-value','PSDB1','expectation','variance','p-value','PT2','expectation','variance','p-value','PSDB2','expectation','variance','p-value','Bolsa/GDP','expectation','variance','p-value','lGDPperCapita','expectation','variance','p-value','Illiteracy','expectation','variance','p-value','Rural','expectation','variance','p-value','Nonadequate','expectation','variance','p-value')
formatC(moran1table,format="f",digits=5)
## computing permutation-based Moran's I
pt1.50km.moran.mc<-moran.mc(pt1, weights.50km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
pt1.80km.moran.mc<-moran.mc(pt1, weights.80km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
pt1.110km.moran.mc<-moran.mc(pt1, weights.110km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
pt1.queen.moran.mc<-moran.mc(pt1, weights.queen.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
pt1.rook.moran.mc<-moran.mc(pt1, weights.rook.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
psdb1.50km.moran.mc<-moran.mc(psdb1, weights.50km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
psdb1.80km.moran.mc<-moran.mc(psdb1, weights.80km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
psdb1.110km.moran.mc<-moran.mc(psdb1, weights.110km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
psdb1.queen.moran.mc<-moran.mc(psdb1, weights.queen.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
psdb1.rook.moran.mc<-moran.mc(psdb1, weights.rook.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
pt2.50km.moran.mc<-moran.mc(pt2, weights.50km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
pt2.80km.moran.mc<-moran.mc(pt2, weights.80km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
pt2.110km.moran.mc<-moran.mc(pt2, weights.110km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
pt2.queen.moran.mc<-moran.mc(pt2, weights.queen.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
pt2.rook.moran.mc<-moran.mc(pt2, weights.rook.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
psdb2.50km.moran.mc<-moran.mc(psdb2, weights.50km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
psdb2.80km.moran.mc<-moran.mc(psdb2, weights.80km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
psdb2.110km.moran.mc<-moran.mc(psdb2, weights.110km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
psdb2.queen.moran.mc<-moran.mc(psdb2, weights.queen.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
psdb2.rook.moran.mc<-moran.mc(psdb2, weights.rook.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
bolsagdp.50km.moran.mc<-moran.mc(bolsagdp, weights.50km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
bolsagdp.80km.moran.mc<-moran.mc(bolsagdp, weights.80km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
bolsagdp.110km.moran.mc<-moran.mc(bolsagdp, weights.110km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
bolsagdp.queen.moran.mc<-moran.mc(bolsagdp, weights.queen.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
bolsagdp.rook.moran.mc<-moran.mc(bolsagdp, weights.rook.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
lgdpcap.50km.moran.mc<-moran.mc(lgdpcap, weights.50km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
lgdpcap.80km.moran.mc<-moran.mc(lgdpcap, weights.80km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
lgdpcap.110km.moran.mc<-moran.mc(lgdpcap, weights.110km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
lgdpcap.queen.moran.mc<-moran.mc(lgdpcap, weights.queen.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
lgdpcap.rook.moran.mc<-moran.mc(lgdpcap, weights.rook.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
illiteracy.50km.moran.mc<-moran.mc(illiteracy, weights.50km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
illiteracy.80km.moran.mc<-moran.mc(illiteracy, weights.80km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
illiteracy.110km.moran.mc<-moran.mc(illiteracy, weights.110km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
illiteracy.queen.moran.mc<-moran.mc(illiteracy, weights.queen.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
illiteracy.rook.moran.mc<-moran.mc(illiteracy, weights.rook.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
rural.50km.moran.mc<-moran.mc(rural, weights.50km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
rural.80km.moran.mc<-moran.mc(rural, weights.80km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
rural.110km.moran.mc<-moran.mc(rural, weights.110km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
rural.queen.moran.mc<-moran.mc(rural, weights.queen.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
rural.rook.moran.mc<-moran.mc(rural, weights.rook.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
nonadequate<-semiadequate+inadequate
nonadequate.50km.moran.mc<-moran.mc(nonadequate, weights.50km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
nonadequate.80km.moran.mc<-moran.mc(nonadequate, weights.80km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
nonadequate.110km.moran.mc<-moran.mc(nonadequate, weights.110km.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
nonadequate.queen.moran.mc<-moran.mc(nonadequate, weights.queen.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
nonadequate.rook.moran.mc<-moran.mc(nonadequate, weights.rook.listw, na.action=na.exclude, nsim=99, zero.policy=TRUE)
## displaying permutation-based Moran's I
moran.mc.50km<-matrix(c(pt1.50km.moran.mc$statistic,pt1.50km.moran.mc$p.value,psdb1.50km.moran.mc$statistic,psdb1.50km.moran.mc$p.value,pt2.50km.moran.mc$statistic,pt2.50km.moran.mc$p.value,psdb2.50km.moran.mc$statistic,psdb2.50km.moran.mc$p.value,bolsagdp.50km.moran.mc$statistic,bolsagdp.50km.moran.mc$p.value,lgdpcap.50km.moran.mc$statistic,lgdpcap.50km.moran.mc$p.value,illiteracy.50km.moran.mc$statistic,illiteracy.50km.moran.mc$p.value,rural.50km.moran.mc$statistic,rural.50km.moran.mc$p.value, nonadequate.50km.moran.mc$statistic,nonadequate.50km.moran.mc$p.value),nrow=18,ncol=1)
moran.mc.80km<-matrix(c(pt1.80km.moran.mc$statistic,pt1.80km.moran.mc$p.value,psdb1.80km.moran.mc$statistic,psdb1.80km.moran.mc$p.value,pt2.80km.moran.mc$statistic,pt2.80km.moran.mc$p.value,psdb2.80km.moran.mc$statistic,psdb2.80km.moran.mc$p.value,bolsagdp.80km.moran.mc$statistic,bolsagdp.80km.moran.mc$p.value,lgdpcap.80km.moran.mc$statistic,lgdpcap.80km.moran.mc$p.value,illiteracy.80km.moran.mc$statistic,illiteracy.80km.moran.mc$p.value,rural.80km.moran.mc$statistic,rural.80km.moran.mc$p.value,nonadequate.80km.moran.mc$statistic,nonadequate.80km.moran.mc$p.value),nrow=18,ncol=1)
moran.mc.110km<-matrix(c(pt1.110km.moran.mc$statistic,pt1.110km.moran.mc$p.value,psdb1.110km.moran.mc$statistic,psdb1.110km.moran.mc$p.value,pt2.110km.moran.mc$statistic,pt2.110km.moran.mc$p.value,psdb2.110km.moran.mc$statistic,psdb2.110km.moran.mc$p.value,bolsagdp.110km.moran.mc$statistic,bolsagdp.110km.moran.mc$p.value,lgdpcap.110km.moran.mc$statistic,lgdpcap.110km.moran.mc$p.value,illiteracy.110km.moran.mc$statistic,illiteracy.110km.moran.mc$p.value,rural.110km.moran.mc$statistic,rural.110km.moran.mc$p.value,nonadequate.110km.moran.mc$statistic,nonadequate.110km.moran.mc$p.value),nrow=18,ncol=1)
moran.mc.rook<-matrix(c(pt1.rook.moran.mc$statistic,pt1.rook.moran.mc$p.value,psdb1.rook.moran.mc$statistic,psdb1.rook.moran.mc$p.value,pt2.rook.moran.mc$statistic,pt2.rook.moran.mc$p.value,psdb2.rook.moran.mc$statistic,psdb2.rook.moran.mc$p.value,bolsagdp.rook.moran.mc$statistic,bolsagdp.rook.moran.mc$p.value,lgdpcap.rook.moran.mc$statistic,lgdpcap.rook.moran.mc$p.value,illiteracy.rook.moran.mc$statistic,illiteracy.rook.moran.mc$p.value,rural.rook.moran.mc$statistic,rural.rook.moran.mc$p.value,nonadequate.rook.moran.mc$statistic,nonadequate.rook.moran.mc$p.value),nrow=18,ncol=1)
moran.mc.queen<-matrix(c(pt1.queen.moran.mc$statistic,pt1.queen.moran.mc$p.value,psdb1.queen.moran.mc$statistic,psdb1.queen.moran.mc$p.value,pt2.queen.moran.mc$statistic,pt2.queen.moran.mc$p.value,psdb2.queen.moran.mc$statistic,psdb2.queen.moran.mc$p.value,bolsagdp.queen.moran.mc$statistic,bolsagdp.queen.moran.mc$p.value,lgdpcap.queen.moran.mc$statistic,lgdpcap.queen.moran.mc$p.value,illiteracy.queen.moran.mc$statistic,illiteracy.queen.moran.mc$p.value,rural.queen.moran.mc$statistic,rural.queen.moran.mc$p.value,nonadequate.queen.moran.mc$statistic,nonadequate.queen.moran.mc$p.value),nrow=18,ncol=1)
moran2table<-cbind(moran.mc.50km,moran.mc.80km,moran.mc.110km,moran.mc.rook,moran.mc.queen)
colnames(moran2table)<-c('50km','80km','110km','rook','queen')
rownames(moran2table)<-c('PT1','p-value','PSDB1','p-value','PT2','p-value','PSDB2','p-value','Bolsa/GDP','p-value','lGDPperCapita','p-value','Illiteracy','p-value','Rural','p-value','Nonadequate','p-value')
moran2table
## local spatial dependence
locmoran<-localmoran(pt2, weights.queen.listw, na.action=na.exclude,zero.policy=TRUE)
summary(locmoran)
## spatial dependence in non-presidential races
# state assemblies
pde13.moran.queen<-moran.test(pde13, weights.queen.listw, na.action=na.exclude, zero.policy=TRUE)
print(pde13.moran.queen)
pde13.moran.queen.mc<-moran.test(pde13, weights.queen.listw, na.action=na.exclude, zero.policy=TRUE)
print(pde13.moran.queen.mc)
pde13.locmoran<-localmoran(pde13, weights.queen.listw, na.action=na.exclude,zero.policy=TRUE)
summary(pde13.locmoran)
# lower house
pdf13.moran.queen<-moran.test(pdf13, weights.queen.listw, na.action=na.exclude, zero.policy=TRUE)
print(pdf13.moran.queen)
pdf13.moran.queen.mc<-moran.test(pdf13, weights.queen.listw, na.action=na.exclude, zero.policy=TRUE)
print(pdf13.moran.queen.mc)
pdf13.locmoran<-localmoran(pdf13, weights.queen.listw, na.action=na.exclude,zero.policy=TRUE)
summary(pdf13.locmoran)
### OLS
meusdados.ols<-lm(pt2 ~ party + bolsagdp + lgdpcap + rural + illiteracy + nonadequate + AL + AM + AP + BA + CE + DF + ES + GO + MA + MG + MS + MT + PA + PB + PE + PI + PR + RJ + RN + RO + RR + RS + SC + SE + SP + TO, data=meusdados)
summary(meusdados.ols)
ols.moran<-lm.morantest(meusdados.ols,weights.queen.listw, zero.policy=TRUE)
print(ols.moran)
lm.LMtests(meusdados.ols,weights.queen.listw,zero.policy=TRUE,test="all")
bptest(meusdados.ols)
vif(meusdados.ols)
### WLSP
meusdados.wlsp<-lm(pt2 ~ party + bolsagdp + lgdpcap + rural + illiteracy + nonadequate + AL + AM + AP + BA + CE + DF + ES + GO + MA + MG + MS + MT + PA + PB + PE + PI + PR + RJ + RN + RO + RR + RS + SC + SE + SP + TO, data=meusdados, weights=lpop)
summary(meusdados.wlsp)
wlsp.moran<-lm.morantest(meusdados.wlsp,weights.queen.listw, zero.policy=TRUE)
print(wlsp.moran)
lm.LMtests(meusdados.wlsp,weights.queen.listw,zero.policy=TRUE,test="all")
bptest(meusdados.wlsp)
vif(meusdados.wlsp)
### WLSV
meusdados.wlsv<-lm(pt2 ~ party + bolsagdp + lgdpcap + rural + illiteracy + nonadequate + AL + AM + AP + BA + CE + DF + ES + GO + MA + MG + MS + MT + PA + PB + PE + PI + PR + RJ + RN + RO + RR + RS + SC + SE + SP + TO, data=meusdados, weights=gw_wt)
summary(meusdados.wlsv)
wlsv.moran<-lm.morantest(meusdados.wlsv,weights.queen.listw, zero.policy=TRUE)
print(wlsv.moran)
lm.LMtests(meusdados.wlsv,weights.queen.listw,zero.policy=TRUE,test="all")
bptest(meusdados.wlsv)
vif(meusdados.wlsv)
### GWR w/ fixed bandwidth
coordin<-cbind(ycoord,xcoord)
meusdados.bw<-gwr.sel(pt2 ~ party + bolsagdp + lgdpcap + rural + illiteracy + nonadequate, data=meusdados, coords=coordin)
meusdados.gwr<-gwr(pt2 ~ party + bolsagdp + lgdpcap + rural + illiteracy + nonadequate, data=meusdados, coords=coordin, bandwidth=meusdados.bw)
print(meusdados.gwr)
### GWR w/ adaptive bandwidth
gw.adapt <- function(dp, fp, quant, longlat=FALSE) {
n1 <- nrow(dp)
n2 <- nrow(fp)
storage.mode(dp) <- "double"
storage.mode(fp) <- "double"
dists <- numeric(n1)
bw <- numeric(n2)
if (quant > 1) {
factor <- quant
quant <- 1
res <- .C("gw_adapt", dp[,1], dp[,2],
fp[,1], fp[,2], as.integer(n1),
as.integer(n2), as.double(bw), as.double(quant),
dists, as.integer(longlat),
PACKAGE="spgwr")[[7]]
res <- res*factor
} else {
n.ideal <- n1*quant
n.lower <- floor(n.ideal)
n.higher <- n.lower+1
q1 <- n.lower/n1
q2 <- n.higher/n1
res1 <- .C("gw_adapt", dp[,1], dp[,2],
fp[,1], fp[,2], as.integer(n1),
as.integer(n2), as.double(bw), as.double(q1),
dists, as.integer(longlat),
PACKAGE="spgwr")[[7]]
res2 <- .C("gw_adapt", dp[,1], dp[,2],
fp[,1], fp[,2], as.integer(n1),
as.integer(n2), as.double(bw), as.double(q2),
dists, as.integer(longlat),
PACKAGE="spgwr")[[7]]
res <- (n.ideal - n.lower)*res2 + (n.higher - n.ideal)*res1
}
res
}
adaptive.bw<-gw.adapt(coordin,coordin,quant=1)
meusdados2.gwr<-gwr(pt2 ~ party + bolsagdp + lgdpcap + rural + illiteracy + nonadequate, data=meusdados, coords=coordin, bandwidth=adaptive.bw)
print(meusdados2.gwr)
### spatial error model
meusdados.err<-errorsarlm(pt2 ~ party + bolsagdp + lgdpcap + rural + illiteracy + nonadequate + AL + AM + AP + BA + CE + DF + ES + GO + MA + MG + MS + MT + PA + PB + PE + PI + PR + RJ + RN + RO + RR + RS + SC + SE + SP + TO, zero.policy=TRUE,weights.queen.listw,na.action=na.omit,data=meusdados,quiet=FALSE)
summary(meusdados.err)
bptest.sarlm(meusdados.err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment