knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Install package

# Install development version from GitHub
install_github("spatialnetworkslab/homelocator")

Load library

#load homelocator package 
library(homelocator)
# load other packages needed for the analysis
library(tidyverse)
library(here)

Load test data

The test data includes 100 random users, and it can be used as an example to get started with homelocator package.

data("test_sample", package = "homelocator")

Validate test sample

The validate_dataset() function makes sure your input dataset contains all three necessary variables: user, location and timestamp. There are 4 arguments in this function:

When validating the dataset, please specify the names of column for user, timestamp and location.

validate_dataset(test_sample, user = "u_id", timestamp = "created_at", location = "grid_id", keep_other_vars = FALSE)

Identify home locations with embedded recipes

Recipe: HMLC

Weighs data points across multiple time frames to ‘score’ potentially meaningful locations for each user

# recipe: homelocator -- HMLC
identify_location(test_sample, user = "u_id", timestamp = "created_at", location = "grid_id", show_n_loc = 1, recipe = "HMLC")

Recipe: FREQ

Selects the most frequently 'visited' location assuming a user is active mainly around their home location.

# recipe: Frequency -- FREQ
identify_location(test_sample, user = "u_id", timestamp = "created_at", location = "grid_id", show_n_loc = 1, recipe = "FREQ")

Recipe: OSNA (Efstathiades et al.2015):

Finds the most 'popular' location during 'rest', 'active' and 'leisure time. Here we focus on 'rest' and 'leisure' time to find the most possible home location for each user.

# recipe: Online Social Network Activity -- OSNA
identify_location(test_sample, user = "u_id", timestamp = "created_at", location = "grid_id", show_n_loc = 1, recipe = "OSNA")

Recipe: APDM (Ahas et al. 2010)

Calculates the average and standard deviation of start time data points by a single user, in a single location.

# recipe: Online Social Network Activity -- APDM
## APDM recipe strictly returns the most likely home location
## It is important to load the neighbors table before you use the recipe!!
## example: st_queen <- function(a, b = a) st_relate(a, b, pattern = "F***T****")
##          neighbors <- st_queen(df_sf) ===> convert result to dataframe 
data("df_neighbors", package = "homelocator")
identify_location(test_sample, user = "u_id", timestamp = "created_at", location = "grid_id", recipe = "APDM", keep_score = F)


spatialnetworkslab/homelocator documentation built on May 7, 2023, 8:18 p.m.