I. Introduction

Census data - why is it important? - brief history? - what questions can be answered with census data? … - why do we need to perform spatial analysis on census data - how variable are socioeconomic patterns in the Victoria Capital Regional District (CRD)?

Spatial Autocorrelation …
- describe global vs local methods of analysis - how can these methods be applied to census data? - entice reader with this! - just give general background to see if this tutorial will be of any use

How to perform spatial analysis of census variables to assess their spatial autocorrelation.

This tutorial will describe how to use R to perform a spatial autocorrelation analysis on multiple census variables, by using both global and local methods to create outputs that will help to visualize the socioeconomic variability of the population in the CRD. Following these instructions, and using the R code chunks, should reproduce the output results that are included.

The median income variable (MdInc) from the Victoria Capital Regional District (CRD) 2016 Census Data will be analyzed to determine if either of them has any spatial autocorrelation with themselves, over space.

The following R code can be used as is to create a full spatial autocorrelation for one variable, and produce maps and tables as outputs. As one of the benefits of R is creating reproducable results, if another variable has been chosen to analyze, the same code can be reused, and run again after only making a few changes in the variable names, and the titles produced for output images files. [Note for Different Variable: After each of the following code segments, a note will be added to identify the code segments needed to be changed as necessary for a different variable.]

II. Set Working Directory

II.1. Set the working directory to where the data will be located and the output images will be stored. This is how R will know how to the locate the data the code needs to run. II.2. In the following R code, the # hashtag symbol is used to make the line of code a ‘comment’, which is used to make a comment to help understand the code, or to ensure that a line of code will not be run. If a package needs to be installed, first remove the comment # from the beginning of the line, then run the code to install the package.

dir <- "/Working/Data"
setwd(dir)
getwd()

III. Install and Load Libraries

In order to use R to do spatial autocorrelation analysis, the following R packages are needed to run the code. Each package contains a bundle of open-source shareable code, documentation and data, each of which is a tool that performs different functions.

  1. plyr: A tool to split, apply and combine data. 1
  2. dplyr: A tool to transform and manipulate data. 2, 3
  3. spdep: Spatial Dependence to create weighted matrix from contiguous polygons. 4, 5
  4. GISTools: A mapping and spatial data tool, especially for choloropleth maps and legends. 6
  5. raster: A tool to read, write, analyze and model spatial gridded data. 7
  6. maptools: Tools to manipulate geographic data. 8
  7. rgdal: Projection transformations for PROJ.4 library. 9
  8. tmap: Thematic map tool to visualize spatial data. 10
  9. BAMMtools: Used for macroevolutionary analysis and visualization. (I’m not actually sure why this package is needed, as its common function don’t seem to be used in this code, however, this package was suggested for use when this code challenge was issued, so I’ve included it as well). 11 10. shinyjs: Allows javascript to be used. 12

If any, or all, of these R packages have not been previously installed, the function install.packages(“packageName”) is used to install each separate package. In the following R code, the # hashtag symbol is used to make the line of code a ‘comment’, so that the code will not be run. If a package needs to be installed, first remove the comment # from the beginning of the line, then run the code to install the package.

install.packages("dplyr")
install.packages("spdep")
install.packages("GISTools")
install.packages("raster")
install.packages("maptools")
install.packages("rgdal")
install.packages("tmap")
install.packages("BAMMtools")
install.packages("shinyjs")

After the R packages have been installed, their Libraries need to be called in order for the R code to access the package-specific functions.

library("plyr")
library("dplyr")
library("spdep")
library("GISTools")
library("raster")
library("maptools")
library("rgdal")
library("tmap")
library("BAMMtools")
library("shinyjs")

IV. Download, Read and Clean Data

IV.1. Download the most recent census shape and data files from Statistics Canada (2016), and save all the data files in the computer’s working directory, as specified in section II. IV.2. Read in the data from the census tract boundaries shapefile, using the rgdal package (don’t include the .shp file extension), and save the data in a object, which is a spatial polygon dataframe, named tracts.

tracts <- readOGR(dsn = ".", layer = "Vic_Census")
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/geographydepartment/Documents/Geog418/Assign3-Geog418/Data/Working/Data", layer: "Vic_Census"
## with 78 features
## It has 2 fields
tracts
## class       : SpatialPolygonsDataFrame 
## features    : 78 
## extent      : 3903547, 3974156, 1910542, 1962451  (xmin, xmax, ymin, ymax)
## crs         : +proj=lcc +lat_1=49 +lat_2=77 +lat_0=63.390675 +lon_0=-91.86666666666666 +x_0=6200000 +y_0=3000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 
## variables   : 2
## names       :      CTUID,       GUID 
## min values  : 9350001.00,    9350001 
## max values  : 9350180.06, 9350180.06

IV.3. Read the data from the 2016 census data .csv file (include the .csv file extension), assigning the newly created data frame object to a variable named census.16.

census.16 <- read.csv("CensusTractData.csv")

IV.4. Summary(census.16) provides summary statistics of the attributes for each of the variable columns of the Census Tract Data, including the minimum, median, mean, maximum, and 1st and 3rd Quartiles. The summary also records the number of missing NA values in each column of data, which will be removed in step 6.

summary(census.16)
## some summary stats have been ommitted ... 
##       GUID              CUID            Pop             PopCh        
##  Min.   :9350000   Min.   :  0.0   Min.   :    94   Min.   :-60.200  
##  1st Qu.:9350102   1st Qu.:101.5   1st Qu.:  3479   1st Qu.:  1.725  
##  Median :9350128   Median :128.0   Median :  4689   Median :  4.100  
##  Mean   :9350110   Mean   :109.6   Mean   :  9430   Mean   :  4.599  
##  3rd Qu.:9350154   3rd Qu.:154.0   3rd Qu.:  6156   3rd Qu.:  7.275  
##  Max.   :9350180   Max.   :180.1   Max.   :367770   Max.   : 44.200  
##                                    NA's   :1        NA's   :1        
##
##     Divorced          Widowed            MdInc        MDAftTaxInc   
##  Min.   :    0.0   Min.   :    0.0   Min.   :12384   Min.   :12192  
##  1st Qu.:  196.2   1st Qu.:  135.0   1st Qu.:34056   1st Qu.:30667  
##  Median :  302.5   Median :  215.0   Median :37481   Median :33380  
##  Mean   :  671.4   Mean   :  476.2   Mean   :37557   Mean   :33388  
##  3rd Qu.:  473.8   3rd Qu.:  328.8   3rd Qu.:42240   3rd Qu.:37328  
##  Max.   :26195.0   Max.   :18565.0   Max.   :57557   Max.   :48939  
##  NA's   :1         NA's   :1         NA's   :4       NA's   :4      
##
##     MVisMin            Owner              Renter             Band        
##  Min.   :   0.00   Min.   :    20.0   Min.   :    0.0   Min.   :  0.000  
##  1st Qu.:  10.00   1st Qu.:   937.5   1st Qu.:  308.8   1st Qu.:  0.000  
##  Median :  20.00   Median :  1277.5   Median :  602.5   Median :  0.000  
##  Mean   :  38.78   Mean   :  2612.4   Mean   : 1557.2   Mean   :  2.821  
##  3rd Qu.:  28.75   3rd Qu.:  1690.0   3rd Qu.: 1071.2   3rd Qu.:  0.000  
##  Max.   :1505.00   Max.   :101885.0   Max.   :60745.0   Max.   :105.000  
##  NA's   :1         NA's   :1          NA's   :1         NA's   :1        
##

IV.5. Merge the two data frames census tract boundaries with the census data, using the common column GUID. Assign this merged data to the variable name crd.data.

crd.data <- merge(tracts, census.16, by = "GUID")

IV.6. Remove any missing ‘na’ values from the merged data, as there is no numerical value associated with this row of data, and thus no calculations can be done on any data that doesn’t contain a value. The function is formatted to start with an ! explanation mark, which means ‘not’, thus !is.na() is used to specify which data in crd.data is not na. Once the dataset has the rows containing a na value removed, the remaining data is reassigned back to the crd.data data frame. IV.6.i) To specify a column to use from the dataset, a $ dollar sign is placed between the data set name and the column name. IV.6.ii) [Note for Different Variable: To choose a different column from the census data, replace the column name after the $ crd/data$NewColumnName].

crd.data <- crd.data[!is.na(crd.data$MInc),]

IV.7. To understand more about the data, use class() to see that crd.data is a “SpatialPolygonsDataFrame”, and use summary() to produce a table of the data frame summary statistics, including the column names of each different variable.

class(crd.data)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
summary(crd.data)
## Object of class SpatialPolygonsDataFrame
## Coordinates:
##       min     max
## x 3903547 3974156
## y 1910542 1962451
## Is projected: TRUE 
## proj4string :
## [+proj=lcc +lat_1=49 +lat_2=77 +lat_0=63.390675
## +lon_0=-91.86666666666666 +x_0=6200000 +y_0=3000000 +ellps=GRS80
## +towgs84=0,0,0,0,0,0,0 +units=m +no_defs]
## Data attributes:
##
## some summary stats have been ommitted ... 
##       GUID                CTUID         CUID            Pop       
##  Min.   :9350001   9350001.00: 1   Min.   :  1.0   Min.   :  332  
##  1st Qu.:9350101   9350002.00: 1   1st Qu.:101.2   1st Qu.: 3538  
##  Median :9350126   9350003.01: 1   Median :126.5   Median : 4744  
##  Mean   :9350108   9350003.02: 1   Mean   :108.4   Mean   : 4964  
##  3rd Qu.:9350153   9350004.00: 1   3rd Qu.:152.8   3rd Qu.: 6156  
##  Max.   :9350180   9350005.00: 1   Max.   :180.1   Max.   :10393  
##                    (Other)   :68                                  
##                                                                 
##    Separated        Divorced         Widowed          MdInc      
##  Min.   :  5.0   Min.   :  10.0   Min.   :  5.0   Min.   :12384  
##  1st Qu.: 60.0   1st Qu.: 200.0   1st Qu.:138.8   1st Qu.:33878  
##  Median : 95.0   Median : 310.0   Median :215.0   Median :37563  
##  Mean   :117.0   Mean   : 353.6   Mean   :250.9   Mean   :37558  
##  3rd Qu.:158.8   3rd Qu.: 473.8   3rd Qu.:328.8   3rd Qu.:42250  
##  Max.   :380.0   Max.   :1065.0   Max.   :670.0   Max.   :57557  
##                                                                  
##    Vminority         MVisMin          Owner            Renter      
##  Min.   : 0.000   Min.   : 0.00   Min.   :  65.0   Min.   :  15.0  
##  1st Qu.: 0.000   1st Qu.:10.00   1st Qu.: 966.2   1st Qu.: 325.0  
##  Median : 0.000   Median :20.00   Median :1287.5   Median : 620.0  
##  Mean   : 5.608   Mean   :20.54   Mean   :1375.5   Mean   : 820.3  
##  3rd Qu.:10.000   3rd Qu.:28.75   3rd Qu.:1690.0   3rd Qu.:1071.2  
##  Max.   :35.000   Max.   :80.00   Max.   :3255.0   Max.   :3945.0  
##                                                                    
## 

V. Map the Data by Making a Chloropleth Map

Using the tmap package …

V.1. Select a colour palette to create the chloropleth map using tmaptools::palette_explorer(), after first removing the comment #. To generate the colour code needed for the map palette, check the Code Generator button for tmap layer function code, then copy/paste the code found under the colour palette. Note, the palette explorer window must be closed in order for R to continue processing the code, which is why it is initially commented out.

tmaptools::palette_explorer()

V.2. The shape of the polygons comes from the merged crd.data file. V.2.i) To classify the polygons, they are coloured by the values in the MdInc column (col=“MdInc”). The title is used for the classification legend. The classification style Jenks uses natural breaks to set up the best classification of variable values, based on the data itself. V.2.ii) Add a scale bar and compass in specific positions. The first value is RIGHT / LEFT and the second value is TOP/BOTTOM (note these positions must be capitalized). https://www.rdocumentation.org/packages/tmap/versions/2.3/topics/tm_scale_bar V.2.iii) The layout function adds a title to the map, in a specific postion. To make room for north arrow and scale bar, place inner.margins around map and image boundary (bottom, left, top, right) https://www.rdocumentation.org/packages/tmap/versions/1.2/topics/tm_layout V.3. [Note for Different Variable: Change the following values tm_polygons(col = “NewColumnName”, title= “New Title”); tm_layout(title= “New Title”)]. For the new variable, the crd.data will reflect the new data column as created in Step IV.6 after removing the na values from the new variable column.

map_PopDens <- tm_shape(crd.data) + 
  tm_polygons(col = "MdInc", 
              title = "Median $ Income", 
              style = "jenks", 
              palette = "viridis", n = 6) + 
  
  tm_scale_bar(width = 0.22, position = c("RIGHT", "BOTTOM")) + 
  
  tm_compass(position = c("RIGHT", "TOP")) + 
  
  tm_layout(title = "CRD Census Tracts Median $ Income 2016", title.position = c("LEFT", "TOP"), inner.margins = c(.08, .03, .08, .03))

V.4. map_PopDens prints the plot in R window. In order to save the map, the file object png is called using a filename to save to, call the plot, then close the graphic device connection with dev.off() in order to return to R. V.5. In order to to save the file, and then close the plot window the function dev.off() must be used. For Mac OSX users, ‘quartz_off_screen’ is a graphic device driver. Calling dev.off() again should return user to the RStudio graphic device or ‘null device’. https://support.rstudio.com/hc/en-us/community/posts/200663616-plots-not-showing-up, https://stackoverflow.com/questions/23621012/display-and-save-the-plot-simultaneously-in-r-rstudio, https://www.rdocumentation.org/packages/grDevices/versions/3.6.1/topics/quartz V.6. [Note for Different Variable: Change the following values name of png file to be saved “newName.png”] V.7. - DESCRIBE OUTPUT: …

map_PopDens

png("map_PopDens_MdInc_2.png")  
map_PopDens
dev.off()

VI. Defining the Neighbourhoods

VI.1. Using the spdep package, VI.1.i) poly2nb builds a list of neighbours from polygon regions whose boundaries are continguous: https://www.rdocumentation.org/packages/spdep/versions/1.1-3/topics/poly2nb VI.1.ii) nb2lines stores spatial coordinates as vectors (needed for weights): https://www.rdocumentation.org/packages/spdep/versions/1.1-3/topics/nb2lines VI.1.iii) queen - FALSE defines the neighbourhood as Rook https://cran.r-project.org/web/packages/spdep/vignettes/nb.pdf VI.2. [Note for Different Variable: the crd.data will reflect the new data column as created in Step IV.6 after removing the na values from the new variable column. The tm_layout value for title will need to be changed for the new variable.] VI.3. Defining Queen Neighbours

crd.nb <- poly2nb(crd.data)
crd.net <- nb2lines(crd.nb,coords=coordinates(crd.data))

VI.3.i) Map Queen Neighbours (default weight scheme)

map_crd.net <- tm_shape(crd.data) + tm_borders(col='lightgrey') + 
  tm_shape(crd.net) + tm_lines(col='red') +
  # add scale bar - first value is TOP, second value is BOTTOM
  tm_scale_bar(width = 0.22, position = c("RIGHT", "BOTTOM")) + 
  # add compass
  tm_compass(position = c("RIGHT", "TOP")) + 
  tm_layout(title = "CRD Census Tracts Median Income\nQueen Neighbours 2016", title.position = c("LEFT", "TOP"), inner.margins = c(.08, .03, .08, .03))

VI.4. Defining Rook Neighbours

crd.nb2 <- poly2nb(crd.data, queen = FALSE)
crd.net2 <- nb2lines(crd.nb2,coords=coordinates(crd.data))

VI.4.i) Mapping Rook Neighbours

map_crd.net2 <- tm_shape(crd.data) + tm_borders(col='lightgrey') + 
  tm_shape(crd.net) + tm_lines(col='red', lwd = 2) +
  tm_shape(crd.net2) + tm_lines(col='blue', lwd = 2) + 
  # add scale bar - first value is TOP, second value is BOTTOM
  tm_scale_bar(width = 0.22, position = c("RIGHT", "BOTTOM")) + 
  # add compass
  tm_compass(position = c("RIGHT", "TOP")) + 
  tm_layout(title = "CRD Census Tracts Median Income\nRook Neighbours 2016", title.position = c("LEFT", "TOP"), inner.margins = c(.08, .03, .08, .03))

VI.5. Create a png image file for the Queen Neighbourhoods weighting (crd.net), and the Rooks Neighbourhoods (crd.net2). VI.6. [Note for Different Variable: change the file name to reflect the new variable.] VI.7. Create Neighbourhood map for Queen’s weight

map_crd.net
## Warning: Current projection of shape crd.net unknown and cannot be
## determined.

VI.8. Create Neighbourhood map for Rook’s weight plus Queens weight

map_crd.net2
## Warning: Current projection of shape crd.net unknown and cannot be
## determined.
## Warning: Current projection of shape crd.net2 unknown and cannot be
## determined.

VI.9. - DESCRIBE OUTPUT: …

# create png of Median Income CRD net
png("map_map_crd.net_MdInc_2.png")  
map_crd.net
## Warning: Current projection of shape crd.net unknown and cannot be
## determined.
dev.off()
## quartz_off_screen 
##                 2
# create png of Median Income CRD net 2
png("map_map_crd.net2_MdInc_2.png")  
map_crd.net2
## Warning: Current projection of shape crd.net unknown and cannot be
## determined.
## Warning: Current projection of shape crd.net2 unknown and cannot be
## determined.
dev.off()
## quartz_off_screen 
##                 2

VII. Creating a Neighbourhood Weights Matrix

VII.1. Using the spdep package, nb2listw uses the neighbours list previously created and adds weight: https://www.rdocumentation.org/packages/spdep/versions/1.1-3/topics/nb2listw

crd.lw <- nb2listw(crd.nb, zero.policy = TRUE, style = "W")
print.listw(crd.lw, zero.policy = TRUE)
## Characteristics of weights list object:
## Neighbour list object:
## Number of regions: 74 
## Number of nonzero links: 424 
## Percentage nonzero weights: 7.742878 
## Average number of links: 5.72973 
## 
## Weights style: W 
## Weights constants summary:
##    n   nn S0       S1       S2
## W 74 5476 74 29.46931 322.5924

VIII. Mapping the Lagged Means

VIII.1. Using the spdep package, lag.listw : https://www.rdocumentation.org/packages/spdep/versions/1.1-3/topics/lag.listw VIII.2. Creates a map using tmap VIII.3. [Note for Different Variable: .] ……

# Calculate Lag Means
crd.data$IncLagMeans = lag.listw(crd.lw, crd.data$MdInc, zero.policy = TRUE)

map_LagMean <- tm_shape(crd.data) + 
  tm_polygons(col = "IncLagMeans", 
              title = "Median Income\nLagged Means", 
              style = "jenks", 
              palette = "viridis", n = 6) +
  # add scale bar - first value is TOP, second value is BOTTOM
  tm_scale_bar(width = 0.22, position = c("RIGHT", "BOTTOM")) + 
  # add compass
  tm_compass(position = c("RIGHT", "TOP")) + 
  tm_layout(title = "CRD Census Tracts Median Income\nLagged Means 2016", title.position = c("LEFT", "TOP"), inner.margins = c(.08, .03, .08, .03))

Lagmean Map - DESCRIBE OUTPUT: … [Note for Different Variable: change the file name to reflect the new variable.]

map_LagMean

# create png of map LagMean Median Income CRD net 2
png("map_LagMean_MdInc_2.png")  
map_LagMean
dev.off()
## quartz_off_screen 
##                 2

IX. Global Morans I Test

IX.1. Morans Global I Test creates one i value for every polygon as if they were all one polygon.

# Morans I test
mi <- moran.test(crd.data$MdInc, crd.lw, zero.policy = TRUE)
mi
## 
##  Moran I test under randomisation
## 
## data:  crd.data$MdInc  
## weights: crd.lw    
## 
## Moran I statistic standard deviate = 2.2665, p-value = 0.01171
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##       0.143170949      -0.013698630       0.004790316
moran.range <- function(lw) {
  wmat <- listw2mat(lw)
  return(range(eigen((wmat + t(wmat))/2)$values))
}
moran.range(crd.lw)
## [1] -0.8193252  1.0821054

IX.2. Calculate the z value, by calculating the values of the Moran’s I, the estimated value of Moran’s I, and the Variable. The z value is Moran’s I less the Estimated Moran’s I, divided by the square root of the variance.

## calculate z value
mI <- mi$estimate[[1]]
eI <- mi$estimate[[2]]
var <- mi$estimate[[3]]

# calculate z value 
z <- (mI - eI) / var^(1/2)
z
## [1] 2.266505

IX.3. Add the data variables to create a table of values

# Add Stats data objects to data.frame ########
data.for.table1 = data.frame(mI, eI, var, z)
data.for.table1
##          mI          eI         var        z
## 1 0.1431709 -0.01369863 0.004790316 2.266505
table1 <- data.for.table1 

IX.4.[Note for Different Variable: change the file name to reflect the new variable.]

# write csv file with Morans Global results for I, E, Var, Z, P
write.csv(table1, "MoransGlobal-MdInc.csv", row.names = FALSE)

X. Local Moran’s i Test (Lisa Test)

X.1. Separate i values are created for every single polygon. X.2. [Note for Different Variable: Change the new variable column name after the $ e.g. crd.data$NewColumnName]

# Lisa Test
lisa.test <- localmoran(crd.data$MdInc, crd.lw)

crd.data$Ii <- lisa.test[,1]
crd.data$E.Ii<- lisa.test[,2]
crd.data$Var.Ii<- lisa.test[,3]
crd.data$Z.Ii<- lisa.test[,4]
crd.data$P<- lisa.test[,5]

X.3. Calculate significance values ……..

crd.data$IiLessEIi <- crd.data$Ii - crd.data$E.Ii

# if z value is less than 95% significance level, value is FALSE
crd.data$Z.Ho <- crd.data$Z.Ii < 1.96
crd.data$Z.Ho
##  [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [12]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE
## [23]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
## [34]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [45]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
## [56]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [67]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
crd.data$Ii.Neg <- crd.data$IiLessEIi < 0
crd.data$Ii.Neg
##  [1] FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE
## [12] FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
## [23] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [34] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE FALSE
## [45] FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
## [56] FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
## [67] FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE

X.4. Add the data values to a data frame to create a new table of values for the Local Moran’s Ii.

# make a data frome from the Lisa Test Data
data.for.table2 = data.frame(crd.data$Ii, crd.data$E.Ii, crd.data$Var.Ii, crd.data$IiLessEIi, crd.data$Ii.Neg, crd.data$Z.Ii, crd.data$Z.Ho, crd.data$P)
table2 <- data.for.table2
table2
##      crd.data.Ii crd.data.E.Ii crd.data.Var.Ii crd.data.IiLessEIi
## 1   0.2744151305   -0.01369863      0.14693811       0.2881137606
## 2   0.0557309861   -0.01369863      0.22656398       0.0694296162
## 3  -0.2349448907   -0.01369863      0.14693811      -0.2212462606
## 4   0.3326591723   -0.01369863      0.22656398       0.3463578025
## 5   0.1990156088   -0.01369863      0.17878846       0.2127142389
## 6  -0.0338955771   -0.01369863      0.14693811      -0.0201969469
## 7   0.0128136506   -0.01369863      0.17878846       0.0265122808
## 8   0.7919282302   -0.01369863      0.17878846       0.8056268603
## 9  -0.0464760893   -0.01369863      0.17878846      -0.0327774592
## 10  0.4657065327   -0.01369863      0.12418786       0.4794051628
## 11  0.0067879072   -0.01369863      0.10712518       0.0204865373
## 12  0.2947025748   -0.01369863      0.09385420       0.3084012049
## 13 -0.0923032394   -0.01369863      0.46544159      -0.0786046092
## 14 -0.0285935895   -0.01369863      0.30618985      -0.0148949593
## 15  0.4126788509   -0.01369863      0.09385420       0.4263774810
## 16 -0.2187477999   -0.01369863      0.10712518      -0.2050491697
## 17  0.4433240328   -0.01369863      0.12418786       0.4570226629
## 18  1.2205874502   -0.01369863      0.30618985       1.2342860804
## 19  0.4616826274   -0.01369863      0.17878846       0.4753812575
## 20  0.2638695873   -0.01369863      0.12418786       0.2775682174
## 21  2.1078481863   -0.01369863      0.22656398       2.1215468164
## 22 -0.0124306146   -0.01369863      0.17878846       0.0012680156
## 23  0.0980896658   -0.01369863      0.08323742       0.1117882959
## 24 -0.0231942136   -0.01369863      0.22656398      -0.0094955835
## 25  0.1047856106   -0.01369863      0.12418786       0.1184842408
## 26  0.0246750827   -0.01369863      0.10712518       0.0383737128
## 27  0.1900350829   -0.01369863      0.14693811       0.2037337130
## 28  0.0019320024   -0.01369863      0.08323742       0.0156306325
## 29  0.9284834191   -0.01369863      0.17878846       0.9421820492
## 30  0.2032721811   -0.01369863      0.17878846       0.2169708112
## 31  0.1259530137   -0.01369863      0.12418786       0.1396516438
## 32  0.4535832667   -0.01369863      0.17878846       0.4672818968
## 33  0.0760594107   -0.01369863      0.14693811       0.0897580408
## 34  0.6622247407   -0.01369863      0.30618985       0.6759233709
## 35  0.2429889826   -0.01369863      0.17878846       0.2566876128
## 36  0.0356433844   -0.01369863      0.17878846       0.0493420145
## 37 -0.0004154355   -0.01369863      0.17878846       0.0132831947
## 38 -0.0193496543   -0.01369863      0.12418786      -0.0056510242
## 39  0.0074249521   -0.01369863      0.17878846       0.0211235823
## 40  0.1856654215   -0.01369863      0.22656398       0.1993640517
## 41  0.0595949852   -0.01369863      0.12418786       0.0732936153
## 42 -0.2359649540   -0.01369863      0.12418786      -0.2222663239
## 43 -0.0579399062   -0.01369863      0.14693811      -0.0442412761
## 44  0.1041228751   -0.01369863      0.30618985       0.1178215053
## 45  0.1458425508   -0.01369863      0.14693811       0.1595411809
## 46 -0.1574332175   -0.01369863      0.17878846      -0.1437345874
## 47  0.0536021511   -0.01369863      0.22656398       0.0673007812
## 48  0.6232149028   -0.01369863      0.14693811       0.6369135329
## 49  0.5484882380   -0.01369863      0.10712518       0.5621868682
## 50 -0.0440773555   -0.01369863      0.02923030      -0.0303787253
## 51 -1.9458534186   -0.01369863      0.30618985      -1.9321547885
## 52 -0.3484095342   -0.01369863      0.14693811      -0.3347109041
## 53 -0.0252503561   -0.01369863      0.12418786      -0.0115517259
## 54  1.0843288469   -0.01369863      0.14693811       1.0980274770
## 55  0.2836125755   -0.01369863      0.10712518       0.2973112057
## 56  0.0384246510   -0.01369863      0.22656398       0.0521232811
## 57  0.0025467591   -0.01369863      0.14693811       0.0162453892
## 58 -0.0346348845   -0.01369863      0.22656398      -0.0209362543
## 59 -3.7243153785   -0.01369863      0.94319681      -3.7106167484
## 60  0.0117550365   -0.01369863      0.09385420       0.0254536667
## 61  0.2614551227   -0.01369863      0.22656398       0.2751537528
## 62  0.1399328018   -0.01369863      0.46544159       0.1536314319
## 63  0.4384667710   -0.01369863      0.46544159       0.4521654011
## 64 -0.3612599654   -0.01369863      0.94319681      -0.3475613352
## 65  0.2206921127   -0.01369863      0.14693811       0.2343907428
## 66  0.4853407163   -0.01369863      0.14693811       0.4990393464
## 67  0.3210323517   -0.01369863      0.22656398       0.3347309818
## 68 -0.0802313849   -0.01369863      0.14693811      -0.0665327548
## 69  0.3159098132   -0.01369863      0.12418786       0.3296084434
## 70  0.6415313555   -0.01369863      0.12418786       0.6552299857
## 71  0.4949972138   -0.01369863      0.17878846       0.5086958439
## 72 -0.0146686830   -0.01369863      0.30618985      -0.0009700528
## 73  0.2927142780   -0.01369863      0.09385420       0.3064129082
## 74  1.0828675345   -0.01369863      0.30618985       1.0965661646
##    crd.data.Ii.Neg crd.data.Z.Ii crd.data.Z.Ho   crd.data.P
## 1            FALSE   0.751617311          TRUE 2.261406e-01
## 2            FALSE   0.145864407          TRUE 4.420142e-01
## 3             TRUE  -0.577176596          TRUE 7.180899e-01
## 4            FALSE   0.727661741          TRUE 2.334103e-01
## 5            FALSE   0.503068148          TRUE 3.074582e-01
## 6             TRUE  -0.052688823          TRUE 5.210101e-01
## 7            FALSE   0.062701416          TRUE 4.750021e-01
## 8            FALSE   1.905303636          TRUE 2.837031e-02
## 9             TRUE  -0.077518533          TRUE 5.308945e-01
## 10           FALSE   1.360389058          TRUE 8.685342e-02
## 11           FALSE   0.062592570          TRUE 4.750455e-01
## 12           FALSE   1.006674841          TRUE 1.570455e-01
## 13            TRUE  -0.115216696          TRUE 5.458633e-01
## 14            TRUE  -0.026918070          TRUE 5.107375e-01
## 15           FALSE   1.391769798          TRUE 8.199606e-02
## 16            TRUE  -0.626487255          TRUE 7.345023e-01
## 17           FALSE   1.296875124          TRUE 9.733708e-02
## 18           FALSE   2.230593514         FALSE 1.285403e-02
## 19           FALSE   1.124274380          TRUE 1.304483e-01
## 20           FALSE   0.787644346          TRUE 2.154524e-01
## 21           FALSE   4.457149340         FALSE 4.152837e-06
## 22           FALSE   0.002998851          TRUE 4.988036e-01
## 23           FALSE   0.387469072          TRUE 3.492045e-01
## 24            TRUE  -0.019949234          TRUE 5.079581e-01
## 25           FALSE   0.336218041          TRUE 3.683532e-01
## 26           FALSE   0.117243303          TRUE 4.533336e-01
## 27           FALSE   0.531490704          TRUE 2.975394e-01
## 28           FALSE   0.054177288          TRUE 4.783970e-01
## 29           FALSE   2.228255999         FALSE 1.293172e-02
## 30           FALSE   0.513134921          TRUE 3.039285e-01
## 31           FALSE   0.396283943          TRUE 3.459478e-01
## 32           FALSE   1.105119431          TRUE 1.345539e-01
## 33           FALSE   0.234156457          TRUE 4.074318e-01
## 34           FALSE   1.221524176          TRUE 1.109438e-01
## 35           FALSE   0.607064965          TRUE 2.719039e-01
## 36           FALSE   0.116693626          TRUE 4.535514e-01
## 37           FALSE   0.031414691          TRUE 4.874694e-01
## 38            TRUE  -0.016035688          TRUE 5.063970e-01
## 39           FALSE   0.049957170          TRUE 4.800783e-01
## 40           FALSE   0.418843150          TRUE 3.376654e-01
## 41           FALSE   0.207982392          TRUE 4.176214e-01
## 42            TRUE  -0.630716351          TRUE 7.358870e-01
## 43            TRUE  -0.115414512          TRUE 5.459417e-01
## 44           FALSE   0.212926233          TRUE 4.156923e-01
## 45           FALSE   0.416203354          TRUE 3.386306e-01
## 46            TRUE  -0.339931605          TRUE 6.330460e-01
## 47           FALSE   0.141391946          TRUE 4.437802e-01
## 48           FALSE   1.661549369          TRUE 4.830158e-02
## 49           FALSE   1.717650982          TRUE 4.293015e-02
## 50            TRUE  -0.177685867          TRUE 5.705152e-01
## 51            TRUE  -3.491777155          TRUE 9.997601e-01
## 52            TRUE  -0.873177696          TRUE 8.087169e-01
## 53            TRUE  -0.032779876          TRUE 5.130749e-01
## 54           FALSE   2.864481232         FALSE 2.088465e-03
## 55           FALSE   0.908375690          TRUE 1.818399e-01
## 56           FALSE   0.109505596          TRUE 4.564007e-01
## 57           FALSE   0.042380189          TRUE 4.830978e-01
## 58            TRUE  -0.043984894          TRUE 5.175418e-01
## 59            TRUE  -3.820717593          TRUE 9.999335e-01
## 60           FALSE   0.083085168          TRUE 4.668919e-01
## 61           FALSE   0.578069434          TRUE 2.816086e-01
## 62           FALSE   0.225189161          TRUE 4.109161e-01
## 63           FALSE   0.662772886          TRUE 2.537380e-01
## 64            TRUE  -0.357874121          TRUE 6.397812e-01
## 65           FALSE   0.611467288          TRUE 2.704451e-01
## 66           FALSE   1.301869827          TRUE 9.648044e-02
## 67           FALSE   0.703235000          TRUE 2.409547e-01
## 68            TRUE  -0.173567448          TRUE 5.688973e-01
## 69           FALSE   0.935316835          TRUE 1.748125e-01
## 70           FALSE   1.859320199          TRUE 3.149088e-02
## 71           FALSE   1.203063216          TRUE 1.144759e-01
## 72            TRUE  -0.001753073          TRUE 5.006994e-01
## 73           FALSE   1.000184696          TRUE 1.586106e-01
## 74           FALSE   1.981707007         FALSE 2.375602e-02
head(table2)
##   crd.data.Ii crd.data.E.Ii crd.data.Var.Ii crd.data.IiLessEIi
## 1  0.27441513   -0.01369863       0.1469381         0.28811376
## 2  0.05573099   -0.01369863       0.2265640         0.06942962
## 3 -0.23494489   -0.01369863       0.1469381        -0.22124626
## 4  0.33265917   -0.01369863       0.2265640         0.34635780
## 5  0.19901561   -0.01369863       0.1787885         0.21271424
## 6 -0.03389558   -0.01369863       0.1469381        -0.02019695
##   crd.data.Ii.Neg crd.data.Z.Ii crd.data.Z.Ho crd.data.P
## 1           FALSE    0.75161731          TRUE  0.2261406
## 2           FALSE    0.14586441          TRUE  0.4420142
## 3            TRUE   -0.57717660          TRUE  0.7180899
## 4           FALSE    0.72766174          TRUE  0.2334103
## 5           FALSE    0.50306815          TRUE  0.3074582
## 6            TRUE   -0.05268882          TRUE  0.5210101

X.5. [Note for Different Variable: change the file name to reflect the new variable.]

# write csv file with Lisa Test results for I, E, Var, Z, P
write.csv(table2, "LisaTest-MdInc.csv", row.names = FALSE)

XI. Mapping the Local Moran’s i

XI.1. [Note for Different Variable: Change the following values tm_polygons(col = “NewColumnName”, title= “New Title”); tm_layout(title= “New Title”)]. For the new variable, the crd.data will reflect the new data column as created in Step IV.6 after removing the na values from the new variable column.

# Map Lisa
map_LISA <- tm_shape(crd.data) + 
  tm_polygons(col = "Ii", 
              title = "Local Moran's i", 
              style = "jenks", 
              palette = "viridis", n = 6)  + 
  # add scale bar - first value is TOP, second value is BOTTOM
  tm_scale_bar(width = 0.22, position = c("RIGHT", "BOTTOM")) + 
  # add compass
  tm_compass(position = c("RIGHT", "TOP")) + 
  tm_layout(title = "CRD Census Tracts Median Income\nLISA Local Moran's i 2016", title.position = c("LEFT", "TOP"), inner.margins = c(.08, .03, .08, .03))

XI.2. [Note for Different Variable: change the file name to reflect the new variable.]

map_LISA

# create png of Map LISA MdInc
png("map_LISA_MdInc_2.png")  
map_LISA
dev.off()
## quartz_off_screen 
##                 2

XII. Local Moran’s i Scatter Plot

XII.1. [Note for Different Variable: When running the code for the second variable, the new column name will need to replace thw item crd.data$MdInc, the xlab=“New Variable Label Name”, the ylab = “New Variable Label Name”, main = New VariableName title.]

# Scatter Plot
moran.plot(crd.data$MdInc, crd.lw, zero.policy=NULL, spChk=NULL, labels=NULL, xlab="Median Income", 
           ylab="Spatially Lagged Mean Median Income", quiet=NULL, main ="Local Moran's i Plot for CRD Median Income")

XII.2. [Note for Different Variable: change the saved file name to reflect the new variable. When running the code for the second variable, the new column name will need to replace thw item crd.data$MdInc, the xlab=“New Variable Label Name”, the ylab = “New Variable Label Name”, main = New VariableName title.]

# create png of Moran scatter plot
png("moran.plot_MdInc_2.png")  
moran.plot(crd.data$MdInc, crd.lw, zero.policy=NULL, spChk=NULL, labels=NULL, xlab="Median Income", 
           ylab="Spatially Lagged Median Income", quiet=NULL, main ="Local Moran's i Plot for CRD Median Income 2016")
dev.off()
## quartz_off_screen 
##                 2

XIII. Summary


Variable II: Renter

IV.ii. Clean Data

  1. Remove the na values in the new column: Renter
## remove 'na' values in data column: MInc
# '!' means 'not'; !is.na will keep everything that is not na
crd.data <- crd.data[!is.na(crd.data$Renter),]
summary(crd.data)
## Object of class SpatialPolygonsDataFrame
## Coordinates:
##       min     max
## x 3903547 3974156
## y 1910542 1962451
## Is projected: TRUE 
## proj4string :
## [+proj=lcc +lat_1=49 +lat_2=77 +lat_0=63.390675
## +lon_0=-91.86666666666666 +x_0=6200000 +y_0=3000000 +ellps=GRS80
## +towgs84=0,0,0,0,0,0,0 +units=m +no_defs]
## Data attributes:
##       GUID                CTUID         CUID            Pop       
##  Min.   :9350001   9350001.00: 1   Min.   :  1.0   Min.   :  332  
##  1st Qu.:9350101   9350002.00: 1   1st Qu.:101.2   1st Qu.: 3538  
##  Median :9350126   9350003.01: 1   Median :126.5   Median : 4744  
##  Mean   :9350108   9350003.02: 1   Mean   :108.4   Mean   : 4964  
##  3rd Qu.:9350153   9350004.00: 1   3rd Qu.:152.8   3rd Qu.: 6156  
##  Max.   :9350180   9350005.00: 1   Max.   :180.1   Max.   :10393  
##                    (Other)   :68                                  
##      PopCh             PopD             Area            Age0yrs      
##  Min.   :-2.000   Min.   :  21.1   Min.   :  0.500   Min.   : 105.0  
##  1st Qu.: 1.800   1st Qu.: 641.2   1st Qu.:  1.383   1st Qu.: 422.5  
##  Median : 4.250   Median :2025.1   Median :  2.500   Median : 560.0  
##  Mean   : 5.791   Mean   :2237.2   Mean   :  9.355   Mean   : 650.7  
##  3rd Qu.: 7.300   3rd Qu.:2899.8   3rd Qu.:  4.992   3rd Qu.: 735.0  
##  Max.   :44.200   Max.   :7582.6   Max.   :221.580   Max.   :2000.0  
##                                                                      
##     Age15yrs       Age65yrs          AvgAge          Detach      
##  Min.   : 210   Min.   :  20.0   Min.   :29.20   Min.   :  70.0  
##  1st Qu.:2232   1st Qu.: 646.2   1st Qu.:41.30   1st Qu.: 503.8  
##  Median :3115   Median : 965.0   Median :43.90   Median : 865.0  
##  Mean   :3263   Mean   :1050.1   Mean   :43.99   Mean   : 866.1  
##  3rd Qu.:4121   3rd Qu.:1418.8   3rd Qu.:46.67   3rd Qu.:1130.0  
##  Max.   :7300   Max.   :2410.0   Max.   :56.50   Max.   :2715.0  
##                                                                  
##      Apart            SemiD             Row           Flat       
##  Min.   :   0.0   Min.   :  0.00   Min.   :  0   Min.   :   0.0  
##  1st Qu.:   0.0   1st Qu.: 25.00   1st Qu.: 25   1st Qu.: 172.5  
##  Median :   7.5   Median : 55.00   Median :100   Median : 305.0  
##  Mean   : 140.7   Mean   : 81.28   Mean   :138   Mean   : 345.7  
##  3rd Qu.:  85.0   3rd Qu.:108.75   3rd Qu.:215   3rd Qu.: 472.5  
##  Max.   :3185.0   Max.   :315.00   Max.   :460   Max.   :1515.0  
##                                                                  
##      ApartH           Attach          Avghhld          OtherA      
##  Min.   :   0.0   Min.   : 0.000   Min.   :1.500   Min.   :  10.0  
##  1st Qu.:  62.5   1st Qu.: 0.000   1st Qu.:2.100   1st Qu.: 511.2  
##  Median : 320.0   Median : 5.000   Median :2.300   Median :1145.0  
##  Mean   : 598.6   Mean   : 4.122   Mean   :2.292   Mean   :1167.0  
##  3rd Qu.: 860.0   3rd Qu.: 5.000   3rd Qu.:2.500   3rd Qu.:1757.5  
##  Max.   :3050.0   Max.   :45.000   Max.   :3.700   Max.   :3290.0  
##                                                                    
##      Wchild          WTChild          FamSize         Married      
##  Min.   :  55.0   Min.   :  10.0   Min.   :2.200   Min.   :  20.0  
##  1st Qu.: 448.8   1st Qu.: 416.2   1st Qu.:2.600   1st Qu.: 631.2  
##  Median : 585.0   Median : 620.0   Median :2.700   Median : 895.0  
##  Mean   : 654.7   Mean   : 649.3   Mean   :2.677   Mean   : 937.0  
##  3rd Qu.: 767.5   3rd Qu.: 827.5   3rd Qu.:2.800   3rd Qu.:1152.5  
##  Max.   :1650.0   Max.   :1340.0   Max.   :3.400   Max.   :2215.0  
##                                                                    
##      ComLaw         LParentF         LParentM         CWTChild     
##  Min.   : 25.0   Min.   : 30.00   Min.   : 10.00   Min.   :  15.0  
##  1st Qu.:130.0   1st Qu.: 91.25   1st Qu.: 30.00   1st Qu.: 438.8  
##  Median :212.5   Median :140.00   Median : 37.50   Median : 647.5  
##  Mean   :234.5   Mean   :156.08   Mean   : 44.19   Mean   : 682.0  
##  3rd Qu.:293.8   3rd Qu.:202.50   3rd Qu.: 60.00   3rd Qu.: 860.0  
##  Max.   :660.0   Max.   :430.00   Max.   :130.00   Max.   :1445.0  
##                                                                    
##     CWChild           Child1          Child2          Child3      
##  Min.   :  25.0   Min.   :  5.0   Min.   :  5.0   Min.   : 15.00  
##  1st Qu.: 287.5   1st Qu.:140.0   1st Qu.:131.2   1st Qu.: 40.00  
##  Median : 475.0   Median :190.0   Median :195.0   Median : 55.00  
##  Mean   : 488.8   Mean   :218.5   Mean   :208.0   Mean   : 63.45  
##  3rd Qu.: 577.5   3rd Qu.:263.8   3rd Qu.:253.8   3rd Qu.: 80.00  
##  Max.   :1320.0   Max.   :515.0   Max.   :615.0   Max.   :225.00  
##                                                                   
##     Lparent1        Lparent2         Lparent3         Single    
##  Min.   : 25.0   Min.   : 10.00   Min.   : 0.00   Min.   : 120  
##  1st Qu.: 80.0   1st Qu.: 31.25   1st Qu.:10.00   1st Qu.: 765  
##  Median :122.5   Median : 40.00   Median :10.00   Median :1095  
##  Mean   :134.9   Mean   : 52.43   Mean   :12.91   Mean   :1182  
##  3rd Qu.:186.2   3rd Qu.: 65.00   3rd Qu.:15.00   3rd Qu.:1572  
##  Max.   :375.0   Max.   :150.00   Max.   :50.00   Max.   :3845  
##                                                                 
##    Separated        Divorced         Widowed          MdInc      
##  Min.   :  5.0   Min.   :  10.0   Min.   :  5.0   Min.   :12384  
##  1st Qu.: 60.0   1st Qu.: 200.0   1st Qu.:138.8   1st Qu.:33878  
##  Median : 95.0   Median : 310.0   Median :215.0   Median :37563  
##  Mean   :117.0   Mean   : 353.6   Mean   :250.9   Mean   :37558  
##  3rd Qu.:158.8   3rd Qu.: 473.8   3rd Qu.:328.8   3rd Qu.:42250  
##  Max.   :380.0   Max.   :1065.0   Max.   :670.0   Max.   :57557  
##                                                                  
##   MDAftTaxInc       GovTrans          EInc            MInc      
##  Min.   :12192   Min.   :  720   Min.   :11616   Min.   :11200  
##  1st Qu.:30491   1st Qu.: 3406   1st Qu.:29105   1st Qu.:31412  
##  Median :33420   Median : 5824   Median :31669   Median :34421  
##  Mean   :33389   Mean   : 5454   Mean   :31549   Mean   :34637  
##  3rd Qu.:37373   3rd Qu.: 7254   3rd Qu.:34292   3rd Qu.:38460  
##  Max.   :48939   Max.   :12079   Max.   :52395   Max.   :53632  
##                                                                 
##     LEnglish        LFrench        LAboriginal       LnAboriginal   
##  Min.   :  330   Min.   :   0.0   Min.   :  0.000   Min.   :  10.0  
##  1st Qu.: 3446   1st Qu.: 353.8   1st Qu.:  0.000   1st Qu.: 585.0  
##  Median : 4698   Median : 460.0   Median :  0.000   Median : 827.5  
##  Mean   : 4784   Mean   : 489.2   Mean   :  8.649   Mean   : 897.8  
##  3rd Qu.: 5776   3rd Qu.: 611.2   3rd Qu.: 10.000   3rd Qu.:1182.5  
##  Max.   :10415   Max.   :1050.0   Max.   :165.000   Max.   :2455.0  
##                                                                     
##    IAmericas         IEurope         IAfrica           IAsia       
##  Min.   :  0.00   Min.   :  0.0   Min.   :  0.00   Min.   :   0.0  
##  1st Qu.: 86.25   1st Qu.:261.2   1st Qu.: 21.25   1st Qu.: 117.5  
##  Median :125.00   Median :367.5   Median : 35.00   Median : 247.5  
##  Mean   :130.07   Mean   :399.3   Mean   : 38.11   Mean   : 298.9  
##  3rd Qu.:165.00   3rd Qu.:535.0   3rd Qu.: 53.75   3rd Qu.: 430.0  
##  Max.   :285.00   Max.   :870.0   Max.   :125.00   Max.   :1185.0  
##                                                                    
##     IOceania       Ifirstgen       I2ndgen          I3rdgen    
##  Min.   : 0.00   Min.   :  15   Min.   :  10.0   Min.   : 305  
##  1st Qu.:10.00   1st Qu.: 750   1st Qu.: 887.5   1st Qu.:1684  
##  Median :15.00   Median :1035   Median :1087.5   Median :2512  
##  Mean   :17.16   Mean   :1029   Mean   :1114.2   Mean   :2685  
##  3rd Qu.:25.00   3rd Qu.:1305   3rd Qu.:1390.0   3rd Qu.:3124  
##  Max.   :50.00   Max.   :2175   Max.   :1970.0   Max.   :7085  
##                                                                
##      EcoIm           FamIm           RefIm          IstNations   
##  Min.   :  0.0   Min.   :  0.0   Min.   :  0.00   Min.   : 10.0  
##  1st Qu.:170.0   1st Qu.:130.0   1st Qu.: 15.00   1st Qu.: 35.0  
##  Median :247.5   Median :175.0   Median : 30.00   Median : 95.0  
##  Mean   :263.0   Mean   :187.4   Mean   : 37.36   Mean   :129.7  
##  3rd Qu.:353.8   3rd Qu.:233.8   3rd Qu.: 55.00   3rd Qu.:176.2  
##  Max.   :785.0   Max.   :540.0   Max.   :120.00   Max.   :735.0  
##                                                                  
##      Metis             Inuk            Treaty          Ntreaty     
##  Min.   :  0.00   Min.   : 0.000   Min.   :  0.00   Min.   :   10  
##  1st Qu.: 35.00   1st Qu.: 0.000   1st Qu.: 25.00   1st Qu.: 3462  
##  Median : 80.00   Median : 0.000   Median : 60.00   Median : 4608  
##  Mean   : 88.18   Mean   : 1.689   Mean   : 96.42   Mean   : 4731  
##  3rd Qu.:103.75   3rd Qu.: 0.000   3rd Qu.:123.75   3rd Qu.: 5736  
##  Max.   :325.00   Max.   :15.000   Max.   :715.00   Max.   :10280  
##                                                                    
##    SAbAncest        MAbAncest           SAsian          Chinese       
##  Min.   :  0.00   Min.   : 0.0000   Min.   :  0.00   Min.   :   0.00  
##  1st Qu.: 10.00   1st Qu.: 0.0000   1st Qu.: 46.25   1st Qu.:  76.25  
##  Median : 30.00   Median : 0.0000   Median : 90.00   Median : 157.50  
##  Mean   : 58.51   Mean   : 0.8784   Mean   :137.77   Mean   : 220.61  
##  3rd Qu.: 58.75   3rd Qu.: 0.0000   3rd Qu.:176.25   3rd Qu.: 290.00  
##  Max.   :615.00   Max.   :35.0000   Max.   :540.00   Max.   :1315.00  
##                                                                       
##      Black          Fillipino       LatinAmer           Arab       
##  Min.   :  0.00   Min.   :  0.0   Min.   :  0.00   Min.   :  0.00  
##  1st Qu.: 15.00   1st Qu.: 15.0   1st Qu.: 15.00   1st Qu.:  0.00  
##  Median : 37.50   Median : 62.5   Median : 25.00   Median : 10.00  
##  Mean   : 47.03   Mean   : 82.3   Mean   : 33.92   Mean   : 19.59  
##  3rd Qu.: 70.00   3rd Qu.:120.0   3rd Qu.: 53.75   3rd Qu.: 28.75  
##  Max.   :200.00   Max.   :305.0   Max.   :155.00   Max.   :125.00  
##                                                                    
##     SEAsian           WAsian          Korean          Japanese    
##  Min.   :  0.00   Min.   : 0.00   Min.   :  0.00   Min.   : 0.00  
##  1st Qu.: 10.00   1st Qu.: 0.00   1st Qu.:  0.00   1st Qu.:15.00  
##  Median : 25.00   Median :10.00   Median : 20.00   Median :35.00  
##  Mean   : 34.26   Mean   :14.59   Mean   : 28.51   Mean   :33.45  
##  3rd Qu.: 48.75   3rd Qu.:20.00   3rd Qu.: 43.75   3rd Qu.:48.75  
##  Max.   :150.00   Max.   :70.00   Max.   :155.00   Max.   :90.00  
##                                                                   
##    Vminority         MVisMin          Owner            Renter      
##  Min.   : 0.000   Min.   : 0.00   Min.   :  65.0   Min.   :  15.0  
##  1st Qu.: 0.000   1st Qu.:10.00   1st Qu.: 966.2   1st Qu.: 325.0  
##  Median : 0.000   Median :20.00   Median :1287.5   Median : 620.0  
##  Mean   : 5.608   Mean   :20.54   Mean   :1375.5   Mean   : 820.3  
##  3rd Qu.:10.000   3rd Qu.:28.75   3rd Qu.:1690.0   3rd Qu.:1071.2  
##  Max.   :35.000   Max.   :80.00   Max.   :3255.0   Max.   :3945.0  
##                                                                    
##       Band            NoDeg          Secondary      Psecondary  
##  Min.   : 0.000   Min.   : 100.0   Min.   :  65   Min.   :  60  
##  1st Qu.: 0.000   1st Qu.: 316.2   1st Qu.: 780   1st Qu.:1891  
##  Median : 0.000   Median : 465.0   Median :1112   Median :2428  
##  Mean   : 1.081   Mean   : 498.7   Mean   :1189   Mean   :2491  
##  3rd Qu.: 0.000   3rd Qu.: 613.8   3rd Qu.:1481   3rd Qu.:3091  
##  Max.   :30.000   Max.   :1340.0   Max.   :2690   Max.   :5015  
##                                                                 
##     Employed        Unemp          CarDrive         CarPass     
##  Min.   :  85   Min.   : 25.0   Min.   :  50.0   Min.   : 15.0  
##  1st Qu.:1710   1st Qu.:100.0   1st Qu.: 963.8   1st Qu.: 65.0  
##  Median :2372   Median :135.0   Median :1340.0   Median : 97.5  
##  Mean   :2529   Mean   :149.7   Mean   :1498.9   Mean   :110.5  
##  3rd Qu.:3131   3rd Qu.:197.5   3rd Qu.:1770.0   3rd Qu.:143.8  
##  Max.   :5560   Max.   :380.0   Max.   :4195.0   Max.   :245.0  
##                                                                 
##     Transit           Walk              Bike           Other       
##  Min.   : 15.0   Min.   :   0.00   Min.   :  0.0   Min.   :  0.00  
##  1st Qu.:125.0   1st Qu.:  66.25   1st Qu.: 70.0   1st Qu.: 31.25  
##  Median :215.0   Median : 160.00   Median :140.0   Median : 50.00  
##  Mean   :251.3   Mean   : 238.18   Mean   :151.8   Mean   : 55.61  
##  3rd Qu.:345.0   3rd Qu.: 278.75   3rd Qu.:216.2   3rd Qu.: 73.75  
##  Max.   :760.0   Max.   :1960.00   Max.   :475.0   Max.   :150.00  
##                                                                    
##     T15mins          T29mins          T44mins          T59mins      
##  Min.   :  45.0   Min.   :  15.0   Min.   :  15.0   Min.   :  10.0  
##  1st Qu.: 417.5   1st Qu.: 635.0   1st Qu.: 236.2   1st Qu.:  55.0  
##  Median : 655.0   Median : 925.0   Median : 380.0   Median :  80.0  
##  Mean   : 686.2   Mean   : 950.2   Mean   : 425.0   Mean   : 143.1  
##  3rd Qu.: 906.2   3rd Qu.:1266.2   3rd Qu.: 477.5   3rd Qu.: 133.8  
##  Max.   :2165.0   Max.   :2065.0   Max.   :1460.0   Max.   :1010.0  
##                                                                     
##   Tover69mins          C5am            C6am             C7am       
##  Min.   :   0.0   Min.   :  0.0   Min.   :  15.0   Min.   :  20.0  
##  1st Qu.:  35.0   1st Qu.: 50.0   1st Qu.: 191.2   1st Qu.: 388.8  
##  Median :  60.0   Median : 75.0   Median : 300.0   Median : 595.0  
##  Mean   : 102.4   Mean   : 97.7   Mean   : 372.9   Mean   : 631.7  
##  3rd Qu.: 108.8   3rd Qu.:115.0   3rd Qu.: 467.5   3rd Qu.: 836.2  
##  Max.   :1155.0   Max.   :575.0   Max.   :1355.0   Max.   :1300.0  
##                                                                    
##       C8am             C9am           C12pm        IncLagMeans   
##  Min.   :  20.0   Min.   :  0.0   Min.   : 15.0   Min.   :30647  
##  1st Qu.: 431.2   1st Qu.:210.0   1st Qu.:206.2   1st Qu.:35583  
##  Median : 552.5   Median :290.0   Median :287.5   Median :37591  
##  Mean   : 569.9   Mean   :307.2   Mean   :327.3   Mean   :38044  
##  3rd Qu.: 697.5   3rd Qu.:387.5   3rd Qu.:442.5   3rd Qu.:41117  
##  Max.   :1420.0   Max.   :770.0   Max.   :780.0   Max.   :45824  
##                                                                  
##        Ii                E.Ii             Var.Ii             Z.Ii         
##  Min.   :-3.72431   Min.   :-0.0137   Min.   :0.02923   Min.   :-3.82072  
##  1st Qu.:-0.01818   1st Qu.:-0.0137   1st Qu.:0.12419   1st Qu.:-0.01247  
##  Median : 0.10445   Median :-0.0137   Median :0.16286   Median : 0.28519  
##  Mean   : 0.14317   Mean   :-0.0137   Mean   :0.20140   Mean   : 0.45976  
##  3rd Qu.: 0.32975   3rd Qu.:-0.0137   3rd Qu.:0.22656   3rd Qu.: 0.98397  
##  Max.   : 2.10785   Max.   :-0.0137   Max.   :0.94320   Max.   : 4.45715  
##                                                                           
##        P               IiLessEIi            Z.Ho           Ii.Neg       
##  Min.   :0.0000042   Min.   :-3.710617   Mode :logical   Mode :logical  
##  1st Qu.:0.1626611   1st Qu.:-0.004481   FALSE:5         FALSE:54       
##  Median :0.3878925   Median : 0.118153   TRUE :69        TRUE :20       
##  Mean   :0.3611524   Mean   : 0.156870                                  
##  3rd Qu.:0.5049726   3rd Qu.: 0.343451                                  
##  Max.   :0.9999335   Max.   : 2.121547                                  
## 
    1. In order to compare the Renter variable between different polygons, the variable needs to be ‘normalized’, by dividing each count of Renters in a polygon by the population of that polygon, and creating a new column with the % of population that is a renter in any polygon … these values can now be compared across different polygons.2) A new column is created $RenterNorm, and this new variable will now be used for the analysis.
## 
crd.data$RenterNorm <- crd.data$Renter / crd.data$Pop
class(crd.data$RenterNorm)
## [1] "numeric"
# new column

V.ii. Make Chloropleth Map: Renter

Using the tmap package …

# make Chloropleth map
map_PopDens <- tm_shape(crd.data) + 
  tm_polygons(col = "RenterNorm", 
              title = "Renter (% of Population)", 
              style = "jenks", 
              palette = "viridis", n = 6) + 
  # add scale bar - first value is TOP, second value is BOTTOM
  tm_scale_bar(width = 0.22, position = c("RIGHT", "BOTTOM")) + 
  # add compass
  tm_compass(position = c("RIGHT", "TOP")) + 
  tm_layout(title = "CRD Census Tracts Renter % of Population 2016", title.position = c("LEFT", "TOP"), inner.margins = c(.08, .03, .08, .03))

map_PopDens

# create png of RenterNormalized % of Population
png("map_PopDens_RenterNorm_2.png")  
map_PopDens
dev.off()
## quartz_off_screen 
##                 2

VI.ii. Defining Neighbourhoods: Renter

  1. Using the spdep package,
  1. poly2nb builds a list of neighbours from polygon regions whose boundaries are continguous: https://www.rdocumentation.org/packages/spdep/versions/1.1-3/topics/poly2nb
  2. nb2lines stores spatial coordinates as vectors (needed for weights): https://www.rdocumentation.org/packages/spdep/versions/1.1-3/topics/nb2lines
  1. Using the tmap package …
# Defining Neighbourhoods
crd.nb <- poly2nb(crd.data)
crd.net <- nb2lines(crd.nb,coords=coordinates(crd.data))

map_crd.net <- tm_shape(crd.data) + tm_borders(col='lightgrey') + 
  tm_shape(crd.net) + tm_lines(col='red') + 
  # add scale bar - first value is TOP, second value is BOTTOM
  tm_scale_bar(width = 0.22, position = c("RIGHT", "BOTTOM")) + 
  # add compass
  tm_compass(position = c("RIGHT", "TOP")) + 
  tm_layout(title = "CRD Census Tracts Renter % of Population\nQueen Neighbours 2016", title.position = c("LEFT", "TOP"), inner.margins = c(.08, .03, .08, .03))

##

crd.nb2 <- poly2nb(crd.data, queen = FALSE)
crd.net2 <- nb2lines(crd.nb2,coords=coordinates(crd.data))

map_crd.net2 <- tm_shape(crd.data) + tm_borders(col='lightgrey') + 
  tm_shape(crd.net) + tm_lines(col='red', lwd = 2) +
  tm_shape(crd.net2) + tm_lines(col='blue', lwd = 2) + 
  # add scale bar - first value is TOP, second value is BOTTOM
  tm_scale_bar(width = 0.22, position = c("RIGHT", "BOTTOM")) + 
  # add compass
  tm_compass(position = c("RIGHT", "TOP")) + 
  tm_layout(title = "CRD Census Tracts Renter % of Population\nRook Neighbours 2016", title.position = c("LEFT", "TOP"), inner.margins = c(.08, .03, .08, .03))

map_crd.net
## Warning: Current projection of shape crd.net unknown and cannot be
## determined.

map_crd.net2
## Warning: Current projection of shape crd.net unknown and cannot be
## determined.
## Warning: Current projection of shape crd.net2 unknown and cannot be
## determined.

# create png of Renter CRD net
png("map_map_crd.net_RenterNorm_2.png")  
map_crd.net
## Warning: Current projection of shape crd.net unknown and cannot be
## determined.
dev.off()
## quartz_off_screen 
##                 2
# create png of Median Income CRD net 2
png("map_map_crd.net2_RenterNorm_2.png")  
map_crd.net2
## Warning: Current projection of shape crd.net unknown and cannot be
## determined.

## Warning: Current projection of shape crd.net2 unknown and cannot be
## determined.
dev.off()
## quartz_off_screen 
##                 2

VII.ii. Weight Matrix: Renter

  1. Using the spdep package, nb2listw uses the neighbours list previously created and adds weight: https://www.rdocumentation.org/packages/spdep/versions/1.1-3/topics/nb2listw
# Weight Matrix
crd.lw <- nb2listw(crd.nb, zero.policy = TRUE, style = "W")
print.listw(crd.lw, zero.policy = TRUE)
## Characteristics of weights list object:
## Neighbour list object:
## Number of regions: 74 
## Number of nonzero links: 424 
## Percentage nonzero weights: 7.742878 
## Average number of links: 5.72973 
## 
## Weights style: W 
## Weights constants summary:
##    n   nn S0       S1       S2
## W 74 5476 74 29.46931 322.5924

VIII.ii. Calculate Lag Means: Renter

  1. Using the spdep package, lag.listw : https://www.rdocumentation.org/packages/spdep/versions/1.1-3/topics/lag.listw2. Creates a map using tmap.
# Calculate Lag Means
crd.data$RenterNormLagMeans = lag.listw(crd.lw, crd.data$RenterNorm, zero.policy = TRUE)

map_LagMean <- tm_shape(crd.data) + 
  tm_polygons(col = "RenterNormLagMeans", 
              title = "Renter % of Population\nLagged Means", 
              style = "jenks", 
              palette = "viridis", n = 6) + 
  # add scale bar - first value is TOP, second value is BOTTOM
  tm_scale_bar(width = 0.22, position = c("RIGHT", "BOTTOM")) + 
  # add compass
  tm_compass(position = c("RIGHT", "TOP")) + 
  tm_layout(title = "CRD Census Tracts Renter % of Population\nLagged Means 2016", title.position = c("LEFT", "TOP"), inner.margins = c(.08, .03, .08, .03))

map_LagMean

# create png of map LagMean Renter Normalized CRD net 2
png("map_LagMean_RenterNorm_2.png")  
map_LagMean
dev.off()
## quartz_off_screen 
##                 2

IX.ii. Morans I test: Renter

Morans Global I Test creates one i value for every polygon as if they were all one polygon.

# Morans I test
mi <- moran.test(crd.data$RenterNorm, crd.lw, zero.policy = TRUE)
mi
## 
##  Moran I test under randomisation
## 
## data:  crd.data$RenterNorm  
## weights: crd.lw    
## 
## Moran I statistic standard deviate = 8.6868, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##       0.595415952      -0.013698630       0.004916714
moran.range <- function(lw) {
  wmat <- listw2mat(lw)
  return(range(eigen((wmat + t(wmat))/2)$values))
}
moran.range(crd.lw)
## [1] -0.8193252  1.0821054
## calculate z value
mI <- mi$estimate[[1]]
eI <- mi$estimate[[2]]
var <- mi$estimate[[3]]

# calculate z value 
z <- (mI - eI) / var^(1/2)
z
## [1] 8.686834
# Add Stats data objects to data.frame ########
data.for.table3 = data.frame(mI, eI, var, z)

data.for.table3
##         mI          eI         var        z
## 1 0.595416 -0.01369863 0.004916714 8.686834
table3 <- data.for.table3 

# write csv file with Morans Global results for I, E, Var, Z, P
write.csv(table3, "MoransGlobal-RenterNorm.csv", row.names = FALSE)

X.ii. Local Moran’s i Test (Lisa Test): Renter

Separate i values are created for every single polygon.

# Lisa Test
lisa.test <- localmoran(crd.data$RenterNorm, crd.lw)

crd.data$Ii <- lisa.test[,1]
crd.data$E.Ii<- lisa.test[,2]
crd.data$Var.Ii<- lisa.test[,3]
crd.data$Z.Ii<- lisa.test[,4]
crd.data$P<- lisa.test[,5]

crd.data$IiLessEIi <- crd.data$Ii - crd.data$E.Ii

# if z value is less than 95% significance level, value is FALSE
crd.data$Z.Ho <- crd.data$Z.Ii < 1.96
crd.data$Z.Ho
##  [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE
## [12]  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE
## [23]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
## [34]  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [45]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
## [56]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [67]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
crd.data$Ii.Neg <- crd.data$IiLessEIi < 0
crd.data$Ii.Neg
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
## [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE
## [23] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
## [34]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [45] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [56] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
## [67] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
# make a data frome from the Lisa Test Data
data.for.table4 = data.frame(crd.data$Ii, crd.data$E.Ii, crd.data$Var.Ii, crd.data$IiLessEIi, crd.data$Ii.Neg, crd.data$Z.Ii, crd.data$Z.Ho, crd.data$P)
table4 <- data.for.table4
table4
##     crd.data.Ii crd.data.E.Ii crd.data.Var.Ii crd.data.IiLessEIi
## 1   0.233821626   -0.01369863      0.15045712        0.247520256
## 2   0.032943097   -0.01369863      0.23218304        0.046641727
## 3   0.549070589   -0.01369863      0.15045712        0.562769219
## 4   0.503692530   -0.01369863      0.23218304        0.517391160
## 5   0.060472308   -0.01369863      0.18314749        0.074170938
## 6   1.556366078   -0.01369863      0.15045712        1.570064708
## 7   3.391433333   -0.01369863      0.18314749        3.405131963
## 8   1.553256264   -0.01369863      0.18314749        1.566954894
## 9   1.140736503   -0.01369863      0.18314749        1.154435133
## 10 -0.071622535   -0.01369863      0.12710685       -0.057923905
## 11  1.333661488   -0.01369863      0.10959416        1.347360119
## 12  0.360821928   -0.01369863      0.09597317        0.374520558
## 13  0.205149532   -0.01369863      0.47736081        0.218848163
## 14  0.316206463   -0.01369863      0.31390896        0.329905094
## 15  3.643380638   -0.01369863      0.09597317        3.657079268
## 16  1.379862981   -0.01369863      0.10959416        1.393561611
## 17  1.839610501   -0.01369863      0.12710685        1.853309131
## 18  0.359894929   -0.01369863      0.31390896        0.373593560
## 19 -0.045775803   -0.01369863      0.18314749       -0.032077173
## 20 -0.018460853   -0.01369863      0.12710685       -0.004762223
## 21  0.640176636   -0.01369863      0.23218304        0.653875266
## 22  1.897336263   -0.01369863      0.18314749        1.911034893
## 23  0.098397443   -0.01369863      0.08507638        0.112096073
## 24 -0.026062633   -0.01369863      0.23218304       -0.012364003
## 25  0.147756323   -0.01369863      0.12710685        0.161454953
## 26  0.431022202   -0.01369863      0.10959416        0.444720832
## 27  0.356600204   -0.01369863      0.15045712        0.370298834
## 28  0.304032189   -0.01369863      0.08507638        0.317730819
## 29  0.577861864   -0.01369863      0.18314749        0.591560494
## 30 -0.168322406   -0.01369863      0.18314749       -0.154623776
## 31 -0.001621923   -0.01369863      0.12710685        0.012076707
## 32  4.474925356   -0.01369863      0.18314749        4.488623986
## 33  0.064613726   -0.01369863      0.15045712        0.078312356
## 34 -0.904832954   -0.01369863      0.31390896       -0.891134324
## 35  0.011019807   -0.01369863      0.18314749        0.024718437
## 36  3.129918431   -0.01369863      0.18314749        3.143617061
## 37  0.136728114   -0.01369863      0.18314749        0.150426744
## 38  0.199580888   -0.01369863      0.12710685        0.213279518
## 39  0.097877099   -0.01369863      0.18314749        0.111575729
## 40  0.529096109   -0.01369863      0.23218304        0.542794739
## 41  0.236191920   -0.01369863      0.12710685        0.249890550
## 42  0.226356428   -0.01369863      0.12710685        0.240055058
## 43  0.604421772   -0.01369863      0.15045712        0.618120402
## 44  0.442216620   -0.01369863      0.31390896        0.455915250
## 45  0.147383189   -0.01369863      0.15045712        0.161081819
## 46  0.713133316   -0.01369863      0.18314749        0.726831946
## 47  0.387150645   -0.01369863      0.23218304        0.400849276
## 48  0.612030700   -0.01369863      0.15045712        0.625729331
## 49  0.156866528   -0.01369863      0.10959416        0.170565159
## 50  0.234052890   -0.01369863      0.02964488        0.247751521
## 51  0.662880165   -0.01369863      0.31390896        0.676578795
## 52  0.630290771   -0.01369863      0.15045712        0.643989401
## 53  0.223824443   -0.01369863      0.12710685        0.237523073
## 54  1.838131703   -0.01369863      0.15045712        1.851830333
## 55  0.305859518   -0.01369863      0.10959416        0.319558149
## 56  0.387356307   -0.01369863      0.23218304        0.401054937
## 57  0.099487976   -0.01369863      0.15045712        0.113186606
## 58  0.046748357   -0.01369863      0.23218304        0.060446987
## 59  0.998762311   -0.01369863      0.96771634        1.012460942
## 60  0.043229883   -0.01369863      0.09597317        0.056928513
## 61 -0.252114651   -0.01369863      0.23218304       -0.238416021
## 62  0.355471073   -0.01369863      0.47736081        0.369169703
## 63  0.645739004   -0.01369863      0.47736081        0.659437634
## 64  0.290854679   -0.01369863      0.96771634        0.304553309
## 65  0.299887489   -0.01369863      0.15045712        0.313586119
## 66  0.601525828   -0.01369863      0.15045712        0.615224458
## 67  0.811880946   -0.01369863      0.23218304        0.825579576
## 68 -0.037825907   -0.01369863      0.15045712       -0.024127277
## 69  0.151580204   -0.01369863      0.12710685        0.165278834
## 70  0.262426181   -0.01369863      0.12710685        0.276124811
## 71  0.584495330   -0.01369863      0.18314749        0.598193960
## 72  0.157706771   -0.01369863      0.31390896        0.171405401
## 73  0.104039684   -0.01369863      0.09597317        0.117738314
## 74  0.768114071   -0.01369863      0.31390896        0.781812701
##    crd.data.Ii.Neg crd.data.Z.Ii crd.data.Z.Ho   crd.data.P
## 1            FALSE    0.63812297          TRUE 2.616968e-01
## 2            FALSE    0.09679644          TRUE 4.614440e-01
## 3            FALSE    1.45085485          TRUE 7.341014e-02
## 4            FALSE    1.07375144          TRUE 1.414671e-01
## 5            FALSE    0.17331386          TRUE 4.312024e-01
## 6            FALSE    4.04772671         FALSE 2.585874e-05
## 7            FALSE    7.95670883         FALSE 8.833791e-16
## 8            FALSE    3.66147450         FALSE 1.253839e-04
## 9            FALSE    2.69754721         FALSE 3.492619e-03
## 10            TRUE   -0.16247006          TRUE 5.645321e-01
## 11           FALSE    4.06995858         FALSE 2.351075e-05
## 12           FALSE    1.20892885          TRUE 1.133451e-01
## 13           FALSE    0.31675212          TRUE 3.757159e-01
## 14           FALSE    0.58882625          TRUE 2.779889e-01
## 15           FALSE   11.80482233         FALSE 1.842843e-32
## 16           FALSE    4.20951901         FALSE 1.279575e-05
## 17           FALSE    5.19832443         FALSE 1.005465e-07
## 18           FALSE    0.66680297          TRUE 2.524490e-01
## 19            TRUE   -0.07495414          TRUE 5.298744e-01
## 20            TRUE   -0.01335750          TRUE 5.053287e-01
## 21           FALSE    1.35699942          TRUE 8.739070e-02
## 22           FALSE    4.46547986         FALSE 3.994474e-06
## 23           FALSE    0.38431373          TRUE 3.503730e-01
## 24            TRUE   -0.02565924          TRUE 5.102354e-01
## 25           FALSE    0.45286305          TRUE 3.253237e-01
## 26           FALSE    1.34336421          TRUE 8.957703e-02
## 27           FALSE    0.95465395          TRUE 1.698764e-01
## 28           FALSE    1.08931840          TRUE 1.380067e-01
## 29           FALSE    1.38228846          TRUE 8.344157e-02
## 30            TRUE   -0.36130652          TRUE 6.410648e-01
## 31           FALSE    0.03387381          TRUE 4.864889e-01
## 32           FALSE   10.48848459         FALSE 4.879043e-26
## 33           FALSE    0.20189424          TRUE 4.199997e-01
## 34            TRUE   -1.59052798          TRUE 9.441421e-01
## 35           FALSE    0.05775911          TRUE 4.769703e-01
## 36           FALSE    7.34563180         FALSE 1.023947e-13
## 37           FALSE    0.35149939          TRUE 3.626069e-01
## 38           FALSE    0.59822515          TRUE 2.748449e-01
## 39           FALSE    0.26071694          TRUE 3.971554e-01
## 40           FALSE    1.12647195          TRUE 1.299829e-01
## 41           FALSE    0.70091499          TRUE 2.416780e-01
## 42           FALSE    0.67332754          TRUE 2.503695e-01
## 43           FALSE    1.59355372          TRUE 5.551801e-02
## 44           FALSE    0.81373362          TRUE 2.078988e-01
## 45           FALSE    0.41527918          TRUE 3.389688e-01
## 46           FALSE    1.69837476          TRUE 4.471853e-02
## 47           FALSE    0.83188991          TRUE 2.027355e-01
## 48           FALSE    1.61317003          TRUE 5.335379e-02
## 49           FALSE    0.51522464          TRUE 3.031980e-01
## 50           FALSE    1.43893589          TRUE 7.508434e-02
## 51           FALSE    1.20758171          TRUE 1.136041e-01
## 52           FALSE    1.66024565          TRUE 4.843252e-02
## 53           FALSE    0.66622560          TRUE 2.526335e-01
## 54           FALSE    4.77413642         FALSE 9.024001e-07
## 55           FALSE    0.96528642          TRUE 1.672007e-01
## 56           FALSE    0.83231672          TRUE 2.026151e-01
## 57           FALSE    0.29180227          TRUE 3.852189e-01
## 58           FALSE    0.12544675          TRUE 4.500849e-01
## 59           FALSE    1.02921058          TRUE 1.516904e-01
## 60           FALSE    0.18376167          TRUE 4.271002e-01
## 61            TRUE   -0.49478917          TRUE 6.896255e-01
## 62           FALSE    0.53432153          TRUE 2.965595e-01
## 63           FALSE    0.95444377          TRUE 1.699295e-01
## 64           FALSE    0.30959168          TRUE 3.784357e-01
## 65           FALSE    0.80844497          TRUE 2.094172e-01
## 66           FALSE    1.58608780          TRUE 5.635970e-02
## 67           FALSE    1.71334055          TRUE 4.332495e-02
## 68            TRUE   -0.06220166          TRUE 5.247989e-01
## 69           FALSE    0.46358861          TRUE 3.214713e-01
## 70           FALSE    0.77449915          TRUE 2.193178e-01
## 71           FALSE    1.39778875          TRUE 8.108826e-02
## 72           FALSE    0.30593041          TRUE 3.798288e-01
## 73           FALSE    0.38005189          TRUE 3.519534e-01
## 74           FALSE    1.39540689          TRUE 8.144659e-02
head(table4)
##   crd.data.Ii crd.data.E.Ii crd.data.Var.Ii crd.data.IiLessEIi
## 1  0.23382163   -0.01369863       0.1504571         0.24752026
## 2  0.03294310   -0.01369863       0.2321830         0.04664173
## 3  0.54907059   -0.01369863       0.1504571         0.56276922
## 4  0.50369253   -0.01369863       0.2321830         0.51739116
## 5  0.06047231   -0.01369863       0.1831475         0.07417094
## 6  1.55636608   -0.01369863       0.1504571         1.57006471
##   crd.data.Ii.Neg crd.data.Z.Ii crd.data.Z.Ho   crd.data.P
## 1           FALSE    0.63812297          TRUE 2.616968e-01
## 2           FALSE    0.09679644          TRUE 4.614440e-01
## 3           FALSE    1.45085485          TRUE 7.341014e-02
## 4           FALSE    1.07375144          TRUE 1.414671e-01
## 5           FALSE    0.17331386          TRUE 4.312024e-01
## 6           FALSE    4.04772671         FALSE 2.585874e-05
# write csv file with Lisa Test results for I, E, Var, Z, P
write.csv(table4, "LisaTest-RenterNorm.csv", row.names = FALSE)

XI.ii. Map Lisa: Renter

Using the tmap package …

# Map Lisa
map_LISA <- tm_shape(crd.data) + 
  tm_polygons(col = "Ii", 
              title = "Local Moran's i", 
              style = "jenks", 
              palette = "viridis", n = 6)  + 
  # add scale bar - first value is TOP, second value is BOTTOM
  tm_scale_bar(width = 0.22, position = c("RIGHT", "BOTTOM")) + 
  # add compass
  tm_compass(position = c("RIGHT", "TOP")) + 
  tm_layout(title = "CRD Census Tracts Renter % of Population\nLISA Local Moran's i 2016", title.position = c("LEFT", "TOP"), inner.margins = c(.08, .03, .08, .03))

map_LISA

# create png of Map LISA Renter Normalized
png("map_LISA_RenterNorm_2.png")  
map_LISA
dev.off()
## quartz_off_screen 
##                 2

XII.ii. Scatter Plot: Renter

When running the code for the second variable, the new column name will need to replace this item crd.data$Renter.

# Scatter Plot
moran.plot(crd.data$RenterNorm, crd.lw, zero.policy=NULL, spChk=NULL, labels=NULL, xlab="Renter % of Population", 
           ylab="Spatially Lagged Renter % of Population", quiet=NULL, main ="Local Moran's i Plot for CRD Renter % of Population 2016")

# create png of Moran scatter plot
png("moran.plot_RenterNorm_2.png")  
moran.plot(crd.data$RenterNorm, crd.lw, zero.policy=NULL, spChk=NULL, labels=NULL, xlab="Renter % of Population", 
           ylab="Spatially Lagged Renter % of Population", quiet=NULL, main ="Local Moran's i Plot for CRD Renter % of Population 2016")
dev.off()
## quartz_off_screen 
##                 2

Built with R v.3.6.1 with R Markdown in RStudio v.1.1.463.

References


  1. https://www.rdocumentation.org/packages/plyr/versions/1.8.4

  2. https://dplyr.tidyverse.org/

  3. https://github.com/rstudio/cheatsheets/blob/master/data-transformation.pdf

  4. https://cran.r-project.org/web/packages/spdep/index.html

  5. https://cran.r-project.org/web/packages/spdep/spdep.pdf

  6. https://cran.r-project.org/web/packages/GISTools/GISTools.pdf

  7. https://cran.r-project.org/web/packages/raster/raster.pdf

  8. https://cran.r-project.org/web/packages/maptools/maptools.pdf

  9. https://cran.r-project.org/web/packages/rgdal/rgdal.pdf

  10. https://cran.r-project.org/web/packages/tmap/tmap.pdf

  11. https://cran.r-project.org/web/packages/BAMMtools/BAMMtools.pdf

  12. https://deanattali.com/shinyjs/overview