Using the Dow-Eff functions in R

The latest version of the Dow-Eff functions (Manual: pdf; html) can perform analyses on four different ethnological datasets:

abbreviation dataset codebook
WNAI Western North American Indians codebook
SCCS Standard Cross-Cultural Sample codebook
EA Ethnographic Atlas codebook
LRB Louis R. Binford's forager data codebook

The code below outlines the workflow for working with the WNAI.

You will need a number of R packages to run the Dow-Eff functions. These are loaded using the “library” command. If a package is “not found”, it should be first installed. The following command will initiate the installation of a package named “mice”, for example:

install.packages("mice")
# --set working directory and load needed libraries--
setwd("e:/Dropbox/functions/")
library(mice)
library(foreign)
library(stringr)
library(psych)
library(AER)
library(spdep)
library(geosphere)
library(relaimpo)

The Dow-Eff functions, as well as the four ethnological datasets, are contained in an R-workspace, located in the cloud.

load(url("http://dl.dropbox.com/u/9256203/DEf01.Rdata"), .GlobalEnv)
ls()  #-can see the objects contained in DEf01.Rdata
##  [1] "addesc"    "chK"       "chkpmc"    "CSVwrite"  "doMI"     
##  [6] "doOLS"     "EA"        "EAcov"     "EAfact"    "EAkey"    
## [11] "gSimpStat" "kln"       "llm"       "LRB"       "LRBcov"   
## [16] "LRBfact"   "LRBkey"    "mkdummy"   "SCCS"      "SCCScov"  
## [21] "SCCSfact"  "SCCSkey"   "setDS"     "WNAI"      "WNAIcov"  
## [26] "WNAIfact"  "WNAIkey"

The setDS( xx ) command sets one of the four ethnological datasets as the source for the subsequent analysis. The four valid options for xx are: “WNAI”, “LRB”, “EA”, “SCCS”. The setDS() command creates objects:

object name description
cov Names of covariates to use during imputation step
dx The selected ethnological dataset is now called dx
dxf The factor version of dx
key A metadata file for dx
wdd A geographic proximity weight matrix for the societies in dx
wee An ecological similarity weight matrix for the societies in dx
wll A linguistic proximity weight matrix for the societies in dx
setDS("WNAI")

The next step in the workflow is to create any new variables and add them to the dataset dx. New variables can be created directly, as in the following example. When created in this way, one should also record a description of the new variable, using the command addesc(). The syntax takes first the name of the new variable, and then the description.

dx$salmon <- ((dx$v122 == 2) + (dx$v123 == 2) + (dx$v124 == 2) + (dx$v125 == 
    2) + (dx$v126 == 2))
addesc("salmon", "Number of species of salmon present")

Dummy variables (variables taking on the values zero or one) should be added using the command mkdummy(). This command will, in most cases, automatically record a variable description. Dummy variables are appropriate for categorical variables. The syntax of mkdummy() takes first the categorical variable name, and then the category number (these can be found in the codebook for each ethnological dataset). Note that the resulting dummy variable will be called variable name+“.d”+category number.

mkdummy("v111", 2)
## [1] "This dummy variable is named v111.d2"
mkdummy("v112", 2)
## [1] "This dummy variable is named v112.d2"
mkdummy("v163", 6)
## [1] "This dummy variable is named v163.d6"

After making any new variables, list the variables you intend to use in your analysis in the following form.

evm <- c("v115", "v189", "v197", "v202", "v207", "v426", "v427", "v438", "v294", 
    "v111.d2", "v112.d2", "v163.d6", "v7")

Missing values of these variables are then imputed, using the command doMI(). Below, the number of imputed datasets is 2, and 3 iterations are used to estimate each imputed value (these values are too low: nimp=10 and maxit=7 are the defaults and are reasonable for most purposes). The stacked imputed datasets are collected into a single dataframe which here is called smi.

This new dataframe smi will contain not only the variables in evm, but also a set of normalized (mean=0, sd=1) variables related to climate, location, and ecology (these are used in the OLS analysis to address problems of endogeneity). In addition, squared values are calculated automatically for variables with at least three discrete values and maximum absolute values no more than 300. These squared variables are given names in the format variable name+“Sq”.

Finally, smi contains a variable called “.imp”, which identifies the imputed dataset, and a variable called “.id” which gives the society name.

smi <- doMI(evm, nimp = 2, maxit = 3)
## [1] "v426"
## [1] "v427"
## [1] "v438"
## [1] "v163.d6"
## Time difference of 1.795 secs
dim(smi)  # dimensions of new dataframe smi
## [1] 344  82
smi[1:2, ]  # first two rows of new dataframe smi
##   .imp           .id v426 v427 v438 v163.d6 v115 v189 v197 v202 v207 v294
## 1    1 North Tlingit    2    2    3       1    3    1    4    2    2    3
## 2    1 South Tlingit    2    2    2       1    3    1    4    2    2    3
##   v111.d2 v112.d2 v7 mht.name.d1 mht.name.d2 mht.name.d3 koeppengei.d2
## 1       0       0  5           0           0           0             0
## 2       0       0  5           0           0           1             0
##   koeppengei.d6 koeppengei.d7 region.d2  bio.1  bio.2  bio.3   bio.4
## 1             0             0         1 -1.855 -1.811 -2.262  0.1801
## 2             0             0         1 -1.080 -2.286 -2.087 -0.8734
##    bio.5   bio.6   bio.8   bio.9 bio.10  bio.11 bio.12 bio.13 bio.14
## 1 -1.945 -1.2969 -1.2541 -1.1893  -1.70 -1.7168 0.6532 0.5705  1.518
## 2 -1.936 -0.1602 -0.3019 -0.6718  -1.39 -0.6841 2.5651 2.4516  4.636
##    bio.15 bio.16 bio.17 bio.18 bio.19 meanalt   mnnpp   sdalt      x     y
## 1 -0.8257 0.3912  1.413  1.213 0.1761 -0.4951 -1.3567  1.7120 -2.705 2.805
## 2 -0.9711 1.9626  4.563  4.020 1.2326 -1.1827 -0.9476 -0.8804 -2.396 2.497
##      x2    y2     xy Hokan NaDene Penutian Salishan UtoAztecan v438Sq
## 1 7.315 7.868 -7.586     0      1        0        0          0      9
## 2 5.741 6.233 -5.982     0      1        0        0          0      4
##   v115Sq v189Sq v197Sq v202Sq v207Sq v294Sq v7Sq bio.1Sq bio.2Sq bio.3Sq
## 1      9      1     16      4      4      9   25   3.441   3.280   5.117
## 2      9      1     16      4      4      9   25   1.167   5.227   4.356
##   bio.4Sq bio.5Sq bio.6Sq bio.8Sq bio.9Sq bio.10Sq bio.11Sq bio.12Sq
## 1 0.03243   3.782 1.68199 1.57274  1.4144    2.890    2.947   0.4267
## 2 0.76283   3.750 0.02566 0.09115  0.4513    1.931    0.468   6.5799
##   bio.13Sq bio.14Sq bio.15Sq bio.16Sq bio.17Sq bio.18Sq bio.19Sq meanaltSq
## 1   0.3255    2.305   0.6818    0.153    1.997    1.472  0.03101    0.2451
## 2   6.0105   21.489   0.9431    3.852   20.819   16.161  1.51923    1.3988
##   mnnppSq sdaltSq
## 1  1.8407  2.9309
## 2  0.8979  0.7751

All of the variables selected to play a role in the model must be found in the new dataframe smi. Below, the variables are organized according to the role they will play.

# --dependent variable--
dpV <- "v427"
# --independent variables in UNrestricted model--
UiV <- c("v115", "v189", "v197", "v202", "v207", "v426", "v438", "v294", "v111.d2", 
    "v112.d2", "v163.d6")
# --additional exogenous variables (use in Hausman tests)--
oxog <- c("v7")
# --independent variables in restricted model (all must be in UiV above)--
RiV <- c("v438", "v294", "v111.d2", "v112.d2", "v163.d6")

The command doOLS() estimates the model on each of the imputed datasets, collecting output from each estimation and processing them to obtain final results. To control for Galton's Problem, a network lag model is used, with the user able to choose a combination of geographic proximity (dw), linguistic proximity (lw), and ecological similarity (ew) weight matrices. In most cases, the user should choose the default of dw=TRUE, lw=TRUE, ew=FALSE.

There are several options that increase the time doOLS() takes to run: stepW runs a background stepwise regression to find which variables perform best over the set of estimations; relimp calculates the relative importance of each variable in the restricted model, using a technique to partition R2; slmtests calculates LaGrange multiplier tests for spatial dependence using the three weight matrices. All of these should be set to FALSE if one wishes to speed up estimation times.

h <- doOLS(smi, depvar = dpV, indpv = UiV, rindpv = RiV, othexog = oxog, dw = TRUE, 
    lw = TRUE, ew = FALSE, stepW = TRUE, relimp = TRUE, slmtests = FALSE)
## [1] "--finding optimal weight matrix------"
## [1] "--looping through the imputed datasets--"
## [1] 1
## [1] 2
## Time difference of 11 secs
names(h)
##  [1] "DependVarb"       "URmodel"          "Rmodel"          
##  [4] "EndogeneityTests" "Diagnostics"      "OtherStats"      
##  [7] "DescripStats"     "totry"            "didwell"         
## [10] "dfbetas"          "data"

The output from doOLS, here called h, is a list containing 11 items.

name description
DependVarb Description of dependent variable
URmodel Coefficient estimates from the unrestricted model (includes standardized coefficients and VIFs). Two pvalues are given for H0: \( \beta \)=0. One is the usual pvalue, the other (hcpval) is heteroskedasticity consistent. If stepkept=TRUE, the table will also include the proportion of times a variable is retained in the model using stepwise regression.
Rmodel Coefficient estimates from the restricted model. If relimp=TRUE, the R2 assigned to each independent variable is shown here.
EndogeneityTests Hausman tests (H0: variable is exogneous), with F-statistic for weak instruments (a rule of thumb is that the instrument is weak if the F-stat is below 10), and Sargan test (H0: instrument is uncorrelated with second-stage 2SLS residuals).
Diagnostics Regression diagnostics for the restricted model: RESET test (H0: model has correct functional form); Wald test (H0: appropriate variables dropped); Breusch-Pagan test (H0: residuals homoskedastic; Shapiro-Wilkes test (H0: residuals normal); Hausman test (H0: Wy is exogenous); Sargan test (H0: residuals uncorrelated with instruments for Wy). If slmtests=TRUE, the LaGrange multiplier tests (H0: spatial lag term not needed) are reported here.
OtherStats Other statistics: Composite weight matrix weights (see details); R2 for restricted model and unrestricted model; number of imputations; number of observations; Fstat for weak instruments for Wy.
DescripStats Descriptive statistics for variables in unrestricted model.
totry Character string of variables that were most significant in the unrestricted model as well as additional variables that proved significant using the add1 function on the restricted model.
didwell Character string of variables that were most significant in the unrestricted model.
dfbetas Influential observations for dfbetas (see details)
data Data as used in the estimations. Observations with missing values of the dependent variable have been dropped.

The last two items in the list are large, but the first nine provide a nice overview.

h[1:9]
## $DependVarb
## [1] "Dependent variable='v427': Black imitative magic used to harm humans"
## 
## $URmodel
##                 coef  stdcoef   VIF stepkept  hcpval    pval star
## (Intercept)  0.51727      NaN   NaN        1 0.12676 0.15380     
## v111.d2     -0.11861 -0.11789 3.337        1 0.08659 0.10741     
## v112.d2      0.05112  0.01316 1.475        0 0.57293 0.78679     
## v115         0.05436  0.06189 1.658        0 0.20592 0.24328     
## v163.d6     -0.13605 -0.13725 3.820        1 0.05099 0.08003    *
## v189        -0.04234 -0.10759 3.282        1 0.05008 0.13894     
## v197        -0.03269 -0.07279 6.164        0 0.32988 0.46568     
## v202        -0.12275 -0.12765 2.139        1 0.04659 0.03032   **
## v207         0.03500  0.05971 2.332        1 0.25618 0.34464     
## v294        -0.04866 -0.06921 1.446        1 0.18577 0.16107     
## v426         0.84021  0.83365 2.430        1 0.00000 0.00000  ***
## v438        -0.02164 -0.05895 1.503        1 0.25862 0.24851     
## Wy           0.08755  0.05519 3.789        0 0.42029 0.49100     
##                                                                                                             desc
## (Intercept)                                                                                                 <NA>
## v111.d2                                                     Pronghorn antelope, Antilocapra americana == Present
## v112.d2                                                                            Bison, Bison bison == Present
## v115                                        Total number of 19 species of land mammals available in tribal area.
## v163.d6                                    Dominant boat types: those most frequent or preferred == Dugout canoe
## v189                                                                        Agricultural products grown for food
## v197         Local "fishing"--procurement of all types of aquatic animals (shellfish, aquatic mammals, and fish)
## v202                                 Local hunting--procurement of all types of game (fowl, large mammals, etc.)
## v207                                         Local gathering--all food sources (nuts, seeds, berries, and roots)
## v294        Estimated incidence of polygamy, or plural marriage (all forms of polygamy, polygyny, and polyandry)
## v426         White imitative magic used to bring good health, or kill animals, or insure good crops, or the like
## v438                                                                                            Status of slaves
## Wy                                                                                              Network lag term
## 
## $Rmodel
##                 coef  stdcoef   VIF  relimp  hcpval   pval star
## (Intercept) -0.10221      NaN   NaN     NaN 0.77425 0.7433     
## v111.d2     -0.04883 -0.04854 2.313 0.06646 0.65062 0.6573     
## v112.d2      0.39727  0.10228 1.031 0.01217 0.00076 0.1587     
## v163.d6     -0.03652 -0.03684 2.231 0.07009 0.75525 0.7309     
## v294        -0.04001 -0.05690 1.276 0.02719 0.45247 0.4814     
## v438         0.04559  0.12392 1.255 0.01365 0.11155 0.1217     
## Wy           1.11438  0.70321 2.403 0.28935 0.00000 0.0000  ***
##                                                                                                             desc
## (Intercept)                                                                                                 <NA>
## v111.d2                                                     Pronghorn antelope, Antilocapra americana == Present
## v112.d2                                                                            Bison, Bison bison == Present
## v163.d6                                    Dominant boat types: those most frequent or preferred == Dugout canoe
## v294        Estimated incidence of polygamy, or plural marriage (all forms of polygamy, polygyny, and polyandry)
## v438                                                                                            Status of slaves
## Wy                                                                                              Network lag term
## 
## $EndogeneityTests
##         weakidF p.Sargan n.IV Fstat      df pvalue star
## v111.d2  13.275    0.000 11.0 2.000 1027938  0.000  ***
## v112.d2  15.590    0.035 12.0 0.749   14421  0.387     
## v163.d6  12.000    0.129 12.0 0.040     109  0.841     
## v294      6.601    0.298  7.5 0.051    3510  0.822     
## v438      6.633    0.000  5.5 0.000      65  1.000     
## 
## $Diagnostics
##                                                           Fstat   df
## RESET test. H0: model has correct functional form         0.914 4308
## Wald test. H0: appropriate variables dropped             42.000    2
## Breusch-Pagan test. H0: residuals homoskedastic           0.024 7649
## Shapiro-Wilkes test. H0: residuals normal                12.014  627
## Hausman test. H0: Wy is exogenous                         2.000 1759
## Sargan test. H0: residuals uncorrelated with instruments  3.112 6246
##                                                          pvalue star
## RESET test. H0: model has correct functional form         0.339     
## Wald test. H0: appropriate variables dropped              0.000  ***
## Breusch-Pagan test. H0: residuals homoskedastic           0.876     
## Shapiro-Wilkes test. H0: residuals normal                 0.001  ***
## Hausman test. H0: Wy is exogenous                         0.000  ***
## Sargan test. H0: residuals uncorrelated with instruments  0.078    *
## 
## $OtherStats
##      d    l e Weak.Identification.Fstat R2.final.model R2.UR.model nimp
## 1 0.95 0.05 0                     20.51         0.4129      0.8252    2
##   nobs
## 1  122
## 
## $DescripStats
##                                                                                                         desc
## v427                                                               Black imitative magic used to harm humans
## v115                                    Total number of 19 species of land mammals available in tribal area.
## v189                                                                    Agricultural products grown for food
## v197     Local "fishing"--procurement of all types of aquatic animals (shellfish, aquatic mammals, and fish)
## v202                             Local hunting--procurement of all types of game (fowl, large mammals, etc.)
## v207                                     Local gathering--all food sources (nuts, seeds, berries, and roots)
## v426     White imitative magic used to bring good health, or kill animals, or insure good crops, or the like
## v438                                                                                        Status of slaves
## v294    Estimated incidence of polygamy, or plural marriage (all forms of polygamy, polygyny, and polyandry)
## v111.d2                                                 Pronghorn antelope, Antilocapra americana == Present
## v112.d2                                                                        Bison, Bison bison == Present
## v163.d6                                Dominant boat types: those most frequent or preferred == Dugout canoe
##         nobs  mean    sd min max
## v427     122 1.418 0.495   1   2
## v115     172 3.657 0.634   3   5
## v189     172 1.657 1.295   1   5
## v197     172 2.570 1.093   1   4
## v202     172 2.541 0.523   2   4
## v207     172 3.017 0.834   1   4
## v426     145 1.510 0.502   1   2
## v438     134 0.567 1.147   0   3
## v294     172 2.186 0.742   1   4
## v111.d2  172 0.512 0.501   0   1
## v112.d2  172 0.058 0.235   0   1
## v163.d6  171 0.374 0.485   0   1
## 
## $totry
## [1] "bio.15" "v189"   "v189Sq" "v426"   "v189"   "v202"   "v207"   "v426"  
## 
## $didwell
## [1] "v111.d2" "v163.d6" "v294"    "v438"    "v112.d2"

One can also write the list h to a csv format file that can be opened as a spreadsheet. The following command writes h to a file in the working directory called “olsresults.csv”.

CSVwrite(h, "olsresults", FALSE)