SITE UNDER CONSTRUCTION, FULL CONTENT SOON

Welcome to the RPubs for the book ‘Applied Spatial Statistics And Econometrics: Data Analysis in R’ (Routledge, 2nd edition) (ed.Katarzyna Kopczewska).

This site is a collection of all R codes included in a book.

Introduction: Reading the data and dataset description

The book uses a few datasets that are available at: ……. Before you start, load the major packages:

# download the packages – do it once only
install.packages("sf")
install.packages("ggplot2")

# load the packages for each session 
library(sf) 
library(ggplot2) 

and set working directory:

# set working directory – please set your own path
setwd("C:/mypath")

A3: Read regional dataset

# read regional data
data<-read.csv("regional_dataset.csv", header=TRUE, sep=";", dec=",", encoding="UTF-8")
data23<-data[data$year==2023,]  # subset for a selected year

dim(data23)     # number of rows and columns
head(data23)    # here just 2 rows, by default it is 6 rows
summary(data23) # here just first 4 variables, instead of 40
names(data23)

A4: Reading shapefiles

# load shapefile files into sf class
# 16 regional countours (Voivoidships) - NUTS2 level
voi<-st_read("wojewodztwa.shp", options="ENCODING=UTF-8") # 16 units 
voi<-st_transform(voi, 4326)        # reproject to WGS84

# 380 subregional units (poviats) – LAU1 level
pov<-st_read("powiaty.shp", options="ENCODING=UTF-8") # 380 units
pov<-st_transform(pov, 4326)        # reproject to WGS84

# 1 km x 1km grid with population data for Lubelskie region (NUTS2)
grid<-st_read("POP_lub.shp", stringsAsFactors=FALSE)
grid<-st_transform(grid, 4326)
dim(grid)

class(pov)      # check the class of sf object

#graphics - contour maps plotted separately
plot(st_geometry(voi), main="NUTS2 regions") 
plot(st_geometry(pov), main="LAU1 subregions (poviats)")

Figure 1. Figure X: Plots of contour maps from shapefiles: a) NUTS2 with 16 units, b) LAU1 with 380 units