Spatial Lag Model, one weight matrix, MLE
From InterSciWiki
Matrices http://stat.ethz.ch/R-manual/R-patched/library/base/html/colSums.html
- Spatial Lag Model, one weight matrix, MLE
load("http://intersci.ss.uci.edu/wiki/Eff/SCCS_stata.dta")
tmpfile <- tempfile()
download.file('http://intersci.ss.uci.edu/wiki/Eff/SCCS_stata.dta',tmpfile)
risk2 <- read.dta(tmpfile)
unlink(tmpfile)
#datafile 1: http://intersci.ss.uci.edu/wiki/Eff/SCCS_stata.dta #distancematrix: http://intersci.ss.uci.edu/wiki/Eff/distmat.dbf #languagemetrix: http://intersci.ss.uci.edu/wiki/Eff/langmat.dbf #This program: http://intersci.ss.uci.edu/wiki/Eff/A_EFF_SAR_R #Change the following to your own working directory (note UNIX slant of slashes--even on Windows machine) library(spdep) setwd("d:/projects/dow/") #source("http://intersci.ss.uci.edu/wiki/pub/R-KinSim.R") #net=source("C:/Program Files/pajek/PAJEK/PajekR.r") #setwd("C:/Program Files/pajek/PAJEK/") setwd("C:/Program Files/pajek/PAJEK/Anthon_Eff-Autocorrelation") options(echo=TRUE) library(foreign) library(spdep) #Read in the dbf format weight matrices-the dbf file is 186x186 (no row names) #Comment the matrix you do NOT want to use lds<-read.dbf("langmat.dbf") #language phylogeny #lds<-read.dbf("distmat.dbf") #great circle distance #convert to matrix lds<-as.matrix(lds) #take a quick look at the upper left hand corner to see that it is OK lds[1:5,1:5] #read in SCCS data. It is in STATA format, since this is numeric--there are problems with the SPSS version, #since R imports the value labels from SPSS and the variables become non-numeric gg<-read.dta("SCCS_stata.dta") length(gg[,1]) #the number of observations length(gg[1,]) #the number of variables #create a data frame containing our variables, also give the variables names df<-data.frame(femsubs=gg$v826,fishimp=gg$v816,huntimp=gg$v817,pathstress=gg$v1260,rainfall=gg$v855,polygamy=gg$v79,eboysxp=gg$v353,fixres=gg$v150,landtrans=gg$v154,polinteg=gg$v157,socstrat=gg$v158) #since the estimation doesn't work with missing values, here we identify all observations with non-missing values kk<-as.matrix(df) oo<-matrix(1,length(kk[1,]),1) tm<-kk%*%oo rr<-which(tm[,1]!="NA") #here we restrict the weight matrix and data to include only non-missing values wmat<-mat2listw(lds[rr,rr]) ffd<-df[rr,] length(df[,1]) #number of observations before dropping those with missing values length(ffd[,1]) #number of observations after dropping those with missing values summary(ffd) #We estimate a spatial lag model library(spdep) #I ADDED THIS AFTER INSTALLING SPDEP-DW col.lm<-lagsarlm(femsubs~fishimp+huntimp+pathstress+rainfall+polygamy+eboysxp+fixres+landtrans+polinteg+socstrat, data=ffd,wmat,quiet=FALSE) #this next displays parameter estimates and diagnostics for the spatial lag model summary(col.lm) pseudoR2<-cor(col.lm$fitted.values,ffd$femsubs)^2 #could not find function lagsarlm--> install packages -->spdep library(spdep) #help{"lagsarlm")