R
with tmap
, sf
and other helpers“I invoke the first law of geography - everything is related to everything else, but near things are more related than distant things.” (Tobler, 1970)
Tobler, W. R. 1970. A computer movie simulating urban growth in the Detroit region. Economic Geography 46: 234–40.
morans_I
in the file morans_i.R
adjacency.csv
source('morans_I.R') adj <- read_csv('adjacency.csv') adj
## # A tibble: 7 x 2 ## County1 County2 ## <chr> <chr> ## 1 Armagh Antrim ## 2 Down Antrim ## 3 Londonderry Antrim ## 4 Tyrone Antrim ## 5 Antrim Armagh ## 6 Down Armagh ## 7 Louth County Armagh
counties.geojson
)counties <- st_read('counties.geojson')
m_I <- morans_I(counties,adj,County,mean_rain) m_I
## [1] 0.5218756
morans_I(#1,#2,#3,#4)
# make a list for randomised Moran's I values # start by filling with zeroes then fill in values random_m_I <- rep(0,10000) ## Loop to calculate the randomised Moran's I's for (i in 1:10000) { test <- mutate(counties,mean_rain=sample(mean_rain)) # Randomise random_m_I[i] <- morans_I(test,adj,County,mean_rain) # calculate I } ## Quick summary of results fivenum(random_m_I)
## [1] -0.31701185 -0.09599609 -0.03478540 0.03124031 0.42912956
hist(random_m_I,xlim=c(-0.7,0.7), main="Randomised Values of Moran's I", xlab="Moran's I") abline(v=m_I,col='brown',lwd=2) text(0.5,1000,'Actual Value',srt=90,col='brown')
ylim
not xlim
here!!boxplot(random_m_I, horizontal=TRUE, main="Randomised Values of Moran's I", xlab="Moran's I",ylim=c(-0.7,0.7)) abline(v=m_I,col='brown',lwd=2) text(0.5,1,'Actual Value',srt=90,col='brown')
dens <- density(random_m_I) plot(dens, main="Randomised Values of Moran's I", xlab="Moran's I",xlim=c(-0.7,0.7)) abline(v=m_I,col='brown',lwd=2) text(0.5,2,'Actual Value',srt=90,col='brown')
length(which(random_m_I > m_I))/10000
## [1] 0
dplyr
, the tidyverse
and pipelinesjava
in terms of loops, functions etctidyverse
approach (using libraries like dplyr
) useful for ‘flow based approach’Version \(\Rightarrow\) | Ordinary R |
Tidyverse R |
---|---|---|
f1(f2(f3(x))) |
x %>% f3() %>% f2() %>% f1() |
|
f1(f2(f3(x,b),a)) |
x %>% f3(b) %>% f2(a) %>% f1() |
|
y <- f1(f2(f3(x,b),a)) |
y <- x %>% f3(b) %>% f2(a) %>% f1() |
|
Or | y <- f1(f2(f3(x,b),a)) |
x %>% f3(b) %>% f2(a) %>% f1() -> y |
Ordinary R:
high_counties <- st_drop_geometry(arrange(filter(counties,mean_rain > 80), mean_rain))
Tidyverse:
library(tidyverse) counties %>% filter(mean_rain > 80) %>% arrange(mean_rain) %>% st_drop_geometry() -> high_counties
tidyverse
most useful for processing data tables->
’ or ‘<-
’ in tidyverse expressions?
New general ideas
tidyverse
New techniques
Practical issues
Next lecture - More Investigating Relationships / ggplot
This link may be useful for some of the ideas - follow up on the first two suggested links.