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

homelocator

Overview

The goal of `homelocator` is to provide a consistent framework and interface for the adoption of different approaches for identifying home locations for users. With the package, you are able to write structured, algorithmic 'recipes' to identify home locations according to your research requirements. The package also has a number of built-in 'recipes' that have been translated from approaches in the existing literature.

Installation

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

Example

These are some basic examples that show you how to use common functions in the package.

Validate input dataset

You need to make sure the input dataset includes three essential attributes:

You can use validate_dataset() to validate your input dataset before starting identifying meaningful locations. In this function, you need to specify the names of three essential attribute that used in your dataset.

# Load homelocator library
library(homelocator)
# Load other needed libraries
library(tidyverse)
library(here)
# load test sample dataset 
data("test_sample", package = "homelocator")
df_validated <- validate_dataset(test_sample, user = "u_id", timestamp = "created_at", location = "grid_id")
head(df_validated)

Nesting users for parallel computing

To speed up computing progress, you can nest the validated dataset by user so that the subsequent location inference can be applied to each user at the same time.

df_nested <- nest_verbose(df_validated, c("created_at", "grid_id"))
head(df_nested)
head(df_nested$data[[1]])

Enrich variables from timestamp

Add additional needed varialbes derived from the timestamp column. These are often used/needed as intermediate variables in home location algorithms, such as year, month, day, day of the week and hour of the day, etc.

df_enriched <- enrich_timestamp(df_nested, timestamp = "created_at")
head(df_enriched$data[[1]])

Use built-in recipes

Current available recipes, where HMLC is the default recipe used in identify_location:

HMLC

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

FREQ

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

OSNA

# 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")

APDM

# recipe: Online Social Network Activity -- APDM
## APDM recipe strictly returns the most likely home location
## It is important to create your location 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", 
                  show_n_loc = 1, recipe = "APDM")


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