knitr::opts_chunk$set(echo = TRUE)
Here is my new function:
library(mytestpkg) followers(3)
More Information can be found (need to look this up).
library(mytestpkg) followers(5)
On Wednesday we started with a challenge to get familiar with work with all this. You can see the result in Figure \@ref(fig:cars) and Table \@ref(tab:tableone) Here is my table to practise using markdown files.
plot(cars, col="blue")
In Figure \@ref(fig:cars) you can clearly see that i managed to produce a plot. You can find interesting literature by an author called Cars here: [@madanipour2000social]. If you don't like figures, here is a table with numbers:
knitr::kable(tail(cars), caption = "My table with random cars")
I also created a new function.
library(mytestpkg) m <- half(2) n <- half(5) o <- half(10) print(c(m, n, o))
Useful for output description: paste("the value is", x) glue::glue("the value is", y)
library(binford) data(LRB) knitr::kable(head(LRB))
harran <- read.table("../data/Sites_HarranPlain.csv", header = TRUE, sep = ",") str(harran)
library(spatstat) library(sp) coordinates(harran) <- ~X+Y proj4string(harran) <- CRS("+init=epsg:4326") #overrides projection information - be careful!! harran <- spTransform(harran, CRSobj = CRS("+init=epsg:32637")) #326 stands for utm and 37 for 37N str(harran) harran_ppp <- ppp(x = harran@coords[,1], y = harran@coords[,2], window = owin(xrange = harran@bbox[1,], yrange = harran@bbox[2,])) str(harran_ppp) plot(harran_ppp) harran_ppp <- ppp(x = harran@coords[,1], y = harran@coords[,2], window = owin(xrange = harran@bbox[1,], yrange = c(min(harran@coords[,2]), min(harran@coords[,2]+52000)))) # you get the warning that you throw out some points that will no longer be included in the analysis # and some are duplicates (find out with duplicated.ppp and remove with unique.ppp) # alternative: add "check = FALSE" to delete duplicates and points outside the area anyDuplicated(harran_ppp) harran_sing <- unique(harran_ppp) # alternative: harran_unique <- harran[!is.TRUE(duplicated(harran_ppp))] #! means give me the opposite # you could use this without "is.True" because it is a true/false answer
Spatstat has very nice descriptions, manual etc. It's the way to go if you want to work with spatial statistics and it's enough to have data in a table.
# Nearest neighbour harran_sing_nn <- nndist(harran_sing) str(harran_sing_nn) # distance is shown in m because we transformed the proj. barplot(sort(harran_sing_nn)) hist(harran_sing_nn) #create kernel density estimation harran_dens <- density.ppp(harran_sing, sigma = mean(harran_sing_nn)) harran_dens_standard <- density(harran_sing, bw = mean(harran_sing_nn)) plot(harran_dens) plot(harran_dens_standard) # bw.ppl(harran_sing) or bw.diggle(harran_sing) to check which bw you should set plot(bw.ppl(harran_sing), xlim = c(2000, 5000)) #you see the max value #enter elevation as covariate library(raster) dem <- raster("../data/dem.tif") #rasterframe #older alternative: library(rgdal) dem2 <- readGDAL("../data/dem.tif") #spatialgriddataframe hist(dem2$band1) im_dem <- as.im(as.image.SpatialGridDataFrame(as(dem, "SpatialGridDataFrame"))) # you need to transform because old spatstats can only work with this image format harran_rhohat <- rhohat(object = harran_sing, covariate = im_dem, bw = 200) plot(harran_rhohat) # x-axis: elevation of our points, y-axis: intensity of points - the lower the elevation the higher # the point density but the larger the sigma the less evident this relationship # contrary to point correlation you have continuous datasets here because they are interpolated in the area # compare theoretical rhohat function with original data rho_dem <-predict(harran_rhohat) plot(rho_dem) diff_rho <- harran_dens - rho_dem plot(diff_rho) # first order effect map
challenge: produce random points that have the same density like the global density of our area (density = points per area)
#poisson process pattern - spatial randomness set.seed(123) harran_rpoispp2 <- rpoispp(lambda = harran_sing$n/area.owin(harran_sing$window), win = harran_sing$window) set.seed(123) harran_rpoispp3 <- rpoispp(intensity(harran_sing), win = Window(harran_sing)) set.seed(123) harran_rpoispp4 <- rpoispp(ex = harran_sing) plot(harran_sing) points(harran_rpoispp2, col="red") points(harran_rpoispp3, col="blue") points(harran_rpoispp4, col="green") #set.seed use random start number and restore data points for future use
To put it on github you need to change 4 files: - description (insert all imports), - travis.yml (addons - apt - packages, script) - docker (line 11-15) - README.Rmd (copied from Daniels repo)
# g-Function compares NN-distance harran_g <- Gest(harran_sing) str(harran_g) # r is distance radius, km is empirial data plot(harran_g) # blue line is poisson function, black line is real data, blue and green are data with edge correction # at low distance we have more points than in theory so seems to be clustering, around 1000 m it seems random, # in upper distance seems clustering again harran_ge <- envelope(harran_sing, fun = "Gest") # takes random points 99 times to calculate g-function plot(harran_ge) # shows that distribution of points in harran plain is completely random harran_ge_large <- envelope(harran_sing, fun = "Gest", nsim = 999) plot(harran_ge_large) # the more values you use, the wider the gray area gets harran_f <- Fest(harran_sing) plot(harran_f) harran_k <- Kest(harran_sing) plot(harran_k) # inhomogeneous data with covariate influence can be tested against Ginhom, Finhom, Kinhom # e.g. if you know you have a first order effect, you predict the density image harran_gi <- Ginhom(harran_sing, lambda = predict(harran_rhohat)) plot(harran_gi)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.