#Spatial Lag Model, one weight matrix, MLE #Change the following to your own working directory (note UNIX slant of slashes--even on Windows machine) setwd("d:/projects/dow/") 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 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)