Quantcast
Channel: CodeSection,代码区,网络安全 - CodeSec
Viewing all articles
Browse latest Browse all 12749

Introducing the Kernelheaping Package II

0
0

(This article was first published on INWT-Blog-RBloggers , and kindly contributed toR-bloggers)

In the first part of <a target="_blank" style="text-decoration:underline;" href="https://www.inwt-statistics.com/read-blog/introducing-the-kernelheaping-package-512.html" rel="nofollow">Introducing the Kernelheaping Package</a> I showed how to compute and plot kernel density estimates on rounded or interval censored data using the Kernelheaping package. Now, let’s make a big leap forward to the 2-dimensional case. Interval censoring can be generalised to rectangles or alternatively even arbitrary shapes. That may include counties, zip codes, electoral districts or administrative districts. Standard area-level mapping methods such as chloropleth maps suffer from very different area sizes or odd area shapes which can greatly distort the visual impression. The Kernelheaping package provides a way to convert these area-level data to a smooth point estimate. For the German capital city of Berlin, for example, there exists an open data initiative, where data on e.g. demographics is publicly available. We first load a dataset on the Berlin population, which can be downloaded from: <a target="_blank" style="text-decoration:underline;" href="https://www.statistik-berlin-brandenburg.de/opendata/EWR201512E_Matrix.csv" rel="nofollow">https://www.statistik-berlin-brandenburg.de/opendata/EWR201512E_Matrix.csv</a> ```r library(dplyr) library(fields) library(ggplot2) library(Kernelheaping) library(maptools) library(RColorBrewer) library(rgdal) gpclibPermit() ``` ```r data <- read.csv2("EWR201512E_Matrix.csv") ``` This dataset contains the numbers of inhabitants in certain age groups for each administrative districts. Afterwards, we load a shapefile with these administrative districts, available from: <a target="_blank" style="text-decoration:underline;" href="https://www.statistik-berlin-brandenburg.de/opendata/RBS_OD_LOR_2015_12.zip" rel="nofollow">https://www.statistik-berlin-brandenburg.de/opendata/RBS_OD_LOR_2015_12.zip</a> ```r berlin <- readOGR("RBS_OD_LOR_2015_12/RBS_OD_LOR_2015_12.shp") ``` ```r berlin <- spTransform(berlin, CRS("+proj=longlat +datum=WGS84")) ``` Now, we form our input data set, which contains the area/polygon centers and the variable of interest, whose density should be calculated. In this case we' like to calculate the spatial density of people between 65 and 80 years of age: ```r dataIn <- lapply(<a href="/cdn-cgi/l/email-protection" data-cfemail="593b3c2b35303719293635203e36372a">[email protected]</a>, function(x) <a href="/cdn-cgi/l/email-protection" data-cfemail="fd85bd919c9f8d89">[email protected]</a>) %>% do.call(rbind, .) %>% cbind(data$E_E65U80) ``` In the next step we calculate the bivariate kernel density with the “dshapebivr” function (this may take some minutes) using the prepared data and the shape file: ```r est <- dshapebivr(data = dataIn, burnin = 5, samples = 10, adaptive = FALSE, shapefile = berlin, gridsize = 325, boundary = TRUE) ``` To plot the map with "ggplot2", we need to perform some additional data preparation steps: ```r <a href="/cdn-cgi/l/email-protection" data-cfemail="d5b7b0a7b9bcbb95b1b4a1b4">[email protected]</a>$id <- as.character(<a href="/cdn-cgi/l/email-protection" data-cfemail="a5c7c0d7c9cccbe5c1c4d1c4">[email protected]</a>$PLR) <a href="/cdn-cgi/l/email-protection" data-cfemail="b9dbdccbd5d0d7f9ddd8cdd8">[email protected]</a>$E_E65U80 <- data$E_E65U80 berlinPoints <- fortify(berlin, region = "id") berlinDf <- left_join(berlinPoints, <a href="/cdn-cgi/l/email-protection" data-cfemail="ccaea9bea0a5a28ca8adb8ad">[email protected]</a>, by = "id") kData <- data.frame(expand.grid(long = est$Mestimates$eval.points[[1]], lat = est$Mestimates$eval.points[[2]]), Density = as.vector(est$Mestimates$estimate)) %>% filter(Density > 0) ``` Now, we are able to plot the density together with the administrative districts: ```r ggplot(kData) + geom_raster(aes(long, lat, fill = Density)) + ggtitle("Bivariate density of Inhabitants between 65 and 80 years") + scale_fill_gradientn(colours = c("#FFFFFF", "#5c87c2", "#19224e")) + geom_path(color = "#000000", data = berlinDf, aes(long, lat, group = group)) + coord_quickmap() ```
Introducing the Kernelheaping Package II
This map gives a much better overall impression of the distribution of older people than a simple choropleth map: ```r ggplot(berlinDf) + geom_polygon(aes(x = long, y = lat, fill = E_E65U80, group = id)) + ggtitle("Number of Inhabitants between 65 and 80 years by district") + scale_fill_gradientn(colours = c("#FFFFFF", "#5c87c2", "#19224e"), "n") + geom_path(color = "#000000", data = berlinDf, aes(long, lat, group = group)) + coord_quickmap() ```
Introducing the Kernelheaping Package II
Often, as the case with Berlin we may have large uninhabited areas such as woods or lakes. Furthermore, we may like to compute the proportion of older people compared to the overall population in a spatial setting. The third part of this series shows how you can compute boundary corrected and smooth proportion estimates with the Kernelheaping package.

Further parts of the article series Introducing the Kernelheaping Package :

Part 1: Univariate Kernel Density Estimation for Heaped Data Part 2: Bivariate Kernel Density Estimation for Heaped Data

Viewing all articles
Browse latest Browse all 12749

Latest Images

Trending Articles





Latest Images