# Spatial pattern analysis in R

This is a post originally posted on stacksoverflow, but I was recommended to post it here also.

I have a lot of position data from fishing vessels, from where I would like to detect fishing activity. Basically fishing vessel are either steaming, fishing or inactive. I uploaded an example of a typical days data here:

https://files.dtu.dk/userportal/#/shared/public/_CVkU00lZFzuJWRS/Example.rds

``````l <- readRDS(paste0(myDownloadFolder, "Example.rds"))
``````

Make it spatial with sf:

``````library(sf)
st_geometry(l) <- l\$geometry
``````

And investigate it with mapview:

``````library(mapview)
mapview(l, col.regions = sf.colors(10))
``````

From here we can see that they are basically fishing in two different places at that particular day.

If we plot a histogram of the speed, we get two distinguised peak, one around 2-4 and one around 7-9:

``````hist(l\$speed,1000, xlim = c(1,15), ylim = c(0,30))
``````

### Is there a way to automatically detect the thresholds?

Only using points in that interval

``````inc <- l[l\$speed>2 & l\$speed<4,]
``````

We can find time difference between those:

``````inc\$INTV2 <- difftime(lead(inc\$log), inc\$log)
inc\$INTV3 <- difftime((inc\$log), lag(inc\$log))
hist(as.numeric(inc\$INTV2),1000, xlim = c(0,1000), ylim=c(0,100))
``````

There will usually be at least 1 minute (60 sec) between each haul, but lets set it to 90 in this example.

``````stop <- inc[INTV2>90 | is.na(INTV2)]\$log
start <- inc[INTV3>90 | is.na(INTV3)]\$log

haul <- data.table(start, stop)
haul\$length <- difftime(haul\$stop, haul\$start)
``````

We can use hauls to get lines from the original data:

``````library(data.table)
library(dplyr)
DT <- data.table(l)

setDT(DT)[,log2 := log]
#allow for data table merge - tidspunkt
setkey(DT, log, log2)

## Merge with blackbox data on afstid og anktid
pts <- foverlaps(haul, DT, by.x=c("start", "stop"), nomatch=0)

st_geometry(pts) <- pts\$geometry

l2 <- pts %>%
dplyr::group_by(haulid) %>%
summarise(do_union = FALSE) %>%
sf::st_cast("LINESTRING")

mapview(l2, zcol="haulid")
``````

This looks ok, but some of the hauls seems to consist of two hauls (Fishermen dont turn 180 degrees within a single haul):

Is it possible to use heading to distinguish when the haul should be cut (when they start turning and when they end the turn)? Would be nice to have a way where only straight lines are considered hauls.

This ended up in a long rambling - sorry about that. To conclude there is two issues I would like to have answered: - How to automatically detect speed threholds - How to cut hauls when they are turning.

• You should cut this down to just your first question. I'd say that histogram had three clusters in it. If you did cluster analysis with a fixed number of cluster centres (2 in your case) you might get the categorisation you want. – Spacedman Sep 26 '18 at 13:30